コード例 #1
0
ファイル: models.py プロジェクト: vvrmahendra/travelBlog
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20),
                           nullable=False,
                           default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post', backref='author', lazy=True)

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}')"
コード例 #2
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
class Role(db.Model):
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    role_name = db.Column(db.String(30))
    date_granted = db.Column(db.DateTime(), default=dt.utcnow)

    def __repr__(self):
        return f'<User {self.user_id} is {self.role_name}>'
コード例 #3
0
ファイル: models.py プロジェクト: vvrmahendra/travelBlog
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    date_posted = db.Column(db.DateTime,
                            nullable=False,
                            default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Post('{self.title}', '{self.date_posted}')"
コード例 #4
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
class Message(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    sender_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    title = db.Column(db.String(200), nullable=False)
    body = db.Column(db.Text(2000), nullable=False)

    date_send = db.Column(db.DateTime(), default=dt.utcnow)
    seen = db.Column(db.Boolean(), default=False)
    deleted = db.Column(db.Boolean())

    def __repr__(self):
        return f'<Message №{self.id} from {self.sender} to {self.recipient}>'
コード例 #5
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
class UserInfo(db.Model):
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    birthdate = db.Column(db.Date())
    avatar = db.Column(db.String(120), default='default.png')
    gender = db.Column(db.String(1))
    job = db.Column(db.String(60))
    about = db.Column(db.Text(600))
    origin_country_id = db.Column(db.Integer, db.ForeignKey('country.id'))
    registration_date = db.Column(db.Date(), default=dt.utcnow)
    last_seen = db.Column(db.DateTime(), default=dt.utcnow)

    origin_country = db.relationship('Country',
                                     lazy=True,
                                     uselist=False,
                                     backref=db.backref('natives',
                                                        uselist=True))

    def __repr__(self):
        return f'<Info about {self.user.username}>'
コード例 #6
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
class Country(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(60), index=True, unique=True)
    code = db.Column(db.String(2), index=True, unique=True)

    info = db.relationship('CountryInfo',
                           backref='country',
                           uselist=False,
                           cascade='all')

    articles = db.relationship('Article',
                               secondary=country_tag,
                               lazy='dynamic',
                               backref='country_tags',
                               cascade='all')

    def __repr__(self):
        return f'<Country {self.name}>'

    @staticmethod
    def get_country_list():
        with current_app.app_context():
            return Country.query.all()
コード例 #7
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    article_id = db.Column(db.Integer, db.ForeignKey('article.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    body = db.Column(db.Text(2000), nullable=False)

    date_posted = db.Column(db.DateTime(), default=dt.utcnow)
    last_updated = db.Column(db.DateTime())

    def __repr__(self):
        return f'<Comment №{self.id} on post {self.article.title}>'
コード例 #8
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    body = db.Column(db.Text(100000), nullable=False)
    title = db.Column(db.String(200), index=True, nullable=False)
    article_type = db.Column(db.String(10))
    date_posted = db.Column(db.DateTime(), default=dt.utcnow)
    last_updated = db.Column(db.DateTime())

    comments = db.relationship('Comment',
                               backref='article',
                               cascade='all',
                               lazy='dynamic')

    def __repr__(self):
        return (f'<Article {self.id} title {self.title} '
                f'by user {self.article_author.username}>')
コード例 #9
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    active = db.Column(db.Boolean())

    info = db.relationship('UserInfo',
                           backref='user',
                           uselist=False,
                           cascade='all')
    comments = db.relationship('Comment', backref='comment_author')
    articles = db.relationship('Article', backref='article_author')

    followers = db.relationship(
        'User',
        secondary=user_followers,
        primaryjoin=id == user_followers.c.followed_id,
        secondaryjoin=id == user_followers.c.follower_id,
        backref='follows')

    sent_messages = db.relationship('Message',
                                    foreign_keys='Message.sender_id',
                                    lazy='dynamic',
                                    backref=db.backref('sender', lazy=True))

    received_messages = db.relationship('Message',
                                        foreign_keys='Message.recipient_id',
                                        lazy='dynamic',
                                        backref=db.backref('recipient',
                                                           lazy=True))

    followed_countries = db.relationship('Country',
                                         secondary=country_follower,
                                         lazy=True,
                                         backref='followers')

    visited_countries = db.relationship('Country',
                                        secondary=country_visitor,
                                        lazy=True,
                                        backref='visitors')

    liked_articles = db.relationship('Article',
                                     secondary=article_likes,
                                     lazy=True,
                                     backref='likes')

    disliked_articles = db.relationship('Article',
                                        secondary=article_dislikes,
                                        lazy=True,
                                        backref='dislikes')

    role = db.relationship('Role', backref='user', uselist=False)

    def __repr__(self):
        return f'<User {self.username}>'

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

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

    def make_token(self):
        payload = {
            'exp': dt.utcnow() + timedelta(hours=1),
            'iat': dt.utcnow(),
            'id': self.id
        }
        return jwt.encode(payload, current_app.config['SECRET_KEY'],
                          'HS256').decode('utf-8')

    @staticmethod
    def process_token(token):
        try:
            id = jwt.decode(token, current_app.config['SECRET_KEY'],
                            'HS256')['id']
        except:
            return None
        return User.query.get(id)
コード例 #10
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
class CountryInfo(db.Model):
    country_id = db.Column(db.Integer,
                           db.ForeignKey('country.id'),
                           primary_key=True)
    capital = db.Column(db.String(20))
    population = db.Column(db.Integer)
    land_area = db.Column(db.Integer)
    currency = db.Column(db.String(40))
    currency_code = db.Column(db.String(3))
    language = db.Column(db.String(30))
    time_zone = db.Column(db.String(50))
    time_offset = db.Column(db.String(10))
    weather = db.Column(db.Text(600))
    description = db.Column(db.Text(2000))
    region = db.Column(db.String(20))
    subregion = db.Column(db.String(40))
    last_updated = db.Column(db.DateTime(), default=dt.utcnow)

    def __repr__(self):
        return f'<Info about {self.country.name}>'
コード例 #11
0
ファイル: models.py プロジェクト: 1Zarkos1/travelblog-flask
from datetime import datetime as dt, timedelta
from hashlib import md5
from time import time

import jwt
from flask import current_app
from flask_login import UserMixin, current_user
from flask_admin.contrib.sqla import ModelView
from werkzeug.security import generate_password_hash, check_password_hash

from travelblog import db, login, admin

country_visitor = db.Table(
    'country_visitor',
    db.Column('user_id',
              db.Integer,
              db.ForeignKey('user.id'),
              primary_key=True),
    db.Column('country_id',
              db.Integer,
              db.ForeignKey('country.id'),
              primary_key=True))

country_follower = db.Table(
    'country_follower',
    db.Column('user_id',
              db.Integer,
              db.ForeignKey('user.id'),
              primary_key=True),
    db.Column('country_id',
              db.Integer,
              db.ForeignKey('country.id'),