forked from stvngrcia/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstate.py
executable file
·40 lines (35 loc) · 1.07 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
37
38
39
40
#!/usr/bin/python3
"""
Implementation of the State class
"""
from models.base_model import BaseModel, Base
from sqlalchemy import Column, String
from sqlalchemy.orm import relationship
from models.city import City
from os import getenv
import models
storage_type = getenv("HBNB_TYPE_STORAGE")
class State(BaseModel, Base):
'''
Implementation for the State.
'''
__tablename__ = 'states'
if storage_type == 'db':
name = Column(String(128), nullable=False)
cities = relationship("City", backref="state",
cascade="all, delete-orphan")
else:
name = ""
if storage_type != 'db':
@property
def cities(self):
"""
get list of City instances with state_id
equals to the current State.id
"""
list_cities = []
all_cities = models.storage.all(City)
for key, city_obj in all_cities.items():
if city_obj.state_id == self.id:
list_cities.append(city_obj)
return list_cities