-
Notifications
You must be signed in to change notification settings - Fork 22
/
place.py
executable file
·79 lines (72 loc) · 2.77 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
#!/usr/bin/python3
""" holds class Place"""
from models.base_model import BaseModel, Base
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime
from sqlalchemy import Float
from sqlalchemy.orm import relationship
import models
if models.storage_type == "db":
class PlaceAmenity(Base):
__tablename__ = "place_amenity"
place_id = Column(String(80),
ForeignKey("places.id"),
primary_key=True,
nullable=False)
amenity_id = Column(String(80),
ForeignKey("amenities.id"),
primary_key=True,
nullable=False)
metadata = Base.metadata
class Place(BaseModel, Base):
"""Representation of Place """
if models.storage_type == "db":
__tablename__ = "places"
city_id = Column(String(60),
ForeignKey("cities.id"))
user_id = Column(String(60),
ForeignKey("users.id"))
name = Column(String(128),
nullable=False)
description = Column(String(1024),
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)
longitude = Column(Float)
reviews = relationship("Review",
backref="place",
cascade="delete")
amenities = relationship("Amenity",
secondary="place_amenity",
viewonly=False)
else:
city_id = ""
user_id = ""
name = ""
number_rooms = 0
number_bathrooms = 0
max_guest = 0
price_by_night = 0
@property
def reviews(self):
"""Returns respective list of reviews"""
all_reviews = models.storage.all(Review)
return list(filter((lambda c: c.place_id == self.id), all_reviews))
@property
def amenities(self):
"""Returns respective list of reviews"""
amenities = models.storage.all(Amenity)
return list(filter((lambda c: c.place_id == self.id), amenities))
def __init__(self, *args, **kwargs):
"""initializes Place"""
super().__init__(*args, **kwargs)