def update_permission(user, role): from app.libs.db import db_session from app.models import Permission user.role |= Permission.get_by_role(role).bit db_session.add(user) db_session.commit()
def mark_as_read(self): self.unread = False try: db_session.add(self) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise
def update(self, content): self.content = content try: db_session.add(self) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise
def delete(self): try: db_session.delete(self.profile) db_session.delete(self) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise
def create(cls, role): p = cls(role=role) try: db_session.add(p) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return p
def create(cls, username, content): user = User.get_by_name(username) a = cls(author_id=user.id, content=content) try: db_session.add(a) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return a
def create(cls, username, comment_id): user = User.get_by_name(username) cu = cls(user_id=user.id, comment_id=comment_id) try: db_session.add(cu) db_session.commit() except (DataError, IntegrityError, ProgrammingError): cls.rollback() raise return cu
def create(cls, username, post_id): user = User.get_by_name(username) f = cls(user_id=user.id, post_id=post_id) try: db_session.add(f) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return f
def create(cls, username, blocked_name): user = User.get_by_name(username) blocked = User.get_by_name(blocked_name) b = cls(user_id=user.id, blocked_id=blocked.id) try: db_session.add(b) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return b
def create(cls, author_name, topic_id, content): user = User.get_by_name(author_name) now = functions.now() Topic.get(topic_id)._new_comment(now) c = cls(author_id=user.id, topic_id=topic_id, content=content, date=now) try: db_session.add(c) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return c
def create(cls, sender_name, recipient_name, message): sender = User.get_by_name(sender_name) recipient = User.get_by_name(recipient_name) pm = cls(sender_id=sender.id, recipient_id=recipient.id, message=message) try: db_session.add(pm) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return pm
def update(self, description=None, rules=None, avatar=None, state=None): if description: self.description = description if rules: self.rules = rules if state: self.state = state try: db_session.add(self) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.roolback() raise
def create(cls, name, created_name, avatar, description, rules, why, state=0): user = User.get_by_name(created_name) t = Topic( name=name, admin_id=user.id, avatar=avatar, description=description, rules=rules, why=why, state=state ) try: db_session.add(t) db_session.commit() user.update_permission(Roles.TopicEdit.format(t.id)) except (DataError, IntegrityError, ProgrammingError): cls.rollback() raise return t
def update(self, keywords=None, content=None, keep_silent=False, is_draft=None): if keywords: self.keywords = keywords self.content = content self.keep_silent = keep_silent if is_draft: self.is_draft = is_draft try: db_session.add(self) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise
def create(cls, sender_name, recipient_name, activity_type, header, content): sender = User.get_by_name(sender_name) recipient = User.get_by_name(recipient_name) n = cls(sender_id=sender.id, recipient_id=recipient.id, activity_type=activity_type, header=header, content=content) try: db_session.add(n) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return n
def create(cls, author_name, topic_id, title, keywords, content="", keep_silent=False, is_draft=False): user = User.get_by_name(author_name) p = cls( topic_id=topic_id, author_id=user.id, title=title, keywords=keywords, content=content, keep_silent=keep_silent, is_draft=is_draft, ) try: db_session.add(p) db_session.commit() user.update_permission(Roles.PostEdit.format(p.id)) except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return p
def update(self, **kwargs): for k, v in kwargs.items(): if hasattr(self, k): setattr(self, k, v) elif hasattr(self.profile, k): if k == 'gold': v += getattr(self.profile, k) setattr(self.profile, k, v) if self.profile.gold >= Level['gold']['topic_creation']: self.role |= Permission.get_by_role('topic_creation').bit if self.profile.gold >= Level['gold']['vote']: self.role |= Permission.get_by_role('vote').bit try: db_session.add(self) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise
def create(cls, **kwargs): user_attrs = dict() profile_attrs = dict() for k, v in kwargs.items(): if hasattr(cls, k): user_attrs[k] = v elif hasattr(Profile, k): profile_attrs[k] = v profile = Profile(**profile_attrs) user_attrs.update({'profile': profile}) user = cls(**user_attrs) try: db_session.add(user) db_session.add(profile) db_session.commit() except (DataError, IntegrityError, ProgrammingError): db_session.rollback() raise return user
def check_proposal(topic_id): from app.libs.db import db_session from app.models import Topic, User, TopicUpVote, TopicDownVote from app.settings import Gold user_count = User.count() topic = Topic.get(topic_id) user = User.get(topic.admin_id) up_votes = TopicUpVote.count_by_topic(topic_id) down_votes = TopicDownVote.count_by_topic(topic_id) # Check if the proposal can be accepted or rejected. if (up_votes - down_votes) > (user_count / 2): topic.state = 1 user.profile.gold += Gold['proposal_accepted'] else: topic.state = -1 user.profile.gold += Gold['proposal_rejected'] db_session.add(user) db_session.add(topic) db_session.commit()
def update_permission(self, role): p = Permission.create(role) self.role |= p.bit db_session.add(self) db_session.commit()