class TargetMember(db.Model): __tablename__ = 'target_member' target_id = db.Column( UUID(as_uuid=True), db.ForeignKey('target.id'), primary_key=True, ) member_id = db.Column( UUID(as_uuid=True), db.ForeignKey('member.id'), primary_key=True, ) member = db.relationship( 'Member', foreign_keys=[member_id], ) target = db.relationship( 'Target', foreign_keys=[target_id], ) def init(self, member_id, target_id): self.member_id = member_id self.target_id = target_id def to_dict(self): return dict( member_id=self.member_id, target_id=self.target_id, )
class Logic(db.Model): __tablename__ = 'logic' id = db.Column(db.Integer, primary_key=True) project_id = db.Column(db.Integer, db.ForeignKey('projects.id')) component_1_id = db.Column(db.Integer, db.ForeignKey('components.id')) component_2_id = db.Column(db.Integer, db.ForeignKey('components.id')) message_type = db.Column( db.Enum('BasicEvent', 'RequestEvent', 'Response', 'NewEvent', name='message_type')) __table_args__ = (UniqueConstraint("project_id", "component_1_id", "component_2_id", "message_type"), ) @classmethod def generate_fake(cls, project, component1, component2): from random import randint message_type = Logic.message_type.property.columns[0].type.enums[ randint(0, 1)] fake = Logic(project_id=project.id, component_1_id=component1.id, component_2_id=component2.id, message_type=message_type) db.session.add(fake) db.session.commit() return fake def __repr__(self): return '<Logic project:%d component.No1:%d component.No2:%d message type:%r>' % ( self.project_id, self.component_1_id, self.component_2_id, self.message_type)
class Transaction(db.Model): _tablename__ = 'transactions' id = db.Column(db.Integer, primary_key=True) created_at = db.Column(db.DateTime, default=datetime.utcnow) amount = db.Column(db.Float) status = db.Column(db.Integer) plan = db.Column(db.Integer, db.ForeignKey('plans.id')) owner = db.Column(db.Integer, db.ForeignKey('users.id'))
class ConversationMember(db.Model): id = db.Column(db.UUID(as_uuid=True),default=uuid.uuid4, primary_key=True) conversation_id = db.Column(db.UUID, db.ForeignKey('conversation.id')) member = db.Column(db.String, db.ForeignKey('user.id')) avatar_URL = db.Cloumn(db.String) status = db.Cloumn(db.String) created = DateTimeField(default=datetime.datetime.now) last_open =
class UserContact(db.Model): user = db.Column(db.String, db.ForeignKey('user.id')) contact = db.Column(db.String, db.ForeignKey('user.id')) conversation = db.Column(db.UUID, db.ForeignKey('conversation.id')) unread_messges_count = IntField() last_open = DateTimeField() avatar = StringField() title = StringField()
class Follow(db.Model): __tablename__ = 'follows' follower_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) followed_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) timestamp = db.Column(db.DateTime, default=datetime.utcnow)
class Star(db.Model): __tablename__ = 'stars' id = db.Column(db.Integer, primary_key=True) component_id = db.Column(db.Integer, db.ForeignKey('components.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) amount = db.Column(db.Integer, default=0) __table_args__ = (UniqueConstraint("component_id", "user_id"), )
class Video(db.Model): __tablename__ = 'video' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) title = db.Column(db.String(64), nullable=True) text = db.Column(db.String(512), nullable=True) video_url = db.Column(db.String(256), nullable=False) thumbnaiUrl = db.Column(db.String(256), nullable=True)
class Sound(db.Model): __tablename__ = 'sound' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) time = db.Column(db.TIME, nullable=True) singer = db.Column(db.String(64), nullable=True) sound_url = db.Column(db.String(256), nullable=False) name = db.Column(db.String(128), nullable=True)
class CompulsoryPurchaseOrderInvestigation(db.Model, OrderedMixin): compulsory_purchase_order_investigation = db.Column(db.Integer(), primary_key=True, nullable=False) status = db.Column(db.String()) inspector_report_url = db.Column(db.String()) decision_url = db.Column(db.String()) start_date = db.Column(db.Date()) end_date = db.Column(db.Date()) compulsory_purchase_order_id = db.Column( db.String, db.ForeignKey('compulsory_purchase_order.compulsory_purchase_order')) compulsory_purchase_order = db.relationship( 'CompulsoryPurchaseOrder', back_populates='investigations') def is_final_state(self): return self.status in final_states def __hash__(self): return hash(self.compulsory_purchase_order_investigation) def __eq__(self, other): return self.compulsory_purchase_order_investigation == other.compulsory_purchase_order_investigation
class Event(db.Model): __tablename__ = 'events' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), nullable=False) description = db.Column(db.String(80), nullable=False) date = db.Column(db.Date, nullable=False) time = db.Column(db.Time, nullable=False) type = db.Column(ChoiceType(EventType), nullable=False) category = db.Column(ChoiceType(CategoryType), nullable=False) location_id = db.Column(db.Integer, db.ForeignKey("locations.id")) location = db.relationship("Location", back_populates="events", lazy='joined') address = db.Column(db.String(80), nullable=False) seats = db.Column(db.Integer) enrollments = db.relationship("Enrollment", back_populates="event", lazy='joined') participants = db.relationship('Participant', secondary='enrollments', back_populates='events', lazy='joined') def __str__(self): return f'{self.title}'
class UserData(db.Model, CRUDMixin, TimeMixin): """ collection of user data, from query used as cache, data encrpted with aes """ id = db.Column(db.UUID(as_uuid=True), default=uuid.uuid4, primary_key=True) data = db.Column(db.String()) name = db.Column(db.String()) user_id = db.Column(db.String, db.ForeignKey('user.id')) status = db.Column(db.String()) # client_id = StringField(default='client_') def to_json(self): return { 'data': self.data, 'name': self.name, 'created': self.created, 'status': self.status } def get_client(self): client = redis_store.get(user_data.client_id, None) return client def lock_save(self, pw): in_len = len(self.data) pad_size = 16 - (in_len % 16) self.data = self.data.ljust(in_len + pad_size, chr(pad_size)) if len(pw) < 16: pw += '0' * (16 - len(pw)) iv = binascii.a2b_hex('000102030405060708090a0b0c0d0e0f') obj = AES.new(pw, AES.MODE_CBC, iv) self.data = str(binascii.b2a_base64(obj.encrypt(self.data)))[2:-2] self.save()
class UserAbility(db.Model, UserMixin): __tablename__ = 'user_ability' id = db.Column(db.Integer, primary_key=True) aid = db.Column(db.Integer) uid = db.Column(db.Integer, db.ForeignKey('users.id')) __table_args__ = ( UniqueConstraint("aid", "uid"), )
class StudentClass(db.Model, CRUDMixin, TimeMixin): student_id = db.Column(db.String, db.ForeignKey('undergraduate_student.id'), primary_key=True) class_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey('class.id'), primary_key=True) # grade_1 = db.Column(db.Integer, default=None) # grade_2 = db.Column(db.Integer, default=None) grade = db.Column(db.Float, default=None) point = db.Column(db.Float, default=None) _class = db.relationship("Class", back_populates="students") student = db.relationship("UndergraduateStudent", back_populates="classes") @property def is_grade_register(self): return self.grade_1 is not None and self.grade_2 is not None
class Component(db.Model): __tablename__ = 'components' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), nullable=False) deploy_version = db.Column(db.String(16)) private = db.Column(db.Boolean, default=True) mean = db.Column(db.Float(precision=1), default=0) owner_id = db.Column(db.Integer, db.ForeignKey('users.id')) def generate_token(self): s = Serializer(current_app.config['SECRET_KEY']) return s.dumps({'id': self.id}) @classmethod def verify_token(cls, token): """ :raises BadSignature :param token: :return: component object that match token """ s = Serializer(current_app.config['SECRET_KEY']) data = s.loads(token) component = cls.query.get(data['id']) return component def component_files(self): return [ filename for filename in os.listdir( current_app.config['COMPONENT_UPLOAD_FOLDER']) if filename.startswith(str(self.id)) ] def to_json(self): return { "pid": self.id, "name": self.name, "deploy_version": self.deploy_version, "private": self.private } @classmethod def generate_fake(cls, user): from shutil import copyfile from random import randint fake = Component(name='Fake Component', deploy_version=randint(0, 10), owner_id=user.id) db.session.add(fake) db.session.commit() """apply deploy version again""" copyfile( current_app.config['FAKE_UPLOAD'], os.path.join(current_app.config['UPLOAD_FOLDER'], 'components', '%s.%s' % (str(fake.id), 'jar'))) return fake def __repr__(self): return '<Component %r>' % self.name
class Class(db.Model, CRUDMixin, TimeMixin): id = db.Column(db.UUID(as_uuid=True), default=uuid.uuid4, primary_key=True) course_id = db.Column(db.String, db.ForeignKey('course.id'), index=True) term = db.Column(db.String, index=True) class_id = db.Column(db.String, index=True) teacher_id = db.Column(db.String, db.ForeignKey('teacher.id'), index=True) teacher_raw = db.Column(db.String) time = db.Column(db.String, index=True) classroom = db.Column(db.String) capacity = db.Column(db.Integer, default=0, index=True) enroll = db.Column(db.Integer, default=0) campus = db.Column(db.String, index=True) q_time = db.Column(db.String) q_place = db.Column(db.String) credit = db.Column(db.String, index=True) status = db.Column(db.String) students = db.relationship('StudentClass', back_populates="_class") __table_args__ = (db.UniqueConstraint('class_id', 'course_id', 'term'), )
class GraduateStudent(User): id = db.Column(db.String, db.ForeignKey('user.id'), primary_key=True) __mapper_args__ = { 'polymorphic_identity': 'graduate_student', } def __unicode__(self): return self.name
class Book(db.Model): __tablename__ = 'book' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) title = db.Column(db.String(64), nullable=True) author = db.Column(db.String(256), nullable=True) page_number = db.Column(db.Integer, nullable=True) book_url = db.Column(db.String(256), nullable=False) thumbnaiUrl = db.Column(db.String(256), nullable=True)
class UndergraduateStudent(User): id = db.Column(db.String, db.ForeignKey('user.id'), primary_key=True) # classes = db.relationship('Class', secondary=student_classes, lazy='subquery', # backref=db.backref('dtudents', lazy=True)) classes = db.relationship('StudentClass', back_populates="student") __mapper_args__ = { 'polymorphic_identity': 'undergraduate_student', } def __unicode__(self): return self.name
class Enrollment(db.Model): __tablename__ = 'enrollments' __table_args__ = (db.UniqueConstraint( 'event_id', 'participant_id', name='event-participant-constraint'), ) id = db.Column(db.Integer, primary_key=True) event_id = db.Column(db.Integer, db.ForeignKey("events.id")) event = db.relationship("Event", back_populates="enrollments", lazy='joined') participant_id = db.Column(db.Integer, db.ForeignKey("participants.id")) participant = db.relationship("Participant", back_populates="enrollments", lazy='joined') datetime = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) def __str__(self): return f'{self.id}'
class Project(db.Model): __tablename__ = 'projects' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) private_key = db.Column(db.String(100)) create_date = db.Column(db.Date) owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) plan_id = db.Column(db.Integer, db.ForeignKey('plans.id')) @classmethod def generate_fake(cls, user): fake = Project(name="Fake Project", owner_id=user.id, private_key=str(uuid4()), create_date=datetime.utcnow()) db.session.add(fake) db.session.commit() return fake def __repr__(self): return '<Project %r>' % self.name
class Message(db.Model, SoftDeleteMixin): __tablename__ = 'message' id = db.Column( UUID(as_uuid=True), default=uuid4, primary_key=True ) body = db.Column( db.String, nullable=False, ) sender_id = db.Column( UUID(as_uuid=True), db.ForeignKey('member.id') ) reciever_id = db.Column( UUID(as_uuid=True), db.ForeignKey('member.id') ) target_id = db.Column( UUID(as_uuid=True), db.ForeignKey('target.id'), ) sender = db.relationship( "Member", foreign_keys=[sender_id], uselist=False ) reciever = db.relationship( "Member", foreign_keys=[reciever_id], uselist=False ) def __init__(self, body, sender_id, reciever_id, target_id): self.body = body self.sender_id = sender_id self.reciever_id = reciever_id self.target_id = target_id
class Post(db.Model, CRUDMixin, TimeMixin): id = db.Column(db.UUID(as_uuid=True), default=uuid.uuid4, primary_key=True) author_id = db.Column(db.String, db.ForeignKey('user.id')) type = db.Column(db.String) # lost or found tags = db.Column(db.ARRAY(db.String, dimensions=1)) title = db.Column(db.String) category = db.Column(db.String) content = db.Column(db.String) img_URLs = db.Column(postgresql.ARRAY(db.String, dimensions=1)) site = db.Column(db.String) hit = db.Column(db.Integer, default=0) author = db.relationship('User', backref=db.backref( 'posts', lazy=True))
class CorpusResult(db.Model): """Data model for result returned from Watson NLU service.""" __tablename___ = 'corpus_result' id = db.Column(db.Integer, primary_key=True) corpus_id = db.Column(db.Integer, db.ForeignKey("corpus.id"), nullable=False) name = db.Column(db.String(255), nullable=False) data = db.Column(db.BINARY, nullable=True) def __repr__(self): return '<Corpus Result {}>'.format(self.id)
def reference_col(tablename, nullable=False, pk_name="id", foreign_key_kwargs=None, column_kwargs=None): foreign_key_kwargs = foreign_key_kwargs or {} column_kwargs = column_kwargs or {} return Column(db.ForeignKey("{0}.{1}".format(tablename, pk_name), **foreign_key_kwargs), nullable=nullable, **column_kwargs)
class ConversationMessage(db.Model): id = db.Column(db.UUID(as_uuid=True),default=uuid.uuid4, primary_key=True) conversation_id = db.Column(db.UUID, db.ForeignKey('conversation.id')) from_user_id = db.Column(db.String, db.ForeignKey('user.id')) content = db.Column(db.String) read = db.Column(db.Boolean,default=False) created = DateTimeField(default=datetime.datetime.now) # expire_from = DateTimeField(default=lambda: datetime.datetime(2100, 1, 1)) def to_dict(self): return { 'created': str(self.created), 'sender': self.sender.card_id, 'content': self.content } @classmethod def pre_save(cls, sender, document, **kwargs): if document.sort == 'private' and document.read: document.expire_from = datetime.datetime.now() def __unicode__(self): return self.content
class Applicant(db.Model): __tablename__ = 'applicants' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False) phone = db.Column(db.String(20), nullable=False) email = db.Column(db.String(80), nullable=False) status = db.Column(ChoiceType(ApplicantStatus), nullable=False) group_id = db.Column(db.Integer, db.ForeignKey("groups.id")) group = db.relationship("Group", back_populates="applicants", lazy='joined') def __str__(self): return f'{self.name}'
class SocialOAuth(db.Model, CRUDMixin, TimeMixin): id = db.Column(db.UUID(as_uuid=True), default=uuid.uuid4, primary_key=True) user_id = db.Column(db.String, db.ForeignKey('user.id')) session_key = db.Column(db.String()) source = db.Column(db.String()) site = db.Column(db.String()) site_uid = db.Column(db.String()) site_uname = db.Column(db.String()) unionid = db.Column(db.String()) open_id = db.Column(db.String()) access_token = db.Column(db.String()) refresh_token = db.Column(db.String()) expire_date = db.Column(db.DateTime) def bind_user(self, user_id): self.user_id = user_id self.save()
class Teacher(User): id = db.Column(db.String, db.ForeignKey('user.id'), primary_key=True) # degree = db.Column(db.String()) # sex = db.Column(db.String()) # title = db.Column(db.String()) # education = db.Column(db.String()) # dept = db.Column(db.String()) # cs = db.Column(db.String()) # intro = db.Column(db.String()) __mapper_args__ = { 'polymorphic_identity': 'teacher', } def __unicode__(self): return self.name
class ShopMember(db.Model): __tablename__ = 'shop_members' id = db.Column(db.Integer, primary_key=True) mobile_number = db.Column(db.String(100)) is_manager = db.Column(db.Boolean, default=False) shop_id = db.Column(db.Integer, db.ForeignKey('shops.id')) shop = db.relationship("Shop", back_populates="members") @classmethod def create(cls, mobile_number, is_manager, shop_id): shop_member = ShopMember(mobile_number=mobile_number, is_manager=is_manager, shop_id=shop_id) db.session.add(shop_member) db.session.commit() return shop_member