class Sample(DeclarativeBase): __tablename__ = 'plugproject_samples' uid = Column(Integer, autoincrement=True, primary_key=True) name = Column(Unicode(16)) user_id = Column(Integer, ForeignKey(primary_key(app_model.User))) user = relation(app_model.User)
class TemplateTranslation(DeclarativeBase): __tablename__ = 'mailtemplates_template_translations' _id = Column(Integer, autoincrement=True, primary_key=True) mail_model_id = Column(Integer, ForeignKey(primary_key(MailModel))) mail_model = relation(MailModel, backref=backref('template_translations')) language = Column(Unicode(128), nullable=False) subject = Column(Unicode(500)) body = Column(UnicodeText())
class Registration(DeclarativeBase): __tablename__ = 'registration_registration' uid = Column(Integer, autoincrement=True, primary_key=True) time = Column(DateTime, default=datetime.now) user_name = Column(Unicode(255), nullable=False) email_address = Column(Unicode(255), nullable=False) password = Column(Unicode(255), nullable=False) code = Column(Unicode(255), nullable=False) activated = Column(DateTime) user_id = Column(Integer, ForeignKey(primary_key(app_model.User))) user = relation(app_model.User, uselist=False, backref=backref('registration', uselist=False, cascade='all')) @property def dictified(self): return vars(self) @cached_property def activation_link(self): return url(mount_point('registration') + '/activate', params=dict(code=self.code), qualified=True) @classmethod def generate_code(cls, email): code_space = string.ascii_letters + string.digits def _generate_code_impl(): base = ''.join(random.sample(code_space, 8)) base += email base += str(time.time()) return hashlib.sha1(base.encode('utf-8')).hexdigest() code = _generate_code_impl() while DBSession.query(cls).filter_by(code=code).first(): code = _generate_code_impl() return code @classmethod def clear_expired(cls): for expired_reg in DBSession.query(cls).filter_by(activated=None)\ .filter(Registration.time<datetime.now()-timedelta(days=2)): DBSession.delete(expired_reg) @classmethod def get_inactive(cls, code): return DBSession.query(Registration).filter_by(activated=None)\ .filter_by(code=code).first()
def create(self, actor, verb, target=None, action_obj=None, description=None, extra=None, recipients=None): actor_id = primary_key(actor.__class__).name target_id = primary_key(target.__class__).name if target else '' action_obj_id = primary_key( action_obj.__class__).name if action_obj else '' _recipients = None if recipients: _recipients = [{ '_type': r.__class__.__name__, '_id': instance_primary_key(r) } for r in recipients] return model.provider.create( model.Action, dict( actor_type=actor.__class__.__name__, actor_id=getattr(actor, actor_id), verb=verb, target_type=target.__class__.__name__ if target else None, target_id=getattr(target, target_id, None), action_obj_type=action_obj.__class__.__name__ if action_obj else None, action_obj_id=getattr(action_obj, action_obj_id, None), description=description, extra=extra, _recipients=_recipients, ))
class FlatPage(DeclarativeBase): __tablename__ = 'flatpages_page' uid = Column(Integer, autoincrement=True, primary_key=True) template = Column(Unicode(1024), nullable=False, default=config['_flatpages'].get('templates')[0][0]) slug = Column(Unicode(128), index=True, unique=True, nullable=False) title = Column(Unicode(512), nullable=False) content = Column(Unicode(64000), default='') required_permission = Column(Unicode(256), nullable=True, default=None) updated_at = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) created_at = Column(DateTime, nullable=False, default=datetime.utcnow) author_id = Column(Integer, ForeignKey(primary_key(app_model.User))) author = relation(app_model.User) @classmethod def by_id(cls, _id): return DBSession.query(cls).get(_id) @classmethod def by_slug(cls, slug): return DBSession.query(cls).filter_by(slug=slug).first() @cached_property def url(self): return plug_url('flatpages', '/' + self.slug) @cached_property def html_content(self): format = config['_flatpages']['format'] formatter = FORMATTERS[format] content = self.content if content.startswith('file://'): package_path = config['paths']['root'] file_path = os.path.join(package_path, content[7:]) with closing(open(file_path)) as f: content = f.read() return formatter(content)
class CommentVote(DeclarativeBase): __tablename__ = 'tgcomments_comments_votes' __table_args__ = (Index('idx_comment_voter', "comment_id", "user_id", unique=True), ) uid = Column(Integer, autoincrement=True, primary_key=True) created_at = Column(DateTime, default=datetime.now, nullable=False) value = Column(Integer, default=1) user_id = Column(Integer, ForeignKey(primary_key(app_model.User)), nullable=False) user = relation(app_model.User, backref=backref('comments_votes')) comment_id = Column(Integer, ForeignKey(Comment.uid), nullable=False) comment = relation(Comment, backref=backref('votes'))
class FlatFile(DeclarativeBase): __tablename__ = 'flatpages_file' uid = Column(Integer, autoincrement=True, primary_key=True) name = Column(Unicode(99), index=True, unique=True, nullable=False) file = Column(UploadedFileField(upload_storage='flatfiles'), nullable=False) updated_at = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) created_at = Column(DateTime, nullable=False, default=datetime.utcnow) author_id = Column(Integer, ForeignKey(primary_key(app_model.User))) author = relation(app_model.User) @cached_property def url(self): return plug_url('flatpages', '/flatfiles/' + self.file.file_id)
class GoogleAuth(DeclarativeBase): __tablename__ = 'googleplusauth_info' uid = Column(Integer, autoincrement=True, primary_key=True) registered = Column(Boolean, default=False, nullable=False) just_connected = Column(Boolean, default=False, nullable=False) profile_picture = Column(String(512), nullable=True) user_id = Column(Integer, ForeignKey(primary_key(app_model.User)), nullable=False) user = relation(app_model.User, backref=backref('googleplusauth', uselist=False, cascade='all, delete-orphan')) google_id = Column(Unicode(255), nullable=False, index=True, unique=True) access_token = Column(UnicodeText, nullable=False) access_token_expiry = Column(DateTime, nullable=False) @classmethod def ga_user_by_google_id(cls, google_id): google_auth_user = DBSession.query(cls).filter_by(google_id=google_id).first() return google_auth_user @classmethod def googleplusauth_user(cls, user_id): return DBSession.query(cls).filter_by(user_id=user_id).first()
def get_entity_descriptor(cls, entity): Type = entity.__class__ type_primary_key = primary_key(Type) return Type.__name__, getattr(entity, type_primary_key.name)
class Comment(DeclarativeBase): __tablename__ = 'tgcomments_comments' __table_args__ = (Index('idx_commented_entity', "entity_type", "entity_id"), ) uid = Column(Integer, autoincrement=True, primary_key=True) body = Column(UnicodeText, nullable=False) created_at = Column(DateTime, default=datetime.now, nullable=False) hidden = Column(Boolean, default=False, nullable=False) user_id = Column(Integer, ForeignKey(primary_key(app_model.User)), nullable=True) user = relation(app_model.User, backref=backref('comments')) author_username = Column(Unicode(2048), default='anon') author_name = Column(Unicode(2048), nullable=False) author_pic = Column(Unicode(2048), nullable=True) entity_id = Column(Integer, nullable=False, index=True) entity_type = Column(Unicode(255), nullable=False, index=True) @property def voters(self): return DBSession.query(app_model.User).join(CommentVote).filter( CommentVote.comment_id == self.uid) @property def my_vote(self): values = [ vote.value for vote in self.votes if vote.user_id == instance_primary_key(tg.request.identity['user']) ] return values[0] if len(values) != 0 else None @property def rank(self): return sum((v.value for v in self.votes)) def votes_by_value(self, v): return DBSession.query(CommentVote).filter_by( comment_id=self.uid).filter_by(value=v).all() @classmethod def get_entity_descriptor(cls, entity): Type = entity.__class__ type_primary_key = primary_key(Type) return Type.__name__, getattr(entity, type_primary_key.key) @classmethod def comments_for(cls, entity, hidden='auto'): entity_type, entity_id = cls.get_entity_descriptor(entity) comments = DBSession.query(cls).filter_by(entity_type=entity_type)\ .filter_by(entity_id=entity_id) if not (hidden==True or \ (hidden=='auto' and tg.request.identity and cls.manager_permission())): comments = comments.filter_by(hidden=False) return comments.order_by(cls.created_at.desc()).all() @staticmethod def manager_permission(): return in_group('tgcmanager') or has_permission('tgcmanager') @classmethod def add_comment(cls, entity, user, body): entity_type, entity_id = cls.get_entity_descriptor(entity) c = Comment(body=body, entity_type=entity_type, entity_id=entity_id) if isinstance(user, dict): c.author_username = user.get('user_name') c.author_name = user['name'] c.author_pic = user.get('avatar') else: c.user = user c.author_username = user.user_name c.author_name = user.display_name c.author_pic = get_user_avatar(user) DBSession.add(c) return c