ciclos

ref: responsive

src/types/group.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
from . import db
from datetime import datetime as dt
from .product import Product

class Group(db.Model):
    __tablename__ = 'groups'
    
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(255), nullable = False)
    location = db.Column(db.String(255), nullable = False)
    lat = db.Column(db.String(255))
    lng = db.Column(db.String(255))

    products = db.relationship("Product", backref = "group", lazy = True)

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

    def __init__(self, name, location, lat, lng):
        self.name = name
        self.location = location
        self.lat = lat
        self.lng = lng

    def create(self):
        db.session.add(self)
        db.session.commit()
    
    def to_json(self):
        return {
            "id": self.id,
            "name": self.name,
            "location": self.location,
            "products": list(map(lambda p: p.to_json(),self.products))
        }