forked from learn-co-curriculum/python-p3-freebie-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
92 lines (61 loc) · 3.37 KB
/
models.py
File metadata and controls
92 lines (61 loc) · 3.37 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
from sqlalchemy import (Column, String, Integer, ForeignKey, create_engine, asc)
from sqlalchemy.orm import relationship, backref, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
Base = declarative_base()
def create_session():
engine = create_engine("sqlite:///freebies.db")
Session = sessionmaker(bind=engine)
session = Session()
return session
class Freebie(Base):
__tablename__ = 'freebies'
id = Column(Integer(), primary_key=True)
item_name = Column(String())
value = Column(Integer())
company_id = Column(ForeignKey('companies.id')) #we say companies.id here because we are referencing the TABLENAME. SQLAlchemy uses the table name to find the table and generates a foreign key using the id column
dev_id = Column(ForeignKey('devs.id'))
def __repr__(self):
return f'<Freebie {self.item_name}>'
def print_details(self):
print(f'{self.dev.name} owns {self.item_name} from {self.company.name}')
class Company(Base):
__tablename__ = 'companies'
id = Column(Integer(), primary_key=True)
name = Column(String())
founding_year = Column(Integer())
freebies = relationship('Freebie', backref='company')
devs = association_proxy('freebies', 'dev', creator=lambda d: Freebie(dev=d))
#why do we need an association proxy?
#an association proxy creates a read/write view of an attribute ACROSS a relationship. The benefit is that it essentially conceals the usage of a middle attribute within a many to many relationship
#in the above, we look through the freebies table and create an association between the Dev and Company models using the dev column within freebies.
def __repr__(self):
return f'<Company {self.name}>'
def give_freebie(self, dev, item_name, value):
pass
freebie = Freebie(item_name = item_name, value = value, company_id = self.id, dev_id = dev.id)
@classmethod
def oldest_company(cls):
pass
session = create_session()
oldest_company = session.query(cls).order_by(asc(cls.founding_year)).limit(1).first()
session.close()
return oldest_company
class Dev(Base):
__tablename__ = 'devs'
id = Column(Integer(), primary_key=True)
name= Column(String())
freebies = relationship('Freebie', backref='dev')
companies = association_proxy('freebies', 'company', creator=lambda c: Freebie(company=d))
#why do we need the creator method here?
#when we add a new company to our companies attribute, we need to create a new instance of the middle object. Under the hood, Freebies is what establishes the relationship between Dev and Company. To create a new instance of Freebies, you would need all of its required fields like item_name, value, company_id, dev_id, etc. The creator function lets us avoid this problem by allowing us to create that middle object instance using a single argument.
def __repr__(self):
return f'<Dev {self.name}>'
def received_one(self, item_name):
return item_name in [freebie.item_name for freebie in self.freebies]
def give_away(self, dev, freebie):
#if the freebie belongs to the current dev
if (self.received_one(freebie.item_name)):
freebie.dev_id = dev.id
return freebie