コード例 #1
0
class User(db.Model, UserMixin):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.Unicode(255),
                      nullable=False,
                      server_default=u'',
                      unique=True)
    first_name = db.Column(db.String(40))
    last_name = db.Column(db.Unicode(50), nullable=False, server_default=u'')
    active = db.Column('is_active',
                       db.Boolean(),
                       nullable=False,
                       server_default='0',
                       default=False)
    password_hash = db.Column(db.String(255),
                              nullable=False,
                              server_default='')
    last_login = db.Column(db.DateTime)
    is_admin = db.Column(db.Boolean, default=False)
    create_date = db.Column(db.DateTime, default=db.func.now())

    @property
    def password(self):
        return self.password_hash

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

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return '<User: {}>'.format(self.email)
コード例 #2
0
class User(UserMixin, CRUDMixin, db.Model):
    # email uses as login
    login = db.Column(db.Unicode(30), unique=True, nullable=False)
    password = db.Column(db.Unicode(60), unique=True, nullable=False)
    invite = db.Column(db.Unicode(36), unique=True)

    created_at = db.Column(db.DateTime, default=datetime.now)
    last_login = db.Column(db.DateTime)

    admin = db.Column(db.BOOLEAN, default=False)

    def __repr__(self):
        return str(self.id)

    def __unicode__(self):
        return self.login

    @staticmethod
    def hash_password(password):
        return unicode(generate_password_hash(password))

    def verify_password(self, password):
        return check_password_hash(self.password, password)

    @staticmethod
    def generate_password(n=8):
        """
        Generate random password of n characters.
        :param n: number of characters.
        """
        return ''.join(
            random.choice(string.ascii_uppercase + string.digits)
            for _ in range(n))

    @staticmethod
    def create_user(email, invite_str):
        password = User.generate_password()
        user = User.create(login=email,
                           password=User.hash_password(password),
                           invite=invite_str,
                           created_at=datetime.now())

        mail.send_login_info(email, password)
        return user

    @staticmethod
    def set_admin(login, password):
        login = unicode(login)
        admin = User.query.filter_by(login=login).first()
        if admin:
            admin.update(password=User.hash_password(password), admin=True)
        else:
            User.create(login=login,
                        password=User.hash_password(password),
                        created_at=datetime.now(),
                        admin=True)
コード例 #3
0
class CatalogItem(db.Model):
    __tablename__ = 'catalog_items'
    query_class = CatalogItemQuery

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(50))
    description = db.Column(db.String(140))
    image_url = db.Column(db.String(140))
    price = db.Column(db.Numeric)
    order_detail_id = db.Column(db.Integer, db.ForeignKey('order_details.id'))
    category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
    search_vector = db.Column(TSVectorType('name'))

    def from_dict(self, data):
        for field in [
                'name', 'description', 'image_url', 'category_id', 'price'
        ]:
            if field in data:
                setattr(self, field, data[field])

    def to_dict(self):
        data = {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'image_url': self.image_url,
            'category_id': self.category_id,
            'price': str(self.price),
        }
        return data
コード例 #4
0
ファイル: topic.py プロジェクト: yuankunzhang/FunTalk
class Topic(db.Model):

    __tablename__ = 'topics'

    id = db.Column(db.Integer, primary_key=True)
    subject = db.Column(db.Unicode(255), nullable=False)
    description = db.Column(db.UnicodeText, nullable=False)
    tags = db.Column(db.UnicodeText, nullable=True)
    user_id = db.Column(db.Integer,
                        db.ForeignKey(User.id, ondelete='CASCADE'),
                        nullable=False)

    completed = db.Column(db.Boolean, default=False)
    completed_at = db.Column(db.DateTime, nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow)

    user = db.relation(User, innerjoin=True, lazy="joined")

    def __init__(self, *args, **kwargs):
        super(Topic, self).__init__(*args, **kwargs)

    def __str__(self):
        return self.subject

    def __repr__(self):
        return "<Topic(%s)>" % self
コード例 #5
0
class Invite(UserMixin, CRUDMixin, db.Model):
    invite = db.Column(db.Unicode(36), unique=True, nullable=False)
    email = db.Column(
        db.Unicode(30), unique=False,
        nullable=False)  # invite can be sent many times to one email

    created_at = db.Column(db.DateTime, default=datetime.now)
    used = db.Column(db.BOOLEAN, default=False)

    def __repr__(self):
        return str(self.id)

    def __unicode__(self):
        return self.email

    @staticmethod
    def create_invite(email):
        invite_str = unicode(uuid4())
        return Invite.create(invite=invite_str, email=email)
コード例 #6
0
class User(db.Model):

    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120), unique=True, nullable=False)
    first_name = db.Column(db.Unicode(80), nullable=False, default=u'')
    last_name = db.Column(db.Unicode(80), nullable=False, default=u'')
    vote_count = db.Column(db.SmallInteger, default=1)

    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow)

    def __init__(self, *args, **kwargs):
        super(User, self).__init__(*args, **kwargs)

    def __str__(self):
        return self.email

    def __repr__(self):
        return "<User(%s)>" % self
コード例 #7
0
ファイル: models.py プロジェクト: rometw/Flask-Demos
class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(64))

    def __str__(self):
        return "{}".format(self.name)
コード例 #8
0
class Tweet(db.Model):
    id = db.Column(db.BigInteger, primary_key=True)
    text = db.Column(db.Unicode(500), nullable=False)
    embedding = db.Column(db.PickleType, nullable=False)
    user_id = db.Column(db.BigInteger,
                        db.ForeignKey('user.id'),
                        nullable=False)
    user = db.relationship("User",
                           backref=db.backref('tweets',
                                              lazy='dynamic',
                                              cascade="all, "
                                              "delete-orphan"))

    @staticmethod
    def add_user_tweets(user):
        print(f"getting {user.username}'s tweets...")
        # get first 200 tweets
        timeline = TWITTER.user_timeline(user.username,
                                         count=200,
                                         exclude_replies=False,
                                         include_rts=False,
                                         tweet_mode='extended')
        # this is for the pagination to get the rest of them
        earliest_tweet = min(timeline, key=lambda x: x.id).id

        while True:
            # this loops through and gets all the tweets i can
            tweets = TWITTER.user_timeline(user.username,
                                           count=200,
                                           exclude_replies=False,
                                           include_rts=False,
                                           max_id=earliest_tweet,
                                           tweet_mode='extended')

            if not tweets:
                # if there are no tweets, break gracefully?
                break

            new_earliest = min(tweets, key=lambda x: x.id).id

            if new_earliest == earliest_tweet:
                break
            else:
                earliest_tweet = new_earliest
                timeline += tweets

        # this is because there was some weird stuff and i was getting the
        # the same tweets multiple times ¯\_(ツ)_/¯
        timeline = {status.id: status for status in timeline}
        # update the user to have tweet count
        user.tweet_count = len(timeline)

        print(f"got {len(timeline)} tweets from {user.username}'s timeline!")
        print("\n")

        # embedding the tweets in big batches
        print(f"embedding {user.username}'s tweets...")
        tweets = [status.full_text for status in timeline.values()]
        with BASILICA as c:
            embeddings = list(c.embed_sentences(tweets, model="twitter"))
        print("tweets embedded!")
        print("\n")

        # adding all the tweets and embeddings to the db
        print("adding tweets to db...")
        for status, embedding in zip(timeline.values(), embeddings):
            tweet_data = {
                'id': status.id,
                'text': status.full_text[:500],
                'embedding': embedding,
                'user_id': user.id
            }
            db.session.add(Tweet(**tweet_data))

        print("added tweets to db!")
        print("\n")
        db.session.commit()

    @staticmethod
    def get_ten_newest():
        return Tweet.query.order_by(Tweet.id.desc()).limit(10).all()