aedoardo1990 / alternative-travels-api

Back End API of Social Media App "Alternative Travels"
0 stars 0 forks source link
django-rest-framework python

Alternative Travels API

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

Table of Contents

User Stories

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:

Login and Registration

Profiles

Posts with Images or Videos

Comments

Likes

Marketplace

Likes and Comments of the Marketplace (apps are called Loves and Opinions)

Followers

Tags

Search and Filter

Geolocation

Data Models

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.

Diagram

Features

Homepage

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.

homepage

Profiles Data

In the Profiles section, a user can view a list of all profiles in the API.

Profile List

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.

Admin

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:

Posts Data

In the Posts section, a user can view a list of all posts in the API.

Posts

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.

Posts Admin

Comments Data

In the Comments section, a user can view a list of all the comments in the API.

Comments

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.

Likes Data

In the Likes section, a user can view a list of all the liked posts in the API.

Likes

Marketplace Data

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.

Marketplace

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.

Marketplace Admin

Opinions Data

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.

Opinions

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.

Loves Data

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.

Opinions

Followers Data

In the Followers section, a user can view a list of all follower posts in the API.

Followers

The owner is the user following. In the followed_name field there is the user who is followed by the owner.

Agile Methodologies

Kanban Board

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:

Kanban Board img Issues img

Link to the Kanban Board

Kanban Board

Link to the issues page

Issues Page

Testing

PEP 8 Linter

Only a few errors found in the pages of the drf_api folder in settings.py file. These are related to the code provided by CI and I preferred to not touch it to avoid causing any error. For all the other apps no errors were found #### DRF_API - permissions.py ![PEP8 Validation](assets/readme/permiss-set.png) - serializers.py ![PEP8 Validation](assets/readme/serial-set.png) - settings.py ![PEP8 Validation](assets/readme/settings-set.png) - views.py ![PEP8 Validation](assets/readme/views-set.png) - urls.py ![PEP8 Validation](assets/readme/urls-set.png) #### COMMENTS - apps.py ![PEP8 Validation](assets/readme/apps-comment.png) - serializers.py ![PEP8 Validation](assets/readme/serializers-comments.png) - models.py ![PEP8 Validation](assets/readme/models-comments.png) - views.py ![PEP8 Validation](assets/readme/views-comments.png) - urls.py ![PEP8 Validation](assets/readme/urls-comments.png) #### FOLLOWERS - apps.py ![PEP8 Validation](assets/readme/apps-follow.png) - serializers.py ![PEP8 Validation](assets/readme/serializers-follow.png) - models.py ![PEP8 Validation](assets/readme/models-folllow.png) - views.py ![PEP8 Validation](assets/readme/views-follow.png) - urls.py ![PEP8 Validation](assets/readme/urls-follow.png) #### LIKES - apps.py ![PEP8 Validation](assets/readme/likes-apps.png) - serializers.py ![PEP8 Validation](assets/readme/serializers-likes.png) - models.py ![PEP8 Validation](assets/readme/likes-models.png) - views.py ![PEP8 Validation](assets/readme/likes-views.png) - urls.py ![PEP8 Validation](assets/readme/likes-urls.png) #### LOVES - apps.py ![PEP8 Validation](assets/readme/apps-loves.png) - serializers.py ![PEP8 Validation](assets/readme/serial-loves.png) - models.py ![PEP8 Validation](assets/readme/models-loves.png) - views.py ![PEP8 Validation](assets/readme/views-loves.png) - urls.py ![PEP8 Validation](assets/readme/urls-loves.png) #### MARKETPLACE - admin.py ![PEP8 Validation](assets/readme/admin-market.png) - apps.py ![PEP8 Validation](assets/readme/apps-market.png) - serializers.py ![PEP8 Validation](assets/readme/serial-market.png) - models.py ![PEP8 Validation](assets/readme/models-market.png) - views.py ![PEP8 Validation](assets/readme/views-market.png) - urls.py ![PEP8 Validation](assets/readme/urls-market.png) #### OPINIONS - apps.py ![PEP8 Validation](assets/readme/apps-opinion.png) - serializers.py ![PEP8 Validation](assets/readme/serial-op.png) - models.py ![PEP8 Validation](assets/readme/models-op.png) - views.py ![PEP8 Validation](assets/readme/views-op.png) - urls.py ![PEP8 Validation](assets/readme/urls-op.png) #### POSTS - admin.py ![PEP8 Validation](assets/readme/admin-post.png) - apps.py ![PEP8 Validation](assets/readme/apps-post.png) - serializers.py ![PEP8 Validation](assets/readme/serial-posts.png) - models.py ![PEP8 Validation](assets/readme/models-posts.png) - tests.py ![PEP8 Validation](assets/readme/tests-post.png) - views.py ![PEP8 Validation](assets/readme/views-post.png) - urls.py ![PEP8 Validation](assets/readme/urls-posts.png) #### PROFILES - apps.py ![PEP8 Validation](assets/readme/apps-pro.png) - serializers.py ![PEP8 Validation](assets/readme/serial-pro.png) - models.py ![PEP8 Validation](assets/readme/models-pro.png) - views.py ![PEP8 Validation](assets/readme/views-pro.png) - urls.py ![PEP8 Validation](assets/readme/urls-pro.png)

User Story Testing

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.

Bugs

Unfixed Bugs

Not able to login into account in REST Framework format

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.

login

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/

Technologies Used

Languages

Frameworks and Software

Libraries

The libraries used in this project are located in the requirements.txt. Here below a list of all the libraries.

Deployment

The project was deployed to Heroku. To deploy, please follow the process below:

  1. 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'.

  2. Fill in the details for the new repository and then click 'Create Repository From Template'.

  3. When the repository has been created, click on the 'Gitpod' button to open it in the GitPod Editor.

  4. Now it's time to install Django and the supporting libraries that are needed, using the following commands:

  1. When Django and the libraries are installed we need to create a requirements file.
  1. Now it's time to create the project.
  1. When the project is created we can now create the applications. My project consists of the following apps: Posts, Comments, Likes, Profiles, Followers, Marketplace, Opinions, Loves.
  1. We now need to add the applications to settings.py in the INSTALLED_APPS list.

  2. 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.

  1. Now it is time to create our application on Heroku, attach a database, prepare our environment and settings.py file and setup the Cloudinary storage for our static and media files.
  1. Choose a unique app name, choose your region and click 'Create app".

  2. 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:

    • Name
    • Tiny Turtle Free Plan
    • Selected data center near you

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.

  1. 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.

  2. Go back to GitPod and create a new env.py in the top level directory. Then add these rows.

  1. 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.

  2. 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

  1. In the settings file, remove the insecure secret key and replace it with: SECRET_KEY = os.environ.get('SECRET_KEY')

  2. 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.

  1. Save all your fields and migrate the changes again.

python3 manage.py migrate

  1. 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.

  2. 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://************************"

  1. 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.

  2. 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.

  1. 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.

  2. Now we just need to create the basic file directory in Gitpod.

  1. Now you can save all the files and prepare for the first commit and push to Github by writing the lines below.
  1. 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.

  2. Scroll down to the manual deployment section and click 'Deploy Branch'.

Credits

Acknowledgments

My mentor Antonio for pointing me always in the right direction. 30 minutes with him are worth 30 professional lessons.