-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstate.py
executable file
·35 lines (30 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
#!/usr/bin/python3
"""This is the state class"""
from models.base_model import BaseModel, Base
from models.city import City
from sqlalchemy import Column, Integer, String, ForeignKey, MetaData
from sqlalchemy.orm import relationship, backref
import models
from os import environ
class State(BaseModel, Base):
"""This is the class for State
Attributes:
name: input name
"""
__tablename__ = 'states'
name = Column(String(128), nullable=False)
if environ.get('HBNB_TYPE_STORAGE') == "db":
cities = relationship("City",
backref="state",
cascade="all, delete, delete-orphan")
else:
@property
def cities(self):
""" Returns the list of City instances with
state_id == current State.id """
all_cities = models.storage.all(City)
state_cities = []
for city_ins in all_cities.values():
if city_ins.state_id == self.id:
state_cities.append(city_ins)
return state_cities