예제 #1
0
class CategorieItem(db.Model):

    name = db.Column(db.String(20), unique=False, nullable=False)
    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(250))
    price = db.Column(db.String(8))
    picture = db.Column(db.String(200))
    categorie_id = db.Column(db.Integer,
                             db.ForeignKey('categorie.id'),
                             nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow())

    @property
    def serialize(self):
        """Return object data in easily serializeable format"""
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'price': self.price
        }

    def __repr__(self):
        return f"CategorieItem('{self.name}', '{self.description}','{self.id})"
예제 #2
0
class Item(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(65), nullable=False, unique=True)
    date = db.Column(db.DateTime, default=datetime.utcnow)
    description = db.Column(db.String(255), nullable=False)
    photo_path = db.Column(db.String(125))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    db.relationship('User')
    category_id = db.Column(db.Integer, db.ForeignKey(
        'category.id'), nullable=False)
    db.relationship('Category')

    @property
    def serialize(self):
        return {
            'id': self.id,
            'name': self.name,
            'date': self.date,
            'description': self.description,
            'photo_path': self.photo_path,
            'user_id': self.user_id,
            'category_id': self.category_id
        }

    @staticmethod
    def newest(num):
        return Item.query.order_by(desc(Item.date)).limit(num)

    def __repr__(self):
        return '<Item %r>' % self.name
예제 #3
0
class Categorie(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=False, nullable=False)
    categoriesItem = db.relationship('CategorieItem',
                                     backref='categorie',
                                     lazy=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Categorie('{self.name}','{self.id}')"
예제 #4
0
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(65), nullable=False, unique=True)
    picture = db.Column(db.String(250), nullable=False)
    email = db.Column(db.String(120), unique=True)
    items = db.relationship('Item', backref='user', lazy='dynamic')

    @property
    def serialize(self):
        return {
            'name': self.name,
            'picture': self.picture,
            'email': self.email,
            'items': self.serialize_items
        }

    @property
    def serialize_items(self):
        return [item.serialize for item in self.items]

    @staticmethod
    def __repr__(self):
        return '<User %r>' % self.name
예제 #5
0
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(100), unique=True, nullable=True)
    name = db.Column(db.String(100), nullable=True)
    password_hash = db.Column(db.String(64))
    picture = db.Column(db.String(200))
    active = db.Column(db.Boolean, default=False)
    items = db.relationship('CategorieItem', backref='author', lazy=True)
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow())

    def hash_password(self, password):
        self.password_hash = pwd_context.hash(password)

    def verify_password(self, password):
        return pwd_context.verify(password, self.password_hash)

    def generate_auth_token(self, expiration=600):
        s = Serializer(secret_key, expires_in=expiration)
        return s.dumps({'id': self.id})

    @staticmethod
    def verify_auth_token(token):
        s = Serializer(secret_key)
        try:
            data = s.loads(token)
        except SignatureExpired:
            # Valid Token, but expired
            return None
        except BadSignature:
            # Invalid Token
            return None
        user_id = data['id']
        return user_id

    def __repr__(self):
        return f"User('{self.name}', '{self.email}', '{self.id}')"
예제 #6
0
class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(65), nullable=False, unique=True)
    items = db.relationship('Item', backref="category", lazy='dynamic')

    @property
    def serialize(self):
        return {
            'id': self.id,
            'title': self.title,
            'items': self.serialize_items}

    @property
    def serialize_items(self):
        return [item.serialize for item in self.items]

    @staticmethod
    def getCategories():
        return Category.query.all()

    def __repr__(self):
        return '<Category %r>' % self.title
예제 #7
0
class LoginUser(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    password_hash = db.Column(db.String)

    @property
    def password(self):
        raise AttributeError('password: is a write-only field')

    @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)

    @staticmethod
    def get_by_username(username):
        return LoginUser.query.filter_by(username=username).first()

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