class Contact(DB.Model): id = DB.Column(DB.Integer, primary_key=True) first_name = DB.Column(DB.String(255), nullable=False) last_name = DB.Column(DB.String(255)) number = DB.Column(DB.Integer, nullable=False) __table_args__ = (DB.UniqueConstraint('first_name', 'last_name', 'number'), ) @classmethod def create(cls, **kwargs): kwargs['number'] = _clean_phone_number(kwargs['number']) contact = Contact(**kwargs) DB.session.add(contact) DB.session.commit() return contact def serialize(self): return { 'id': self.id, 'first_name': self.first_name, 'last_name': self.last_name, 'number': _format_phone_number(self.number) } def delete(self): DB.session.delete(self) DB.session.commit() return {'success': True}
class UserSolve(Model): """ A user's solve for a specific scramble, in a specific event, at a competition. Solve times are in centiseconds (ex: 1234 = 12.34s).""" __tablename__ = 'user_solves' __table_args__ = (DB.UniqueConstraint( 'scramble_id', 'user_event_results_id', name='unique_scramble_user_results'), ) id = Column(Integer, primary_key=True) time = Column(Integer) is_dnf = Column(Boolean, default=False) is_inspection_dnf = Column(Boolean, default=False) is_plus_two = Column(Boolean, default=False) scramble_id = Column(Integer, ForeignKey('scrambles.id')) user_event_results_id = Column(Integer, ForeignKey('user_event_results.id')) UserEventResults = relationship("UserEventResults") fmc_explanation = Column(Text) def get_total_time(self): """ Returns the solve's time with +2s penalty counted, if applicable. """ return (self.time + 200) if self.is_plus_two else self.time def get_friendly_time(self): """Returns a user-friendly representation of this solve's overall time, including penalties. """ if not self.time: return '' # TODO: handle blind DNFs, which show "DNF(time here)" if self.is_dnf: return 'DNF' total_time = self.get_total_time() if self.UserEventResults.is_fmc: converted_value = int(total_time) / 100 if converted_value == int(converted_value): converted_value = int(converted_value) return converted_value if self.UserEventResults.is_mbld: return build_mbld_results(total_time) converted_to_friendly = convert_centiseconds_to_friendly_time( total_time) if self.is_plus_two: converted_to_friendly = converted_to_friendly + '+' return converted_to_friendly
class StudGroup(DB.Model, _ObjectWithYear): """Модель сущности 'Студенческая группа'""" __tablename__ = 'stud_group' __table_args__ = ( DB.UniqueConstraint( 'stud_group_year', 'stud_group_semester', 'stud_group_num', 'stud_group_subnum' ), ) id = DB.Column('stud_group_id', DB.SMALLINT, primary_key=True, autoincrement=True) year = DB.Column('stud_group_year', DB.SMALLINT, nullable=False) semester = DB.Column('stud_group_semester', DB.SMALLINT, nullable=False) num = DB.Column('stud_group_num', DB.SMALLINT, nullable=False) subnum = DB.Column('stud_group_subnum', DB.SMALLINT, nullable=False, default=0) active = DB.Column('stud_group_active', DB.BOOLEAN, nullable=False, default=True) students = DB.relationship('Student', lazy=True, backref='stud_group', order_by="Student.surname, Student.firstname, Student.middlename") curriculum_units = DB.relationship( 'CurriculumUnit', lazy=True, backref='stud_group', order_by="CurriculumUnit.id" ) @property def num_print(self): """Свойство для представления группы и номера группы""" if self.num is None or self.subnum is None: return None return "%d.%d" % (self.num, self.subnum) if self.subnum != 0 else str(self.num) def __repr__(self): return "StudGroup(id={id}, " \ "year={year}, " \ "semester={semester}, " \ "num={num}, " \ "subnum={subnum}, " \ "active={active})" \ .format(id=self.id, year=self.year, semester=self.semester, num=self.num, subnum=self.subnum, active=self.active)
class CurriculumUnit(DB.Model): """Класс для сущности 'Единица учебного плана'""" __tablename__ = 'curriculum_unit' __table_args__ = ( DB.UniqueConstraint('subject_id', 'stud_group_id'), ) id = DB.Column('curriculum_unit_id', DB.INTEGER, primary_key=True, autoincrement=True) subject_id = DB.Column(DB.ForeignKey('subject.subject_id'), nullable=False, index=True) stud_group_id = DB.Column(DB.ForeignKey('stud_group.stud_group_id'), nullable=False, index=True) teacher_id = DB.Column(DB.ForeignKey('teacher.teacher_id'), nullable=False, index=True) mark_type = DB.Column('mark_type', DB.Enum(MarkType), nullable=False) hours_att_1 = DB.Column('hours_att_1', DB.SMALLINT, nullable=False) hours_att_2 = DB.Column('hours_att_2', DB.SMALLINT, nullable=False) hours_att_3 = DB.Column('hours_att_3', DB.SMALLINT, nullable=False) subject = DB.relationship('Subject') teacher = DB.relationship('Teacher') att_marks = DB.relationship('AttMark', lazy=True, backref='curriculum_unit') teaching_lessons = DB.relationship( 'TeachingLesson', secondary='teaching_lesson_and_curriculum_unit' ) def __repr__(self): return "CurriculumUnit(id={id}, subject_id={subject_id}, stud_group_id={stud_group_id}," \ " teacher_id={teacher_id}," \ " mark_type={mark_type}, hours_att_1={hours_att_1}," \ " hours_att_2={hours_att_2}," \ " hours_att_3={hours_att_3})" \ .format(id=self.id, subject_id=self.subject_id, stud_group_id=self.stud_group_id, teacher_id=self.teacher_id, mark_type=self.mark_type, hours_att_1=self.hours_att_1, hours_att_2=self.hours_att_2, hours_att_3=self.hours_att_3)
class AttMark(DB.Model): """Класс для сущности 'Аттестационная оценка'""" __tablename__ = 'att_mark' __table_args__ = ( DB.UniqueConstraint('curriculum_unit_id', 'student_id'), ) att_mark_id = DB.Column(DB.INTEGER, primary_key=True, autoincrement=True) curriculum_unit_id = DB.Column( DB.ForeignKey('curriculum_unit.curriculum_unit_id'), nullable=False ) student_id = DB.Column(DB.ForeignKey('student.student_id'), nullable=False, index=True) att_mark_1 = DB.Column(DB.SMALLINT) att_mark_2 = DB.Column(DB.SMALLINT) att_mark_3 = DB.Column(DB.SMALLINT) att_mark_exam = DB.Column(DB.SMALLINT) att_mark_append_ball = DB.Column(DB.SMALLINT) student = DB.relationship('Student') def __repr__(self): return "AttMark(att_mark_id={att_mark_id}," \ " curriculum_unit_id={curriculum_unit_id}," \ " student_id={student_id}," \ " att_mark_1={att_mark_1}," \ " att_mark_2={att_mark_2}," \ " att_mark_3={att_mark_3}," \ " att_mark_exam={att_mark_exam}," \ " att_mark_append_ball={att_mark_append_ball})". \ format(att_mark_id=self.att_mark_id, curriculum_unit_id=self.curriculum_unit_id, student_id=self.student_id, att_mark_1=self.att_mark_1, att_mark_2=self.att_mark_2, att_mark_3=self.att_mark_3, att_mark_exam=self.att_mark_exam, att_mark_append_ball=self.att_mark_append_ball)