class User(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), unique=True, index=True) password = db.Column(db.String(120)) first_name = db.Column(db.String(20)) last_name = db.Column(db.String(20)) active = db.Column(db.Boolean(), default=False) def __init__(self, email, password, first_name=None, last_name=None, active=False): self.email = email self.password = password self.first_name = first_name self.last_name = last_name self.active = active def serialize(self): return { 'email': self.email, 'first_name': self.first_name, 'last_name': self.last_name, 'active': self.active } @staticmethod def doc(): return { 'email': '<User email address>', 'firs_name': '<User first name>', 'last_name': '<User last name>', 'active': 'True/False' }
class Product(db.Model): id = db.Column(db.String(32), primary_key=True) product_name = db.Column(db.JSON) product_code = db.Column(db.String(32), index=True, unique=True) product_price = db.Column(db.JSON) product_status = db.Column(db.String(32)) product_class_id = db.Column(db.String(32), db.ForeignKey('product_class.id'), index=True) search_list = db.Column(db.ARRAY(db.String), index=True) created = db.Column(db.DateTime, default=datetime.datetime.utcnow) lut = db.Column(db.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow) def __repr__(self): return '<Product %r>' % self.product_name.get('chinese') or \ self.product_name.get('english') def brief(self): return dict( id=self.id, product_name=self.product_name, product_code=self.product_code, ) def to_dict(self): return dict(id=self.id, product_name=self.product_name, product_code=self.product_code, product_price=self.product_price, product_status=self.product_status, product_class=self.product_class and self.product_class.product_class_name)
class ScreenGroup(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) location = db.Column(db.String(100)) active = db.Column(db.Boolean, default=True) deleted = db.Column(db.Boolean, default=False) config_v = db.Column(db.Integer) config_id = db.Column(db.Integer(), db.ForeignKey('configuration.id')) def __init__(self, name, location, active=True, config_id=1): self.name = name self.location = location self.active = active self.config_v = 1 self.config_id = config_id def serialize(self): members = Screen.query.filter_by(group_id=self.id, deleted=False).all() members_list = [screen.serialize() for screen in members] return { 'id': self.id, 'name': self.name, 'location': self.location, 'active': self.active, 'deleted': self.deleted, 'config_id': self.config_id, 'members': members_list } def sanitize_members_config(self): members = members = Screen.query.filter_by(group_id=self.id, deleted=False).all() for member in members: member.config_id = self.config_id db.session.commit() def push_on_the_fly(self): self.config_v = self.config_v + 1 db.session.commit() @staticmethod def doc(): return { 'id': '<Group id>', 'name': '<Group name>', 'location': '<Group location>', 'active': 'True/False', 'deleted': 'True/False', 'config_id': '<Config ID>', 'members': [Screen.doc()] }
class Screen(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) location = db.Column(db.String(100)) group_id = db.Column(db.Integer(), db.ForeignKey('screen_group.id')) active = db.Column(db.Boolean, default=True) deleted = db.Column(db.Boolean, default=False) config_v = db.Column(db.Integer) config_id = db.Column(db.Integer(), db.ForeignKey('configuration.id')) def __init__(self, name, location=None, group_id=0, active=True, config_id=1): self.name = name self.location = location self.group_id = group_id self.active = active self.config_v = 1 self.config_id = config_id def serialize(self): return { 'id': self.id, 'name': self.name, 'location': self.location, 'active': self.active, 'deleted': self.deleted, 'group_id': self.group_id, 'config_id': self.config_id } def push_on_the_fly(self): self.config_v = self.config_v + 1 if self.group_id: group = ScreenGroup.query.filter_by(id=self.group_id).first() if group: group.push_on_the_fly() db.session.commit() @staticmethod def doc(): return { 'id': '<Screen id>', 'name': '<Screen name>', 'location': '<Screen location>', 'active': 'True/False', 'deleted': 'True/False', 'group_id': '<Group id>', 'config_id': '<Config id>' }
class UserModel(db.Model): # Creating SQLAlchemy Model __tablename__ = 'users' # Define the db fields and tables id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(15)) # Create a user object def __init__(self, username): self.username = username # Save current object to database def save_to_db(self): db.session.add(self) db.session.commit() # Class Methods @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first() @classmethod def registerUser(cls, username): return UserModel(username)
class ProductClass(db.Model): id = db.Column(db.String(32), primary_key=True) product_class_name = db.Column(db.JSON) product_class_level = db.Column(db.Integer, index=True) product_list = db.relationship('Product', backref='product_class', lazy='dynamic') product_class_parent_id = db.Column(db.String(32), db.ForeignKey('product_class.id')) children = db.relationship("ProductClass", backref=db.backref('parent', remote_side=[id])) created = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __repr__(self): return '<ProductClass %r>' % self.product_class_name.get('chinese') or \ self.product_class_name.get('english')
class Price(db.Model): id = db.Column(db.String(32), primary_key=True) source = db.Column(db.String(32)) product_code = db.Column(db.String(32)) product_abbr_name = db.Column(db.String(128)) today_opening_price = db.Column(db.DECIMAL()) yesterday_closing_price = db.Column(db.DECIMAL()) current_price = db.Column(db.DECIMAL()) highest_price = db.Column(db.DECIMAL()) lowest_price = db.Column(db.DECIMAL()) buying_price = db.Column(db.DECIMAL()) selling_price = db.Column(db.DECIMAL()) dealed_number = db.Column(db.Integer) dealed_amount = db.Column(db.DECIMAL()) buying_number_1 = db.Column(db.Integer) buying_price_1 = db.Column(db.DECIMAL()) buying_number_2 = db.Column(db.Integer) buying_price_2 = db.Column(db.DECIMAL()) buying_number_3 = db.Column(db.Integer) buying_price_3 = db.Column(db.DECIMAL()) buying_number_4 = db.Column(db.Integer) buying_price_4 = db.Column(db.DECIMAL()) buying_number_5 = db.Column(db.Integer) buying_price_5 = db.Column(db.DECIMAL()) selling_number_1 = db.Column(db.Integer) selling_price_1 = db.Column(db.DECIMAL()) selling_number_2 = db.Column(db.Integer) selling_price_2 = db.Column(db.DECIMAL()) selling_number_3 = db.Column(db.Integer) selling_price_3 = db.Column(db.DECIMAL()) selling_number_4 = db.Column(db.Integer) selling_price_4 = db.Column(db.DECIMAL()) selling_number_5 = db.Column(db.Integer) selling_price_5 = db.Column(db.DECIMAL()) price_date = db.Column(db.DATE()) price_time = db.Column(db.TIME()) status = db.Column(db.String(32)) create_time = db.Column(db.DateTime()) def __repr__(self): return '<Price %r>' % self.id def to_dict(self): return dict( )
class TaskModel(db.Model): # Define the tablename __tablename__ = 'task' # Define the fields/table id = db.Column(db.Integer, primary_key=True) job_id = db.Column(db.String(80)) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) status = db.Column(db.String(20)) result = db.Column(db.String(2000)) created_at = db.Column(db.DateTime) started_at = db.Column(db.DateTime) ended_at = db.Column(db.DateTime) enqueued_at = db.Column(db.DateTime) origin = db.Column(db.String(25)) def __init__(self, job_id, user_id, status=None, result=None, created_at=None, started_at=None, ended_at=None, enqueued_at=None, origin=None): self.job_id = job_id self.user_id = user_id self.status = status self.result = result self.created_at = created_at self.started_at = started_at self.ended_at = ended_at self.enqueued_at = enqueued_at self.origin = origin def save_to_db(self): db.session.add(self) db.session.commit() def update_from_queue(self): try: job = queue.fetch_job(self.job_id) # Update if it's not failed or finished if self.status in ['failed', 'finished']: return self.json() self.status = job.get_status() self.result = job.result self.created_at = job.created_at self.started_at = job.started_at self.ended_at = job.ended_at self.enqueued_at = job.enqueued_at self.origin = job.origin self.save_to_db() except: pass return self.json() def get_job_position(self): try: job = queue.fetch_job(self.job_id).get_position() return job except Exception: return None def json(self, update=False): # Fetch the most current data from the queue if update: self.update_from_queue() return { 'id': self.id, 'job_id': self.job_id, 'user_id': self.user_id, 'status': self.status, 'result': self.result, 'job_position': self.get_job_position(), 'created_at': self.created_at.__str__(), 'started_at': self.started_at.__str__(), 'ended_at': self.ended_at.__str__(), 'enqueued_at': self.enqueued_at.__str__(), 'origin': self.origin } @classmethod def find_by_user_id(cls, user_id): result = cls.query.filter_by(user_id=user_id).first() if result.user_id == current_identity.id: return result else: return None @classmethod def find_by_job_id(cls, job_id): result = cls.query.filter_by(job_id=job_id).first() if result.user_id == current_identity.id: return result else: return None @classmethod def find_by_id(cls, id): result = cls.query.filter_by(id=id).first() if result: if result.user_id == current_identity.id: result.update_from_queue() return result else: return None return None @classmethod def create_task(cls, job_id, user_id, status=None, result=None, created_at=None, started_at=None, ended_at=None, enqueued_at=None, origin=None): return TaskModel(job_id, user_id, status, result, created_at, started_at, ended_at, enqueued_at, origin)
class Configuration(db.Model): id = db.Column(db.Integer, primary_key=True) deleted = db.Column(db.Boolean) description = db.Column(db.String(2000)) head_active = db.Column(db.Boolean) head_height = db.Column(db.String(10)) head_fontSize = db.Column(db.String(10)) head_bgColor = db.Column(db.String(100)) head_textColor = db.Column(db.String(100)) head_borderColor = db.Column(db.String(100)) head_logo_active = db.Column(db.Boolean) head_logo_url = db.Column(db.String(1000)) head_content_active = db.Column(db.Boolean) head_content_text = db.Column(db.String(1000)) head_clock_active = db.Column(db.Boolean) head_clock_textColor = db.Column(db.String(100)) head_clock_bgColor = db.Column(db.String(100)) bottom_active = db.Column(db.Boolean) bottom_content = db.Column(db.String(5000)) bottom_marquee = db.Column(db.Boolean) bottom_height = db.Column(db.String(10)) bottom_fontSize = db.Column(db.String(10)) bottom_bgColor = db.Column(db.String(100)) bottom_textColor = db.Column(db.String(100)) body_background_bgColor = db.Column(db.String(100)) body_background_bgImage = db.Column(db.String(2000)) body_content_fixedContent = db.Column(db.String()) body_content_columns = db.Column(db.String()) def __init__( self, deleted = False, description = ("Nuova configurazione (%s)" % (time.strftime("%d/%m/%Y %H:%M:%S"))), head_active = True, head_height = '70px', head_fontSize = '2em', head_bgColor = '#003459', head_textColor = '#fff', head_borderColor = '#fff', head_logo_active = False, head_logo_url = None, head_content_active = True, head_content_text = 'Digital Signage', head_clock_active = True, head_clock_textColor = '#fff', head_clock_bgColor = '#003459', bottom_active = True, bottom_content = None, bottom_marquee = False, bottom_height = '70px', bottom_fontSize = '2em', bottom_bgColor = '#003459', bottom_textColor = '#fff', body_background_bgColor = '#00A8E8', body_background_bgImage = None, body_content_fixedContent = [], body_content_columns = [] ): self.deleted = deleted self.description = description self.head_active = head_active self.head_height = head_height self.head_fontSize = head_fontSize self.head_bgColor = head_bgColor self.head_textColor = head_textColor self.head_borderColor = head_borderColor self.head_logo_active = head_logo_active self.head_logo_url = head_logo_url self.head_content_active = head_content_active self.head_content_text = head_content_text self.head_clock_active = head_clock_active self.head_clock_textColor = head_clock_textColor self.head_clock_bgColor = head_clock_bgColor self.bottom_active = bottom_active self.bottom_content = bottom_content self.bottom_marquee = bottom_marquee self.bottom_height = bottom_height self.bottom_fontSize = bottom_fontSize self.bottom_bgColor = bottom_bgColor self.bottom_textColor = bottom_textColor self.body_background_bgColor = body_background_bgColor self.body_background_bgImage = body_background_bgImage self.body_content_fixedContent = str(body_content_fixedContent) self.body_content_columns = str(body_content_columns) def serialize(self): return { 'id': self.id, 'deleted': self.deleted, 'description': self.description, 'head': { 'active': self.head_active, 'height': self.head_height, 'fontSize': self.head_fontSize, 'bgColor': self.head_bgColor, 'textColor': self.head_textColor, 'borderColor': self.head_borderColor, 'logo': { 'active': self.head_logo_active, 'url': self.head_logo_url }, 'content': { 'active': self.head_content_active, 'text': self.head_content_text }, 'clock': { 'active': self.head_clock_active, 'textColor': self.head_clock_textColor, 'bgColor': self.head_clock_bgColor } }, 'bottom': { 'active': self.bottom_active, 'content': self.bottom_content, 'marquee': self.bottom_marquee, 'height': self.bottom_height, 'fontSize': self.bottom_fontSize, 'bgColor': self.bottom_bgColor, 'textColor': self.bottom_textColor }, 'body': { 'background': { 'bgColor': self.body_background_bgColor, 'bgImage': self.body_background_bgImage }, 'content': { 'fixedContentText': self.body_content_fixedContent, 'columnsText': self.body_content_columns, 'fixedContent': ast.literal_eval(self.body_content_fixedContent), 'columns': ast.literal_eval(self.body_content_columns) } } } @staticmethod def doc(deleted=False): return { 'id': 0, 'deleted': deleted, 'description': 'Description...', 'head': { 'active': True, 'height': '70px', 'fontSize': '3em', 'bgColor': '#003459', 'textColor': '#fff', 'borderColor': '#fff', 'logo': { 'active': True, 'url': 'logo.png' }, 'content': { 'active': False, 'text': 'Digital Signage' }, 'clock': { 'active': True, 'textColor': '#fff', 'bgColor': '#003459' } }, 'bottom': { 'active': True, 'content': 'Lorem ipsum dolor sit amet', 'marquee': True, 'height': '70px', 'fontSize': '2em', 'bgColor': '#003459', 'textColor': '#fff' }, 'body': { 'background': { 'bgColor': '#00A8E8', 'bgImage': None }, 'content': { 'fixedContent': [ { 'active': True, 'bgColor': '#007EA7', 'textColor': '#fff', 'borderColor': '#fff', 'fontSize': '3em', 'marquee': False, 'content': 'Riunione ore 10 sala 210A' } ], 'columns': [ { 'borderColor': '#fff', 'textColor': '#fff', 'html': 'bla bla' } ] } } }