ciclos

ref: configuration

src/types/product.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from . import db
from .balance import Balance
from datetime import datetime as dt

class Product(db.Model):
    __tablename__ = 'products'
    id = db.Column(db.Integer, primary_key = True)
    title = db.Column(db.String(256), nullable = False)
    description = db.Column(db.Text)
    group_id = db.Column(db.Integer, db.ForeignKey('groups.id'), nullable = False)
    category_id = db.Column(db.Integer, db.ForeignKey('product_categories.id'), nullable = False)
    price = db.Column(db.Numeric(10, 2), nullable = False, default = 0.0)

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


    balance = db.relationship("Balance", backref = "product", lazy = True, uselist = False)

    def __init__(self, title, description, group_id, category_id, price):
        self.title = title
        self.description = description
        self.group_id = group_id
        self.category_id = category_id
        self.price = price

    def create(self):
        db.session.add(self)
        db.session.commit()

        Balance(0.0, 0.0, self.id).create()

        return self

    def price_on(self, cycle):
        margin = (cycle.price_margin * self.price) / 100
        return self.price + margin

    def to_json(self):
        return {
            "title": self.title,
            "description": self.description,
            "group_id": self.group_id,
            "category_id": self.category_id,
            "price": self.price,
            "created_at": self.created_at.__str__(),
            "updated_at": self.updated_at.__str__(),
            "balance": (self.balance.to_json() if self.balance else None)
        }