JoeyV55 / ARORA-Server

ARORA server with Django 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Welcome to ARORA Server

NOTE: This fork is not updated and is a snapshot of my work on the ARORA Project's backend. Please use the original repository for the current production code.

This is a server project based on Django REST Framework.

Package Dependency

  1. Django 2.2.1
  2. djangorestframework 3.9.2
  3. djangorestframework-jwt 1.11.0
  4. rest-condtion 1.0.3

How to Set Up the Server

First time installation steps

  1. Connect to the arora development server via ssh. Follow command line prompts to create/enter a password:

    ssh [id]@[ip_address]

  2. Clone this server repository to your root folder on the arora server using:

    git clone https://github.com/CANIS-NAU/ARORA-Server.git
  3. From the root directory, create your virtual environment by running:

    python3 -m venv aroraenv

  4. Then, still from root directory, activate the virtual environment using:

    source aroraenv/bin/activate

    You should now have a command-line prefix of arornaenv (or whatever name you initialzed the environment with)

  5. Now we need to install the requirements for the server. From the home directory run:

    pip install -r ARORA-Server/requirements.txt

Run the server (some steps are repeated from above, not a typo!)

  1. Connect to the arora development server via ssh. Follow command line prompts to enter your created password:

    ssh [id]@[ip_address]

  2. From the root directory, activate your virtual environment using:

    source aroraenv/bin/activate

    You should now have a command-line prefix of arornaenv (or whatever name you initialzed the environment with)

  3. Change directory into the cloned ARORA-Server directory:

    cd ARORA-Server

  4. Create migration files for building the database:

    python3 manage.py makemigrations

  5. Create a brand new database:

    python3 manage.py migrate

  6. If the above command caused an error, it was likely due to tables not syncing. Run this instead:

    python3 manage.py migrate --run-syncdb

  7. Create a super user if this is the first time creating this database:

    python3 manage.py createsuperuser

  8. Run the server:

    python3 manage.py runserver [IP_ADDR]:[PORT]
    python3 manage.py runserver [server_ip_address]:8000

  9. The server will now be up and running. Open your browser and go to [server_ip_address]:8000/admin to access admin site.

  10. Log in with your super user account created above.

  11. Now, you can manage the database by your super user account. Use it to manually modify table entries and add users, for example.

Note:

    Additional Steps When Modifying Models and Adding New Tables

    • You must manually migrate the new/modified table:
      python3 manage.py makemigrations TABLENAME

      Example for modifying UserInfos:
      python3 manage.py makemigrations UserInfos

      Note: New clones must migrate UserInfos as it was not initialized previously!
    • Using admin site is a optional way to manage your database.
    • If you meet any problems when you do the above process, please make sure nothing wrong with your migration files. Learn more.

Models

  1. UserInfo
    |–> user_info_id (integer)
    |–> user_id (integer)
    |–> user_name_of_strength* (string)
    |–> user_created_at (datetime)
    |–> user_current_mood* (integer)
    |–> user_current_mood_updated* (datetime)
    |–> user_current_location_lat* (decimal)
    |–> user_current_location_long* (decimal)
    |–> user_current_location_updated* (datetime)
    |–> user_current_butterfly* (integer)
    |–> user_pollen* (integer)
    |–> user_points* (integer)
    |–> username (Django bulit-in field) (string)
    |–> email (Django bulit-in field) (string)
    |–> password (Django bulit-in field) (string)

  2. ButterflyType
    |–> butterfly_type_id (integer)
    |–> butterfly_type_description* (integer)
    |–> butterfly_type_image* (string)

  3. Butterfly
    |–> butterfly_id (integer)
    |–> butterfly_type_id* (integer)
    |–> butterfly_create_at (datetime)

  4. UserButterfly
    |–> user_butterfly_id (integer)
    |–> butterfly_id* (integer)
    |–> time_caught (datetime) (Record the date time when it is created)
    |–> user_id* (integer)

  5. ButterflyLike
    |–> butteefly_like_id (integer)
    |–> butterfly_id* (integer)
    |–> user_id* (integer)
    |–> like_created_at (datetime)

  6. ButterflyComment
    |–> butterfly_comment_id (integer)
    |–> butterfly_id* (integer)
    |–> user_id* (integer)
    |–> comment_created_at (datetime)
    |–> comment_text* (string)

  7. BaselineReport
    |–> baseline_report_id (integer)
    |–> user_id* (integer)
    |–> baseline_report_created_at (datetime)
    |–> baseline_report_results* (string)

  8. LocationReport
    |–> location_report_id (integer)
    |–> location_report_lat* (decimal)
    |–> location_report_long* (decimal) location_report_create_at
    |–> location_report_create_at (datetime)
    |–> user_id* (integer)

  9. MoodType
    |–> mood_type_id (integer)
    |–> mood_type_description* (string)

  10. MoodReport
    |–> mood_report_id (integer)
    |–> mood_report_created_at
    |–> user_id* (integer)
    |–> mood_type* (integer)
    |–> user_text* (string)

  11. Phrase
    |–> phrase_id (integer)
    |–> phrase_english_text* (string)
    |–> phrase_indigenous_text* (string)

  12. QuestType
    |–> quest_type_id (integer)
    |–> quest_type_description* (string)

  13. QuestStatus
    |–> quest_status_id (integer)
    |–> quest_status_description* (string)

  14. Quest
    |–> quest_id (integer)
    |–> quest_type_id* (integer)
    |–> quest_status_id* (integer)

  15. QuestReport
    |–> quest_report_id (integer)
    |–> quest_id* (integer)
    |–> user_id* (integer)
    |–> quest_started_at (datetime)
    |–> quest_ended_at (datetime) (Record the date time when it is updated)

  16. SystemReport
    |–> system_report_id (integer)
    |–> user_id* (integer)
    |–> power_level* (integer)
    |–> request_latnecy* (string)
    |–> system_report_created_at (datetime)

  17. UserInteractionType
    |–> user_interaction_type_id (integer)
    |–> user_interaction_description* (string)

  18. UserInteraction
    |–> user_interaction_id (integer)
    |–> user_interaction_created_at (datetime)
    |–> initator_user_id* (integer)
    |–> receiver_user_id* (integer)
    |–> user_interaction_type_id* (integer)
    |–> user_interaction_content* (string)

    |-> quest_report_id* (integer)

Note:

  1. This is a liitle special for creating a new user, due to use default django user model:

    Method: POST
    DATA in JSON: { “username”: “< user_name >”, “password”: “< password >”, “email”: “< email >” }

  2. The server returns all fields in the models back when it gets a GET request.

  3. The fields in the models wtih star notation are entirely requried at POST/PUT request.

  4. The fields in the models wtih star notation are partially requried at PATCH request.

  5. There are two situation for non-requried field( the fields without star notation):
    (1) The field is a prime key, such as, user_info_id, quest_id, and mood_report_id.
    (2) The field is a date time field which is created or updated, such as, time_caught, location_report_create_at, and quest_ended_at.

  6. The source code reserves all the right for the final explanation.

Endpoints URL

General URL Principle:
POST: < DOMAIN_NAME or IP_ADDRESS >/< RESOURCE_NAME >
e.g: http://127.0.0.1:8000/userinfo

GET: < DOMAIN_NAME or IP_ADDRESS >/< RESOURCE_NAME >/< RETRIEVED_ITEM_ID >
e.g: http://127.0.0.1:8000/userinfo/1

GET(All items): < DOMAIN_NAME or IP_ADDRESS >/< RESOURCE_NAME s>/< RETRIEVED_ITEM_ID >
e.g: http://127.0.0.1:8000/quests http://127.0.0.1:8000/phrases

PUT: < DOMAIN_NAME or IP_ADDRESS >/< RESOURCE_NAME >/< UPDATED_ITEM_ID >
e.g: http://127.0.0.1:8000/quest/1

PATCH: < DOMAIN_NAME or IP_ADDRESS >/< RESOURCE_NANE >/< UPDATED_ITEM_ID >
e.g: http://127.0.0.1:8000/userinteraction/1

DELETE: < DOMAIN_NAME or IP_ADDRESS >/< RESOURCE_NAME >/< DELETED_ITEM_ID >
e.g: http://127.0.0.1:8000/userbutterfly/1

Model Name Resource Name Accept Method
UserInfo userinfo POST; GET; PUT; PATCH
UserInfo nearbyusers GET
ButterflyType butterflytype POST; GET; PUT; PATCH
Butterfly butterfly POST; GET; PUT; PATCH
UserButterfly userbutterfly POST; GET; PUT; PATCH; DELETE
ButterflyLike butterflylike POST; GET; PUT; PATCH
ButterflyComment butterflycomment POST; GET; PUT; PATCH
BaselineReport baselinereport POST; GET; PUT; PATCH
LocationReport locationreport POST; GET; PUT; PATCH
MoodType moodtype POST; GET; PUT; PATCH
MoodReport moodreport POST; GET; PUT; PATCH
Phrase phrase POST; GET; PUT; PATCH
Quest quest POST; GET; PUT; PATCH
QuestType questtype POST; GET; PUT; PATCH
QuestStatus queststatus POST; GET; PUT; PATCH
QuestReport questreport POST; GET; PUT; PATCH
SystemReport systemreport POST; GET; PUT; PATCH
UserInteractionType userinteractiontype POST; GET; PUT; PATCH
UserInteraction userinteraction POST; GET; PUT; PATCH

Here I recommend you use POSTMAN, a API development environemnt, to test the endpoints and know how to access those endpoints more directly. And I have a collection contains all useage of the endpoints, which could let us have a easier life.
Sharing Link: https://www.getpostman.com/collections/921a12a8fa5a925e0c28

A Example of How to Use:

  1. Afer you have installed and opened POSTMAN. Click Import button. Let’s start importing the collection.
    Start Import

  2. Choose Import From Link, then Copy the Sharing Link to it.
    enter the link

  3. After you sucessfully import the collection, you can see the pop-up at the downside.
    enter image description here

  4. Let’s try to create a user. Of cource, please make sure the server is running properly. And the set-up of the request should be correct aslo.
    correct set-up 1
    correct set-up 2

  5. After every thing is ok, Click the Send button. And we get a response shows the user_id of our new user back.
    enter image description here

Entity Relationship (ER) Diagram

Note:
All validation part depends on this foreign key relationship map.
Example:
If we want to create a new quest, there are two fields to be validated, quest_status_id and quest_type_id. Assuming the incoming quest_status_id is 100, however, this is no item whose quest_status_id is 100, creating is fail. And it returns a 409 CONFLICT error.

Token

  • Token system in this server is based on REST framework
    JWT
    .
  • Any request with the token takes a user object to the server.
  • For more information, here.

Get a Token

URL: http://127.0.0.1:8000/api-token-auth/
Method: POST
Data in JSON: { “username”:"< user_name >", “password”:"< password >"}

Verify a Token

URL: http://127.0.0.1:8000/api-token-verify
Method: POST
Data in JSON: { “token”:“JWT < token_to_be_refresh >”}

Refresh a Token

URL: http://127.0.0.1:8000/api-token-refresh
Method: POST
Data in JSON: { “token”:“JWT < token_to_be_refresh >”}
Note: ‘JWT_ALLOW_REFRESH’ should be True to enable refreshing token.

And here is a simple tutorial to fast set-up about Django-REST-framework and Retrofit.

About

ARORA server with Django 2

License:GNU General Public License v3.0


Languages

Language:Python 100.0%