class Commentary(JsonSerializerMixin, SurrogatePK, Model): __tablename__ = 'commentaries' RELATIONSHIPS_TO_DICT = True content = Column(db.Text, nullable=False) created_at = Column(db.DateTime, default=dt.datetime.utcnow()) user_id = reference_col('users', nullable=False) user = relationship('models.User') action_id = reference_col('actions', nullable=False) action = relationship('Action') is_journal = Column(db.Boolean, nullable=False, default=False) @staticmethod def count_for_actions(action_ids: list): return db.session.query( Commentary.action_id, func.count(Commentary.action_id))\ .filter( and_( Commentary.action_id.in_(action_ids), Commentary.is_journal == None ) ).group_by(Commentary.action_id)\ .all()
class Tags(JsonSerializerMixin, SurrogatePK, Model): __tablename__ = 'tags' RELATIONSHIPS_TO_DICT = True name = Column(db.Text, nullable=False) parent_id = reference_col('tags', nullable=True) sons = relationship('Tags') user_id = reference_col('users', nullable=True) rank = Column(db.Integer, default=1) def __init__(self, name, parent_id, user_id, rank): self.name = name self.parent_id = parent_id self.user_id = user_id self.rank = rank @classmethod def get_tree(cls, user_id): subquery = Tags.query.filter(or_(Tags.user_id == user_id, Tags.user_id is None)) return subquery.filter(Tags.rank == 1).all() @staticmethod def build_tags_slug(tags_slug, new_tags): # setup tags slug tags_slug_splitted = tags_slug.split('-') ith_zero = 0 good_slug = [] last_parent_id = None for nb in tags_slug_splitted: if nb == '0': tag = new_tags[ith_zero] if tag.parent_id is None: tag.update(parent_id=last_parent_id) good_slug.append(str(tag.id)) ith_zero += 1 last_parent_id = tag.id else: good_slug.append(nb) last_parent_id = int(nb) return '-'.join(good_slug) @staticmethod def create_all(tags, user_id): return [ Tags.create(name=tag['name'], parent_id=tag['parent_id'], user_id=user_id, rank=tag['rank']) for tag in tags ] @staticmethod def delete_sons(tag): for t in tag.sons: t.delete()
class UserAction(JsonSerializerMixin, SurrogatePK, Model): __tablename__ = 'user_actions' RELATIONSHIPS_TO_DICT = True user_id = reference_col('users', nullable=False) user = relationship('models.User') action_id = reference_col('actions', nullable=False) action = relationship('models.Action') created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) start_date = Column(db.DateTime, nullable=False) end_date = Column(db.DateTime, nullable=False) last_succeed = Column(db.DateTime, nullable=True) nb_succeed = Column(db.Integer, nullable=False, default=0) tags = db.relationship('UserActionTagMapping', uselist=True) def __init__(self, user_id, action_id, start_date, end_date, tag=None): self.user_id = user_id self.action_id = action_id self.start_date = start_date self.end_date = end_date self.tag = tag @staticmethod def get_count_for_action_ids(action_ids): return db.session.query( UserAction.action_id, func.count(UserAction.action_id) ).filter(UserAction.action_id.in_(action_ids))\ .group_by(UserAction.action_id)\ .all() def has_been_realised_today(self): if self.last_succeed is None: return False now = dt.utcnow(self) return self.last_succeed.year == now.year and self.last_succeed.day == now.day def have_to_do_it(self, date): if not isinstance(date, dt.datetime): raise ValueError('date not an instance of datetime') return self.start_date.date() <= date.date() <= self.end_date.date() def realised(self): return self.update(last_succeed=dt.datetime.utcnow(), nb_succeed=self.nb_succeed + 1) def custom_delete(self): db.engine.execute( text( 'begin transaction;' 'delete from user_action_tag_mapping where user_action_id = :uaid;' 'delete from user_actions where id = :uaid;' 'end TRANSACTION;'), **{'uaid': self.id})
class Resource(JsonSerializerMixin, SurrogatePK, Model): __tablename__ = 'resources' user_id = reference_col('users', nullable=False) user = relationship('models.User', backref='resources') action_id = reference_col('actions', nullable=False) action = relationship('Action', backref='resources') nblike = Column(db.BigInteger, default=0) url = Column(db.Text, nullable=True) content = Column(db.Text, nullable=True) def __init__(self, user_id, action_id, url, content): self.user_id = user_id self.action_id = action_id self.content = content self.url = url
class Action(JsonSerializerMixin, SurrogatePK, Model): __tablename__ = 'actions' RELATIONSHIPS_TO_DICT = True title = Column(db.String(200), nullable=False) description = Column(db.Text, nullable=False) image_url = Column(db.Text, nullable=True) initial_nb_days = Column(db.Integer, default=1) public = Column(db.Boolean, default=True) is_event = Column(db.Boolean, default=False) created_at = Column(db.DateTime, default=dt.datetime.utcnow) default_tag = Column(db.Text, nullable=True) creator_user_id = reference_col('users', nullable=False) creator = relationship('models.User') user_likes = Column(JSONB)
class Followings(JsonSerializerMixin, SurrogatePK, Model): __tablename__ = 'followings' followed_user_id = reference_col('users', nullable=False) followed_users = relationship('models.User', foreign_keys=[followed_user_id], backref='following_users') following_user_id = reference_col('users', nullable=False) following_users = relationship('models.User', foreign_keys=[following_user_id], backref='followed_users') created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
class User(JsonSerializerMixin, UserMixin, SurrogatePK, Model): __tablename__ = 'users' RELATIONSHIPS_TO_DICT = True username = Column(db.String(80), unique=True, nullable=False) email = Column(db.String(80), unique=True, nullable=False) password = Column(db.Binary(128), nullable=True) #: The hashed password created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) first_name = Column(db.String(30), nullable=True) last_name = Column(db.String(30), nullable=True) has_image = Column(db.Boolean(), default=False) active = Column(db.Boolean(), default=False) is_admin = Column(db.Boolean(), default=False) points = Column(db.Integer, default=0) connexions = Column(JSONB) def __init__(self, username, email, password=None, **kwargs): db.Model.__init__(self, username=username, email=email, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): self.password = bcrypt.generate_password_hash(password) def check_password(self, value): return bcrypt.check_password_hash(self.password, value) @property def full_name(self): return '{0} {1}'.format(self.first_name, self.last_name) def user_actions(self, requested_date): with open( './bemychange/model/user/sql/user_actions.sql') as file_handle: content = file_handle.read() rows = db.engine.execute( text(content), **{ 'requested_date': requested_date, 'user_id': self.get_id() }) return rows.first()['result'] or [] @staticmethod def find(user_id): rows = db.engine.execute( text( 'select row_to_json(_) as result from (select * from users where id = :user_id)_' ), **{'user_id': user_id}) return rows.first()['result'] or None
class Role(SurrogatePK, Model): __tablename__ = 'roles' name = Column(db.String(80), unique=True, nullable=False) user_id = reference_col('users', nullable=True) user = relationship('models.User', backref='roles')
class UserActionTagMapping(JsonSerializerMixin, SurrogatePK, Model): __tablename__ = 'user_action_tag_mapping' user_action_id = reference_col('user_actions', nullable=False) tag_slug = Column(db.Text, default="")