Web application that simulates some of the features of Airbnb for academic purposes.
In this moment we are the third part "MySQL storage"
https://github.com/kael1706/AirBnB_clone/blob/master/assets/uml.jpg
64-bit system , Ubuntu Install MySQLdb module version 1.3.x
$ sudo apt-get install python3-dev
$ sudo apt-get install libmysqlclient-dev
$ sudo apt-get install zlib1g-dev
$ sudo pip3 install mysqlclient==1.3.10
...
$ python3
>>> import MySQLdb
>>> MySQLdb.__version__
'1.3.10'
Install SQLAlchemy module version 1.2.x
$ pip3 install SQLAlchemy==1.2.5
...
$ python3
>>> import sqlalchemy
>>> sqlalchemy.__version__
'1.2.5'
- Create your data model
- Manage (create, update, destroy, etc) objects via a console / command interpreter
- Store and persist objects to a file (JSON file)
- learn HTML/CSS
- create the HTML of your application
- create template of each objec
- replace the file storage by a Database storage
- map your models to a table in database by using an O.R.M.
- create your first web server in Python
- make your static HTML file dynamic by using objects stored in a file or database
- expose all your objects stored via a JSON web interface
- manipulate your objects via a RESTful API
- learn JQuery
- load objects from the client side by using your own RESTful API
The first piece is to manipulate a powerful storage system. This storage engine will give us an abstraction between “My object” and “How they are stored and persisted”. This means: from your console code (the command interpreter itself) and from the front-end and RestAPI you will build later, you won’t have to pay attention (take care) of how your objects are stored.
We can also use DBStorage to handle the information using a relational database like mysql. In this case we map our models into tables for the database using an ORM.
./console.py
- Start the console
- Write the desired command with its respective syntax
Available classes: BaseModel, User, Place, State, City, Amenity and Review
Command | Syntax | Description |
---|---|---|
help | help |
print commands |
create | create <class> |
create a object |
show | show <class> <id> |
show an specific object |
destroy | destroy <class> <id> |
destroy an specific object |
all | all <class> |
show all objects by class |
all | all |
show all objects |
update | update <class> <id> <attr_name> <new_value> |
update an object with the new values |
create | create <class> <key name>=<value> |
create a object with arguments |
other way... |
Command | Syntax | Description |
---|---|---|
all | <class name>.all() |
show all objects by class |
count | <class name>.count() |
a counter of specific class |
show | <class name>.show(<id>) |
show an specific object |
destroy | <class name>.destroy(<id>) |
destroy an specific object |
update | <class name>.update(<id>, <attribute name>, <attribute value>) |
update an object with the new values |
- Press the enter button
(hbnb) create User
(hbnb) show User fce12f8a
(hbnb) destroy User fce12f8a
(hbnb) all User
(hbnb) all
(hbnb) update User name holberton
(hbnb) User.all()
(hbnb) User.count()
(hbnb) User.show(246c227a)
(hbnb) User.destroy(246c227a)
(hbnb) User.update(38f22813-2753-4d42-b37c-57a17f1e4f88, "first_name", "John")
(hbnb) create User name="Hugo"
(hbnb) create User edad=20
- Set up sufficient permissions to the respective database. You could try running the files: setup_mysql_dev.sql and setup_mysql_test.sql. You can also adapt it to your own database.
- Dont start the console only write the command in the prompt
- Write the desired command with its respective
echo 'create State name="California"' | HBNB_MYSQL_USER=hbnb_dev HBNB_MYSQL_PWD=hbnb_dev_pwd HBNB_MYSQL_HOST=localhost HBNB_MYSQL_DB=hbnb_dev_db HBNB_TYPE_STORAGE=db ./console.py
Available classes: BaseModel, User, Place, State, City, Amenity and Review
echo '<filestorage command>' | HBNB_MYSQL_USER=<username> HBNB_MYSQL_PWD=<database_password> HBNB_MYSQL_HOST=<iphost> HBNB_MYSQL_DB=<name_database> HBNB_TYPE_STORAGE=db ./console.py
echo 'create State name="California"' | HBNB_MYSQL_USER=hbnb_dev HBNB_MYSQL_PWD=hbnb_dev_pwd HBNB_MYSQL_HOST=localhost HBNB_MYSQL_DB=hbnb_dev_db HBNB_TYPE_STORAGE=db ./console.py
Read or watch:
- cmd module
- packages
- unittest module
- args/kwargs
- SQLAlchemy tutorial
- How To Create a New User and Grant Permissions in MySQL
- Jimmy’s (My)SQL Cheat Sheet
- Python3 and environment variables
- Object Relational Mapping with Python’s SQL Alchemy
- SQLAlchemy
- MySQL 5.7 SQL Statement Syntax
- AirBnB clone - ORM
- Isaac Wong, Cohort #5,
What you should learn from this project:
- What is Unit testing and how to implement it in a large project
- What is *args and how to use it
- What is **kwargs and how to use it
- How to handle named arguments in a function
- How to create a MySQL database
- How to create a MySQL user and grant it privileges
- What ORM means
- How to map a Python Class to a MySQL table
- How to handle 2 different storage engines with the same codebase
- How to use environment variables
- In the industry, you will work on an existing codebase 90% of the time. Your first thoughts upon looking at it might include:
- Do you remember the unittest module?
- Update the def do_create(self, arg): function of your command interpreter (console.py) to allow for object creation with given parameters:
- Write a script that prepares a MySQL server for the project:
- Write a script that prepares a MySQL server for the project:
- Update FileStorage: (models/engine/file_storage.py)
- SQLAlchemy will be your best friend!
- Update User: (models/user.py)
- Update Place: (models/place.py)
- Update Review: (models/review.py)
- Update User: (models/user.py)
- Update Place: (models/place.py)
- Update Amenity: (models/amenity.py)
- Update Place: (models/place.py)
- Update Place class