-
Notifications
You must be signed in to change notification settings - Fork 1
/
place.py
109 lines (97 loc) · 3.64 KB
/
place.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python3
""" Place Module for HBNB project """
from tempfile import gettempprefix
from models.base_model import BaseModel, Base
from sqlalchemy import Integer, Float, String, ForeignKey, Column, Table
from sqlalchemy.orm import relationship
from models.amenity import Amenity
from models.review import Review
from os import getenv
storage_type = getenv("HBNB_TYPE_STORAGE")
if storage_type == "db":
place_amenity = Table(
"place_amenity",
Base.metadata,
Column("place_id", String(60),
ForeignKey("places.id"), primary_key=True),
Column("amenity_id", String(60),
ForeignKey("amenities.id"),
primary_key=True),
)
class Place(BaseModel, Base):
"""A place to stay"""
__tablename__ = "places"
if storage_type == "db":
city_id = Column(String(60), ForeignKey("cities.id"), nullable=False)
user_id = Column(String(60), ForeignKey("users.id"), nullable=False)
name = Column(String(128), nullable=False)
description = Column(String(128), nullable=True)
number_rooms = Column(Integer, nullable=False, default=0)
number_bathrooms = Column(Integer, nullable=False, default=0)
max_guest = Column(Integer, nullable=False, default=0)
price_by_night = Column(Integer, nullable=False, default=0)
latitude = Column(Float, nullable=True)
longitude = Column(Float, nullable=True)
amenity_ids = []
reviews = relationship("Review", cascade="all,delete", backref="place")
amenities = relationship(
"Amenity",
secondary="place_amenity",
viewonly=False,
back_populates="place_amenities",
)
else:
city_id = ""
user_id = ""
name = ""
description = ""
number_rooms = 0
number_bathrooms = 0
max_guest = 0
price_by_night = 0
latitude = 0.0
longitude = 0.0
amenity_ids = []
def __init__(self, *args, **kwargs):
"""initializes Place"""
super().__init__(*args, **kwargs)
@property
def reviews(self):
"""
Getter attribute reviews that returns the list of Review instances
with place_id equals
to the current Place.id => It will be the FileStorage
relationship between Place and Review
"""
from models import storage
reviews_list = []
for k, v in storage.all():
if k.partition(".")[0] == "Review":
if v.place_id == self.id:
reviews_list.append(v)
return reviews_list
@property
def amenities(self):
"""
Getter attribute amenities that
returns the list of Amenity instances
based on the attribute amenity_ids
that contains all Amenity.id linked to the Place
"""
from models import storage
amenins = []
for k, v in storage.all(Amenity).items():
if k.partition(".")[0] == "Amenity":
if v.id in self.amenity_ids:
amenins.append(v)
return amenins
@amenities.setter
def amenities(self, amenity):
"""
Setter attribute amenities that handles append method
for adding an Amenity.id to the attribute amenity_ids.
This method should accept only Amenity object, otherwise,
do nothing.
"""
if amenity.__class__.__name__ == "Amenity":
self.amenity_ids.append(amenity.id)