-
Notifications
You must be signed in to change notification settings - Fork 106
/
place.py
executable file
·94 lines (88 loc) · 3.42 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
#!/usr/bin/python3
""" holds class Place"""
import models
from models.base_model import BaseModel, Base
from os import getenv
import sqlalchemy
from sqlalchemy import Column, String, Integer, Float, Table, ForeignKey
from sqlalchemy.orm import relationship
if getenv('HBNB_TYPE_STORAGE') == 'db':
place_amenity = Table('place_amenity', Base.metadata,
Column('place_id',
String(60),
ForeignKey('places.id'),
primary_key=True,
nullable=False),
Column('amenity_id',
String(60),
ForeignKey('amenities.id'),
primary_key=True,
nullable=False))
class Place(BaseModel, Base):
"""Representation of Place """
if getenv('HBNB_TYPE_STORAGE') == 'db':
__tablename__ = 'places'
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(1024),
nullable=True)
number_rooms = Column(Integer,
default=0,
nullable=False)
number_bathrooms = Column(Integer,
default=0,
nullable=False)
max_guest = Column(Integer,
default=0,
nullable=False)
price_by_night = Column(Integer,
default=0,
nullable=False)
latitude = Column(Float, default=0.0)
longitude = Column(Float, default=0.0)
reviews = relationship("Review", cascade="all, delete",
backref="places")
amenities = relationship("Amenity",
secondary='place_amenity',
viewonly=False,
backref="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):
"""attribute that returns list of Review instances"""
values_review = models.storage.all("Review").values()
list_review = []
for review in values_review:
if review.place_id == self.id:
list_review.append(review)
return list_review
if getenv('HBNB_TYPE_STORAGE') != 'db':
@property
def amenities(self):
"""attribute that returns list of Amenity instances"""
values_amenity = models.storage.all("Amenity").values()
list_amenity = []
for amenity in values_amenity:
if amenity.place_id == self.id:
list_amenity.append(amenity)
return list_amenity