forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
place.py
executable file
·83 lines (75 loc) · 3.07 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
#!/usr/bin/python3
"""This is the place class"""
from sqlalchemy.ext.declarative import declarative_base
from models.base_model import BaseModel, Base
from sqlalchemy import Column, Table, String, Integer, Float, ForeignKey
from sqlalchemy.orm import relationship
from os import getenv
import models
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):
"""This is the class for Place
Attributes:
city_id: city id
user_id: user id
name: name input
description: string of description
number_rooms: number of room in int
number_bathrooms: number of bathrooms in int
max_guest: maximum guest in int
price_by_night:: pice for a staying in int
latitude: latitude in flaot
longitude: longitude in float
amenity_ids: list of Amenity ids
"""
__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))
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)
longitude = Column(Float)
amenity_ids = []
if getenv("HBNB_TYPE_STORAGE") == "db":
reviews = relationship("Review", cascade='all, delete, delete-orphan',
backref="place")
amenities = relationship("Amenity", secondary=place_amenity,
viewonly=False,
back_populates="place_amenities")
else:
@property
def reviews(self):
""" Returns list of reviews.id """
var = models.storage.all()
lista = []
result = []
for key in var:
review = key.replace('.', ' ')
review = shlex.split(review)
if (review[0] == 'Review'):
lista.append(var[key])
for elem in lista:
if (elem.place_id == self.id):
result.append(elem)
return (result)
@property
def amenities(self):
""" Returns list of amenity ids """
return self.amenity_ids
@amenities.setter
def amenities(self, obj=None):
""" Appends amenity ids to the attribute """
if type(obj) is Amenity and obj.id not in self.amenity_ids:
self.amenity_ids.append(obj.id)