-
Notifications
You must be signed in to change notification settings - Fork 5
/
city.py
executable file
·37 lines (32 loc) · 1.18 KB
/
city.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
36
37
#!/usr/bin/python3
""" holds class City"""
import models
from models import BaseModel, Base
from os import getenv
from sqlalchemy import Column, String
from sqlalchemy.orm import relationship
from sqlalchemy import ForeignKey
class City(BaseModel, Base):
"""Representation of city """
__tablename__ = 'cities'
name = Column(String(128),
nullable=False)
state_id = Column(String(60),
ForeignKey('states.id'),
nullable=False)
if getenv("HBNB_TYPE_STORAGE") in ["db", "sl"]:
places = relationship("Place",
backref="cities",
cascade="all, delete-orphan")
else:
@property
def places(self):
"""Return all places associated with the current city"""
place_values = models.storage.all("Place").values()
return list(filter(lambda p: p.city_id == self.id,
place_values))
def __init__(self, *args, **kwargs):
"""initializes city"""
self.name = kwargs.pop("name", "")
self.state_id = kwargs.pop("state_id", "")
super().__init__(*args, **kwargs)