This is the first step towards building a first full web application: the AirBnB clone. This first step is very important because you will use what you build during this project with all other following projects: HTML/CSS templating, database storage, API, front-end integration…
Each task is linked and will help you to:
- put in place a parent class (called
BaseModel
) to take care of the initialization, serialization and deserialization of your future instances - create a simple flow of serialization/deserialization: Instance <-> Dictionary <-> JSON string <-> file
- create all classes used for AirBnB (User, State, City, Place…) that inherit from BaseModel
- create the first abstracted storage engine of the project: File storage. create all unittests to validate all our classes and storage engine What’s a command interpreter?
- Do you remember the Shell? It’s exactly the same but limited to a specific use-case. In our case, we want to be able to manage the objects of our project
The console works both in interactive mode and non-interactive mode, much like a Unix shell. It prints a prompt (hbnb) and waits for the user for input.
Command | Example |
---|---|
Run the console | ./console.py |
Quit the console | (hbnb) quit |
Display the help for a command | (hbnb) help <command> |
Create an object (prints its id) | (hbnb) create <class> |
Show an object | (hbnb) show <class> <id> or (hbnb) <class>.show(<id>) |
Destroy an object | (hbnb) destroy <class> <id> or (hbnb) <class>.destroy(<id>) |
Show all objects, or all instances of a class | (hbnb) all or (hbnb) all <class> |
Update an attribute of an object | (hbnb) update <class> <id> <attribute name> "<attribute value>" or (hbnb) <class>.update(<id>, <attribute name>, "<attribute value>") |
Non-interactive mode example
$ echo "help" | ./console.py
(hbnb)
Documented commands (type help <topic>):
========================================
EOF all count create destroy help quit show update
The folder models contains all the classes used in this project.
File | Description | Attributes |
---|---|---|
base_model.py | BaseModel class for all the other classes | id, created_at, updated_at |
user.py | User class for future user information | email, password, first_name, last_name |
amenity.py | Amenity class for future amenity information | name |
city.py | City class for future location information | state_id, name |
state.py | State class for future location information | name |
place.py | Place class for future accomodation information | city_id, user_id, name, description, number_rooms, number_bathrooms, max_guest, price_by_night, latitude, longitude, amenity_ids |
review.py | Review class for future user/host review information | place_id, user_id, text |
The folder engine manages the serialization and deserialization of all the data, following a JSON format.
A FileStorage class is defined in file_storage.py with methods to follow this flow:
<object> -> to_dict() -> <dictionary> -> JSON dump -> <json string> -> FILE -> <json string> -> JSON load -> <dictionary> -> <object>
The init.py file contains the instantiation of the FileStorage class called storage, followed by a call to the method reload() on that instance. This allows the storage to be reloaded automatically at initialization, which recovers the serialized data.
All the code is tested with the unittest module. The test for the classes are in the test_models folder.
Juliana Sau
MIT