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
149 lines (114 loc) · 4.94 KB
/
models.py
File metadata and controls
149 lines (114 loc) · 4.94 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from sqlalchemy import ForeignKey, Column, Integer, String, MetaData, create_engine
from sqlalchemy.orm import relationship, declarative_base, sessionmaker
import os
convention = {
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
}
metadata = MetaData(naming_convention=convention)
Base = declarative_base(metadata=metadata)
class Company(Base):
__tablename__ = 'companies'
id = Column(Integer(), primary_key=True)
name = Column(String())
founding_year = Column(Integer())
# Relationship to freebies
freebies = relationship('Freebie', back_populates='company')
def __repr__(self):
return f'<Company {self.name}>'
# Aggregate Methods
def give_freebie(self, dev, item_name, value):
"""Creates a new Freebie associated with this company and the given dev"""
# Create new freebie
new_freebie = Freebie(
item_name=item_name,
value=value,
dev=dev,
company=self
)
return new_freebie
@classmethod
def oldest_company(cls):
"""Returns the Company instance with the earliest founding year"""
# Fix database path - use current directory
db_path = os.path.join(os.path.dirname(__file__), 'freebies.db')
engine = create_engine(f'sqlite:///{db_path}')
Session = sessionmaker(bind=engine)
session = Session()
oldest = session.query(cls).order_by(cls.founding_year.asc()).first()
session.close()
return oldest
@property
def devs(self):
"""Returns a collection of all devs who collected freebies from the company"""
return list(set([freebie.dev for freebie in self.freebies]))
class Dev(Base):
__tablename__ = 'devs'
id = Column(Integer(), primary_key=True)
name = Column(String())
# Relationship to freebies
freebies = relationship('Freebie', back_populates='dev')
def __repr__(self):
return f'<Dev {self.name}>'
@property
def companies(self):
"""Returns a collection of all companies that the Dev has collected freebies from"""
return list(set([freebie.company for freebie in self.freebies]))
# Aggregate Methods
def received_one(self, item_name):
"""Returns True if any of the freebies associated with the dev has that item_name"""
return any(freebie.item_name == item_name for freebie in self.freebies)
def give_away(self, dev, freebie):
"""Changes the freebie's dev to be the given dev if the freebie belongs to this dev"""
if freebie in self.freebies:
freebie.dev = dev
return True
return False
class Freebie(Base):
__tablename__ = 'freebies'
id = Column(Integer(), primary_key=True)
item_name = Column(String(), nullable=False)
value = Column(Integer(), nullable=False)
# Foreign Keys
dev_id = Column(Integer(), ForeignKey('devs.id'), nullable=False)
company_id = Column(Integer(), ForeignKey('companies.id'), nullable=False)
# Relationships
dev = relationship('Dev', back_populates='freebies')
company = relationship('Company', back_populates='freebies')
def __repr__(self):
return f'<Freebie {self.item_name}>'
def print_details(self):
"""Returns a formatted string with freebie details"""
return f"{self.dev.name} owns a {self.item_name} from {self.company.name}"
# Test the models if run directly
if __name__ == "__main__":
print(" Models loaded successfully!")
print("Available models: Company, Dev, Freebie")
# Test database connection
try:
db_path = os.path.join(os.path.dirname(__file__), 'freebies.db')
engine = create_engine(f'sqlite:///{db_path}')
# Test if we can connect
with engine.connect() as conn:
print(f" Database connection successful: {db_path}")
# Check if tables exist
if os.path.exists(db_path):
Session = sessionmaker(bind=engine)
session = Session()
try:
company_count = session.query(Company).count()
dev_count = session.query(Dev).count()
freebie_count = session.query(Freebie).count()
print(f" Current data:")
print(f" Companies: {company_count}")
print(f" Developers: {dev_count}")
print(f" Freebies: {freebie_count}")
if company_count == 0:
print(" Run 'python seed.py' to add sample data")
except Exception as e:
print(f" Tables may not exist yet. Run 'alembic upgrade head' first")
finally:
session.close()
else:
print(" Database file doesn't exist. Run 'alembic upgrade head' and 'python seed.py'")
except Exception as e:
print(f" Database connection failed: {e}")