예제 #1
0
class Store(BaseModel):
    __tablename__ = "stores"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(150), unique=True, index=True)
    slug = db.Column(db.String(255))
    description = db.Column(db.String(255), nullable=True)
    user_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)

    products = db.relationship("Product", back_populates="store")
    owner = db.relationship("User", uselist=False, back_populates="store")

    @classmethod
    def find_by_slug(cls, slug):
        store = cls.query.filter_by(slug=slug).first()
        if not store:
            return False
        return store

    @validates("name")
    def validate_name(self, key, name):
        if len(name.strip()) <= 3:
            raise ValueError(
                "The store name must be greater than 3 characters long")
        return name

    @staticmethod
    def generate_slug(target, value, old_value, initiator):
        if value and (not target.slug or value != oldvalue):
            target.slug = slugify(value)
예제 #2
0
class Store(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(225), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    user = db.relationship('User', uselist=False, back_populates='store')
    products = db.relationship('Product', back_populates='store', lazy='joined')

    @validates('name')
    def validate_name(self, key, name):
        if len(name.strip()) <= 3:
            raise ValueError('needs to have a name')
        return name
예제 #3
0
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(225), nullable=False)
    password = db.Column(db.String(225), nullable=False)

    store = db.relationship('Store', uselist=False, back_populates='user')
    products = db.relationship("Product", back_populates='creator')

    @classmethod
    def create(cls, email, password):
        """ Usage: User.create('*****@*****.**', 'example')
        """
        hashed_password = generate_password_hash(password)
        return User(email=email.lower().strip(), password=hashed_password)
예제 #4
0
class Order(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    purchaser_email = db.Column(db.String(255), nullable=False)
    purchased_at = db.Column(db.DateTime, nullable=False)
    payment_reference_id = db.Column(db.String(255))
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False)

    product = db.relationship("Product", back_populates="orders")
예제 #5
0
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), nullable=False)
    password = db.Column(db.String(), nullable=False)

    store = db.relationship("Store", uselist=False, back_populates='user')
    products = db.relationship("Product", back_populates='creator')

    @classmethod
    def create(cls, email, password):
        if not email or not password:
            raise ValueError('email and password are required')
        hashed_password = generate_password_hash(password)
        return User(email=email.lower().strip(), password=hashed_password)

    @property
    def is_authenticated(self):
        return not isinstance(self, AnonymousUserMixin)
예제 #6
0
class Product(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(255), nullable = False)
    description = db.Column(db.String(120), nullable = True)
    creator_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    store_id = db.Column(db.Integer, db.ForeignKey('store.id'))
    price_cents = db.Column(db.Integer)
    picture_url = db.Column(db.Text)

    creator = db.relationship("User", uselist=False, back_populates="products")
    store = db.relationship('Store', uselist=False, back_populates='products')

    @property
    def primary_image_url(self):
        return self.picture_url or "https://placehold.co/600x400?text={}".format(self.name)

    @validates('name')
    def validate_name(self, key, name):
        if len(name.strip()) <= 3:
            raise ValueError("Needs to have a real name")
        return name
예제 #7
0
파일: models.py 프로젝트: dieume-n/Yumroad
class Product(BaseModel):
    __tablename__ = "products"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    description = db.Column(db.String(150), nullable=True)
    store_id = db.Column(db.Integer, db.ForeignKey(Store.id), nullable=False)
    price_cents = db.Column(db.Integer)
    picture_url = db.Column(db.String, default="default.png")

    store = db.relationship("Store", uselist=False, back_populates="products")

    @validates("name")
    def validate_name(self, key, name):
        if len(name.strip()) <= 3:
            raise ValueError(
                "The name of the product must be greater than 3 characters long"
            )
        return name

    @property
    def picture(self):
        return f"img/{self.picture_url}"