コード例 #1
0
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80))
    subject = db.Column(db.String(50))
    imgFile = db.Column(db.String(128))
    date = db.Column(db.DateTime, default=datetime.today())
    description = db.Column(db.Text,nullable=False)
    ownerId = db.Column(db.Integer,db.ForeignKey('user.id'),nullable=False)
    comment = db.relationship('Comment', backref='post', lazy=True)
    like = db.relationship('User', secondary=like, lazy='subquery',
                           backref=db.backref('posts', lazy=True))
    collect = db.relationship('User', secondary=collect, lazy='subquery',
                           backref=db.backref('Post', lazy=True))

    def __repr__(self):
        return '<Post %r>' % self.id
コード例 #2
0
class Accueillant(db.Model):
    __tablename__ = 'accueillants'
    id = db.Column(db.Integer, primary_key=True)
    disponibilite = db.Column(db.String(120), unique=False, nullable=True)
    nom = db.Column(db.String(120), unique=True, nullable=False)
    tel = db.Column(db.String(120), unique=False, nullable=True)
    adresse = db.Column(db.String(120), unique=False, nullable=True)
    email = db.Column(db.String(120), unique=False, nullable=True)
    next_action = db.Column(db.Text, unique=False, nullable=True)
    remarques = db.Column(db.Text, unique=False, nullable=True)
    sejours = db.relationship('Sejour', backref='infos')
    accueillis = db.relationship("Accueilli",
                                 secondary='accueils',
                                 backref=db.backref('accueillants'))

    def __repr__(self):
        return f"Accueillant : {self.nom}, {self.email}"

    def __init__(self, disponibilite, nom, tel, adresse, email, next_action,
                 remarques):
        self.disponibilite = disponibilite
        self.nom = nom
        self.tel = tel
        self.adresse = adresse
        self.email = email
        self.next_action = next_action
        self.remarques = remarques
コード例 #3
0
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)
    likes = db.relationship('Movie',
                            secondary=likes,
                            lazy='subquery',
                            backref=db.backref('liked_by', lazy=True))
    dislikes = db.relationship('Movie',
                               secondary=dislikes,
                               lazy='subquery',
                               backref=db.backref('disliked_by', lazy=True))

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}')"
コード例 #4
0
ファイル: models.py プロジェクト: WatchingBoss/Learn
class User(db.Model, UserMixin):
    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))
    about_me = db.Column(db.String(140))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow())
    posts = db.relationship('Post', backref='author', lazy='dynamic')
    followed = db.relationship('User', secondary=followers,
                               primaryjoin=(followers.columns.follower_id == id),
                               secondaryjoin=(followers.columns.followed_id == id),
                               backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')

    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 avatar(self, size):
        return f"https://www.gravatar.com/avatar/{md5(self.email.lower().encode('utf-8')).hexdigest()}?" \
               f"{urlencode({'d': 'retro', 's': size})}"

    def is_following(self, user):
        return self.followed.filter(followers.columns.followed_id == user.id).count() > 0

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(user)

    def followed_posts(self):
        followed = Post.query.join(followers, (followers.columns.followed_id == Post.user_id))\
                             .filter(followers.columns.follower_id == self.id)
        own = Post.query.filter_by(user_id=self.id)
        return followed.union(own).order_by(Post.timestamp.desc())

    def get_reset_password_token(self, expires_in=600):
        return jwt.encode({'reset_password': self.id, 'exp': time() + expires_in},
                          current_app.config['SECRET_KEY'], algorithm='HS256')

    @staticmethod
    def verify_reset_password_token(token):
        try:
            id = jwt.decode(token, current_app.config['SECRET_KEY'], algorithms=['HS256'])['reset_password']
        except:
            return
        return User.query.get(id)
コード例 #5
0
ファイル: models.py プロジェクト: makkarlabs/flask_app
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    first_name = db.Column(db.String(255))
    last_name = db.Column(db.String(255))
    organization = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
    backref=db.backref('users', lazy='dynamic'))
コード例 #6
0
class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40), unique=True, nullable=False)
    movies = db.relationship('Movie',
                             secondary=categories,
                             lazy='subquery',
                             backref=db.backref('categories', lazy=True))

    def __repr__(self):
        return f"Category('{self.name}')"

    def __lt__(self, other):
        return self.name < other.name
コード例 #7
0
class Employee(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    emp_id = db.Column(db.String(30), unique=True, nullable=False)
    name = db.Column(db.String(30), unique=False, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    features = db.relationship('Feature',
                               secondary=feature_assign,
                               lazy=True,
                               backref=db.backref('employees', lazy='joined'))
    profile_picture = db.Column(db.String(20),
                                nullable=False,
                                default='default.png')
    admin = db.Column(db.Boolean, unique=False, default=False)
    verified = db.Column(db.Boolean, unique=False, default=False)

    def __repr__(self):
        return f'Employee {self.name}, Emp_id : {self.emp_id} email : {self.email}'
コード例 #8
0
ファイル: uploads.py プロジェクト: ml-dev-9spl/covid19
class UserUploads(db.Model, BaseModel, metaclass=MetaBaseModel):
    """ The User model """

    __tablename__ = "user_uploads"

    id = db.Column(db.Integer, primary_key=True)
    file = db.Column(db.String(100), unique=True)
    diesease = db.Column(db.Enum(DieseaseType), default=DieseaseType.COVID19)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    user = db.relationship('User', backref=db.backref('uploads', lazy=True))
    is_approved = db.Column(db.Boolean, default=False)

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

    @property
    def get_diesease_display(self):
        return self.diesease.name

    @property
    def get_xray_url(self):
        return xrays.url(filename=self.file)
コード例 #9
0
ファイル: models.py プロジェクト: kkl116/Stonks
class User(db.Model, UserMixin):
    """static methods cant modify instnace or class state - provides a way to restrict the data
    that a method can access, primarily a way to namespace your methods"""

    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='')
    password = db.Column(db.String(60), nullable=False)
    verified = db.Column(db.Boolean(), nullable=False, default=False)
    currency = db.Column(db.String(), nullable=False, default='GBP')
    """here lazily load b/c no need to load it unless in the watchlist page..."""
    #one to many relationships
    watchlistItems = db.relationship('WatchlistItem',
                                     backref='user',
                                     lazy=True,
                                     cascade="all, delete, delete-orphan",
                                     passive_deletes=True)
    portfolioOrders = db.relationship('PortfolioOrder',
                                      backref='user',
                                      lazy=True,
                                      cascade="all, delete, delete-orphan",
                                      passive_deletes=True)
    positions = db.relationship('Position',
                                backref='user',
                                lazy=True,
                                cascade='all, delete, delete-orphan',
                                passive_deletes=True)
    alerts = db.relationship('Alert',
                             backref='user',
                             lazy=True,
                             cascade='all, delete, delete-orphan',
                             passive_deletes=True)
    #many to many relationships
    subscriptions = db.relationship('Quote',
                                    secondary=user_subscriptions,
                                    backref=db.backref('users', lazy=True),
                                    lazy=True)

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

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

    @staticmethod
    def verify_token(token, timed=False):
        if timed:
            s = TimedSerializer(current_app.config['SECRET_KEY'])
        else:
            s = Serializer(current_app.config['SECRET_KEY'])

        try:
            user_id = s.loads(token)['user_id']
        except Exception as e:
            return None

        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}')"
コード例 #10
0
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))
    posts = db.relationship('Post', backref='author', lazy='dynamic')
    comments = db.relationship('Comment', backref='author', lazy='dynamic')
    about_me = db.Column(db.String(140))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)
    followed = db.relationship('User',
                               secondary=followers,
                               primaryjoin=(followers.c.follower_id == id),
                               secondaryjoin=(followers.c.followed_id == id),
                               backref=db.backref('followers', lazy='dynamic'),
                               lazy='dynamic')
    messages_sent = db.relationship('Message',
                                    foreign_keys='Message.sender_id',
                                    backref='author',
                                    lazy='dynamic')
    messages_received = db.relationship('Message',
                                        foreign_keys='Message.recipient_id',
                                        backref='recipient',
                                        lazy='dynamic')
    last_message_read_time = db.Column(db.DateTime)
    notifications = db.relationship('Notification',
                                    backref='user',
                                    lazy='dynamic')

    def __repr__(self):
        return '<User {}>'.format(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 avatar(self, size):
        digest = md5(self.email.lower().encode('utf-8')).hexdigest()
        return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(
            digest, size)

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(user)

    def is_following(self, user):
        return self.followed.filter(
            followers.c.followed_id == user.id).count() > 0

    def followed_posts(self):
        followed = Post.query.join(
            followers, (followers.c.followed_id == Post.user_id)).filter(
                followers.c.follower_id == self.id)
        own = Post.query.filter_by(user_id=self.id)
        return followed.union(own).order_by(Post.timestamp.desc())

    def get_reset_password_token(self, expires_in=600):
        return jwt.encode(
            {
                'reset_password': self.id,
                'exp': time() + expires_in
            },
            current_app.config['SECRET_KEY'],
            algorithm='HS256').decode('utf-8')

    @staticmethod
    def verify_reset_password_token(token):
        try:
            id = jwt.decode(token,
                            current_app.config['SECRET_KEY'],
                            algorithms=['HS256'])['reset_password']
        except:
            return
        return User.query.get(id)

    def new_messages(self):
        last_read_time = self.last_message_read_time or datetime(1980, 1, 1)
        return Message.query.filter_by(recipient=self).filter(
            Message.timestamp > last_read_time).count()

    def add_notification(self, name, data):
        self.notifications.filter_by(name=name).delete()
        n = Notification(name=name, payload_json=json.dumps(data), user=self)
        db.session.add(n)
        return n