class ApplicationVersion(db.Model): __tablename__ = 'application_versions' __table_args__ = (db.UniqueConstraint('app_name', 'app_version'), ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) app_name = db.Column(db.String(128), nullable=False) app_version = db.Column(db.String(128), nullable=False) build_id = db.Column(db.Integer, db.ForeignKey('builds.id')) build = db.relationship('Build')
class CrontabSchedule(Model): """ Task result/status. """ __tablename__ = "crontab_schedule" __table_args__ = (db.UniqueConstraint('minute', 'hour', 'day_of_week', 'day_of_month', 'month_of_year'), ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) minute = db.Column(db.String(length=120), default="*") hour = db.Column(db.String(length=120), default="*") day_of_week = db.Column(db.String(length=120), default="*") day_of_month = db.Column(db.String(length=120), default="*") month_of_year = db.Column(db.String(length=120), default="*") periodic_tasks = db.relationship('PeriodicTask') def __str__(self): rfield = lambda f: f and str(f).replace(' ', '') or '*' return '{0} {1} {2} {3} {4} (m/h/d/dM/MY)'.format( rfield(self.minute), rfield(self.hour), rfield(self.day_of_week), rfield(self.day_of_month), rfield(self.month_of_year), ) @property def schedule(self): spec = { 'minute': self.minute, 'hour': self.hour, 'day_of_week': self.day_of_week, 'day_of_month': self.day_of_month, 'month_of_year': self.month_of_year } return schedules.crontab(**spec) # noinspection PyProtectedMember @classmethod def from_schedule(cls, session, schedule): spec = { 'minute': schedule._orig_minute, 'hour': schedule._orig_hour, 'day_of_week': schedule._orig_day_of_week, 'day_of_month': schedule._orig_day_of_month, 'month_of_year': schedule._orig_month_of_year } obj = cls.filter_by(session, **spec).first() if obj is None: return cls(**spec) else: return obj
class ApplicationVersion(db.Model): __tablename__ = 'application_versions' __table_args__ = (db.UniqueConstraint('value', 'application_id'), ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) value = db.Column(db.String(128), nullable=False) application_id = db.Column(db.Integer, db.ForeignKey('applications.id'), nullable=False) environment_id = db.Column(db.Integer, db.ForeignKey('environments.id'), nullable=False) enabled = db.Column(db.Boolean, nullable=False, default=True) def __lt__(self, other): return self.value < other.value
class MessageReaction(InternalAPIMixin, db.Model): __tablename__ = 'message_reactions' __table_args__ = (db.UniqueConstraint('message_id', 'user_id', 'value'), ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) value = db.Column(db.String(32)) message_id = db.Column(db.Integer, db.ForeignKey('messages.id', ondelete='CASCADE')) user_id = db.Column(db.Integer) @property def request_user(self): return partial(self.internal_request, 'login', 'get_user') async def get_user(self) -> RemoteUser: data = await self.request_user(user_id=self.user_id) return RemoteUser(**data)
class VotingMember(InternalAPIMixin, db.Model): __tablename__ = 'voting_members' __table_args__ = ( db.UniqueConstraint('user_id', 'voting_id'), ) id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, nullable=False) voting_id = db.Column(db.Integer, db.ForeignKey('voting.id'), nullable=False) result = db.Column(ScalarListType(), nullable=False) created = db.Column(db.DateTime, default=timezone.now) updated = db.Column(db.DateTime, onupdate=timezone.now) voted = db.Column(db.Boolean, default=False) enabled = db.Column(db.Boolean, default=True) async def get_user(self) -> RemoteUser: data = await self.internal_request('login', 'get_user', user_id=self.user_id) return RemoteUser(**data) @as_future def vote(self, items: Optional[list] = None) -> None: items = self.result or items if not items: raise VotingError('No items to vote') if not self.voting.active: raise VotingError('Voting not active') if not self.enabled: raise VotingError('Voting member disabled') if self.voted: raise VotingError('Already voted') if len(items) != self.voting.select_count: raise VotingError('Voting items count: %s/%s' % (len(items), self.voting.select_count)) if not set(items).issubset(self.voting.items): raise VotingError('Voting items is %s. Must be a subset ' 'of %s' % (','.join(items), ','.join(self.voting.items))) self.result = items self.voted = True self.save() @as_future def discard(self) -> None: if self.voting.can_discard: self.delete() else: raise VotingError('Cannot discard')
class EventParticipation(InternalAPIMixin, db.Model): __tablename__ = 'event_participations' __table_args__ = ( db.UniqueConstraint('user_id', 'event_id'), ) STATUSES = ( ('joined', _('Joined')), ('leaved', _('Leaved')) ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) created_at = db.Column(db.DateTime, nullable=False, default=timezone.now) status = db.Column(ChoiceType(STATUSES)) payload = db.Column(JSONType, nullable=False, default={}) user_id = db.Column(db.Integer, nullable=False) event_id = db.Column(db.Integer, db.ForeignKey('events.id'), nullable=False) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.Schema = self.get_schema_class() @classmethod def get_schema_class(cls): class _Schema(ma.Schema): class Meta: model = cls fields = ('payload', 'created_at', 'status', 'event') return _Schema def dumps(self) -> dict: return self.Schema().dump(self).data async def on_status_changed(self) -> None: user = await self.get_user() msg = { 'type': EVENT_PARTICIPATION_STATUS_CHANGED, 'data': self.dumps() } await user.send_message(message=json.dumps(msg), content_type='application/json') async def get_user(self): return await self.internal_request('login', 'get_user', user_id=self.user_id)
class Item(db.Model): __tablename__ = 'items' __table_args__ = (db.UniqueConstraint('store_id', 'name'), ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(128), nullable=True) store_id = db.Column(db.Integer, db.ForeignKey('stores.id')) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) payload = db.Column(JSONType, nullable=False, default={}) orders = db.relationship('Order', backref='item', lazy='dynamic') created = db.Column(db.DateTime, default=timezone.now) active = db.Column(db.Boolean, nullable=False, default=True) def description(self, lang): raise NotImplementedError def title(self, lang): raise NotImplementedError
class BaseApplicationVersion(db.Model): __abstract__ = True __table_args__ = (db.UniqueConstraint('value', 'application_id'), ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) active = db.Column(db.Boolean, nullable=False, default=True) value = db.Column(db.String(128), nullable=False) @declared_attr def application_id(self): return db.Column(db.Integer, db.ForeignKey('applications.id'), nullable=False) @classmethod def latest(cls): pass def __lt__(self, other): return self.value < other.value