Build a Django application with following functionalities
- Exposes an API to store the emails. Feel free to use Django REST Framework
- A view to list down the emails in the reverse chronological order and show the number of new emails added this calendar month
- Integrate the api with the email collection widget present in this project.
- Bonus - set up a celery task that runs every Monday and Wednesday and prints the number of new emails added in the current calendar month to the console.
- Django project named mysite
- Project has only 1 custom app: unity
- Store: to save Store's information
- Leads: to save Store's Leads, who subscribed to each individual Store Beside unity app models, the User Model was used to present store owner. User data model used to implement authentication as well.
I had changed from TokenAPI to Django OAuth Toolkit
After install and configure app, we need go to creat clientapp for each Store.
- login to the application
- Access to the link: http://127.0.0.1:8000/admin
- Enter user and password
- Create application
- Click to the add button Applications on the group Django Oauth Toolkit
- Enter information to create Application for client - Kafa
- Client application was added, we will use Client id and Client secret to get Access token key for client application to consume our API.
- We can send request to get access token key like below:
curl -X POST -d "grant_type=password&username=<user_name>&password=" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
The result returned will be something like this:
{ "access_token": "j0qGAqBCKlAt0gh30V4Ap9sQ9uoqV3", "expires_in": 36000, "token_type": "Bearer", "scope": "read write groups", "refresh_token": "BiYIe2C4fyfHBeSSAtYY9Oo91Q1t91" }
- Consume API - Leads Subscribed Once done, we can use the api for leads to subscribe. Send request to API like below:
When API was called, it will check the Access Token key was assigned to which application, and we can get User to determine Store accordingly.
- setup crontab to run on 0th minute of each Monday and Wednesday
- apply crontab to the task ![](/doc_assets/images/schedule task.png) We will use rabbitMQ and Celery to demo simple automation task.
We can run Docker Command to execute rabbitMQ
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.9-management
celery -A unity beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler