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 pathdev.py
More file actions
40 lines (30 loc) · 1.19 KB
/
dev.py
File metadata and controls
40 lines (30 loc) · 1.19 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
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, backref
from .base import Base
from .association import company_dev
class Dev(Base):
__tablename__ = 'devs'
id = Column(Integer(), primary_key=True)
name= Column(String(), nullable=False)
freebies = relationship('Freebie', backref=backref('dev'), cascade='all, delete-orphan')
companies = relationship('Company', secondary=company_dev, back_populates='devs')
def __repr__(self):
return (
f'Dev(id={self.id},'
f'name={self.name})'
)
def received_one(self, item_name:str) -> bool:
"""
Checks if a dev has a freebie with the indicated item name.
"""
return any(freebie.item_name == item_name for freebie in self.freebies)
def give_away(self, dev, freebie):
"""
if a dev already owns a certain freebie, this method allows the dev
to transfer such a freebie to another dev.
"""
if freebie in self.freebies:
freebie.dev = dev
return freebie
else:
raise ValueError(f"{self.name} does not own this freebie.")