-
Notifications
You must be signed in to change notification settings - Fork 13
/
state.py
executable file
·36 lines (32 loc) · 1.11 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
""" holds class State"""
import models
from models.base_model import BaseModel, Base
from models.city import City
from os import getenv
import sqlalchemy
from sqlalchemy import Column, String, ForeignKey
from sqlalchemy.orm import relationship
class State(BaseModel, Base):
"""Representation of state """
if models.storage_t == "db":
__tablename__ = 'states'
name = Column(String(128), nullable=False)
cities = relationship("City",
backref="state",
cascade="all, delete, delete-orphan")
else:
name = ""
def __init__(self, *args, **kwargs):
"""initializes state"""
super().__init__(*args, **kwargs)
if models.storage_t != "db":
@property
def cities(self):
"""getter for list of city instances related to the state"""
city_list = []
all_cities = models.storage.all(City)
for city in all_cities.values():
if city.state_id == self.id:
city_list.append(city)
return city_list