Example #1
0
class Editorial_topic(Base):
    __tablename__ = 'editorial_topic'
    index = Column(Integer, primary_key=True, autoincrement=True)
    # TODO: list campaign table schema here
    editorial_topic_id = Column(Integer, unique=True)
    editorial_topic_name = Column(String)
    editorial_topic_description = Column(String)
    tags = db.relationship('Tag',
                           secondary=editorial_topic_tag,
                           backref=db.backref('editorial_topics',
                                              lazy='dynamic'))
    articles = db.relationship('Article',
                               secondary=editorial_topic_article,
                               backref=db.backref('editorial_topics',
                                                  lazy='dynamic'))

    def keys(self):
        self.hide('index')
        return self.fields

    @staticmethod
    def create_editorial_topic(campaign_id,
                               editorial_topic_id,
                               editorial_topic_name,
                               editorial_topic_description,
                               editorial_topic_tags=None):
        with db.auto_commit():
            new_editorial_topic = Editorial_topic(
                editorial_topic_id=editorial_topic_id,
                editorial_topic_name=editorial_topic_name,
                editorial_topic_description=editorial_topic_description)
            linked_campaign = Campaign.query.filter_by(
                campaign_id=campaign_id).first_or_404()
            new_editorial_topic.campaigns.append(linked_campaign)
            db.session.add(new_editorial_topic)
        return linked_campaign

    @staticmethod
    def delete_topic(editorial_topic_id):
        with db.auto_commit():
            delete_editorial_topic = Editorial_topic.query.filter_by(
                editorial_topic_id=editorial_topic_id).first_or_404()
            db.session.delete(delete_editorial_topic)
        return delete_editorial_topic

    @staticmethod
    def delete_article_from_topic(editorial_topic_id, article_id):
        with db.auto_commit():
            target_editorial_topic = Editorial_topic.query.filter_by(
                editorial_topic_id=editorial_topic_id).first_or_404()
            for article in target_editorial_topic.articles:
                if article.article_id == article_id:
                    target_editorial_topic.articles.remove(article)
        return target_editorial_topic
class Article(Base):
    __tablename__ = 'article'
    index = Column(Integer, primary_key=True, autoincrement=True)
    # TODO: list campaign table schema here
    article_id = Column(Integer, unique=True)
    article_importance = Column(Float)
    author = Column(String)
    source = Column(String)
    title = Column(String)
    url = Column(Text)
    time_created = Column(Date)
    thumbnail_url = Column(Text)
    abstract = Column(Text)
    tags = db.relationship('Tag',
                           secondary=article_tag,
                           backref=db.backref('articles', lazy='dynamic'))

    def keys(self):
        self.hide('index')
        return self.fields

    @staticmethod
    def upload_and_parse_url(editorial_topic_id, target_url):
        with db.auto_commit():
            linked_editorial_topic = Editorial_topic.query.filter_by(
                editorial_topic_id=editorial_topic_id).first_or_404()
            linked_article = Article(url=target_url)
            linked_editorial_topic.articles.append(linked_article)
        return linked_editorial_topic

    @staticmethod
    def create_article(editorial_topic_id, article_id):
        with db.auto_commit():
            linked_editorial_topic = Editorial_topic.query.filter_by(
                editorial_topic_id=editorial_topic_id).first_or_404()
            new_article = Article(article_id=article_id)
            new_article.editorial_topics.append(linked_editorial_topic)
            db.session.add(new_article)
        return linked_editorial_topic

    @staticmethod
    def delete_tag_from_article(article_id, article_tags):
        with db.auto_commit():
            target_article = Article.query.filter_by(
                article_id=article_id).first_or_404()
            for delete_tag in article_tags:
                for exist_tag in target_article.tags:
                    if exist_tag.index == delete_tag['tag_id']:
                        target_article.tags.remove(exist_tag)
                        break
        return target_article

    @staticmethod
    def edit_article_importance(article_id, article_importance):
        with db.auto_commit():
            target_article = Article.query.filter_by(
                article_id=article_id).first_or_404()
            target_article.article_importance = article_importance
        return target_article
Example #3
0
class Category(Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(50), nullable=False, unique=True)

    #反向调用  post.categorys.append(<class(Category)>) secondary 关系表
    posts = relationship('Post',
                         secondary=Post_category.__table__,
                         backref=db.backref('categorys', lazy='dynamic'))
Example #4
0
class Role(Base):
    __tablename__ = 'roles'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(80), unique=True)
    description = Column(String(255))
    menus = db.relationship('Menu',
                            secondary=role_menu,
                            backref=db.backref('roles', lazy='dynamic'))
    roleMenu = role_menu

    def __repr__(self):
        return '<Role %r>' % self.name
class User(Base):
    __tablename__ = 'user_table'
    index = Column(Integer, primary_key=True, autoincrement=True)
    # TODO: list campaign table schema here
    user_id = Column(Integer, unique=True)
    campaigns = db.relationship('Campaign',
                                secondary=user_campaign,
                                backref=db.backref('users', lazy='dynamic'))

    def keys(self):
        self.hide('index')
        return self.fields

    @staticmethod
    def create_user(user_id):
        with db.auto_commit():
            new_user = User(user_id=user_id)
            db.session.add(new_user)
        return 'create user success'
Example #6
0
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32), unique=True, index=True)
    holder = Column(String(32))
    holderId = Column(Integer, ForeignKey('employee.id'))
    phone = Column(String(11))
    password_hash = Column(String(128))
    roles = db.relationship('Role',
                            secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))
    lastLoginTime = db.Column(db.DateTime)  # 上次登录时间
    lastLoginIp = db.Column(db.String(32), nullable=True)  # 上次登录IP
    currentLoginIp = db.Column(db.String(32), nullable=True)  # 当前登录IP
    userRole = roles_users
    transferRecord = db.relationship('EmployeeTransfer',
                                     backref='transfer_operator',
                                     lazy=True)
    operateLog = db.relationship('OperateLog', backref='user', lazy=True)

    def __repr__(self):
        return '<User %r>' % self.username

    @property
    def password(self):
        raise AttributeError("password is not a readable attribute!")

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        """验证密码"""
        return check_password_hash(self.password_hash, password)

    @staticmethod
    def verify(username, password):
        user = User.query.filter_by(username=username).first_or_404()
        if not user.verify_password(password):
            raise AuthFailed(msg="登录验证失败!", error_code=1006)
        role = user.roles
        return {'uid': user.id, 'roles': role, 'holderId': user.holderId}
class Campaign(Base):
    __tablename__ = 'campaign'
    index = Column(Integer, primary_key=True, autoincrement=True)
    # TODO: list campaign table schema here
    # user_id = Column(UUID)
    # token = Column(String)
    campaign_id = Column(Integer, unique=True)
    campaign_name = Column(String)
    campaign_obj_type = Column(
        Enum('私域促活/留存', '私域拉新', '裂变', name='campaign objective type'))
    campaign_time_start = Column(Date)
    campaign_time_end = Column(Date)
    campaign_core_metric_type = Column(String)
    campaign_core_metric_value = Column(Float)
    editorial_topics = db.relationship('Editorial_topic',
                                       secondary=campaign_editorial_topic,
                                       backref=db.backref('campaigns',
                                                          lazy='dynamic'))

    def keys(self):
        self.hide('index')
        return self.fields

    @staticmethod
    def create_campaign(user_id, campaign_id, campaign_name, campaign_obj_type,
                        campaign_time_start, campaign_time_end):
        date_number_start = list(
            map(int,
                re.compile(r'\d+').findall(campaign_time_start)))
        date_number_end = list(
            map(int,
                re.compile(r'\d+').findall(campaign_time_end)))
        campaign_time_start = datetime(date_number_start[0],
                                       date_number_start[1],
                                       date_number_start[2])
        campaign_time_end = datetime(date_number_end[0], date_number_end[1],
                                     date_number_end[2])
        with db.auto_commit():
            new_campaign = Campaign(campaign_id=campaign_id,
                                    campaign_name=campaign_name,
                                    campaign_obj_type=campaign_obj_type,
                                    campaign_time_start=campaign_time_start,
                                    campaign_time_end=campaign_time_end)
            linked_user = User.query.filter_by(user_id=user_id).first_or_404()
            new_campaign.users.append(linked_user)
            db.session.add(new_campaign)
        return linked_user

    @classmethod
    def get_campaigns(cls, page=1, size=15):
        paginator = Campaign.query.paginate(page=page,
                                            per_page=size,
                                            error_out=True)

        return {
            'total': paginator.total,
            'current_page': paginator.page,
            'items': paginator.items
        }

    @classmethod
    def get_campaigns_by_user_token(cls, user_id, token, page=1, size=15):
        paginator = Campaign.query.filter_by(
            user_id=user_id, token=token).paginate(page=page,
                                                   per_page=size,
                                                   error_out=True)

        return {
            'total': paginator.total,
            'current_page': paginator.page,
            'items': paginator.items
        }
Example #8
0
class User(db.Model, Base, UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    _password = db.Column(db.String(128), nullable=False)
    email = db.Column(db.String(64), nullable=False, unique=True)
    first_name = db.Column(db.String(32), nullable=False)
    middle_name = db.Column(db.String(32))
    last_name = db.Column(db.String(32), nullable=False)
    title = db.Column(db.String(16), nullable=False)
    degree = db.Column(db.String(16))
    # nick_name = db.Column(db.String(32))
    phone_number = db.Column(db.String(16))
    fax_number = db.Column(db.String(16))
    secondary_email = db.Column(db.String(64))
    institution_phone_number = db.Column(db.String(16))
    institution = db.Column(db.String(32))
    department = db.Column(db.String(32))
    street = db.Column(db.String(64))
    city = db.Column(db.String(16), nullable=False)
    country_id = db.Column(db.Integer, db.ForeignKey("countries.id"))
    state_or_province = db.Column(db.String(16))
    zip = db.Column(db.String(32))
    # person_classifications   todo:这个键作为单独的一张表
    person_keywords = db.Column(db.Text, nullable=False)
    roles = db.relationship('Role',
                            secondary=user_roles,
                            backref=db.backref('users', lazy='dynamic'))

    # article = db.relationship('NewSubmission', back_populates='uid')
    # editor_in_chief = db.relationship('NewSubmission', back_populates='editor_in_chief')
    # associate_editor = db.relationship('NewSubmission', back_populates='associate_editor')
    def __repr__(self):
        return '<User id={0}, email={1}, roles={2}>'.format(
            self.id, self.email, self.roles)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        if not self.roles:
            if self.email in current_app.config['ADMIN_EMAIL']:
                self.roles.append(
                    Role.query.filter_by(permissions=Permission.ADMIN).first())
            if not self.roles:
                self.roles.append(Role.query.filter_by(default=True).first())

    @property
    def password(self):
        return AttributeError('password is not a readable attribute')

    @password.setter
    def password(self, raw):
        self._password = generate_password_hash(raw)

    def check_password(self, raw):
        return check_password_hash(self._password, raw)

    def can(self, perm):
        role = Role.query.filter_by(permissions=perm).first()
        if role in self.roles:
            return True
        else:
            return False

    def is_admin(self):
        return self.can(Permission.ADMIN)