This repo holds the culmination of HBNB - a Fullstack Project I did at Alx-Software Engineering Programme that collectively covers fundamental concepts of higher level programming. It is built in stages(versions) and involves collaborating with peers at several stages as was advised in the programme. The goal of AirBnB Clone project is to eventually deploy on a server, a simple copy(HBnB) of the AirBnB Website.
It is a complete web application composed by:
- A command interpreter to manipulate data without a visual interface, like in a Shell (perfect for development and debugging)
- A website (the front-end) that shows the final product to everybody: static and dynamic
- A database or files that store data (data = objects)
- An API that provides a communication interface between the front-end and the data (retrieve, create, delete, update them)
See a snapshot of the success below.
This is a unique overview and not an exhaustive documentation of different processes and aspects of the project that lead to this final repo.
- THE CONSOLE
- Environment
- Installation
- File Descriptions
- Examples of Console use
- VERSION 2
- Console improvements
- Dataflow and Relationship diagram
- Additional updates 1: Deploy Static
- Additional updates 2: Web framework and Templating
- VERSION 3: RESTful API
- VERSION 4
- Additional updates 3: Application Server
- Bugs
- Authors
- License
The console is the first segment of the AirBnB project. A command line interpreter is created in this segment to manage objects for the AirBnB(HBnB) website.
Concepts learned by building this command interpreter include:
- Creating a Python package and creating a command interpreter in Python using the cmd module
- Unit testing and how to implement it in a large project
- Serializing and deserializing a Class, How to write and read a JSON file.
- Mananaging datetime and making use of an UUID for unique IDs
- '*args' and '**kwargs' and handling named arguments in a function
- Create a new object (ex: a new User or a new Place)
- Retrieve an object from a file, a database etc...
- Do operations on objects (count, compute stats, etc...)
- Update attributes of an object
- Destroy an object
This project is interpreted/tested on Ubuntu 14.04 LTS using python3 (version 3.4.3)
- Clone this repository:
git clone "https://github.com/David-Inkheart/HBNB_V4.git"
- Access HBNB directory:
cd HBNB_V4
- Run hbnb(interactively):
./console
and enter command - Run hbnb(non-interactively):
echo "<command>" | ./console.py
console.py - the console contains the entry point of the command interpreter. List of commands this console current supports:
EOF
- exits consolequit
- exits console<emptyline>
- overwrites default emptyline method and does nothingcreate
- Creates a new instance ofBaseModel
, saves it (to the JSON file) and prints the iddestroy
- Deletes an instance based on the class name and id (save the change into the JSON file).show
- Prints the string representation of an instance based on the class name and id.all
- Prints all string representation of all instances based or not on the class name.update
- Updates an instance based on the class name and id by adding or updating attribute (save the change into the JSON file).
base_model.py - The BaseModel class from which future classes was be derived
def __init__(self, *args, **kwargs)
- Initialization of the base modeldef __str__(self)
- String representation of the BaseModel classdef save(self)
- Updates the attributeupdated_at
with the current datetimedef to_dict(self)
- returns a dictionary containing all keys/values of the instance
Classes inherited from Base Model:
/models/engine
directory contains File Storage class that handles JASON serialization and deserialization :
file_storage.py - serializes instances to a JSON file & deserializes back to instances
def all(self)
- returns the dictionary __objectsdef new(self, obj)
- sets in __objects the obj with key .iddef save(self)
- serializes __objects to the JSON file (path: __file_path)def reload(self)
- deserializes the JSON file to __objects
/tests
directory contains all unit test cases for this project:
/test_models/test_base_model.py - Contains the TestBaseModel and TestBaseModelDocs classes TestBaseModelDocs class:
def setUpClass(cls)
- Set up for the doc testsdef test_pep8_conformance_base_model(self)
- Test that models/base_model.py conforms to PEP8def test_pep8_conformance_test_base_model(self)
- Test that tests/test_models/test_base_model.py conforms to PEP8def test_bm_module_docstring(self)
- Test for the base_model.py module docstringdef test_bm_class_docstring(self)
- Test for the BaseModel class docstringdef test_bm_func_docstrings(self)
- Test for the presence of docstrings in BaseModel methods
TestBaseModel class:
def test_is_base_model(self)
- Test that the instatiation of a BaseModel worksdef test_created_at_instantiation(self)
- Test created_at is a pub. instance attribute of type datetimedef test_updated_at_instantiation(self)
- Test updated_at is a pub. instance attribute of type datetimedef test_diff_datetime_objs(self)
- Test that two BaseModel instances have different datetime objects
/test_models/test_amenity.py - Contains the TestAmenityDocs class:
def setUpClass(cls)
- Set up for the doc testsdef test_pep8_conformance_amenity(self)
- Test that models/amenity.py conforms to PEP8def test_pep8_conformance_test_amenity(self)
- Test that tests/test_models/test_amenity.py conforms to PEP8def test_amenity_module_docstring(self)
- Test for the amenity.py module docstringdef test_amenity_class_docstring(self)
- Test for the Amenity class docstring
/test_models/test_city.py - Contains the TestCityDocs class:
def setUpClass(cls)
- Set up for the doc testsdef test_pep8_conformance_city(self)
- Test that models/city.py conforms to PEP8def test_pep8_conformance_test_city(self)
- Test that tests/test_models/test_city.py conforms to PEP8def test_city_module_docstring(self)
- Test for the city.py module docstringdef test_city_class_docstring(self)
- Test for the City class docstring
/test_models/test_file_storage.py - Contains the TestFileStorageDocs class:
def setUpClass(cls)
- Set up for the doc testsdef test_pep8_conformance_file_storage(self)
- Test that models/file_storage.py conforms to PEP8def test_pep8_conformance_test_file_storage(self)
- Test that tests/test_models/test_file_storage.py conforms to PEP8def test_file_storage_module_docstring(self)
- Test for the file_storage.py module docstringdef test_file_storage_class_docstring(self)
- Test for the FileStorage class docstring
/test_models/test_place.py - Contains the TestPlaceDoc class:
def setUpClass(cls)
- Set up for the doc testsdef test_pep8_conformance_place(self)
- Test that models/place.py conforms to PEP8.def test_pep8_conformance_test_place(self)
- Test that tests/test_models/test_place.py conforms to PEP8.def test_place_module_docstring(self)
- Test for the place.py module docstringdef test_place_class_docstring(self)
- Test for the Place class docstring
/test_models/test_review.py - Contains the TestReviewDocs class:
def setUpClass(cls)
- Set up for the doc testsdef test_pep8_conformance_review(self)
- Test that models/review.py conforms to PEP8def test_pep8_conformance_test_review(self)
- Test that tests/test_models/test_review.py conforms to PEP8def test_review_module_docstring(self)
- Test for the review.py module docstringdef test_review_class_docstring(self)
- Test for the Review class docstring
/test_models/state.py - Contains the TestStateDocs class:
def setUpClass(cls)
- Set up for the doc testsdef test_pep8_conformance_state(self)
- Test that models/state.py conforms to PEP8def test_pep8_conformance_test_state(self)
- Test that tests/test_models/test_state.py conforms to PEP8def test_state_module_docstring(self)
- Test for the state.py module docstringdef test_state_class_docstring(self)
- Test for the State class docstring
/test_models/user.py - Contains the TestUserDocs class:
def setUpClass(cls)
- Set up for the doc testsdef test_pep8_conformance_user(self)
- Test that models/user.py conforms to PEP8def test_pep8_conformance_test_user(self)
- Test that tests/test_models/test_user.py conforms to PEP8def test_user_module_docstring(self)
- Test for the user.py module docstringdef test_user_class_docstring(self)
- Test for the User class docstring
...web-01:~/HBNB_V4$ ./console.py
(hbnb) help
Documented commands (type help <topic>):
========================================
EOF all create destroy help quit show update
(hbnb) all MyModel
** class doesn't exist **
(hbnb) create BaseModel
7da56403-cc45-4f1c-ad32-bfafeb2bb050
(hbnb) all BaseModel
[[BaseModel] (7da56403-cc45-4f1c-ad32-bfafeb2bb050) {'updated_at': datetime.datetime(2017, 9, 28, 9, 50, 46, 772167), 'id': '7da56403-cc45-4f1c-ad32-bfafeb2bb050', 'created_at': datetime.datetime(2017, 9, 28, 9, 50, 46, 772123)}]
(hbnb) show BaseModel 7da56403-cc45-4f1c-ad32-bfafeb2bb050
[BaseModel] (7da56403-cc45-4f1c-ad32-bfafeb2bb050) {'updated_at': datetime.datetime(2017, 9, 28, 9, 50, 46, 772167), 'id': '7da56403-cc45-4f1c-ad32-bfafeb2bb050', 'created_at': datetime.datetime(2017, 9, 28, 9, 50, 46, 772123)}
(hbnb) destroy BaseModel 7da56403-cc45-4f1c-ad32-bfafeb2bb050
(hbnb) show BaseModel 7da56403-cc45-4f1c-ad32-bfafeb2bb050
** no instance found **
(hbnb) quit
For this aspect, I collaborated with Davidson. One of the goals of this aspect was to simulate what is obtainable in the industry, where we will work on an existing codebase majority of the time. Our first thoughts upon looking at it included:
“Who did this code?”, “How it works?”, “Where are unittests?”, “Where is this?”, “Why did they do that like this?”, “I don’t understand anything.”, “… I will refactor everything…”
But the worst thing we could possibly do is to redo everything. The existing codebase might be perfect, or it might have errors. We were taught not to always trust the existing codebase!
First, we were to fork an existing codebase where the console had already been built, run our checks, available tests. Then build on the existing, adding more test where necessary.
Our original repo lives HERE
The major additions we did on this Iteration includes: Console improvement and using an ORM to map our objects to a database. The goals we were able to accomplish include:
- Improving on the console someone else built using cmd module
- Implementation of Unit testing in a large project
- Using
*args
and**kwargs
and handling named arguments in a function - Creating a MySQL database and creating a MySQL user with granted privileges
- Using an ORM(SqlAlchemy) to map a Python Class to MySQL table
- Handling 2 different storage engines with the same codebase (jsonified File storage and MySQL db)
- Using environment variables
- Managing both storage engines and performing CRUD on both seamlessly
- Updated the def do_create(self, arg): function of the command interpreter (console.py) to allow for object creation with given parameters:
Command syntax: create <Class name> <param 1> <param 2> <param 3>...
Param syntax: <key name>=<value>
Value syntax:
String: "<value>" => starts with a double quote
- any double quote inside the value must be escaped with a backslash \
- all underscores '_' must be replaced by spaces ' '.
Example: I want to set the string
My little house
to the attribute name, the command line must be name="My_little_house"
Float: <unit>.<decimal> => contains a dot .
Integer: <number> => default case
If any parameter doesn’t fit with these requirements or can’t be recognized correctly by the program, it will be skipped
As part of the SysAdmin sub-track in the program, I learnt about servers and load balancing, I was given acccess to 2 web servers + 1 load balancer
but nothing to distribute with them yet. This update uses them to take the project so far, public!😎
- In this first deployment aspect, I deployed my
web_static
work. I used Fabric (for Python3). - Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.
- The important concept learned here: execution of commands locally or remotely. Locally means in my laptop (physical laptop or inside my Vagrant), and Remotely means on my server(s). Fabric is taking care of all network connections (SSH, SCP etc.), it’s an easy tool for transferring, executing, etc. commands from locale to a remote server.
Generally, I learned:
- How to execute Fabric command locally and remotely, transfer files with Fabric, deploy code to a server easily by compressing into a tgz archive
- How to manage Nginx configuration and the difference between root and alias in a Nginx configuration
- This was also a brush with CI/CD - a lean/agile way of working. The project was not done yet, but going forward, I would continuously integrate and continuously deploy.
Additional updates were added by me as part of my learning journey with ALX. - WEB FRAMEWORK AND TEMPLATING
The goal of this includes:
- learning about Web Frameworks and how to build a web framework with Flask
- defining routes in Flask and handling variables in a route
- creating a HTML response in Flask by using a template (Jinja)
- creating dynamic templates (using loops, conditions…) and displaying data from a MySQL database in HTML
At the end of this, HBNB came alive!
all additional updates are still in the same verson repo here
Implementation of RESTful API
Once again, we were mandated to work with a new codebase that had covered our journey so far into building the clone.
I and Lateef collaborated on this and we learned:
- flask_cheatsheet.pdf
- What REST, API and CORS mean.
- How to design a RESTful API with Python and Flask
- Implementation of CRUD methods to consume created RESTFUL API:
This version repo can be found HERE
I made this copy to write an extensive readme that chronicles my personal process and journey.
I collaborated with Philip on this version and the original repo for our joint work lies HERE
Did we fork a new codebase again?. YES😂
Generally, we learned and implemented:
- How cool is it to request our own API? (Yes, we consumed our own API, the one we built and served in v3)
- How to modify an HTML element style
- How to get and update an HTML element content
- How to modify the DOM
- How to make a GET request with JQuery Ajax
- How to make a POST request with JQuery Ajax
- How to listen/bind to DOM events
- How to listen/bind to user events
We worked on the codebase using Flasgger, we installed it locally first before starting the RestAPI:
$ sudo apt-get install -y python3-lxml
$ sudo pip3 install flask_cors # if it was not installed yet
$ sudo pip3 install flasgger
If anyone clones this repo and tries to install locally, and the RestAPI does not start, please read the error message. Based on the(se) error message(s), you will have to troubleshoot potential dependencies issues.
Here are some possible sample solutions:
ERROR: jsonschema exception
$ sudo pip3 uninstall -y jsonschema
$ sudo pip3 install jsonschema==3.0.1
Error: No module named 'pathlib2'
$ sudo pip3 install pathlib2
Additional updates were added by me as part of my learning journey with ALX. - DEPLOYMENT WITH GUNICORN
The web infrastructure is already serving web pages via Nginx that was installed in my first web stack project. While a web server can also serve dynamic content, this task is usually given to an application server. In this project I added this piece to The infrastructure, plugged it to the Nginx and made is serve my Airbnb clone project.
In this Final deployment aspect. I learned and implemented:
- Implemented a webserver and an application server in my web infrastructure
- Served a flask application with Nginx and Gunicorn.
- Systemd script to automatically start my Gunicorn processess when server is booted, freeing me from having to manually restart them
Some configuration files, backups and scripts used to transfer or install dependencies for this project have been removed from the school server (was shut down after finishing the program) and saved HERE
No known bugs at this time.
All contributors to the last part of the clone, including those whose codebase were forked and used:
Alexa Orrico - Github / Twitter
Jennifer Huang - Github / Twitter
Jhoan Zamora - Github / Twitter
David Ovalle - Github / Twitter
Ukanwoke Philip - Github / Twitter
David Okolie - Github / Twitter
Joann Vuong
Public Domain. No copy write protection.