class Boarding_passes(db.Model): __tablename__ = 'boarding_passes' __table_args__ = ( db.UniqueConstraint('flight_id', 'boarding_no', name='boarding_passes_flight_id_boarding_no_key'), db.UniqueConstraint('flight_id', 'seat_no', name='boarding_passes_flight_id_seat_no_key'), db.PrimaryKeyConstraint('ticket_no', 'flight_id', name='boarding_passes_pkey'), db.ForeignKeyConstraint( ['ticket_no', 'flight_id'], ['ticket_flights.ticket_no', 'ticket_flights.flight_id'], name='boarding_passes_ticket_no_fkey')) ticket_no = db.Column(CHAR(13), nullable=False) flight_id = db.Column(INTEGER, nullable=False) boarding_no = db.Column(INTEGER, nullable=False) seat_no = db.Column(CHAR(4), nullable=False) def __init__(self, ticket_no, flight_id, boarding_no, seat_no): self.ticket_no = ticket_no self.flight_id = flight_id self.boarding_no = boarding_no self.seat_no = seat_no
class PersonHasRole(db.Model, ChangesMixin): __tablename__ = 'person_has_role' id = db.Column(db.Integer, primary_key=True, autoincrement=True) __table_args__ = ( db.UniqueConstraint('person_id', 'function', name='_person_has_role_function_uc'), db.UniqueConstraint('person_id', 'document_id', name='_person_has_role_document_uc'), ) person_id = db.Column(db.Integer, db.ForeignKey('person.id', ondelete='CASCADE'), nullable=False) document_id = db.Column(db.Integer, db.ForeignKey('document.id', ondelete='CASCADE'), nullable=False) person_role_id = db.Column( db.Integer, db.ForeignKey('person_role.id', ondelete='CASCADE')) function = db.Column(db.String(100), nullable=True) person = db.relationship("Person", backref=db.backref("persons_having_roles"), single_parent=True) document = db.relationship("Document", backref=db.backref( "persons_having_roles", cascade="all, delete-orphan"), single_parent=True) person_role = db.relationship("PersonRole", backref=db.backref("persons_having_roles"), single_parent=True)
class user(db.Model): __tablename__ = 'user' __table_args__ = ( db.UniqueConstraint('initials', 'tenant_uuid'), db.UniqueConstraint('email', 'tenant_uuid'), ) id = db.Column(db.Integer, primary_key=True) uuid = db.Column(db.String(), nullable=False, unique=True) initials = db.Column(db.String(), nullable=False) name = db.Column(db.String(), nullable=False) email = db.Column(db.String(), nullable=False) phone = db.Column(db.String(), nullable=False) role = db.Column(db.String(), nullable=False) active = db.Column(db.Boolean()) locked = db.Column(db.Boolean()) contact = db.Column(db.Boolean()) confirmed = db.Column(db.Boolean()) tenant_uuid = db.Column(db.String(), nullable=False) createdBy = db.Column(db.String(), nullable=False) created = db.Column(db.DateTime(), nullable=False) modifiedBy = db.Column(db.String()) modified = db.Column(db.DateTime()) groups = db.relationship('group', secondary=user_groups, backref=db.backref('userGroups', lazy='dynamic')) risks = db.relationship('risk', backref='riskOwner', lazy='dynamic', passive_deletes='all')
class WikiPage(db.Model, WikiBase, ModificationInfoMixin): __tablename__ = 'wiki_page' __searchable__ = ['title'] __table_args__ = ( db.UniqueConstraint('title'), db.UniqueConstraint('slug'), )
class Approvevpn(db.Model): __tablename__ = 'approvevpn' id = db.Column(db.Integer, primary_key=True) public_id = db.Column(db.String(80), unique=True, nullable=False) request_id = db.Column(db.String(80), unique=False, nullable=False) user_id = db.Column(db.String(80), unique=False, nullable=False) approved_by = db.Column(db.String(80), unique=False, nullable=False) remarks = db.Column(db.String(80), unique=False, nullable=False) approve_as = db.Column(db.Enum('1', '3', '4', '00', name="approve_as"), nullable=False) approved_at = db.Column(db.DateTime, nullable=False, default=datetime.now) __table_args__ = ( db.UniqueConstraint(request_id, approve_as), db.UniqueConstraint(request_id, approved_by), )
class user_depo_accounts(db.Model): id = db.Column(db.Integer, primary_key=True) accountId = db.Column(db.Integer, db.ForeignKey('depo_accounts.id'), index=True) userId = db.Column(db.Integer, db.ForeignKey('users.id'), index=True) branchId = db.Column(db.Integer, db.ForeignKey('branches.id'), index=True) __table_args__ =( db.UniqueConstraint('accountId', 'userId', name='con1'), db.UniqueConstraint('branchId', 'userId', name='con2'), ) def __init__(self, accountId, userId, branchId): self.accountId = accountId self.userId = userId self.branchId = branchId
class Commentary(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) doc_id = db.Column(db.Integer, db.ForeignKey('document.id', ondelete='CASCADE')) type_id = db.Column(db.Integer, db.ForeignKey('commentary_type.id', ondelete='CASCADE'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE')) content = db.Column(db.Text) type = db.relationship("CommentaryType", backref="commentary") notes = association_proxy('commentary_has_note', 'note') __table_args__ = ( db.UniqueConstraint('doc_id', 'user_id', 'type_id', name='UniqueCommentaryType'), ) def notes_of_user(self, user_id): return [ dict({"ptr_start": chn.ptr_start, "ptr_end": chn.ptr_end}, **(chn.note.serialize())) for chn in self.commentary_has_note if chn.note.user_id == int(user_id)] def serialize(self): return { 'doc_id': self.doc_id, 'user_id': self.user_id, 'type': self.type.serialize(), 'content': self.content, 'notes': self.notes_of_user(self.user_id) }
class Meal(db.Model): """ This is a meal, there will be one entry per meal per day Takes list_id, name and order """ __tablename__ = "meals" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(250), nullable=False) order = db.Column(db.Integer, nullable=False) __table_args__ = (db.UniqueConstraint("list_id", "name"), ) list_id = db.Column( db.Integer, db.ForeignKey("list.id", ondelete="CASCADE"), nullable=False, ) list_ = db.relationship("List", backref=db.backref("meals", passive_deletes=True)) # backref entries -> Entry def __repr__(self): return "<Meal {} of List {}>".format(self.name, self.list_.name) def to_dict(self): return {"id": self.id, "name": self.name}
class ActivityVersion(EventBaseModel, Period): backref_base_name = "activity_revisions" activity_id = db.Column( db.Integer, db.ForeignKey("activity.id"), index=True, nullable=False ) activity = db.relationship("Activity", backref=backref("revisions")) version = db.Column(db.Integer, nullable=False) context = db.Column(JSONB(none_as_null=True), nullable=True) @property def type(self): return self.activity.type __table_args__ = ( db.UniqueConstraint( "version", "activity_id", name="unique_version_among_same_activity_versions", ), db.Constraint( name="activity_version_start_time_before_reception_time" ), db.Constraint(name="activity_version_end_time_before_reception_time"), db.Constraint(name="activity_version_start_time_before_end_time"), ) def __repr__(self): return f"<Revision [{self.id}] of {self.activity}>"
class Corpus_Engine(db.Model): __tablename__ = 'corpus_engine' id = db.Column(db.Integer, primary_key=True) corpus_id = db.Column(db.Integer, db.ForeignKey('corpus.id')) engine_id = db.Column(db.Integer, db.ForeignKey('engine.id')) phase = db.Column(db.String(64)) is_info = db.Column( db.Boolean, default=False ) # Whether the corpus is used for training or information purposes selected_size = db.Column(db.Integer) engine = db.relationship(Engine, backref=db.backref("engine_corpora", cascade="all, delete-orphan")) corpus = db.relationship("Corpus", backref=db.backref("corpus_engines")) __table_args__ = (db.UniqueConstraint('corpus_id', 'engine_id', 'phase'), ) def __init__(self, corpus=None, engine=None, phase=None, is_info=False, selected_size=None): self.corpus = corpus self.engine = engine self.phase = phase self.is_info = is_info self.selected_size = selected_size
class SectionTranslation(db.Model): __tablename__ = 'section_translation' __table_args__ = tuple([ db.UniqueConstraint('section_id', 'language', name='uq_section_id_language') ]) id = db.Column(db.Integer(), primary_key=True) section_id = db.Column(db.Integer(), db.ForeignKey('section.id'), nullable=False) language = db.Column(db.String(2), nullable=False) name = db.Column(db.String(255), nullable=False) description = db.Column(db.String(), nullable=False) show_for_values = db.Column(db.JSON(), nullable=True) section = db.relationship('Section', foreign_keys=[section_id]) def __init__(self, section_id, language, name, description, show_for_values=None): self.section_id = section_id self.language = language self.name = name self.description = description self.show_for_values = show_for_values
class NameMapping(db.Model): id = db.Column(db.Integer, primary_key=True) description = db.Column(db.String(120)) # bank entered name name = db.Column(db.String(120)) # user entered name exact_match = db.Column(db.Boolean(), default=True) __table_args__ = (db.UniqueConstraint("description"), )
class ObservationLocation(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) farm_id = db.Column(db.Integer, db.ForeignKey('farm.id', ondelete='CASCADE')) field_id = db.Column(db.Integer, db.ForeignKey('field.id', ondelete='CASCADE')) crop_field_id = db.Column( db.Integer, db.ForeignKey('crop_field.id', ondelete='CASCADE')) access_id = db.Column(db.Integer, db.ForeignKey('accessibility_status.id'), nullable=False) owner_id = db.Column(db.Integer, db.ForeignKey('owner.id', ondelete='CASCADE'), nullable=False) observations = db.relationship("Observation", backref="observation_location", passive_deletes=True) __table_args__ = (db.UniqueConstraint('farm_id', 'field_id', 'crop_field_id', 'owner_id', name='uix_observation_loc'), ) def __init__(self, farm_id, field_id, crop_field_id, access_id, owner_id): self.farm_id = farm_id self.field_id = field_id self.crop_field_id = crop_field_id self.access_id = access_id self.owner_id = owner_id
class ObservedContext(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) context_type = db.Column(db.Enum(ObservedContextType), default=ObservedContextType.ENVIRONMENT, nullable=False) context = db.Column(db.String(50), nullable=False) parameter_id = db.Column(db.Integer, db.ForeignKey('parameter_type.id'), nullable=False) observations = db.relationship('Observation', backref="observed_context") __table_args__ = (db.UniqueConstraint('context_type', 'context', 'parameter_id', name='uix_context'), ) def __init__(self, context_type, context, parameter_id): self.context_type = context_type self.context = context self.parameter_id = parameter_id def to_json(self, param_type): if param_type: param = param_type.get_param() else: param = None return { 'context_type': self.context_type.value, 'context': self.context, 'parameter': param }
class InsuranceBusinessOwner(BusinessOwner): __tablename__ = 'insurance_business_owners' id = db.Column(db.Integer, db.ForeignKey('business_owners.id'), primary_key=True) branch_code = db.Column(db.String(6), nullable=False) company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable=False) company = db.relationship("Company") __mapper_args__ = { 'polymorphic_identity': 'InsuranceBusinessOwner', } __table_args__ = (db.UniqueConstraint('branch_code', name='unique_branch_code'), ) # __tablename__ = 'insurance_business_owners' # id = db.Column(db.Integer, db.ForeignKey('business_ownerss.id'), primary_key=True) # branch_code = db.Column(db.String(20), nullable=True) # __mapper_args__ = { # 'polymorphic_identity':'InsuranceBusinessOwner', # # 'polymorphic_on': 'business_owner_type' # } def __repr__(self): return '<InsuranceBusinessOwner %r>' % self.id
class Game(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(64), nullable=False) typ = db.Column(db.String(64)) db.UniqueConstraint('name', 'typ', name='NameTypUnique') args = db.Column(db.String(500)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) answers = db.relationship('Answer', backref='game', lazy='dynamic') def __repr__(self): return '<Game {};{}:{}>'.format(self.id, self.name, self.typ) @property def eval(self): if self.typ == '*': games = list( Game.query.filter_by(name=self.name).filter( Game.typ.notlike('%*%'))) return random.choice(games) if '*' in self.typ: games = list( Game.query.filter_by(name=self.name).filter( Game.typ.notlike('%*%')).filter( Game.typ.like(self.typ.replace('*', '%')))) return random.choice(games) return self
class Selection(db.Model): __tablename__ = "selection" sid = db.Column(db.Integer, primary_key=True) # 選課唯一編號 user_id = db.Column(db.Integer, db.ForeignKey( # 使用者 uid 'user.uid'), nullable=False) # course_id = db.Column(db.Integer, db.ForeignKey( # 課程 cid 'course.cid'), nullable=False) # priority = db.Column(db.Integer, default=0) # 優先權 # timestamp = db.Column(db.DateTime(timezone=True), # 選擇時間紀錄 default=datetime.datetime.utcnow) __table_args__ = (db.UniqueConstraint( 'user_id', 'course_id', name='unique_index_selection'), ) def __init__(self, user_id=0, course_id=0, priority=0): self.user_id = user_id self.course_id = course_id self.priority = priority def to_json(self): return {'sid': self.sid, 'user_id': self.user_id, 'course_id': self.course_id, 'priority': self.priority, 'timestamp': self.timestamp, } def __repr__(self): return "<Selection '%r'>" % self.sid
class Observation(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) observed_context_id = db.Column( db.Integer, db.ForeignKey('observed_context.id'), nullable=False, ) eq_id = db.Column(db.Integer, nullable=True) conditions = db.Column(JSONB, nullable=True) location_id = db.Column(db.Integer, db.ForeignKey('observation_location.id', ondelete='CASCADE'), nullable=False) unit_id = db.Column(db.Integer, db.ForeignKey('unit.id'), nullable=False) logs = db.relationship("SensingLog", backref="observation", passive_deletes=True) __table_args__ = (db.UniqueConstraint('observed_context_id', 'conditions', name='uix_observation'), ) def __init__(self, observed_context_id, eq_id, conditions, location_id, unit_id): self.observed_context_id = observed_context_id self.eq_id = eq_id self.conditions = conditions self.location_id = location_id self.unit_id = unit_id
class ServicesDefinition(BaseMixin, db.Model): __tablename__ = 'services_definition' service_grade = db.Column(String(50), nullable=False) # TODO pay attention to name="cat" db.Enum('M', 'F', name='gender_types') https://stackoverflow.com/questions/24254775/error-when-running-migrations-sqlalchemy-exc-compileerror-postgresql-enum-type service_category = db.Column(db.Enum("AutoService", "Insurance", "CarWash", name="service_category"), nullable= False) pay = db.Column(Numeric, nullable=False) question_set_id = db.Column(db.Integer, db.ForeignKey('question_sets.id')) question_set = db.relationship("QuestionSet", backref=db.backref("services_definition", uselist=False)) service_type_id = db.Column(db.Integer, db.ForeignKey('service_types.id')) service_type = db.relationship("ServiceType", back_populates="service_definitions") __table_args__ = (db.UniqueConstraint('service_grade', 'service_type_id', name='unique_services'),) @staticmethod def load_service_by_id(service_definition_id=None, service_type_id=None): try: query = ServicesDefinition.load_service_query_builder(service_definition_id, service_type_id) return True, query.first() except: return db_error_message(logger) @staticmethod def load_service_query_builder(service_definition_id, service_type_id): query = db.session.query(ServicesDefinition) if service_definition_id is not None: query = query.filter(ServicesDefinition.id == service_definition_id) if service_type_id is not None: query = query.filter(ServicesDefinition.service_type_id == service_type_id) return query
class Art(BaseModel, TagBase, RefBase): # Table definitions __table_args__ = (db.UniqueConstraint('title', 'source'), ) id = db.Column(db.Integer, primary_key=True) artist = db.Column(db.Text) title = db.Column(db.Text) description = db.Column(db.Text) source = db.Column(db.Text) # Table associations tags = db.relationship('Tag', secondary=art_tags, lazy='dynamic', backref=db.backref('art', lazy=True)) refs = db.relationship('Reference', secondary=art_refs, lazy='dynamic', backref=db.backref('art', lazy=True)) # Instance functions def __repr__(self): return '<Art: {} by {}>'.format(self.title, self.artist) def is_tagged(self, tag): return super().is_tagged(tag, art_tags) def is_referenced(self, ref): return super().is_referenced(ref, art_refs)
class FeatureRequest(db.Model): ''' Model class for FeatureRequest. Contains fields and methods for class. ''' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) description = db.Column(db.String(100)) client = db.Column(db.String(100)) date = db.Column(db.DateTime) priority = db.Column(db.Integer) productarea = db.Column(db.String(100)) constraint = db.UniqueConstraint('client', 'priority', name='clientpriority') def __init__(self, name, description, client, date, priority, productarea): self.name = name self.description = description self.client = client self.date = datetime.strptime(date, "%Y-%m-%d") if self.date.date() < datetime.now().date(): raise Exception('Date cannot be in the past') self.priority = priority self.productarea = productarea self.__table_args__ = (self.constraint)
class Indicator(db.Model): __tablename__ = "indicator" id = db.Column(db.Integer, primary_key=True) value = db.Column(db.String(64), index=True, nullable=False) data_type = db.Column(db.String(64), nullable=False) direction = db.Column(db.String(64), nullable=False) ttl = db.Column(db.Integer, nullable=False) created = db.Column(db.DateTime, nullable=False) last = db.Column(db.DateTime, nullable=False, onupdate=datetime.datetime.utcnow) details = db.Column(db.Text()) source = db.Column(db.String(64), nullable=False) __table_args__ = (db.UniqueConstraint("value", "data_type", "source"), ) def __init__(self, value, ttl, data_type, source, direction='both', details='{}'): if not (ttl in TTL_VALUES and data_type in DATA_TYPES and direction in DIRECTIONS and source in SOURCES): raise Exception("Incorrect ttl, data_type, or direction") rex = VALIDATE.get(data_type) if rex and not re.search(rex, value): raise Exception("Value doesn't match data_type") self.value = value self.source = source self.direction = direction self.ttl = ttl self.data_type = data_type self.details = details self.created = datetime.datetime.utcnow() self.last = datetime.datetime.utcnow() def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def __repr__(self): return '<Indicator %r: %r>' % (self.source, self.value)
class Indicator(db.Model): __tablename__ = "indicator" id = db.Column(db.Integer, primary_key=True) ioc = db.Column(db.String(64), index=True, nullable=False) comment = db.Column(db.String(255)) enrich = db.Column(db.String(255)) enrich_full = db.Column(db.Text()) first_seen = db.Column(db.DateTime, nullable=False) last_seen = db.Column(db.DateTime, index=True, nullable=False) pending = db.Column(db.Boolean, nullable=False) event_id = db.Column(db.Integer, db.ForeignKey('event.id'), nullable=False) control_id = db.Column(db.Integer, db.ForeignKey('control.id'), nullable=False) itype_id = db.Column(db.Integer, db.ForeignKey('itype.id'), nullable=False) control = db.relationship('Control', foreign_keys=control_id) itype = db.relationship('Itype', foreign_keys=itype_id) rel_indicators = db.relationship('Links', backref='indicator', lazy='dynamic') mitigations = db.relationship('Mitigation', backref='indicator', lazy='dynamic') __table_args__ = (db.UniqueConstraint("ioc", "event_id", "itype_id", "control_id"), ) @hybrid_property def rel_list(self): return ','.join([str(i.rel_event_id) for i in self.rel_indicators]) def __init__(self, event_id, ioc, comment, control, itype, pending=False, enrich=None, enrich_full=None): self.event_id = event_id self.ioc = ioc self.comment = comment self.control = control self.itype = itype self.pending = pending self.enrich = enrich self.enrich_full = enrich_full self.first_seen = datetime.datetime.utcnow() self.last_seen = datetime.datetime.utcnow() def as_dict(self): return '%s' % { c.name: getattr(self, c.name) for c in self.__table__.columns } def __repr__(self): return '<Indicator %r>' % (self.ioc)
class Book(db.Model): """ Class for storing books in the vocabulary. Books are unique across the whole application. """ __table_args__ = (db.UniqueConstraint('title', 'author', 'language', name='unique_book_identification'), ) id = db.Column(db.Integer, primary_key=True, nullable=False) title = db.Column(db.String(100), unique=False, nullable=False, index=True) author = db.Column(db.String(200), unique=False, nullable=False) language = db.Column(db.String(2), unique=False, nullable=False) vocabularies = db.relationship('Vocabulary', backref='book', lazy=True) def json(self): """ Return a web service appropriate JSON representation""" obj = self.__dict__.copy() obj.pop('_sa_instance_state') return obj def __repr__(self): return '<Book %s %s %s>' % (self.title, self.author, self.language) def __str__(self): return 'Book %s, %s, %s' % (self.title, self.author, self.language)
class ReviewSectionTranslation(db.Model): __tablename__ = 'review_section_translation' __table_args__ = tuple([ db.UniqueConstraint('review_section_id', 'language', name='uq_review_section_id_language') ]) id = db.Column(db.Integer(), primary_key=True) review_section_id = db.Column(db.Integer(), db.ForeignKey('review_section.id'), nullable=False) language = db.Column(db.String(2), nullable=False) headline = db.Column(db.String(), nullable=True) description = db.Column(db.String(), nullable=True) review_section = db.relationship('ReviewSection', foreign_keys=[review_section_id]) def __init__(self, review_section_id, language, headline=None, description=None): self.review_section_id = review_section_id self.language = language self.headline = headline self.description = description
class Vocabulary(db.Model): """ Class for storing a particular vocabulary. The vocabulary is referencing the book in order to allow for a normalised data model and holds a letterLimit attribute to define the minimum world length taken into account. """ __table_args__ = (db.UniqueConstraint( 'catalogueId', 'bookId', name='unique_vocabulary_assignment'), ) id = db.Column(db.Integer, primary_key=True, nullable=False) catalogueId = db.Column(db.Integer, db.ForeignKey('catalogue.id'), nullable=False) bookId = db.Column(db.Integer, db.ForeignKey('book.id'), nullable=False) letterLimit = db.Column(db.Integer, primary_key=False, nullable=False) words = db.relationship('WordUsage', backref='vocabulary', lazy=True) def json(self): """ Return a web service appropriate JSON representation""" obj = self.__dict__.copy() obj.pop('_sa_instance_state') wordsJson = [] for word in self.words: wordsJson.append(word.json()) obj['words'] = wordsJson return obj def __repr__(self): return '<Vocabulary %s %s>' % (self.catalogue.user.userName, self.book.title) def __str__(self): return 'Vocabulary of User %s and Book %s, holding %d words' % ( self.catalogue.user.userName, self.book.title, len(self.words))
class Entry(db.Model): """ One entry in the food planner Takes day_id, key (e.g lunch) and value (e.g. spaghetti) """ __tablename__ = "entry" id = db.Column(db.Integer, primary_key=True) value = db.Column(db.String(256), nullable=False, default="") __table_args__ = (db.UniqueConstraint("day_id", "meal_id"), ) day_id = db.Column(db.Integer, db.ForeignKey("day.id", ondelete="CASCADE"), nullable=False) day = db.relationship("Day", backref=db.backref("entries", passive_deletes=True)) meal_id = db.Column(db.Integer, db.ForeignKey("meals.id", ondelete="CASCADE")) meal = db.relationship("Meal", backref=db.backref("entries", passive_deletes=True)) def __repr__(self): return "<Entry {} of Day {} in List {}>".format( self.id, self.day.day, self.day.list_.name) def to_dict(self): return {"key": self.meal.name, "id": self.id, "value": self.value}
class SectionModel(BaseModel): __tablename__ = 'section' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) department = db.Column(db.Integer, ForeignKey('department.id')) year = db.Column(db.Integer) size = db.Column(db.Integer) courses = relationship('CourseModel', secondary='section_course_link') __table_args__ = ( db.UniqueConstraint(name, year), ) def to_json(self): return dict( ID=self.id, name=self.name, size=self.size, year=self.year, department=self.department, courses=list(map(lambda x: x.to_json(), self.courses)), ) @classmethod def find_by_name(cls, kls): return cls.query.filter_by(name=kls)
class Translation(db.Model): __table_args__ = ( db.UniqueConstraint('doc_id', 'user_id', name='uix_user'), ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) doc_id = db.Column(db.Integer, db.ForeignKey('document.id', ondelete='CASCADE')) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE')) content = db.Column(db.Text) notes = association_proxy('translation_has_note', 'note') # notes = db.relationship("TranslationHasNote", back_populates="translation", cascade="all, delete-orphan") def notes_of_user(self, user_id): return [ dict({"ptr_start": thn.ptr_start, "ptr_end": thn.ptr_end}, **(thn.note.serialize())) for thn in self.translation_has_note if thn.note.user_id == int(user_id)] def serialize_for_user(self, user_id): return { 'id': self.id, 'doc_id': self.doc_id, 'user_id': self.user_id, 'content': self.content, 'notes': self.notes_of_user(user_id) }
class Project(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(255), nullable=False) job_number = db.Column(db.String(40), nullable=False) assigned_to_id = db.Column(db.Integer(), db.ForeignKey('user.id')) description = db.Column(db.String(500)) start_date = db.Column(db.DateTime()) completion_date = db.Column(db.DateTime()) site_id = db.Column(db.Integer(), db.ForeignKey('site.ID', ondelete='CASCADE')) ITP = db.relationship('ITP', backref='project') tickets = db.relationship('FlicketTicket', backref='project', lazy='dynamic') __table_args__ = (db.UniqueConstraint('site_id', 'name', name='_site_name_uc'), ) def __init__(self, name, job_number, description, site_id): self.name = name self.job_number = job_number self.description = description self.site_id = site_id self.start_date = datetime.datetime.now() def __repr__(self): return self.name