Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
peterchibunna committed Feb 23, 2023
1 parent 3a94c09 commit cbd1c37
Show file tree
Hide file tree
Showing 17 changed files with 534 additions and 1,830 deletions.
4 changes: 3 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

Jennifer Huang <[email protected]>
Alexa Orrico <[email protected]>
Joann Vuong <[email protected]>
Joann Vuong <[email protected]>
Peter Chibunna
Clinton Mokoya
7 changes: 3 additions & 4 deletions api/v1/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def not_found(error):
"""
return make_response(jsonify({'error': "Not found"}), 404)


app.config['SWAGGER'] = {
'title': 'AirBnB clone Restful API',
'uiversion': 3
Expand All @@ -42,8 +43,6 @@ def not_found(error):
""" Main Function """
host = environ.get('HBNB_API_HOST')
port = environ.get('HBNB_API_PORT')
if not host:
host = '0.0.0.0'
if not port:
port = '5000'
host = host if host else '0.0.0.0'
port = port if port else 5000
app.run(host=host, port=port, threaded=True)
5 changes: 0 additions & 5 deletions package.json

This file was deleted.

2 changes: 2 additions & 0 deletions web_dynamic/0-hbnb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/python3
""" Starts a Flash Web Application """
import uuid

from models import storage
from models.state import State
from models.city import City
Expand Down
13 changes: 8 additions & 5 deletions web_dynamic/1-hbnb.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/usr/bin/python3
""" Starts a Flash Web Application """
import uuid

from models import storage
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.place import Place
from os import environ
from flask import Flask, render_template

app = Flask(__name__)


# app.jinja_env.trim_blocks = True
# app.jinja_env.lstrip_blocks = True

Expand All @@ -34,11 +39,9 @@ def hbnb():
places = storage.all(Place).values()
places = sorted(places, key=lambda k: k.name)

return render_template('1-hbnb.html',
states=st_ct,
amenities=amenities,
places=places,
cache_id=uuid.uuid4())
return render_template(
'1-hbnb.html', states=st_ct, amenities=amenities, places=places,
cache_id=uuid.uuid4())


if __name__ == "__main__":
Expand Down
13 changes: 8 additions & 5 deletions web_dynamic/2-hbnb.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/usr/bin/python3
""" Starts a Flash Web Application """
import uuid

from models import storage
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.place import Place
from os import environ
from flask import Flask, render_template

app = Flask(__name__)


# app.jinja_env.trim_blocks = True
# app.jinja_env.lstrip_blocks = True

Expand All @@ -34,11 +39,9 @@ def hbnb():
places = storage.all(Place).values()
places = sorted(places, key=lambda k: k.name)

return render_template('2-hbnb.html',
states=st_ct,
amenities=amenities,
places=places,
cache_id=uuid.uuid4())
return render_template(
'2-hbnb.html', states=st_ct, amenities=amenities, places=places,
cache_id=uuid.uuid4())


if __name__ == "__main__":
Expand Down
161 changes: 161 additions & 0 deletions web_dynamic/3-hbnb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/python3
""" Starts a Flash Web Application """
import uuid

from flask import Flask, render_template
from flask import jsonify, abort, request
from models import storage
from api.v1.views import app_views
from models.city import City
from models.place import Place
from models.user import User
from models.state import State
from models.amenity import Amenity
from flasgger.utils import swag_from

app = Flask(__name__)


# app.jinja_env.trim_blocks = True
# app.jinja_env.lstrip_blocks = True


@app_views.teardown_appcontext
def close_db(error):
""" Remove the current SQLAlchemy Session """
storage.close()


@app.route('/3-hbnb/', strict_slashes=False)
def hbnb():
""" HBNB is alive! """
states = storage.all(State).values()
states = sorted(states, key=lambda k: k.name)
st_ct = []

for state in states:
st_ct.append([state, sorted(state.cities, key=lambda k: k.name)])

amenities = storage.all(Amenity).values()
amenities = sorted(amenities, key=lambda k: k.name)

places = storage.all(Place).values()
places = sorted(places, key=lambda k: k.name)

return render_template(
'3-hbnb.html', states=st_ct, amenities=amenities, places=places,
cache_id=uuid.uuid4())


@app_views.route(
"/cities/<city_id>/places", methods=["GET"], strict_slashes=False)
def place_by_city(city_id):
"""View function that return place objects by city"""
city = storage.get(City, city_id)
if city is None:
abort(404)
return jsonify([place.to_dict() for place in city.places])


@app_views.route("/places/<place_id>", methods=["GET"], strict_slashes=False)
def show_place(place_id):
"""Endpoint that return a Place object"""
place = storage.get(Place, place_id)
if place is None:
abort(404)
return jsonify(place.to_dict())


@app_views.route("/places/<place_id>", methods=["DELETE"],
strict_slashes=False)
def delete_place(place_id):
"""Endpoint that delete a Place object"""
place = storage.get(Place, place_id)
if place is None:
abort(404)
place.delete()
storage.save()
return jsonify({})


@app_views.route("/cities/<city_id>/places", methods=["POST"],
strict_slashes=False)
def insert_place(city_id):
"""Endpoint that insert a Place object"""
city = storage.get(City, city_id)
if city is None:
abort(404)
res = request.get_json()
if type(res) != dict:
abort(400, description="Not a JSON")
if not res.get("user_id"):
abort(400, description="Missing user_id")
user = storage.get(User, res.get("user_id"))
if user is None:
abort(404)
if not res.get("name"):
abort(400, description="Missing name")
new_place = Place(**res)
new_place.city_id = city_id
new_place.save()
return jsonify(new_place.to_dict()), 201


@app_views.route("/places_search", methods=["POST"], strict_slashes=False)
def places_search():
"""Retrieves all Place objects depending on the body of the request"""
body = request.get_json()
if type(body) != dict:
abort(400, description="Not a JSON")
id_states = body.get("states", [])
id_cities = body.get("cities", [])
id_amenities = body.get("amenities", [])

if id_states == id_cities == []:
places = storage.all(Place).values()
else:
states = [storage.get(State, _id) for _id in id_states if storage.get(
State, _id)]
cities = [city for state in states for city in state.cities]
cities.extend([
storage.get(City, _id) for _id in id_cities
if storage.get(City, _id)
])
cities = list(set(cities))
places = [place for city in cities for place in city.places]

amenities = [
storage.get(Amenity, _id) for _id in id_amenities
if storage.get(Amenity, _id)
]

results = []
for place in places:
results.append(place.to_dict())
for amenity in amenities:
if amenity not in place.amenities:
results.pop()
break

return jsonify(results)


@app_views.route("/places/<place_id>", methods=["PUT"], strict_slashes=False)
def update_place(place_id):
"""update a Place object"""
place = storage.get(Place, place_id)
if place is None:
abort(404)
props = request.get_json()
if type(props) != dict:
abort(400, description="Not a JSON")
for k, v in props.items():
if k not in ["id", "user_id", "city_id", "created_at", "updated_at"]:
setattr(place, k, v)
storage.save()
return jsonify(place.to_dict()), 200


if __name__ == "__main__":
""" Main Function """
app.run(host='0.0.0.0', port=5000)
Binary file removed web_dynamic/__pycache__/0-hbnb.cpython-38.pyc
Binary file not shown.
Binary file removed web_dynamic/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
18 changes: 9 additions & 9 deletions web_dynamic/static/scripts/1-hbnb.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
$( document ).ready(function () {
let amenityList = [];
$('input[type=checkbox]').change (function () {
let name = $(this).attr('data-name');
if ($(this).is(':checked')) {
$(document).ready(function () {
let amenityList = [];
$('input[type=checkbox]').change(function () {
let name = $(this).attr('data-name');
if ($(this).is(':checked')) {
amenityList.push(name);
} else {
} else {
amenityList = amenityList.filter(amen => amen !== name);
}
$('.amenities h4').text(amenityList.join(', '));
});
}
$('.amenities h4').text(amenityList.join(', '));
});
});
43 changes: 21 additions & 22 deletions web_dynamic/static/scripts/2-hbnb.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
$( document ).ready(function () {
let amenityList = [];
$('input[type=checkbox]').change (function () {
let name = $(this).attr('data-name');
if ($(this).is(':checked')) {
$(document).ready(function () {
let amenityList = [];
$('input[type=checkbox]').change(function () {
let name = $(this).attr('data-name');
if ($(this).is(':checked')) {
amenityList.push(name);
} else {
} else {
amenityList = amenityList.filter(amen => amen !== name);
}
$('.amenities h4').text(amenityList.join(', '));
});
}
$('.amenities h4').text(amenityList.join(', '));
});

// display red circle on top right of page if status is OK
$.ajax({
type: 'GET',
url: 'http://0.0.0.0:5001/api/v1/status/',
dataType: 'json',
success: function (data) {
if (data.status === 'OK') {
$('#api_status').addClass('available');

// display red circle on top right of page if status is OK
$.ajax({
type: 'GET',
url: 'http://0.0.0.0:5001/api/v1/status/',
dataType: 'json',
success: function (data) {
if (data.status === 'OK') {
$('#api_status').addClass('available');
} else {
$('#api_status').removeClass('available');
}
$('#api_status').removeClass('available');
}
});

}
});

});
27 changes: 27 additions & 0 deletions web_dynamic/static/scripts/3-hbnb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$(document).ready(function () {
let amenityList = [];
$('input[type=checkbox]').change(function () {
let name = $(this).attr('data-name');
if ($(this).is(':checked')) {
amenityList.push(name);
} else {
amenityList = amenityList.filter(amen => amen !== name);
}
$('.amenities h4').text(amenityList.join(', '));
});

// display red circle on top right of page if status is OK
$.ajax({
type: 'GET',
url: 'http://0.0.0.0:5001/api/v1/status/',
dataType: 'json',
success: function (data) {
if (data.status === 'OK') {
$('#api_status').addClass('available');
} else {
$('#api_status').removeClass('available');
}
}
});

});
Loading

0 comments on commit cbd1c37

Please sign in to comment.