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
155 lines (131 loc) · 4.85 KB
/
Copy pathmodels.py
File metadata and controls
155 lines (131 loc) · 4.85 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
150
151
152
153
154
155
# lib/models.py
from sqlalchemy import ForeignKey, Column, Integer, String, MetaData
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from colorama import Fore, Style, init
# Initialize colorama for colored output
init(autoreset=True)
# Define the naming convention for foreign keys
convention = {
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
}
metadata = MetaData(naming_convention=convention)
# Create the base class for declarative models
Base = declarative_base(metadata=metadata)
class Company(Base):
__tablename__ = 'companies'
# Columns for the companies table
id = Column(Integer(), primary_key=True)
name = Column(String())
founding_year = Column(Integer())
# Relationship: A company has many freebies
freebies = relationship('Freebie', back_populates='company')
# Relationship: A company has many devs through freebies (many-to-many)
# Added overlaps to silence the warning
devs = relationship(
'Dev',
secondary='freebies',
back_populates='companies',
overlaps="company,freebies"
)
def __repr__(self):
return f'<Company {self.name}>'
def give_freebie(self, dev, item_name, value):
"""
Creates a new Freebie instance associated with this company and the given dev.
Args:
dev (Dev): The Dev instance receiving the freebie
item_name (str): The name of the freebie item
value (int): The value of the freebie
Returns:
Freebie: The newly created Freebie instance
"""
freebie = Freebie(item_name=item_name, value=value, company=self, dev=dev)
return freebie
@classmethod
def oldest_company(cls, session):
"""
Returns the Company instance with the earliest founding year.
Args:
session: The SQLAlchemy session to use for the query
Returns:
Company: The oldest company instance
"""
from sqlalchemy import func
return session.query(cls).order_by(cls.founding_year.asc()).first()
class Dev(Base):
__tablename__ = 'devs'
# Columns for the devs table
id = Column(Integer(), primary_key=True)
name = Column(String())
# Relationship: A dev has many freebies
# Added overlaps to silence the warning
freebies = relationship(
'Freebie',
back_populates='dev',
overlaps="devs"
)
# Relationship: A dev has many companies through freebies (many-to-many)
# Added overlaps to silence the warning
companies = relationship(
'Company',
secondary='freebies',
back_populates='devs',
overlaps="company,dev,freebies"
)
def __repr__(self):
return f'<Dev {self.name}>'
def received_one(self, item_name):
"""
Checks if the dev has a freebie with the given item_name.
Args:
item_name (str): The name of the item to check for
Returns:
bool: True if the dev has a freebie with the item_name, False otherwise
"""
return any(freebie.item_name == item_name for freebie in self.freebies)
def give_away(self, dev, freebie):
"""
Transfers a freebie to another dev if the freebie belongs to this dev.
Args:
dev (Dev): The dev to give the freebie to
freebie (Freebie): The freebie to give away
"""
if freebie.dev == self:
freebie.dev = dev
def total_freebie_value(self):
"""
Calculates the total value of all freebies collected by the dev.
Returns:
int: The sum of the values of all freebies
"""
return sum(freebie.value for freebie in self.freebies)
class Freebie(Base):
__tablename__ = 'freebies'
# Columns for the freebies table
id = Column(Integer(), primary_key=True)
item_name = Column(String())
value = Column(Integer())
company_id = Column(Integer(), ForeignKey('companies.id'))
dev_id = Column(Integer(), ForeignKey('devs.id'))
# Explicitly define the dev relationship with overlaps
dev = relationship(
'Dev',
back_populates='freebies',
overlaps="devs"
)
# Explicitly define the company relationship
company = relationship(
'Company',
back_populates='freebies'
)
def print_details(self):
"""
Returns a formatted string with the freebie's details, styled with colors.
Returns:
str: A string in the format '{dev name} owns a {item_name} from {company name}'
"""
dev_name = f"{Fore.CYAN}{self.dev.name}{Style.RESET_ALL}"
item_name = f"{Fore.GREEN}{self.item_name}{Style.RESET_ALL}"
company_name = f"{Fore.MAGENTA}{self.company.name}{Style.RESET_ALL}"
return f"{dev_name} owns a {item_name} from {company_name}"