This is a server project based on Django REST Framework.
- Django 2.2.1
- djangorestframework 3.9.2
- djangorestframework-jwt 1.11.0
- rest-condtion 1.0.3
-
Connect to the arora development server via ssh. Follow command line prompts to create/enter a password:
ssh [id]@[ip_address]
-
Clone this server repository to your root folder on the arora server using:
git clone https://github.com/CANIS-NAU/ARORA-Server.git
-
From the root directory, create your virtual environment by running:
python3 -m venv aroraenv
-
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) -
Now we need to install the requirements for the server. From the home directory run:
pip install -r ARORA-Server/requirements.txt
-
Connect to the arora development server via ssh. Follow command line prompts to enter your created password:
ssh [id]@[ip_address]
-
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) -
Change directory into the cloned ARORA-Server directory:
cd ARORA-Server
-
Create migration files for building the database:
python3 manage.py makemigrations
-
Create a brand new database:
python3 manage.py migrate
-
Next, run this:
python3 manage.py migrate --run-syncdb
-
Create a super user if this is the first time creating this database:
python3 manage.py createsuperuser
-
Run the server:
python3 manage.py runserver [IP_ADDR]:[PORT]
python3 manage.py runserver [server_ip_address]:8000
-
The server will now be up and running. Open your browser and go to [server_ip_address]:8000/admin to access admin site.
-
Log in with your super user account created above.
-
Now, you can manage the database by your super user account. Use it to manually modify table entries and add users, for example.
Note:
- 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.
-
UserInfo
|–> user_info_id (integer)
|–> user_id (integer)
|–> user_name_of_strength* (string)
|–> user_created_at (datetime)
|–> user_current_mood* (integer)
|-> user_current_stress (integer)
|-> user_type (string)
|-> mentor_id(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) -
AccessCodes
|–> access_code(string)
|–> creator_id(integer)
|–> created_at(datetime)
|–> expired_at(datetime)
|–> authority_level(string)
-
ButterflyType
|–> butterfly_type_id (integer)
|–> butterfly_type_description* (integer)
|–> butterfly_type_image* (string) -
Butterfly
|–> butterfly_id (integer)
|–> butterfly_type_id* (integer)
|–> butterfly_create_at (datetime) -
UserButterfly
|–> user_butterfly_id (integer)
|–> butterfly_id* (integer)
|–> time_caught (datetime) (Record the date time when it is created)
|–> user_id* (integer) -
ButterflyLike
|–> butteefly_like_id (integer)
|–> butterfly_id* (integer)
|–> user_id* (integer)
|–> like_created_at (datetime) -
ButterflyComment
|–> butterfly_comment_id (integer)
|–> butterfly_id* (integer)
|–> user_id* (integer)
|–> comment_created_at (datetime)
|–> comment_text* (string) -
BaselineReport
|–> baseline_report_id (integer)
|–> user_id* (integer)
|–> baseline_report_created_at (datetime)
|–> baseline_report_results* (string) -
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) -
MentorChat
|–> message_id(auto)
|–> convo_id(string)
|–> message_text(string)
|–> message_date(datetime)
|–> message_sender_id(integer)
|–> message_reciver_id(integer)
|–> sender_name(string)
-
MoodType
|–> mood_type_id (integer)
|–> mood_type_description* (string) -
MoodReport
|–> mood_report_id (integer)
|–> mood_report_created_at
|–> user_id* (integer)
|–> mood_type* (integer)
|–> user_text* (string) -
Phrase
|–> phrase_id (integer)
|–> phrase_english_text* (string)
|–> phrase_indigenous_text* (string) -
QuestType
|–> quest_type_id (integer)
|–> quest_type_description* (string) -
QuestStatus
|–> quest_status_id (integer)
|–> quest_status_description* (string) -
Quest
|–> quest_id (integer)
|–> quest_type_id* (integer)
|–> quest_status_id* (integer) -
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) -
SystemReport
|–> system_report_id (integer)
|–> user_id* (integer)
|–> power_level* (integer)
|–> request_latnecy* (string)
|–> system_report_created_at (datetime) -
UserInteractionType
|–> user_interaction_type_id (integer)
|–> user_interaction_description* (string) -
UserInteraction
|-> quest_report_id* (integer)
|–> 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)
Note:
-
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 >” } -
The server returns all fields in the models back when it gets a GET request.
-
The fields in the models wtih star notation are entirely requried at POST/PUT request.
-
The fields in the models wtih star notation are partially requried at PATCH request.
-
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. -
The source code reserves all the right for the final explanation.
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 |
Access Codes | AccessCodes | GET; POST; DELETE |
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 |
MentorChat | messages | POST; GET; DELETE |
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:
-
Afer you have installed and opened POSTMAN. Click Import button. Let’s start importing the collection.
-
After you sucessfully import the collection, you can see the pop-up at the downside.
-
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.
-
After every thing is ok, Click the Send button. And we get a response shows the user_id of our new user back.
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 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.
URL: http://127.0.0.1:8000/api-token-auth/
Method: POST
Data in JSON: { “username”:"< user_name >", “password”:"< password >"}
URL: http://127.0.0.1:8000/api-token-verify
Method: POST
Data in JSON: { “token”:“JWT < token_to_be_refresh >”}
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.