-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.py
36 lines (30 loc) · 1.09 KB
/
state.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
#!/usr/bin/python3
""" State Module for HBNB project """
from models.base_model import BaseModel, Base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship
from models.city import City
from os import getenv
storage_type = getenv("HBNB_TYPE_STORAGE")
class State(BaseModel, Base):
"""State class"""
__tablename__ = "states"
if storage_type == "db":
name = Column(String(128), nullable=False)
cities = relationship("City", backref="state", cascade="all,delete")
else:
name = ""
@property
def cities(self):
"""
getter attribute cities that returns the list of City instances
with state_id equals to the current State.id => It will
be the FileStorage relationship between State and City
"""
from models import storage
citiesList = []
citiesAll = storage.all(City)
for city in citiesAll.values():
if city.state_id == self.id:
citiesList.append(city)
return citiesList