Welcome to Alternative Travels API set up using Django REST Framework for the Alternative Travels front-end application, a social media for sharing images, videos of alternative travels with the help of geolocation data and tags. The social media is provided as well with a marketplace section with rare products found by the users during their trips.
Here below the links to the API and the Frontend:
Backend
💻 Live link
Frontend
Repository here Live website here
The back-end section of the project focuses on the administration side and covers the user stories from the point of view of a developer and/or superuser. The User Stories are divided into the following categories:
I have created the following models for the Alternative Travels API
Models | Scope |
---|---|
User | the Django standard User model |
Profiles | to create, update, store and delete profile data |
Posts | to create posts with images or videos, with geolocation and tag |
Comments | to make comments about the posts |
Likes | to like posts |
Followers | to follow other users and be updated about their posts |
Marketplace | to post products to sell |
Opinions | to make comments about the posts of products on sale |
Loves | to like posts of products on sale |
The relationships between the models is summarized in the followed entity relationship diagram.
When you first enter the API website, you are directed to the Root Route homepage, with a welcome message to the API of Alternative Travels.
In the Profiles section, a user can view a list of all profiles in the API.
On top of the fields created in the Profiles model (as shown in the Database Diagram), via the Serializer, the following fields are also added to the JSON data:
Create functionality is enabled to the Developer via the Django Admin Tab.
I have set up an ordering for the profiles list and selected the following parameters to sort the profiles by:
I have set up two field filters on the post list to filter as follows:
In the Posts section, a user can view a list of all posts in the API.
Besides the fields created in the Posts model (as shown in the Database Diagram), via the Serializer, the following fields are also added to the JSON data:
I have set up an ordering for the posts list and selected the following parameters to sort the posts by:
I have set up 3 field filters on the posts list to search by as follows:
I have set up 3 field filters on the posts list to filter as follows:
CRUD functionality of the Posts is enabled to the Developer/Superuser via the Django Admin Tab.
In the Comments section, a user can view a list of all the comments in the API.
On top of the fields created in the Comments model (as shown in the Database Diagram), via the Serializer, the following fields are also added to the JSON data:
I set up as well one field filter to filter the comments by the post they are commenting.
In the Likes section, a user can view a list of all the liked posts in the API.
In the Marketplace section, a user can view a list of all the posts of products in the API which are on sale in the Marketplace tab of the frontend.
Besides the fields created in the Marketplace model (as shown in the Database Diagram), via the Serializer, the following fields are also added to the JSON data:
I have set up an ordering for the posts list of the Marketplace and selected the following parameters to sort the posts by:
I have set up 3 field filters on the posts list of the Marketplace to search by as follows:
CRUD functionality of the Posts of the Marketplace is enabled to the Developer/Superuser via the Django Admin Tab.
In the Opinions section, a user can view a list of all the comments related to posts on the Marketplace in the API. The name Opinions was given to differentiate this section from the comments related to the posts with geolocation and tags, but the model is the same of the Comments one.
On top of the fields created in the Opinions model (as shown in the Database Diagram), via the Serializer, the following fields are also added to the JSON data:
I set up as well one field filter to filter the comments of the Marketplace by the post they are commenting.
In the Loves section, a user can view a list of all the liked posts related to the Marketplace section in the API. The name Loves was given to differentiate this section from the likes related to the posts with geolocation and tags, but the model is the same of the Likes one.
In the Followers section, a user can view a list of all follower posts in the API.
The owner is the user following. In the followed_name field there is the user who is followed by the owner.
I used the Kanban Board to build the Alternative Travels API using Agile principles from the start. The user stories created are for a developer or superuser to follow and test throughout the build process. The Agile Principles helped to organise how to build step by step the whole application.
Each user story has a level of prioritisation using the MoSCoW method. The 3 different levels of prioritization are divided as below:
Link to the Kanban Board
Link to the issues page
Expectation (As a developer/superuser, I want to...) | Result (As a developer/superuser, I...) |
---|---|
access an API endpoint that allows users to register by providing their username and password. | can register new users with username and password. |
have a user profile to be automatically created, when a new user signs up. | have a user profile automatically created if I ll sign up as a new user. |
access an API endpoint that allows users to log in, obtain an authentication token, and access user-specific content. | have an API which allow users to access and modify user specific content only when logged in. |
be able to create, read, update, and delete user profiles via the API. | have full CRUD functionality for user profiles in the API via the Admin Tab. |
create, read, update, and delete posts with images about travels. | have full CRUD functionality via the Admin Tab. |
uploaded images to not exceed a size format limit. | have a validator which does not allow to upload images exceeding 2MB. |
be able to create, update and delete post with videos about travels. | have a validator which does not allow to upload videos exceeding 60MB. |
uploaded videos to be automatically converted to a consistent format | have videos with a fitting format when they are uploaded if they do not exceed 60 MB. |
have access for creating, reading, updating, and deleting comments. | have full CRUD functionality for the Comments via the Admin Tab. |
have access for creating, reading and deleting likes. | have full CRUD for the Likes via the Admin Tab. |
be able to create, read, update and delete posts about products to be sold on the marketplace section of the site. | have full CRUD for the posts of the Marketplace via the Admin Tab. |
have access for creating, reading, updating, and deleting comments and likes as well under the posts of the marketplace. | have full CRUD for the comments and likes related to the Marketplace model. |
have access for creating, reading and deleting follow relationships between users. | have full CRUD to manage follow relationships between users via the Admin Tab. |
have posts with a tag field, so that users can tag their created posts. | have Posts with a tag field to enter multiple tags. |
display a list of tags of all the users. | can access a full list of tags of the users via the Admin tab. |
have a functionality for filtering and sorting results, in order to have more control over what data to display to the user. | can filter and sort but just in the User Section of the Admin Tab. This filter function should be implemented as well for all the other models. |
have the possibility to add a geolocation for each post so that users can create posts with a geolocation and update it if necessary. | have a latitude and a longitude fields in the Post model so that users can create posts with their geolocation. |
Since I changed the code of the API for deploying the site, if I change DEBUG to True and remove JSON format option from settings.py, when I try to log in, I am not redirected to the user page. The page will look as in the image below.
However, via the Admin Tab I have full CRUD as Superuser/Developer. Admin tab is accessible at https://8000-aedoardo199-alternative-anz3oyksrix.ws-eu110.gitpod.io/admin/
The libraries used in this project are located in the requirements.txt. Here below a list of all the libraries.
The project was deployed to Heroku. To deploy, please follow the process below:
To begin with we need to create a GitHub repository from the Code Institute template by following the link and then click 'Use this template'.
Fill in the details for the new repository and then click 'Create Repository From Template'.
When the repository has been created, click on the 'Gitpod' button to open it in the GitPod Editor.
Now it's time to install Django and the supporting libraries that are needed, using the following commands:
pip3 install 'django<4' gunicorn
pip3 install 'dj_database_url psycopg2
pip3 install 'dj3-cloudinary-storage
pip3 freeze --local > requirements.txt
- This will create and add required libraries to requirements.txtdjango-admin startproject YOUR_PROJECT_NAME .
- This will create the new project.python3 manage.py startapp APP_NAME
- This will create an applicationWe now need to add the applications to settings.py in the INSTALLED_APPS list.
Now it is time to do our first migration and run the server to test that everything works as expected. This is done by writing the commands below.
python3 manage.py makemigrations
- This will prepare the migrationspython3 manage.py migrate
- This will migrate the changespython3 manage.py runserver
- This runs the server. To test it, click the open browser button that will be visible after the command is run.Choose a unique app name, choose your region and click 'Create app".
Next we need to connect an external PostgreSQL database to the app from ElephantSQL. Once logged into your ElephantSQL dashboard, you click 'Create New Instance' to create a new database. Give the database a:
and click 'Create Instance'. Return to your ElephantSQL Dashboard, and click into your new database instance. Copy the Database URL and head back to Heroku.
Back in your Heroku app settings, click on the 'Reveal Config Vars' button. Create a config variable called DATABASE_URL and paste in the URL you copied from ElephantSQL. This connects the database into the app.
Go back to GitPod and create a new env.py in the top level directory. Then add these rows.
import os
- This imports the os libraryos.environ["DATABASE_URL"]
- This sets the environment variables.os.environ["SECRET_KEY"]
- Here you can choose whatever secret key you want.Back in the Heroku Config Vars settings, create another variable called SECRET_KEY and copy in the same secret key as you added into the env.py file. Don't forget to add this env.py file into the .gitignore file so that it isn't commited to GitHub for other users to find.
Now we have to connect to our environment and settings.py file. In the settings.py, add the following code:
import os
import dj_database_url
if os.path.isfile("env.py"):
import env
In the settings file, remove the insecure secret key and replace it with:
SECRET_KEY = os.environ.get('SECRET_KEY')
Now we need to comment out the old database settings in the settings.py file (this is because we are going to use the postgres database instead of the sqlite3 database).
Instead, we add the link to the DATABASE_URL that we added to the environment file earlier.
python3 manage.py migrate
Now we can set up Cloudinary (where we will store our static files). First you need to create a Cloudinary account and from the Cloudinary dashboard copy the API Environment Variable.
Go back to the env.py file in Gitpod and add the Cloudinary url (it's very important that the url is correct):
os.environ["CLOUDINARY_URL"] = "cloudinary://************************"
Let's head back to Heroku and add the Cloudinary url in Config Vars. We also need to add a disable collectstatic variable to get our first deployment to Heroku to work.
Back in the settings.py file, we now need to add our Cloudinary Libraries we installed earlier to the INSTALLED_APPS list. Here it is important to get the order correct.
For Django to be able to understand how to use and where to store static files we need to add some extra rows to the settings.py file.
Now we just need to create the basic file directory in Gitpod.
web: gunicorn PROJ_NAME.wsgi?
to it.git add .
git commit -m "Deployment Commit
git push
Now it's time for deployment. Scroll to the top of the settings page in Heroku and click the 'Deploy' tab. For deployment method, select 'Github'. Search for the repository name you want to deploy and then click connect.
Scroll down to the manual deployment section and click 'Deploy Branch'.
My mentor Antonio for pointing me always in the right direction. 30 minutes with him are worth 30 professional lessons.