Ejemplo n.º 1
0
class Microblog(db.Model):
    __tablename__ = 'microblog'

    id = db.Column(db.Integer, primary_key=True)
    people_id = db.Column(
        db.Integer,
        db.ForeignKey(People.id, ondelete='CASCADE'),
        nullable=False
    )
    parent_microblog_id = db.Column(db.Integer, db.ForeignKey('microblog.id', ondelete='SET NULL'))
    content = db.Column(db.Text, nullable=False)
    post_time = db.Column(db.DateTime, default=datetime.datetime.now)
    comments = db.relationship(
        'Comment', backref='microblog', lazy='dynamic',
        passive_deletes=True)

    reposts = db.relationship(
        'Microblog', backref=db.backref('parent_microblog', remote_side=id),
        lazy='dynamic'
    )

    def __init__(self, people_id, content, parent_microblog_id=None):
        self.people_id = people_id

        if parent_microblog_id:
            self.parent_microblog_id = parent_microblog_id
            parent_microblog = Microblog.query.get(parent_microblog_id)
            self.content = content + ' //' + parent_microblog.people.nickname + ': ' + parent_microblog.content
        else:
            self.content = content
Ejemplo n.º 2
0
class Comment(db.Model):
    __tablename__ = 'comment'

    id = db.Column(db.Integer, primary_key=True)
    people_id = db.Column(
        db.Integer,
        db.ForeignKey(People.id, ondelete='CASCADE'),
        nullable=False
    )
    microblog_id = db.Column(
        db.Integer,
        db.ForeignKey(Microblog.id, ondelete='CASCADE'),
        nullable=False
    )
    parent_comment_id = db.Column(
        db.Integer,
        db.ForeignKey('comment.id', ondelete='CASCADE'),
    )
    content = db.Column(db.Text, nullable=False)
    comment_time = db.Column(db.DateTime, default=datetime.datetime.now)

    child_comments = db.relationship(
        'Comment',
        backref=db.backref('parent_comment', remote_side=id),
        lazy='dynamic',
    )

    def __init__(self, people_id, content, microblog_id, parent_commnet_id=None):
        self.people_id = people_id
        self.content = content
        self.microblog_id = microblog_id
        self.parent_comment_id = parent_commnet_id
Ejemplo n.º 3
0
class AssociationHashTags(db.Model):
    __tablename__ = 'association_hash_tags'

    post_id = db.Column(db.Integer,
                        db.ForeignKey('posts.id'),
                        primary_key=True)
    hashtag_id = db.Column(db.Integer,
                           db.ForeignKey('hashtags.id'),
                           primary_key=True)
Ejemplo n.º 4
0
class Chatting(db.Model):
    __tablename__ = 'chatting'

    id = db.Column(db.Integer, primary_key=True)
    from_id = db.Column(db.Integer, db.ForeignKey('people.id', ondelete='CASCADE'), nullable=False)
    to_id = db.Column(db.Integer, db.ForeignKey('people.id', ondelete='CASCADE'), nullable=False)
    content = db.Column(db.Text, nullable=False)
    chat_time = db.Column(db.DateTime, default=datetime.datetime.now)
    has_read = db.Column(db.Boolean, default=False)

    def __init__(self, from_id, to_id, content):
        self.from_id = from_id
        self.to_id = to_id
        self.content = content
Ejemplo n.º 5
0
class LoginInfo(db.Model):
    """User Login Info Model."""

    __tablename__ = 'login_info'

    id = db.Column(db.Integer, primary_key=True)
    user_agent = db.Column(db.String(255), nullable=False)
    ip_address = db.Column(db.String(255), nullable=False)
    expires_at = db.Column(db.DateTime, nullable=False)
    expires_rt = db.Column(db.DateTime, nullable=False)

    # Access and Refresh Token JTIs
    jti_at = db.Column(db.String(120), unique=True, nullable=False)
    jti_rt = db.Column(db.String(120), unique=True, nullable=False)
    revoked = db.Column(db.Boolean, default=False, nullable=False)

    # Relations
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    def delete_itself(self):
        db.session.delete(self)
        db.session.commit()
Ejemplo n.º 6
0
class VisitLog(db.Model):
    __tablename__ = 'visit_log'

    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.String(255), nullable=False)
    method = db.Column(db.String(10))  # GET, POST, PUT, DELETE
    referrer = db.Column(db.String(255))
    platform = db.Column(db.String(80))
    browser = db.Column(db.String(80))
    version = db.Column(db.String(20))
    client_ip = db.Column(db.String(20))
    visit_time = db.Column(db.DateTime, default=datetime.datetime.now)
    people_id = db.Column(db.Integer,
                          db.ForeignKey('people.id', ondelete='SET NULL'))

    people = db.relationship('People', uselist=False)

    def __init__(self, url, method, referrer, platform, browser, version,
                 client_ip, visit_time, people_id):
        self.url = url
        self.referrer = referrer
        self.method = method
        self.platform = platform
        self.browser = browser
        self.version = version
        self.client_ip = client_ip
        self.visit_time = visit_time
        self.people_id = people_id
Ejemplo n.º 7
0
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    
    def __repr__(self):
        return f'<Post {self.body}'
Ejemplo n.º 8
0
class LoginLog(db.Model):
    __tablename__ = 'login_log'
    id = db.Column(db.Integer, primary_key=True)
    people_id = db.Column(db.Integer,
                          db.ForeignKey(People.id, ondelete='CASCADE'),
                          nullable=True)
    login_time = db.Column(db.DateTime, default=datetime.datetime.now)
    login_ip = db.Column(db.String(20))

    def __init__(self, people_id, login_ip):
        self.people_id = people_id
        self.login_ip = login_ip
Ejemplo n.º 9
0
class Post(db.Model):
    """
    Content model
    """
    __tablename__ = 'posts'

    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow())
    title = db.Column(db.String,
                      nullable=False,
                      server_default="Not provided yet")
    body = db.Column(db.String, server_default="Not provided yet")
    raw = db.Column(db.String, server_default="Not provided yet")

    # One to Many or Many to One Relations
    author_id = db.Column(db.Integer,
                          db.ForeignKey('users.id', ondelete='CASCADE'),
                          nullable=False)
    author = db.relationship('User',
                             backref='content',
                             foreign_keys=[author_id])

    # Many to many relations using association tables
    hashtags = db.relationship('HashTags',
                               secondary=AssociationHashTags.__table__,
                               lazy='dynamic',
                               backref='contents')

    def save_to_db(self):
        try:
            db.session.add(self)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            raise ServiceError(*err.DATABASE_ERROR)

    @classmethod
    def find_by_id(cls, _id):
        return cls.query.filter_by(id=_id).first()

    @classmethod
    def get_view_query(cls):
        f_list = []
        default_query = cls.query

        if 'hashtags' in request.args and any(
                request.args['hashtags'].split(',')):
            for v in request.args['hashtags'].split(','):
                f_list.append(getattr(HashTags, 'hashtag') == v)

            default_query = cls.query.filter(or_(*f_list))

        return default_query
Ejemplo n.º 10
0
class Photo(db.Model):
    __tablename__ = 'photo'
    id = db.Column(db.Integer, primary_key=True)
    uri = db.Column(db.String(255), nullable=False)
    description = db.Column(db.Text)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    album_id = db.Column(db.Integer,
                         db.ForeignKey('photo_album.id', ondelete='SET NULL'))
    people_id = db.Column(db.Integer,
                          db.ForeignKey('people.id', ondelete='CASCADE'))

    people = db.relationship('People')

    def __init__(self, uri, people_id, description=None, album_id=None):
        self.uri = uri
        self.description = description
        self.album_id = album_id
        self.people_id = people_id

    def get_uri(self):
        return render_uri(self.uri)


#
#class VoiceAlbum(db.Model):
#    __tablename__ = 'voice_album'
#
#    id = db.Column(db.Integer, primary_key=True)
#    title = db.Column(db.String(80), nullable=False)
#    description = db.Column(db.Text)
#    people_id = db.Column(db.Integer, db.ForeignKey('people.id', ondelete='CASECADE'))

#class Voice(db.Model):
#    __tablename__ = 'voice'
#    id = db.Column(db.Integer, primary_key=True)
#    uri = db.Column(db.String(255))
#    description = db.Column(db.Text)
#    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
#    album_id = db.Column(db.Integer)
Ejemplo n.º 11
0
class PhotoAlbum(db.Model):
    __tablename__ = 'photo_album'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), nullable=False)
    description = db.Column(db.Text)
    people_id = db.Column(db.Integer,
                          db.ForeignKey('people.id', ondelete='CASCADE'))

    photos = db.relationship('Photo',
                             backref=db.backref('album'),
                             lazy='dynamic')
    people = db.relationship('People',
                             backref=db.backref('albums', lazy='dynamic'))

    def __init__(self, title, people_id, description=None):
        self.title = title
        self.people_id = people_id
        self.description = description
Ejemplo n.º 12
0
class Notification(db.Model):
    __tablename__ = 'notification'

    id = db.Column(db.Integer, primary_key=True)
    from_id = db.Column(db.Integer,
                        db.ForeignKey('people.id', ondelete='SET NULL'))
    to_id = db.Column(db.Integer, db.ForeignKey('people.id',
                                                ondelete='CASCADE'))
    object_table = db.Column(db.String(20))
    object_id = db.Column(db.Integer)
    content = db.Column(db.String(255))
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    has_read = db.Column(db.Boolean, default=False)

    from_people = db.relationship('People', primaryjoin=(from_id == People.id))
    to_people = db.relationship('People',
                                primaryjoin=(to_id == People.id),
                                backref=db.backref('notifications',
                                                   lazy='dynamic'),
                                passive_deletes=True)

    def __repr__(self):
        if self.object_table == 'microblog':
            obj = Microblog.query.get(self.object_id)
            pattern = u'<a href="%s">%s</a> 在微博 <a href="%s">%s</a> 中回复了你'
            return pattern % (
                url_for('frontend.people', id=self.from_id),
                Markup.escape(self.from_people.nickname),
                url_for('mblog.comment', mid=self.object_id) if obj else '',
                Markup.escape(obj.content[:20]) if obj else u'抱歉,该微博已删除')
        elif self.object_table == 'comment':
            obj = Comment.query.get(self.object_id)
            pattern = u'<a href="%s">%s</a> 在评论 <a href="%s">%s</a> 中回复了你'
            return pattern % (url_for('frontend.people', id=self.from_id),
                              Markup.escape(self.from_people.nickname),
                              url_for('mblog.comment',
                                      mid=obj.microblog_id,
                                      cid=self.object_id) if obj else '',
                              Markup.escape(obj.parent_comment.content[:20])
                              if obj else u'抱歉,该评论已删除')
        elif self.object_table == 'photo':
            obj = Photo.query.get(self.object_id)
            pattern = u'<a href="%s">%s</a> 在照片 <a href="%s">%s</a> 中回复了你'
            return pattern % (
                url_for('frontend.people', id=self.from_id),
                Markup.escape(self.from_people.nickname),
                url_for('photo.show_photo', pid=obj.id,
                        aid=self.album_id) if obj else '',
                Markup.escape(obj.title[:20]) if obj else u'抱歉,该照片已删除')
        elif self.object_table == 'album':
            obj = PhotoAlbum.query.get(self.object_id)
            pattern = u'<a href="%s">%s</a> 在相册 <a href="%s">%s</a> 中回复了你'
            return pattern % (
                url_for('frontend.people', id=self.from_id),
                Markup.escape(self.from_people.nickname),
                url_for('photo.show_album', id=obj.id) if obj else '',
                Markup.escape(obj.title[:20]) if obj else u'抱歉,该相册已删除')
        elif self.object_table == 'chatting':
            pattern = u'<a href="%s">%s</a> 给你发来了一条 <a href="%s">私信</a>'
            return pattern % (url_for('frontend.people', id=self.from_id),
                              Markup.escape(self.from_people.nickname),
                              url_for('friendship.show_chatting_detail',
                                      box='inbox',
                                      id=self.object_id))
        elif self.object_table == 'friendship':
            pattern = u'<a href="%s">%s</a> 关注了你'
            return pattern % (
                url_for('frontend.people', id=self.from_id),
                Markup.escape(self.from_people.nickname),
            )
Ejemplo n.º 13
0
class PeopleInfo(db.Model):
    __tablename__ = 'people_info'

    id = db.Column(db.Integer, db.ForeignKey('people.id'), primary_key=True)
    fullname = db.Column(db.String(20))
    gender = db.Column(db.Enum(u'男', u'女'))  # 性别,False: Female, True: Male
    sexual_orientation = db.Column(db.Enum(u'男', u'女'))  # 性取向
    birthday = db.Column(db.Date)  # 生日
    age = db.Column(db.SmallInteger)  # 年龄,触发器
    chinese_zodiac = db.Column(
        db.Enum(u'鼠', u'牛', u'虎', u'兔', u'龙', u'蛇', u'马', u'羊', u'猴', u'鸡',
                u'狗', u'猪'))  # 生肖,触发器
    star_sign = db.Column(
        db.Enum(u'白羊座', u'金牛座', u'双子座', u'巨蟹座', u'狮子座', u'处女座', u'天秤座', u'天蝎座',
                u'射手座', u'摩羯座', u'水瓶座', u'双鱼座'))  # 星座,触发器
    blood_type = db.Column(db.Enum(u'A型', u'B型', u'AB型', u'O型'))  # 血型
    profession = db.Column(db.String(20))  # 职业
    education = db.Column(db.String(20))  # 学历
    school = db.Column(db.String(80))  # 毕业院校
    homepage = db.Column(db.String(255))  # 个人网站
    hometown = db.Column(db.String(20))  # 故乡
    location = db.Column(db.String(20))  # 所在地
    address = db.Column(db.String(100))  # 地址
    zip_code = db.Column(db.String(10))  # 邮编
    qq = db.Column(db.String(20))  # QQ 号码
    introduction = db.Column(db.Text)  # 个人简介

    people = db.relationship('People',
                             backref=db.backref('info', uselist=False),
                             uselist=False)

    def __init__(self, id, fullname, gender, sexual_orientation, birthday,
                 blood_type, profession, education, school, homepage, hometown,
                 location, address, zip_code, qq, introduction):
        self.id = id
        self.fullname = fullname
        self.gender = gender
        self.sexual_orientation = sexual_orientation
        self.birthday = birthday
        self.blood_type = blood_type
        self.profession = profession
        self.education = education
        self.school = school
        self.homepage = homepage
        self.hometown = hometown
        self.location = location
        self.address = address
        self.zip_code = zip_code
        self.qq = qq
        self.introduction = introduction

    def change_info(self, fullname, gender, sexual_orientation, birthday,
                    blood_type, profession, education, school, homepage,
                    hometown, location, address, zip_code, qq, introduction):
        self.fullname = fullname
        self.gender = gender
        self.sexual_orientation = sexual_orientation
        self.birthday = birthday
        self.blood_type = blood_type
        self.profession = profession
        self.education = education
        self.school = school
        self.homepage = homepage
        self.hometown = hometown
        self.location = location
        self.address = address
        self.zip_code = zip_code
        self.qq = qq
        self.introduction = introduction

    def update_age(self):
        # 计算年龄
        pass

    def update_chinese_zodiac(self):
        # 计算生肖
        pass

    def update_star_sign(self):
        # 计算星座
        pass
Ejemplo n.º 14
0
    def __init__(self, people_id, login_ip):
        self.people_id = people_id
        self.login_ip = login_ip


class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(10))


people_roles = db.Table(
    'people_roles',
    db.Column('people_id',
              db.Integer,
              db.ForeignKey('people.id', ondelete='CASCADE'),
              primary_key=True),
    db.Column('role_id',
              db.Integer,
              db.ForeignKey('role.id', ondelete='CASCADE'),
              primary_key=True),
)

gender_enum = (u'男', u'女')
sexual_orientation_enum = (u'男', u'女')
profession_enum = (u'在校学生', u'固定工作者', u'自由职业者', u'待业', u'退休')
education_enum = (u'小学及以下', u'初中', u'高中', u'中专', u'大专', u'本科', u'研究生',
                  u'博士及以上')
chinese_zodiac_enum = (u'鼠', u'牛', u'虎', u'兔', u'龙', u'蛇', u'马', u'羊', u'猴',
                       u'鸡', u'狗', u'猪')
star_sign_enum = (u'白羊座', u'金牛座', u'双子座', u'巨蟹座', u'狮子座', u'处女座', u'天秤座',
Ejemplo n.º 15
0
class Group(db.Model):
    __tablename__ = 'group'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    people_id = db.Column(db.Integer, db.ForeignKey('people.id', ondelete='CASCADE'), nullable=False)
Ejemplo n.º 16
0
# -*- coding: utf-8 -*-
import datetime
from microblog.extensions import db


Friendship = db.Table(
    'friendship',
    db.Column('from_id', db.Integer, db.ForeignKey('people.id', ondelete='CASCADE'), primary_key=True),
    db.Column('to_id', db.Integer, db.ForeignKey('people.id', ondelete='CASCADE'), primary_key=True),
    db.Column('follow_time', db.DateTime, default=datetime.datetime.now),
    db.Column('group_id', db.Integer, db.ForeignKey('group.id', ondelete='SET NULL'))
)


Blackship = db.Table(
    'blackship',
    db.Column('from_id', db.Integer, db.ForeignKey('people.id', ondelete='CASCADE'), primary_key=True),
    db.Column('to_id', db.Integer, db.ForeignKey('people.id', ondelete='CASCADE'), primary_key=True),
    db.Column('block_time', db.DateTime, default=datetime.datetime.now)
)


class Group(db.Model):
    __tablename__ = 'group'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    people_id = db.Column(db.Integer, db.ForeignKey('people.id', ondelete='CASCADE'), nullable=False)


class Chatting(db.Model):