<s> import datetime <EOL> import json <EOL> import uuid <EOL> from flask import current_app <EOL> from flask_sqlalchemy import SQLAlchemy <EOL> from flask_login import current_user <EOL> from werkzeug . security import generate_password_hash , check_password_hash <EOL> from werkzeug . http import http_date <EOL> from polaris . utils import sanitize_string , validate_email <EOL> db = SQLAlchemy ( ) <EOL> class User ( db . Model ) : <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . UUID , primary_key = True , default = uuid . uuid4 ) <EOL> email = db . Column ( db . String , nullable = False , unique = True ) <EOL> username = db . Column ( db . String , nullable = False , unique = True ) <EOL> nickname = db . Column ( db . String , default = '<STR_LIT>' , nullable = False ) <EOL> _password = db . Column ( "<STR_LIT:password>" , db . String , nullable = False ) <EOL> is_valid = db . Column ( db . Boolean , default = False , nullable = False ) <EOL> is_superadmin = db . Column ( db . Boolean , default = False , nullable = False ) <EOL> created_at = db . Column ( db . DateTime ( timezone = True ) , <EOL> default = datetime . datetime . now , <EOL> nullable = False ) <EOL> charts = db . relationship ( "<STR_LIT>" , lazy = "<STR_LIT>" , <EOL> order_by = "<STR_LIT>" ) <EOL> chartstars = db . relationship ( "<STR_LIT>" , backref = "<STR_LIT:user>" , lazy = "<STR_LIT>" ) <EOL> dashboards = db . relationship ( "<STR_LIT>" , lazy = "<STR_LIT>" , <EOL> order_by = "<STR_LIT>" ) <EOL> dashboardstars = db . relationship ( "<STR_LIT>" , <EOL> backref = "<STR_LIT:user>" , lazy = "<STR_LIT>" ) <EOL> groups = db . relationship ( "<STR_LIT>" , backref = "<STR_LIT:user>" , lazy = "<STR_LIT>" , <EOL> order_by = "<STR_LIT>" ) <EOL> oauthusers = db . relationship ( "<STR_LIT>" , backref = "<STR_LIT:user>" , lazy = "<STR_LIT>" ) <EOL> is_authenticated = lambda self : True <EOL> is_active = lambda self : self . is_valid <EOL> is_anonymous = lambda self : False <EOL> get_id = lambda self : self . id <EOL> def __init__ ( self , email , username , password , nickname = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . email = email <EOL> self . username = username <EOL> self . password = password <EOL> self . nickname = nickname <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . id , self . email ) <EOL> @ property <EOL> def password ( self ) : <EOL> return self . _password <EOL> @ password . setter <EOL> def password ( self , pwd ) : <EOL> self . _password = generate_password_hash ( pwd ) <EOL> def auth ( self , pwd ) : <EOL> return check_password_hash ( self . password , pwd ) <EOL> @ db . validates ( '<STR_LIT:username>' , include_removes = True ) <EOL> def validate_username ( self , key , username , is_remove ) : <EOL> if is_remove : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return sanitize_string ( username , extra_chars = '<STR_LIT:.>' ) <EOL> @ db . validates ( '<STR_LIT:email>' , include_removes = True ) <EOL> def validate_email ( self , key , email , is_remove ) : <EOL> if is_remove : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> assert validate_email ( email ) <EOL> assert sanitize_string ( email , extra_chars = '<STR_LIT>' ) == email <EOL> return email <EOL> def to_dict ( self ) : <EOL> return { <EOL> "<STR_LIT:id>" : str ( self . id ) , "<STR_LIT:email>" : self . email , "<STR_LIT:username>" : self . username , <EOL> "<STR_LIT>" : self . nickname , "<STR_LIT>" : self . is_valid , <EOL> "<STR_LIT>" : self . is_superadmin , <EOL> "<STR_LIT>" : http_date ( self . created_at ) , <EOL> "<STR_LIT>" : [ c . to_dict ( ) for c in self . charts ] , <EOL> "<STR_LIT>" : [ d . to_dict ( ) for d in self . dashboards ] <EOL> } <EOL> class OauthUser ( db . Model ) : <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> user_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) , nullable = False ) <EOL> provider_id = db . Column ( db . String , nullable = False ) <EOL> provider_email = db . Column ( db . String , nullable = False ) <EOL> provider_info = db . Column ( db . JSON , nullable = False ) <EOL> provider = db . Column ( db . String , nullable = False ) <EOL> created_at = db . Column ( db . DateTime ( timezone = True ) , <EOL> default = datetime . datetime . now , <EOL> nullable = False ) <EOL> class Group ( db . Model ) : <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . UUID , primary_key = True , default = uuid . uuid4 ) <EOL> slug = db . Column ( db . String , nullable = False ) <EOL> name = db . Column ( db . String , nullable = False , unique = True ) <EOL> created_at = db . Column ( db . DateTime ( timezone = True ) , <EOL> default = datetime . datetime . now , <EOL> nullable = False ) <EOL> charts = db . relationship ( "<STR_LIT>" , lazy = "<STR_LIT>" , <EOL> order_by = "<STR_LIT>" ) <EOL> dashboards = db . relationship ( "<STR_LIT>" , lazy = "<STR_LIT>" , <EOL> order_by = "<STR_LIT>" ) <EOL> members = db . relationship ( "<STR_LIT>" , backref = "<STR_LIT>" , lazy = "<STR_LIT>" , <EOL> order_by = "<STR_LIT>" ) <EOL> def __init__ ( self , slug , name ) : <EOL> assert current_user . is_authenticated ( ) <EOL> self . slug = slug <EOL> self . name = name <EOL> self . members . append ( GroupMember ( user_id = current_user . id , <EOL> is_admin = True ) ) <EOL> def add_member ( self , member ) : <EOL> self . members . append ( member ) <EOL> def is_member ( self , user ) : <EOL> return bool ( self . members . filter ( GroupMember . user_id == user . id ) . <EOL> first ( ) ) <EOL> def is_admin ( self , user ) : <EOL> return bool ( self . members . <EOL> filter ( GroupMember . user_id == user . id ) . <EOL> filter ( GroupMember . is_admin ) . <EOL> first ( ) ) <EOL> def to_dict ( self ) : <EOL> return { "<STR_LIT:id>" : str ( self . id ) , "<STR_LIT>" : self . slug , "<STR_LIT:name>" : self . name , <EOL> "<STR_LIT>" : http_date ( self . created_at ) } <EOL> class GroupMember ( db . Model ) : <EOL> __tablename__ = "<STR_LIT>" <EOL> group_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) , <EOL> primary_key = True ) <EOL> user_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) , <EOL> primary_key = True ) <EOL> is_admin = db . Column ( db . Boolean , default = False , nullable = False ) <EOL> created_at = db . Column ( db . DateTime ( timezone = True ) , <EOL> default = datetime . datetime . now , <EOL> nullable = False ) <EOL> class Chart ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . UUID , primary_key = True , default = uuid . uuid4 ) <EOL> name = db . Column ( db . String , nullable = False ) <EOL> description = db . Column ( db . String , nullable = False , default = "<STR_LIT>" ) <EOL> category = db . Column ( db . String , nullable = False , default = "<STR_LIT>" ) <EOL> source = db . Column ( db . String , nullable = False ) <EOL> payload = db . Column ( db . JSON , nullable = False ) <EOL> user_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> group_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> is_public = db . Column ( db . Boolean , default = False , nullable = False ) <EOL> stars = db . Column ( db . Integer , default = <NUM_LIT:0> , nullable = False ) <EOL> fork_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> created_at = db . Column ( db . DateTime ( timezone = True ) , <EOL> default = datetime . datetime . now , <EOL> nullable = False ) <EOL> user = db . relationship ( "<STR_LIT>" , lazy = "<STR_LIT>" ) <EOL> group = db . relationship ( "<STR_LIT>" , lazy = "<STR_LIT>" ) <EOL> parent = db . relationship ( "<STR_LIT>" , remote_side = id ) <EOL> children = db . relationship ( "<STR_LIT>" , remote_side = fork_id , lazy = "<STR_LIT>" ) <EOL> starred_users = db . relationship ( "<STR_LIT>" , backref = "<STR_LIT>" , <EOL> lazy = "<STR_LIT>" ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> assert current_user . is_authenticated ( ) <EOL> for f in ( "<STR_LIT:name>" , "<STR_LIT:description>" , "<STR_LIT:source>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if kwargs . get ( f ) is not None : <EOL> setattr ( self , f , kwargs [ f ] ) <EOL> self . category = current_app . config [ "<STR_LIT>" ] [ self . source ] <EOL> if not self . group_id : <EOL> self . user_id = current_user . id <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( str ( self ) ) <EOL> def __str__ ( self ) : <EOL> return json . dumps ( self . to_dict ( ) ) <EOL> @ property <EOL> def username ( self ) : <EOL> return self . user . username if self . user_id else "<STR_LIT>" <EOL> @ property <EOL> def nickname ( self ) : <EOL> return self . user . nickname if self . user_id else '<STR_LIT>' <EOL> @ property <EOL> def groupname ( self ) : <EOL> return self . group . name if self . group_id else "<STR_LIT>" <EOL> @ property <EOL> def short ( self ) : <EOL> if self . user_id : <EOL> return self . username + '<STR_LIT:/>' + self . slug <EOL> elif self . group_id : <EOL> return self . groupname + '<STR_LIT:/>' + self . slug <EOL> def to_dict ( self ) : <EOL> res = { } <EOL> uuids = ( "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> for u in uuids : <EOL> val = getattr ( self , u ) <EOL> res [ u ] = str ( val ) if val else '<STR_LIT>' <EOL> fields = ( "<STR_LIT:name>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT:source>" , "<STR_LIT>" , <EOL> "<STR_LIT:username>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> res . update ( { f : getattr ( self , f ) for f in fields } ) <EOL> res [ "<STR_LIT>" ] = http_date ( self . created_at ) <EOL> return res <EOL> @ db . validates ( '<STR_LIT>' , include_removes = True ) <EOL> def validate_slug ( self , key , slug , is_remove ) : <EOL> if is_remove : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return sanitize_string ( slug , extra_chars = '<STR_LIT:_>' ) <EOL> @ db . validates ( '<STR_LIT>' ) <EOL> def validate_category ( self , key , category ) : <EOL> assert category in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return category <EOL> class Dashboard ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . UUID , primary_key = True , default = uuid . uuid4 ) <EOL> slug = db . Column ( db . String , nullable = False ) <EOL> name = db . Column ( db . String , nullable = False ) <EOL> description = db . Column ( db . String , nullable = False , default = "<STR_LIT>" ) <EOL> payload = db . Column ( db . JSON , nullable = False ) <EOL> user_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> group_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> is_public = db . Column ( db . Boolean , default = False , nullable = False ) <EOL> stars = db . Column ( db . Integer , default = <NUM_LIT:0> , nullable = False ) <EOL> created_at = db . Column ( db . DateTime ( timezone = True ) , <EOL> default = datetime . datetime . now , <EOL> nullable = False ) <EOL> user = db . relationship ( "<STR_LIT>" , lazy = "<STR_LIT>" ) <EOL> group = db . relationship ( "<STR_LIT>" , lazy = "<STR_LIT>" ) <EOL> starred_users = db . relationship ( "<STR_LIT>" , backref = "<STR_LIT>" , <EOL> lazy = "<STR_LIT>" ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> assert current_user . is_authenticated ( ) <EOL> for f in ( "<STR_LIT>" , "<STR_LIT:name>" , "<STR_LIT:description>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if kwargs . get ( f ) is not None : <EOL> setattr ( self , f , kwargs [ f ] ) <EOL> if not self . group_id : <EOL> self . user_id = current_user . id <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( str ( self ) ) <EOL> def __str__ ( self ) : <EOL> return json . dumps ( self . to_dict ( ) ) <EOL> @ property <EOL> def username ( self ) : <EOL> return self . user . username if self . user_id else "<STR_LIT>" <EOL> @ property <EOL> def nickname ( self ) : <EOL> return self . user . nickname if self . user_id else '<STR_LIT>' <EOL> @ property <EOL> def groupname ( self ) : <EOL> return self . group . name if self . group_id else "<STR_LIT>" <EOL> @ property <EOL> def short ( self ) : <EOL> return '<STR_LIT:/>' . join ( [ self . username , self . slug ] ) <EOL> def to_dict ( self ) : <EOL> res = { } <EOL> uuids = ( "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> for u in uuids : <EOL> val = getattr ( self , u ) <EOL> res [ u ] = str ( val ) if val else '<STR_LIT>' <EOL> fields = ( "<STR_LIT>" , "<STR_LIT:name>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT:username>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> res . update ( { f : getattr ( self , f ) for f in fields } ) <EOL> res [ "<STR_LIT>" ] = http_date ( self . created_at ) <EOL> return res <EOL> @ db . validates ( '<STR_LIT>' , include_removes = True ) <EOL> def validate_slug ( self , key , slug , is_remove ) : <EOL> if is_remove : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return sanitize_string ( slug , extra_chars = '<STR_LIT:_>' ) <EOL> class ChartStar ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = "<STR_LIT>" <EOL> user_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) , <EOL> primary_key = True ) <EOL> chart_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) , <EOL> primary_key = True ) <EOL> created_at = db . Column ( db . DateTime ( timezone = True ) , <EOL> default = datetime . datetime . now , <EOL> nullable = False ) <EOL> class DashboardStar ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = "<STR_LIT>" <EOL> user_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) , <EOL> primary_key = True ) <EOL> dashboard_id = db . Column ( db . UUID , db . ForeignKey ( '<STR_LIT>' ) , <EOL> primary_key = True ) <EOL> created_at = db . Column ( db . DateTime ( timezone = True ) , <EOL> default = datetime . datetime . now , <EOL> nullable = False ) </s>
