To design Event Registration system REST API with ability to do following tasks -
● Users can create events
●Users can limit the number of attendees
● Users can delete an event
● Users can view public events
● Private events can be viewed only by invited users
● Users should be able to register/unregister for an event
● Users cannot register for another event if its schedule overlaps with a previously registered event
The Source-Code for this project is written using Python. Make Sure you have Python Installed on your PC. If you are using Windows/Mac please install from here.
Also make sure you have install pip properly in your system. Link
To check if you have Python and pip installed properly please use following commands in your terminal/command prompt.
- Test Python : - To see if python installed in your terminal type
python3 --version
in Terminal. This should print the version number. so you'll see something like thisPython 3.6.8
. - Test pip :- To see if pip is installed. type
pip3 --version
.This will print the version number. so you'll see something like thispip 9.0.1
.
Note: You have to install Python and pip separately.
Clone the Repository in your local system.
git clone https://github.com/arch888/MiStay_REST
Navigate (cd
) to this folder using following command
cd MiStay_REST/
Next Step is to install Django and other requirements using the following command
pip3 install -r requirements.txt
Migrate the Project such that Database and Tables and everything is synchronised.
python3 manage.py migrate
Now It's turn to host the project on local server which can be done using following command
python3 manage.py runserver 8000
Here 8000 is the port on which we wanted to serve our API if you wanted to use on any other port you can replace there.
If you see something like this in your terminal/Command Prompt
Performing system checks...
System check identified no issues (0 silenced).
January 16, 2020 - 17:48:14
Django version 3.0.2, using settings 'MiStay_REST.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Hurrah ! You are Done with the Setup. If you are facing any issue please create an issue.
To Check the working / tutorial of the whole project can be viewed through this video
For API support, please email [email protected].
In this project I have used the Token-Based Authentication in which accessing some service through a account then we have to forward the User Token as a header in each request.
User model which is used in this Project is something Looks like
email = EmailField(verbose_name="email", max_length=60)
username = CharField(max_length=30, unique=True)
date_joined = DateTimeField(verbose_name='date joined')
last_login = DateTimeField(verbose_name='last login', auto_now=True)
is_admin = BooleanField(default=False)
is_active = BooleanField(default=True)
is_staff = BooleanField(default=False)
is_superuser = BooleanField(default=False)
For Registration of the users we have to create a POST request to the /api/account/register
which can be easily understandable by the following process.
POST http://127.0.0.1:8000/api/account/register
POST Data -
{
"email" : "[email protected]",
"username" : "sample_username",
"password" : "some_password",
"password2" : "some_password"
}
Response -
{
"response": "successfully registered new user.",
"email": "[email protected]",
"username": "[email protected]",
"pk": 3,
"token": "e5122b5645a40f56adaa69458e8ede60270f742f"
}
For Login of the users we have to create a POST request to the /api/account/login
which can be easily understandable by the following process.
POST http://127.0.0.1:8000/api/account/login
POST Data -
{
"username" : "[email protected]",
"password" : "some_password"
}
Response -
{
"response": "Successfully authenticated.",
"pk": 3,
"email": "[email protected]",
"token": "e5122b5645a40f56adaa69458e8ede60270f742f"
}
To get the Properties of the user account we have to create a GET request with the Token of the user account as the Headers.
GET http://127.0.0.1:8000/api/account/properties
Headers -
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
Response -
{
"pk": 3,
"email": "[email protected]",
"username": "[email protected]"
}
To Update the properties of the User Account the user must be successfully authenticated that is passing the Token as Header and then user will get access to update properties.
PUT http://127.0.0.1:8000/api/account/properties/update
Headers
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
POST Data -
{
"pk" : "3",
"email" : "[email protected]",
"username" : "[email protected]"
}
Response -
{
"response": "Account update success"
}
Changing Password is a essential task of the creation of any service. To change password of any user account please following things
PUT http://127.0.0.1:8000/api/account/change_password
Headers -
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
PUT Data -
{
"old_password" : "some_password",
"new_password" : "some_new_password",
"confirm_new_password" : "some_new_password"
}
Response -
{
"response": "successfully changed password"
}
Events are generally the timed scheduled process which is used to happen in which multiple/single entities can be involved. So to manage various module We can use this REST API to schedule the Events and manage any Team or people with this API because this API is flexible of Scheduling Events only when both users have the free time.
Events Model will have following properties -
title = CharField(max_length=50,null=False,blank=False)
description = TextField(max_length=7000,null=True,blank=True)
slug = SlugField(unique=True,auto_created=True)
start_time = DateTimeField()
end_time = DateTimeField()
attendees = IntegerField(default=10)
private = BooleanField(default=False)
organizer = ForeignKey(settings.AUTH_USER_MODEL)
To View / List all the Public Events user must be successfully authenticated / must pass Token of the user as Header in the request. To get all the Public Event follow following command.
GET http://127.0.0.1:8000/api/event/list
Header -
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
Response -
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"title": "Sample Event",
"description": "This Should be under 50 words such that there is no issue. ",
"start_time": "2020-10-12T10:12:00Z",
"end_time": "2020-10-12T12:12:00Z",
"attendees": 10,
"private": false,
"slug": "sample-event-639986198"
}
]
}
In this request we generally invite a user to a event using the slug of the event. This Request can be viewed as following.
POST http://127.0.0.1:8000/api/event/invite/<str:slug>
Headers -
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
POST Data -
{
"username" : "[email protected]"
}
Response -
{
"response" : "Success"
}
On this url we can request in two types that is GET and POST. In GET request we get all the list of the invites of the events to that user whereas in POST request we post slug of a event in which we accept the invite of event. If there is no event scheduled at that time then event invite is accepted.
GET http://127.0.0.1:8000/api/event/accept_invite
Headers -
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
Response -
[
{
"title": "Sample Event",
"description": "This Should be under 50 words such that there is no issue. ",
"start_time": "2020-10-12T10:12:00Z",
"end_time": "2020-10-12T12:12:00Z",
"attendees": 10,
"private": false,
"slug": "sample-event-639986198"
}
]
POST http://127.0.0.1:8000/api/event/accept_invite
Headers -
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
POST Data -
{
"slug" : "sample-event-639986198"
}
Response -
{
"response": "You have already Scheduled Events in this time !"
}
To Create Event we have to pass all the required fields as POST Data to create new event which can be done as follows -
POST http://127.0.0.1:8000/api/event/create
Headers -
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
POST Data -
{
"title" : "Sample Event",
"description" : "This will be more than 50 characters",
"start_time" : "2020-10-5 10:12",
"end_time" : "2020-10-5 10:24",
"attendees" : "20"
}
Response -
{
"success": true
}
To Delete any event we can use the following URL /api/event/delete/<str:slug>
which can be explained in detail as follows
DELETE http://127.0.0.1:8000/api/event/delete/<str:slug>
Headers -
{
"Authorization" : "Token e5122b5645a40f56adaa69458e8ede60270f742f"
}
Response -
{
"response" : "DELETE SUCCESS"
}
Screenshots of all types of request can be found here and the Video of the tutorial can be found here.
Feel free to reach out to me via email. Shoot your doubts at [email protected]