mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 13:49:40 +00:00
2
.gitignore
vendored
2
.gitignore
vendored
@ -21,4 +21,4 @@ static/*
|
||||
__pycache__/
|
||||
*.env
|
||||
ve/*
|
||||
|
||||
celerybeat-schedule
|
||||
|
@ -2,7 +2,7 @@
|
||||
from django.contrib import admin
|
||||
|
||||
"""Import django app"""
|
||||
from .models import UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserNotification, UserDelete
|
||||
from .models import UserEmailOtp, DefaultTaskImages, UserNotification, UserDelete, UserDeviceDetails
|
||||
# Register your models here.
|
||||
|
||||
@admin.register(UserDelete)
|
||||
@ -19,6 +19,7 @@ class UserNotificationAdmin(admin.ModelAdmin):
|
||||
list_display = ['user', 'push_notification', 'email_notification', 'sms_notification']
|
||||
|
||||
def __str__(self):
|
||||
"""Return image url"""
|
||||
return self.image_url
|
||||
@admin.register(DefaultTaskImages)
|
||||
class DefaultTaskImagesAdmin(admin.ModelAdmin):
|
||||
@ -26,6 +27,7 @@ class DefaultTaskImagesAdmin(admin.ModelAdmin):
|
||||
list_display = ['task_name', 'image_url']
|
||||
|
||||
def __str__(self):
|
||||
"""Return image url"""
|
||||
return self.image_url
|
||||
|
||||
@admin.register(UserEmailOtp)
|
||||
@ -37,11 +39,11 @@ class UserEmailOtpAdmin(admin.ModelAdmin):
|
||||
"""Return object in email and otp format"""
|
||||
return self.email + '-' + self.otp
|
||||
|
||||
@admin.register(UserPhoneOtp)
|
||||
class UserPhoneOtpAdmin(admin.ModelAdmin):
|
||||
"""User Phone otp admin"""
|
||||
list_display = ['phone']
|
||||
@admin.register(UserDeviceDetails)
|
||||
class UserDeviceDetailsAdmin(admin.ModelAdmin):
|
||||
"""User profile admin"""
|
||||
list_display = ['user', 'device_id']
|
||||
|
||||
def __str__(self):
|
||||
"""Return object in phone number and otp format"""
|
||||
return self.phone + '-' + self.otp
|
||||
"""Return user email"""
|
||||
return self.user.email
|
||||
|
40
account/custom_middleware.py
Normal file
40
account/custom_middleware.py
Normal file
@ -0,0 +1,40 @@
|
||||
"""middleware file"""
|
||||
"""Django import"""
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.renderers import JSONRenderer
|
||||
"""App django"""
|
||||
from account.utils import custom_error_response
|
||||
from account.models import UserDeviceDetails
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
|
||||
# Custom middleware
|
||||
# when user login with
|
||||
# multiple device simultaneously
|
||||
# It restricted login in
|
||||
# multiple devices only
|
||||
# user can login in single
|
||||
# device at a time"""
|
||||
class CustomMiddleware(object):
|
||||
"""Custom middleware"""
|
||||
def __init__(self, get_response):
|
||||
"""response"""
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
# Code to be executed before the view is called
|
||||
response = self.get_response(request)
|
||||
# Code to be executed after the view is called
|
||||
device_id = request.META.get('HTTP_DEVICE_ID')
|
||||
if request.user.is_authenticated:
|
||||
"""device details"""
|
||||
device_details = UserDeviceDetails.objects.filter(user=request.user, device_id=device_id).last()
|
||||
if device_id and not device_details:
|
||||
custom_error = custom_error_response(ERROR_CODE['2037'], response_status=status.HTTP_404_NOT_FOUND)
|
||||
response = Response(custom_error.data, status=status.HTTP_404_NOT_FOUND)
|
||||
# Set content type header to "application/json"
|
||||
response['Content-Type'] = 'application/json'
|
||||
# Render the response as JSON
|
||||
renderer = JSONRenderer()
|
||||
response.content = renderer.render(response.data)
|
||||
return response
|
@ -0,0 +1,21 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-14 09:34
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('account', '0006_alter_useremailotp_options_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='defaulttaskimages',
|
||||
options={'verbose_name': 'Default Task images', 'verbose_name_plural': 'Default Task images'},
|
||||
),
|
||||
migrations.AlterModelOptions(
|
||||
name='userdelete',
|
||||
options={'verbose_name': 'Deleted User', 'verbose_name_plural': 'Deleted User'},
|
||||
),
|
||||
]
|
31
account/migrations/0008_userdevicedetails.py
Normal file
31
account/migrations/0008_userdevicedetails.py
Normal file
@ -0,0 +1,31 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-14 11:08
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('account', '0007_alter_defaulttaskimages_options_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='UserDeviceDetails',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('device_id', models.CharField(max_length=500)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='user_device_details', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'User Device Details',
|
||||
'verbose_name_plural': 'User Device Details',
|
||||
'db_table': 'user_device_details',
|
||||
},
|
||||
),
|
||||
]
|
18
account/migrations/0009_alter_userdevicedetails_device_id.py
Normal file
18
account/migrations/0009_alter_userdevicedetails_device_id.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-20 11:38
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('account', '0008_userdevicedetails'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='userdevicedetails',
|
||||
name='device_id',
|
||||
field=models.CharField(blank=True, max_length=500, null=True),
|
||||
),
|
||||
]
|
@ -90,6 +90,8 @@ class DefaultTaskImages(models.Model):
|
||||
class Meta(object):
|
||||
""" Meta information """
|
||||
db_table = 'default_task_image'
|
||||
verbose_name = 'Default Task images'
|
||||
verbose_name_plural = 'Default Task images'
|
||||
|
||||
def __str__(self):
|
||||
"""return phone as an object"""
|
||||
@ -112,6 +114,8 @@ class UserDelete(models.Model):
|
||||
class Meta(object):
|
||||
""" Meta information """
|
||||
db_table = 'user_delete_information'
|
||||
verbose_name = 'Deleted User'
|
||||
verbose_name_plural = 'Deleted User'
|
||||
|
||||
def __str__(self):
|
||||
return self.user.email
|
||||
@ -141,3 +145,23 @@ class UserNotification(models.Model):
|
||||
def __str__(self):
|
||||
return self.user.email
|
||||
|
||||
|
||||
class UserDeviceDetails(models.Model):
|
||||
"""
|
||||
User notification details
|
||||
"""
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_device_details')
|
||||
"""Device ID"""
|
||||
device_id = models.CharField(max_length=500, null=True, blank=True)
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta(object):
|
||||
""" Meta information """
|
||||
db_table = 'user_device_details'
|
||||
verbose_name = 'User Device Details'
|
||||
verbose_name_plural = 'User Device Details'
|
||||
|
||||
def __str__(self):
|
||||
return self.user.email
|
||||
|
@ -1,17 +1,47 @@
|
||||
"""Account serializer"""
|
||||
"""Django Imoprt"""
|
||||
import random
|
||||
"""Django Import"""
|
||||
# Import Refresh token of jwt
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
import secrets
|
||||
"""App import"""
|
||||
# Import guardian's model,
|
||||
# Import junior's model,
|
||||
# Import account's model,
|
||||
# Import constant from
|
||||
# base package,
|
||||
# Import messages from
|
||||
# base package,
|
||||
# Import some functions
|
||||
# from utils file"""
|
||||
|
||||
from guardian.models import Guardian
|
||||
from junior.models import Junior
|
||||
from account.models import UserEmailOtp, DefaultTaskImages, UserDelete, UserNotification, UserPhoneOtp
|
||||
from base.constants import GUARDIAN, JUNIOR, SUPERUSER
|
||||
from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR
|
||||
from base.constants import GUARDIAN, JUNIOR, SUPERUSER, NUMBER
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR
|
||||
from .utils import delete_user_account_condition_social, delete_user_account_condition
|
||||
|
||||
# In this serializer file
|
||||
# define google login serializer
|
||||
# update junior profile,
|
||||
# update guardian profile,
|
||||
# super admin serializer,
|
||||
# reset password,
|
||||
# forgot password,
|
||||
# change password,
|
||||
# basic junior serializer,
|
||||
# basic guardian serializer,
|
||||
# user delete account serializer,
|
||||
# user notification serializer,
|
||||
# update user notification serializer,
|
||||
# default task's images serializer,
|
||||
# upload default task's images serializer,
|
||||
# email verification serializer,
|
||||
# phone otp serializer
|
||||
|
||||
# create all serializer here
|
||||
class GoogleLoginSerializer(serializers.Serializer):
|
||||
"""google login serializer"""
|
||||
access_token = serializers.CharField(max_length=5000, required=True)
|
||||
@ -61,6 +91,7 @@ class ResetPasswordSerializer(serializers.Serializer):
|
||||
def create(self, validated_data):
|
||||
verification_code = validated_data.pop('verification_code')
|
||||
password = validated_data.pop('password')
|
||||
# fetch email otp object of the user
|
||||
user_opt_details = UserEmailOtp.objects.filter(otp=verification_code, is_verified=True).last()
|
||||
if user_opt_details:
|
||||
user_details = User.objects.filter(email=user_opt_details.email).last()
|
||||
@ -83,12 +114,14 @@ class ChangePasswordSerializer(serializers.Serializer):
|
||||
|
||||
def validate_current_password(self, value):
|
||||
user = self.context
|
||||
# check old password
|
||||
if self.context.password not in ('', None) and user.check_password(value):
|
||||
return value
|
||||
raise serializers.ValidationError(ERROR_CODE['2015'])
|
||||
def create(self, validated_data):
|
||||
new_password = validated_data.pop('new_password')
|
||||
current_password = validated_data.pop('current_password')
|
||||
"""Check new password is different from current password"""
|
||||
if new_password == current_password:
|
||||
raise serializers.ValidationError({"details": ERROR_CODE['2026']})
|
||||
user_details = User.objects.filter(email=self.context).last()
|
||||
@ -107,15 +140,28 @@ class ForgotPasswordSerializer(serializers.Serializer):
|
||||
class SuperUserSerializer(serializers.ModelSerializer):
|
||||
"""Super admin serializer"""
|
||||
user_type = serializers.SerializerMethodField('get_user_type')
|
||||
auth_token = serializers.SerializerMethodField('get_auth_token')
|
||||
refresh_token = serializers.SerializerMethodField('get_refresh_token')
|
||||
|
||||
def get_auth_token(self, obj):
|
||||
refresh = RefreshToken.for_user(obj)
|
||||
access_token = str(refresh.access_token)
|
||||
return access_token
|
||||
|
||||
def get_refresh_token(self, obj):
|
||||
refresh = RefreshToken.for_user(obj)
|
||||
refresh_token = str(refresh)
|
||||
return refresh_token
|
||||
|
||||
def get_user_type(self, obj):
|
||||
"""user type"""
|
||||
return SUPERUSER
|
||||
return str(NUMBER['three'])
|
||||
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = User
|
||||
fields = ['id', 'username', 'email', 'first_name', 'last_name', 'is_active', 'user_type']
|
||||
fields = ['id', 'auth_token', 'refresh_token', 'username', 'email', 'first_name',
|
||||
'last_name', 'is_active', 'user_type']
|
||||
|
||||
|
||||
class GuardianSerializer(serializers.ModelSerializer):
|
||||
@ -125,19 +171,24 @@ class GuardianSerializer(serializers.ModelSerializer):
|
||||
first_name = serializers.SerializerMethodField('get_first_name')
|
||||
last_name = serializers.SerializerMethodField('get_last_name')
|
||||
auth_token = serializers.SerializerMethodField('get_auth_token')
|
||||
refresh_token = serializers.SerializerMethodField('get_refresh_token')
|
||||
|
||||
def get_auth_token(self, obj):
|
||||
refresh = RefreshToken.for_user(obj.user)
|
||||
access_token = str(refresh.access_token)
|
||||
return access_token
|
||||
|
||||
def get_refresh_token(self, obj):
|
||||
refresh = RefreshToken.for_user(obj.user)
|
||||
refresh_token = str(refresh)
|
||||
return refresh_token
|
||||
|
||||
def get_user_type(self, obj):
|
||||
"""user type"""
|
||||
email_verified = UserEmailOtp.objects.filter(email=obj.user.username).last()
|
||||
if email_verified and email_verified.user_type != None:
|
||||
if email_verified and email_verified.user_type is not None:
|
||||
return email_verified.user_type
|
||||
return '2'
|
||||
return str(NUMBER['two'])
|
||||
|
||||
def get_auth(self, obj):
|
||||
"""user email address"""
|
||||
@ -154,9 +205,9 @@ class GuardianSerializer(serializers.ModelSerializer):
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Guardian
|
||||
fields = ['id', 'auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name',
|
||||
'gender', 'dob', 'referral_code', 'is_active', 'is_complete_profile', 'passcode', 'image',
|
||||
'created_at', 'updated_at', 'user_type', 'country_name']
|
||||
fields = ['id', 'auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code',
|
||||
'phone', 'family_name', 'gender', 'dob', 'referral_code', 'is_active',
|
||||
'is_complete_profile', 'passcode', 'image', 'created_at', 'updated_at', 'user_type', 'country_name']
|
||||
|
||||
|
||||
class JuniorSerializer(serializers.ModelSerializer):
|
||||
@ -166,17 +217,23 @@ class JuniorSerializer(serializers.ModelSerializer):
|
||||
first_name = serializers.SerializerMethodField('get_first_name')
|
||||
last_name = serializers.SerializerMethodField('get_last_name')
|
||||
auth_token = serializers.SerializerMethodField('get_auth_token')
|
||||
refresh_token = serializers.SerializerMethodField('get_refresh_token')
|
||||
|
||||
def get_auth_token(self, obj):
|
||||
refresh = RefreshToken.for_user(obj.auth)
|
||||
access_token = str(refresh.access_token)
|
||||
return access_token
|
||||
|
||||
def get_refresh_token(self, obj):
|
||||
refresh = RefreshToken.for_user(obj.auth)
|
||||
refresh_token = str(refresh)
|
||||
return refresh_token
|
||||
|
||||
def get_user_type(self, obj):
|
||||
email_verified = UserEmailOtp.objects.filter(email=obj.auth.username).last()
|
||||
if email_verified and email_verified.user_type is not None:
|
||||
return email_verified.user_type
|
||||
return '1'
|
||||
return str(NUMBER['one'])
|
||||
|
||||
def get_auth(self, obj):
|
||||
return obj.auth.username
|
||||
@ -190,9 +247,9 @@ class JuniorSerializer(serializers.ModelSerializer):
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Junior
|
||||
fields = ['id', 'auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob',
|
||||
'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image',
|
||||
'updated_at', 'user_type', 'country_name','is_invited']
|
||||
fields = ['id', 'auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code',
|
||||
'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_password_set',
|
||||
'is_complete_profile', 'created_at', 'image', 'updated_at', 'user_type', 'country_name','is_invited']
|
||||
|
||||
class EmailVerificationSerializer(serializers.ModelSerializer):
|
||||
"""Email verification serializer"""
|
||||
@ -210,6 +267,7 @@ class DefaultTaskImagesSerializer(serializers.ModelSerializer):
|
||||
model = DefaultTaskImages
|
||||
fields = ['id', 'task_name', 'image_url']
|
||||
def create(self, validated_data):
|
||||
# create default task object
|
||||
data = DefaultTaskImages.objects.create(**validated_data)
|
||||
return data
|
||||
|
||||
@ -232,10 +290,11 @@ class UserDeleteSerializer(serializers.ModelSerializer):
|
||||
data = validated_data.get('reason')
|
||||
passwd = self.context['password']
|
||||
signup_method = self.context['signup_method']
|
||||
random_num = random.randint(0, 10000)
|
||||
random_num = secrets.randbelow(10001)
|
||||
user_tb = User.objects.filter(id=user.id).last()
|
||||
user_type_datas = UserEmailOtp.objects.filter(email=user.email).last()
|
||||
if user_tb and user_tb.check_password(passwd) and signup_method == '1':
|
||||
# check password and sign up method
|
||||
if user_tb and user_tb.check_password(passwd) and signup_method == str(NUMBER['one']):
|
||||
user_type_data = user_type_datas.user_type
|
||||
instance = delete_user_account_condition(user, user_type_data, user_type, user_tb, data, random_num)
|
||||
return instance
|
||||
@ -264,6 +323,7 @@ class UpdateUserNotificationSerializer(serializers.ModelSerializer):
|
||||
def create(self, validated_data):
|
||||
instance = UserNotification.objects.filter(user=self.context).last()
|
||||
if instance:
|
||||
# change notification status
|
||||
instance.push_notification = validated_data.get('push_notification',instance.push_notification)
|
||||
instance.email_notification = validated_data.get('email_notification', instance.email_notification)
|
||||
instance.sms_notification = validated_data.get('sms_notification', instance.sms_notification)
|
||||
|
@ -8,7 +8,7 @@
|
||||
<tr>
|
||||
<td style="padding: 0 27px 15px;">
|
||||
<p style="margin: 0; font-size: 16px; line-height: 20px; padding: 36px 0 0; font-weight: 500; color: #1f2532;">
|
||||
Hi {{full_name}},
|
||||
Hello,
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
@ -15,7 +15,7 @@
|
||||
<tr>
|
||||
<td style="padding: 0 27px 22px;">
|
||||
<p style="margin: 0;font-size: 14px; font-weight: 400; line-height: 21px; color: #1f2532;">
|
||||
You are receiving this email for join the ZOD bank platform. Please use <b>{{ url }} </b> link to join the platform.
|
||||
You are receiving this email for joining the ZOD bank platform. Please use <b>{{ url }} </b> link to join the platform.
|
||||
<br> Your credentials are:- username = <b>{{email}}</b> and password <b>{{password}}</b><br> <br>Below are the steps to complete the account and how to use this platform.
|
||||
|
||||
</p>
|
||||
|
@ -3,12 +3,32 @@
|
||||
from django.urls import path, include
|
||||
"""Third party import"""
|
||||
from rest_framework import routers
|
||||
"""Import view functions"""
|
||||
# Import view functions
|
||||
# UserLogin views,
|
||||
# SendPhoneOtp views,
|
||||
# UserPhoneVerification views,
|
||||
# UserEmailVerification views,
|
||||
# ReSendEmailOtp views,
|
||||
# ForgotPasswordAPIView views,
|
||||
# ResetPasswordAPIView views,
|
||||
# ChangePasswordAPIView views,
|
||||
# UpdateProfileImage views,
|
||||
# GoogleLoginViewSet views,
|
||||
# SigninWithApple views,
|
||||
# ProfileAPIViewSet views,
|
||||
# UploadImageAPIViewSet views,
|
||||
# DefaultImageAPIViewSet views,
|
||||
# DeleteUserProfileAPIViewSet views,
|
||||
# UserNotificationAPIViewSet views,
|
||||
# UpdateUserNotificationAPIViewSet views,
|
||||
# SendSupportEmail views,
|
||||
# LogoutAPIView views,
|
||||
# AccessTokenAPIView views"""
|
||||
from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp,
|
||||
ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage,
|
||||
GoogleLoginViewSet, SigninWithApple, ProfileAPIViewSet, UploadImageAPIViewSet,
|
||||
DefaultImageAPIViewSet, DeleteUserProfileAPIViewSet, UserNotificationAPIViewSet,
|
||||
UpdateUserNotificationAPIViewSet, SendSupportEmail, LogoutAPIView)
|
||||
UpdateUserNotificationAPIViewSet, SendSupportEmail, LogoutAPIView, AccessTokenAPIView)
|
||||
"""Router"""
|
||||
router = routers.SimpleRouter()
|
||||
|
||||
@ -45,5 +65,6 @@ urlpatterns = [
|
||||
path('api/v1/update-profile-image/', UpdateProfileImage.as_view()),
|
||||
path('api/v1/apple-login/', SigninWithApple.as_view(), name='signup_with_apple'),
|
||||
path('api/v1/send-support-email/', SendSupportEmail.as_view(), name='send-support-email'),
|
||||
path('api/v1/logout/', LogoutAPIView.as_view(), name='logout')
|
||||
path('api/v1/logout/', LogoutAPIView.as_view(), name='logout'),
|
||||
path('api/v1/generate-token/', AccessTokenAPIView.as_view(), name='generate-token')
|
||||
]
|
||||
|
@ -10,13 +10,39 @@ import string
|
||||
from datetime import datetime
|
||||
from calendar import timegm
|
||||
from uuid import uuid4
|
||||
# Import secrets module for generating random number
|
||||
import secrets
|
||||
from rest_framework import serializers
|
||||
# Django App Import
|
||||
# Import models from junior App,
|
||||
# Import models from guardian App,
|
||||
# Import models from account App,
|
||||
# Import messages from base package"""
|
||||
from junior.models import Junior
|
||||
from guardian.models import Guardian
|
||||
from account.models import UserDelete
|
||||
from base.messages import ERROR_CODE
|
||||
from django.utils import timezone
|
||||
|
||||
# Define delete
|
||||
# user account condition,
|
||||
# Define delete
|
||||
# user account
|
||||
# condition for social
|
||||
# login account,
|
||||
# Update junior account,
|
||||
# Update guardian account,
|
||||
# Define custom email for otp verification,
|
||||
# Define support email for user's query,
|
||||
# Define custom success response,
|
||||
# Define custom error response,
|
||||
# Generate access token,
|
||||
# refresh token by using jwt,
|
||||
# Define function for generating
|
||||
# guardian code, junior code,
|
||||
# referral code,
|
||||
# Define function for generating
|
||||
# alphanumeric code
|
||||
|
||||
def delete_user_account_condition(user, user_type_data, user_type, user_tb, data, random_num):
|
||||
"""delete user account"""
|
||||
@ -28,10 +54,11 @@ def delete_user_account_condition(user, user_type_data, user_type, user_tb, data
|
||||
raise serializers.ValidationError({"details": ERROR_CODE['2030'], "code": "400", "status": "failed"})
|
||||
user_tb.email = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower()
|
||||
user_tb.username = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower()
|
||||
user_tb.password = 'None'
|
||||
d_email = user_tb.email
|
||||
o_mail = user.email
|
||||
# update user email with dummy email
|
||||
user_tb.save()
|
||||
"""create object in user delete model"""
|
||||
instance = UserDelete.objects.create(user=user_tb, d_email=d_email, old_email=o_mail,
|
||||
is_active=True, reason=data)
|
||||
|
||||
@ -47,10 +74,11 @@ def delete_user_account_condition_social(user, user_type,user_tb, data, random_n
|
||||
raise serializers.ValidationError({"details": ERROR_CODE['2030'], "code": "400", "status": "failed"})
|
||||
user_tb.email = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower()
|
||||
user_tb.username = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower()
|
||||
user_tb.password = 'None'
|
||||
dummy_email = user_tb.email
|
||||
old_mail = user.email
|
||||
# update user email with dummy email
|
||||
user_tb.save()
|
||||
"""create object in user delete model"""
|
||||
instance_data = UserDelete.objects.create(user=user_tb, d_email=dummy_email, old_email=old_mail,
|
||||
is_active=True, reason=data)
|
||||
|
||||
@ -59,6 +87,7 @@ def junior_account_update(user_tb):
|
||||
"""junior account delete"""
|
||||
junior_data = Junior.objects.filter(auth__email=user_tb.email).first()
|
||||
if junior_data:
|
||||
# Update junior account
|
||||
junior_data.is_active = False
|
||||
junior_data.is_verified = False
|
||||
junior_data.guardian_code = '{}'
|
||||
@ -68,10 +97,12 @@ def guardian_account_update(user_tb):
|
||||
"""update guardian account after delete the user account"""
|
||||
guardian_data = Guardian.objects.filter(user__email=user_tb.email).first()
|
||||
if guardian_data:
|
||||
# Update guardian account
|
||||
guardian_data.is_active = False
|
||||
guardian_data.is_verified = False
|
||||
guardian_data.save()
|
||||
jun_data = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code))
|
||||
"""Disassociate relation between guardian and junior"""
|
||||
for data in jun_data:
|
||||
data.guardian_code.remove(guardian_data.guardian_code)
|
||||
data.save()
|
||||
@ -79,6 +110,7 @@ def send_otp_email(recipient_email, otp):
|
||||
"""Send otp on email with template"""
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [recipient_email]
|
||||
"""Send otp on email"""
|
||||
send_templated_mail(
|
||||
template_name='email_otp_verification.email',
|
||||
from_email=from_email,
|
||||
@ -93,6 +125,7 @@ def send_support_email(name, sender, subject, message):
|
||||
"""Send otp on email with template"""
|
||||
to_email = [settings.EMAIL_FROM_ADDRESS]
|
||||
from_email = settings.DEFAULT_ADDRESS
|
||||
"""Send support email to zod bank support team"""
|
||||
send_templated_mail(
|
||||
template_name='support_mail.email',
|
||||
from_email=from_email,
|
||||
@ -183,7 +216,16 @@ def get_token(data: dict = dict):
|
||||
|
||||
|
||||
def generate_alphanumeric_code(length):
|
||||
"""Generate alphanumeric code"""
|
||||
alphabet = string.ascii_letters + string.digits
|
||||
code = ''.join(secrets.choice(alphabet) for _ in range(length))
|
||||
return code
|
||||
|
||||
|
||||
def generate_code(value, user_id):
|
||||
"""generate referral, junior and guardian code"""
|
||||
code = value + str(user_id).zfill(3)
|
||||
return code
|
||||
|
||||
|
||||
OTP_EXPIRY = timezone.now() + timezone.timedelta(days=1)
|
||||
|
113
account/views.py
113
account/views.py
@ -1,21 +1,29 @@
|
||||
"""Account view """
|
||||
from notifications.utils import remove_fcm_token
|
||||
|
||||
"""Django import"""
|
||||
from datetime import datetime, timedelta
|
||||
from rest_framework import viewsets, status, views
|
||||
from rest_framework.decorators import action
|
||||
import random
|
||||
import logging
|
||||
from PIL import Image
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.utils import timezone
|
||||
import jwt
|
||||
from django.contrib.auth import logout
|
||||
"""App Import"""
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from django.contrib.auth import authenticate, login
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from templated_email import send_templated_mail
|
||||
import google.oauth2.credentials
|
||||
import google.auth.transport.requests
|
||||
from rest_framework import status
|
||||
import requests
|
||||
from rest_framework.response import Response
|
||||
from django.conf import settings
|
||||
"""App Import"""
|
||||
from guardian.models import Guardian
|
||||
from junior.models import Junior
|
||||
from account.models import UserProfile, UserPhoneOtp, UserEmailOtp, DefaultTaskImages, UserNotification
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from account.models import UserDeviceDetails, UserPhoneOtp, UserEmailOtp, DefaultTaskImages, UserNotification
|
||||
from django.contrib.auth.models import User
|
||||
"""Account serializer"""
|
||||
from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer,
|
||||
@ -25,24 +33,17 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali
|
||||
UserNotificationSerializer, UpdateUserNotificationSerializer, UserPhoneOtpSerializer)
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import NUMBER
|
||||
from base.constants import NUMBER, ZOD, JUN, GRD
|
||||
from guardian.tasks import generate_otp
|
||||
from account.utils import (send_otp_email, send_support_email, custom_response, custom_error_response,
|
||||
generate_alphanumeric_code)
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from templated_email import send_templated_mail
|
||||
import google.oauth2.credentials
|
||||
import google.auth.transport.requests
|
||||
from rest_framework import status
|
||||
import requests
|
||||
from rest_framework.response import Response
|
||||
from django.conf import settings
|
||||
generate_code, OTP_EXPIRY)
|
||||
from junior.serializers import JuniorProfileSerializer
|
||||
from guardian.serializers import GuardianProfileSerializer
|
||||
|
||||
class GoogleLoginMixin:
|
||||
class GoogleLoginMixin(object):
|
||||
"""google login mixin"""
|
||||
def google_login(self, request):
|
||||
@staticmethod
|
||||
def google_login(request):
|
||||
"""google login function"""
|
||||
access_token = request.data.get('access_token')
|
||||
user_type = request.data.get('user_type')
|
||||
@ -90,15 +91,15 @@ class GoogleLoginMixin:
|
||||
if str(user_type) == '1':
|
||||
junior_query = Junior.objects.create(auth=user_obj, is_verified=True, is_active=True,
|
||||
image=profile_picture, signup_method='2',
|
||||
junior_code=generate_alphanumeric_code(6),
|
||||
referral_code=generate_alphanumeric_code(6)
|
||||
junior_code=generate_code(JUN, user_obj.id),
|
||||
referral_code=generate_code(ZOD, user_obj.id)
|
||||
)
|
||||
serializer = JuniorSerializer(junior_query)
|
||||
if str(user_type) == '2':
|
||||
guardian_query = Guardian.objects.create(user=user_obj, is_verified=True, is_active=True,
|
||||
image=profile_picture,signup_method='2',
|
||||
guardian_code=generate_alphanumeric_code(6),
|
||||
referral_code=generate_alphanumeric_code(6)
|
||||
guardian_code=generate_code(GRD, user_obj.id),
|
||||
referral_code=generate_code(ZOD, user_obj.id)
|
||||
)
|
||||
serializer = GuardianSerializer(guardian_query)
|
||||
# Return a JSON response with the user's email and name
|
||||
@ -141,14 +142,14 @@ class SigninWithApple(views.APIView):
|
||||
if str(user_type) == '1':
|
||||
junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True,
|
||||
signup_method='3',
|
||||
junior_code=generate_alphanumeric_code(6),
|
||||
referral_code=generate_alphanumeric_code(6))
|
||||
junior_code=generate_code(JUN, user.id),
|
||||
referral_code=generate_code(ZOD, user.id))
|
||||
serializer = JuniorSerializer(junior_query)
|
||||
if str(user_type) == '2':
|
||||
guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True,
|
||||
signup_method='3',
|
||||
guardian_code=generate_alphanumeric_code(6),
|
||||
referral_code=generate_alphanumeric_code(6))
|
||||
guardian_code=generate_code(GRD, user.id),
|
||||
referral_code=generate_code(ZOD, user.id))
|
||||
serializer = GuardianSerializer(guardian_query)
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
||||
response_status=status.HTTP_200_OK)
|
||||
@ -181,6 +182,7 @@ class UpdateProfileImage(views.APIView):
|
||||
return custom_response(SUCCESS_CODE['3017'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return custom_error_response(ERROR_CODE['2036'],response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class ChangePasswordAPIView(views.APIView):
|
||||
@ -205,6 +207,7 @@ class ResetPasswordAPIView(views.APIView):
|
||||
|
||||
class ForgotPasswordAPIView(views.APIView):
|
||||
"""Forgot password"""
|
||||
|
||||
def post(self, request):
|
||||
serializer = ForgotPasswordSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
@ -213,7 +216,7 @@ class ForgotPasswordAPIView(views.APIView):
|
||||
User.objects.get(email=email)
|
||||
except User.DoesNotExist:
|
||||
return custom_error_response(ERROR_CODE['2004'], response_status=status.HTTP_404_NOT_FOUND)
|
||||
verification_code = ''.join([str(random.randrange(9)) for _ in range(6)])
|
||||
verification_code = generate_otp()
|
||||
# Send the verification code to the user's email
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [email]
|
||||
@ -225,8 +228,7 @@ class ForgotPasswordAPIView(views.APIView):
|
||||
'verification_code': verification_code
|
||||
}
|
||||
)
|
||||
|
||||
expiry = timezone.now() + timezone.timedelta(days=1)
|
||||
expiry = OTP_EXPIRY
|
||||
user_data, created = UserEmailOtp.objects.get_or_create(email=email)
|
||||
if created:
|
||||
user_data.expired_at = expiry
|
||||
@ -281,6 +283,7 @@ class UserLogin(viewsets.ViewSet):
|
||||
def login(self, request):
|
||||
username = request.data.get('username')
|
||||
password = request.data.get('password')
|
||||
device_id = request.META.get('HTTP_DEVICE_ID')
|
||||
user = authenticate(request, username=username, password=password)
|
||||
|
||||
try:
|
||||
@ -292,6 +295,10 @@ class UserLogin(viewsets.ViewSet):
|
||||
junior_data = Junior.objects.filter(auth__username=username, is_verified=True).last()
|
||||
if junior_data:
|
||||
serializer = JuniorSerializer(junior_data).data
|
||||
device_details, created = UserDeviceDetails.objects.get_or_create(user=user)
|
||||
if device_details:
|
||||
device_details.device_id = device_id
|
||||
device_details.save()
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer, response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2002"], response_status=status.HTTP_401_UNAUTHORIZED)
|
||||
@ -300,7 +307,8 @@ class UserLogin(viewsets.ViewSet):
|
||||
email_verified = UserEmailOtp.objects.filter(email=username).last()
|
||||
refresh = RefreshToken.for_user(user)
|
||||
access_token = str(refresh.access_token)
|
||||
data = {"auth_token":access_token, "is_profile_complete": False,
|
||||
refresh_token = str(refresh)
|
||||
data = {"auth_token":access_token, "refresh_token":refresh_token, "is_profile_complete": False,
|
||||
"user_type": email_verified.user_type,
|
||||
}
|
||||
is_verified = False
|
||||
@ -315,7 +323,7 @@ class UserLogin(viewsets.ViewSet):
|
||||
return custom_response(ERROR_CODE['2024'], {"email_otp": otp, "is_email_verified": is_verified},
|
||||
response_status=status.HTTP_200_OK)
|
||||
data.update({"is_email_verified": is_verified})
|
||||
return custom_response(None, data, response_status=status.HTTP_200_OK)
|
||||
return custom_response(SUCCESS_CODE['3003'], data, response_status=status.HTTP_200_OK)
|
||||
|
||||
@action(methods=['post'], detail=False)
|
||||
def admin_login(self, request):
|
||||
@ -334,7 +342,8 @@ class UserLogin(viewsets.ViewSet):
|
||||
logging.error(e)
|
||||
refresh = RefreshToken.for_user(user)
|
||||
access_token = str(refresh.access_token)
|
||||
data = {"auth_token": access_token, "user_role": '3'}
|
||||
refresh_token = str(refresh)
|
||||
data = {"auth_token": access_token, "refresh_token":refresh_token, "user_type": '3'}
|
||||
return custom_response(None, data, response_status=status.HTTP_200_OK)
|
||||
|
||||
class UserEmailVerification(viewsets.ModelViewSet):
|
||||
@ -371,7 +380,9 @@ class UserEmailVerification(viewsets.ModelViewSet):
|
||||
guardian_data.save()
|
||||
refresh = RefreshToken.for_user(user_obj)
|
||||
access_token = str(refresh.access_token)
|
||||
return custom_response(SUCCESS_CODE['3011'], {"auth_token":access_token},
|
||||
refresh_token = str(refresh)
|
||||
return custom_response(SUCCESS_CODE['3011'], {"auth_token":access_token,
|
||||
"refresh_token":refresh_token},
|
||||
response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
@ -383,10 +394,12 @@ class ReSendEmailOtp(viewsets.ModelViewSet):
|
||||
"""Send otp on phone"""
|
||||
queryset = UserEmailOtp.objects.all()
|
||||
serializer_class = EmailVerificationSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
otp = generate_otp()
|
||||
if User.objects.filter(email=request.data['email']):
|
||||
expiry = timezone.now() + timezone.timedelta(days=1)
|
||||
expiry = OTP_EXPIRY
|
||||
email_data, created = UserEmailOtp.objects.get_or_create(email=request.data['email'])
|
||||
if created:
|
||||
email_data.expired_at = expiry
|
||||
@ -404,6 +417,8 @@ class ProfileAPIViewSet(viewsets.ModelViewSet):
|
||||
"""Profile viewset"""
|
||||
queryset = User.objects.all()
|
||||
serializer_class = JuniorProfileSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""profile view"""
|
||||
if str(self.request.GET.get('user_type')) == '1':
|
||||
@ -417,14 +432,12 @@ class ProfileAPIViewSet(viewsets.ModelViewSet):
|
||||
serializer = GuardianProfileSerializer(guardian_data)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
|
||||
class UploadImageAPIViewSet(viewsets.ModelViewSet):
|
||||
"""Profile viewset"""
|
||||
"""upload task image"""
|
||||
queryset = DefaultTaskImages.objects.all()
|
||||
serializer_class = DefaultTaskImagesSerializer
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""profile view"""
|
||||
"""upload images"""
|
||||
image_data = request.data['image_url']
|
||||
filename = f"default_task_images/{image_data.name}"
|
||||
if image_data.size == NUMBER['zero']:
|
||||
@ -442,6 +455,7 @@ class DefaultImageAPIViewSet(viewsets.ModelViewSet):
|
||||
"""Profile viewset"""
|
||||
queryset = DefaultTaskImages.objects.all()
|
||||
serializer_class = DefaultTaskImagesDetailsSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""profile view"""
|
||||
queryset = DefaultTaskImages.objects.all()
|
||||
@ -472,6 +486,7 @@ class UserNotificationAPIViewSet(viewsets.ModelViewSet):
|
||||
"""notification viewset"""
|
||||
queryset = UserNotification.objects.all()
|
||||
serializer_class = UserNotificationSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""profile view"""
|
||||
queryset = self.queryset.filter(user=request.user)
|
||||
@ -483,6 +498,7 @@ class UpdateUserNotificationAPIViewSet(viewsets.ModelViewSet):
|
||||
"""Update notification viewset"""
|
||||
queryset = UserNotification.objects.all()
|
||||
serializer_class = UpdateUserNotificationSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""profile view"""
|
||||
@ -496,6 +512,8 @@ class UpdateUserNotificationAPIViewSet(viewsets.ModelViewSet):
|
||||
|
||||
class SendSupportEmail(views.APIView):
|
||||
"""support email api"""
|
||||
permission_classes = (IsAuthenticated,)
|
||||
|
||||
def post(self, request):
|
||||
name = request.data.get('name')
|
||||
sender = request.data.get('email')
|
||||
@ -510,11 +528,30 @@ class SendSupportEmail(views.APIView):
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE['2033'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class LogoutAPIView(views.APIView):
|
||||
"""Log out API"""
|
||||
permission_classes = (IsAuthenticated,)
|
||||
|
||||
def post(self, request):
|
||||
remove_fcm_token(
|
||||
request.auth.payload['user_id'],
|
||||
request.META['HTTP_AUTHORIZATION'].split(" ")[1],
|
||||
request.data.get('registration_id', ""))
|
||||
logout(request)
|
||||
request.session.flush()
|
||||
return custom_response(SUCCESS_CODE['3020'], response_status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class AccessTokenAPIView(views.APIView):
|
||||
"""generate access token API"""
|
||||
|
||||
def post(self, request):
|
||||
# Assuming you have a refresh_token string
|
||||
refresh_token = request.data['refresh_token']
|
||||
# Create a RefreshToken instance from the refresh token string
|
||||
refresh = RefreshToken(refresh_token)
|
||||
# Generate a new access token
|
||||
access_token = str(refresh.access_token)
|
||||
data = {"auth_token": access_token}
|
||||
return custom_response(None, data, response_status=status.HTTP_200_OK)
|
||||
|
||||
|
@ -6,52 +6,25 @@ import os
|
||||
# GOOGLE_URL used for interact with google server to verify user existence.
|
||||
#GOOGLE_URL = "https://www.googleapis.com/plus/v1/"
|
||||
|
||||
# Define Code prefix word
|
||||
# for guardian code,
|
||||
# junior code,
|
||||
# referral code"""
|
||||
ZOD = 'ZOD'
|
||||
JUN = 'JUN'
|
||||
GRD = 'GRD'
|
||||
# Define number variable
|
||||
# from zero to
|
||||
# twenty and
|
||||
# some standard
|
||||
# number"""
|
||||
NUMBER = {
|
||||
'point_zero': 0.0,
|
||||
'zero': 0,
|
||||
'one': 1,
|
||||
'two': 2,
|
||||
'three': 3,
|
||||
'four': 4,
|
||||
'five': 5,
|
||||
'six': 6,
|
||||
'seven': 7,
|
||||
'eight': 8,
|
||||
'nine': 9,
|
||||
'ten': 10,
|
||||
'eleven': 11,
|
||||
'twelve': 12,
|
||||
'thirteen': 13,
|
||||
'fourteen': 14,
|
||||
'fifteen': 15,
|
||||
'sixteen': 16,
|
||||
'seventeen': 17,
|
||||
'eighteen': 18,
|
||||
'nineteen': 19,
|
||||
'twenty_four': 24,
|
||||
'twenty_one': 21,
|
||||
'twenty_two': 22,
|
||||
'twenty_five': 25,
|
||||
'thirty': 30,
|
||||
'thirty_five': 35,
|
||||
'thirty_six': 36,
|
||||
'forty': 40,
|
||||
'fifty': 50,
|
||||
'fifty_nine': 59,
|
||||
'sixty': 60,
|
||||
'seventy_five': 75,
|
||||
'eighty': 80,
|
||||
'ninty_five': 95,
|
||||
'ninty_six': 96,
|
||||
'ninety_nine': 99,
|
||||
'hundred': 100,
|
||||
'one_one_nine': 119,
|
||||
'one_twenty': 120,
|
||||
'four_zero_four': 404,
|
||||
'five_hundred': 500,
|
||||
'minus_one': -1,
|
||||
'point_three': 0.3,
|
||||
'point_seven': 0.7
|
||||
'point_zero': 0.0, 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7,
|
||||
'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15,
|
||||
'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20,
|
||||
'twenty_one': 21, 'twenty_two': 22,'twenty_three': 23, 'twenty_four': 24, 'twenty_five': 25,
|
||||
'thirty': 30, 'forty': 40, 'fifty': 50, 'sixty': 60, 'seventy': 70, 'eighty': 80, 'ninty': 90,
|
||||
'ninety_nine': 99, 'hundred': 100, 'thirty_six_hundred': 3600
|
||||
}
|
||||
|
||||
|
||||
@ -71,10 +44,6 @@ FILE_SIZE = 5 * 1024 * 1024
|
||||
# String constant for configurable date for allocation lock period
|
||||
ALLOCATION_LOCK_DATE = 1
|
||||
|
||||
sort_dict = {
|
||||
'1': 'name',
|
||||
'2': '-name'
|
||||
}
|
||||
"""user type"""
|
||||
USER_TYPE = (
|
||||
('1', 'junior'),
|
||||
@ -86,25 +55,29 @@ GENDERS = (
|
||||
('1', 'Male'),
|
||||
('2', 'Female')
|
||||
)
|
||||
"""Task status"""
|
||||
# Task status"""
|
||||
TASK_STATUS = (
|
||||
('1', 'pending'),
|
||||
('2', 'in-progress'),
|
||||
('3', 'rejected'),
|
||||
('4', 'requested'),
|
||||
('5', 'completed')
|
||||
('5', 'completed'),
|
||||
('6', 'expired')
|
||||
)
|
||||
"""sign up method"""
|
||||
# sign up method
|
||||
SIGNUP_METHODS = (
|
||||
('1', 'manual'),
|
||||
('2', 'google'),
|
||||
('3', 'apple')
|
||||
)
|
||||
"""relationship"""
|
||||
# relationship
|
||||
RELATIONSHIP = (
|
||||
('1', 'parent'),
|
||||
('2', 'legal_guardian')
|
||||
)
|
||||
"""
|
||||
Define task status
|
||||
in a number"""
|
||||
PENDING = 1
|
||||
IN_PROGRESS = 2
|
||||
REJECTED = 3
|
||||
@ -113,6 +86,7 @@ COMPLETED = 5
|
||||
TASK_POINTS = 5
|
||||
# duplicate name used defined in constant PROJECT_NAME
|
||||
PROJECT_NAME = 'Zod Bank'
|
||||
# define user type constant
|
||||
GUARDIAN = 'guardian'
|
||||
JUNIOR = 'junior'
|
||||
SUPERUSER = 'superuser'
|
||||
@ -123,3 +97,15 @@ BYTE_IMAGE_SIZE = 1024
|
||||
|
||||
# validate file size
|
||||
MAX_FILE_SIZE = 1024 * 1024 * 5
|
||||
|
||||
ARTICLE_SURVEY_POINTS = 5
|
||||
MAX_ARTICLE_CARD = 6
|
||||
|
||||
# min and max survey
|
||||
MIN_ARTICLE_SURVEY = 5
|
||||
MAX_ARTICLE_SURVEY = 10
|
||||
|
||||
# real time url
|
||||
time_url = "http://worldtimeapi.org/api/timezone/Asia/Riyadh"
|
||||
|
||||
ARTICLE_CARD_IMAGE_FOLDER = 'article-card-images'
|
||||
|
@ -42,26 +42,60 @@ ERROR_CODE = {
|
||||
"2016": "Invalid search.",
|
||||
"2017": "{model} object with {pk} does not exist",
|
||||
"2018": "Attached File not found",
|
||||
"2019": "Either File extension or File size doesn't meet the requirements",
|
||||
"2019": "Invalid Referral code",
|
||||
"2020": "Enter valid mobile number",
|
||||
"2021": "Already register",
|
||||
"2022": "Invalid Guardian code",
|
||||
"2023": "Invalid user",
|
||||
# email not verified
|
||||
"2024": "Email not verified",
|
||||
"2025": "Invalid input. Expected a list of strings.",
|
||||
# check old and new password
|
||||
"2026": "New password should not same as old password",
|
||||
"2027": "data should contain `identityToken`",
|
||||
"2028": "You are not authorized person to sign up on this platform",
|
||||
"2029": "Validity of otp verification is expired",
|
||||
"2030": "Use correct user type and token",
|
||||
# invalid password
|
||||
"2031": "Invalid password",
|
||||
"2032": "Failed to send email",
|
||||
"2033": "Missing required fields",
|
||||
"2034": "Junior is not associated",
|
||||
# image size
|
||||
"2035": "Image should not be 0 kb",
|
||||
"2036": "Choose valid user"
|
||||
"2036": "Choose valid user",
|
||||
# log in multiple device msg
|
||||
"2037": "You are already log in another device",
|
||||
"2038": "Choose valid action for task",
|
||||
# card length limit
|
||||
"2039": "Add at least one article card or maximum 6",
|
||||
"2040": "Add at least 5 article survey or maximum 10",
|
||||
# add article msg
|
||||
"2041": "Article with given id doesn't exist.",
|
||||
"2042": "Article Card with given id doesn't exist.",
|
||||
"2043": "Article Survey with given id doesn't exist.",
|
||||
"2044": "Task does not exist",
|
||||
"2045": "Invalid guardian",
|
||||
# past due date
|
||||
"2046": "Due date must be future date",
|
||||
# invalid junior id msg
|
||||
"2047": "Invalid Junior ID ",
|
||||
"2048": "Choose right file for image",
|
||||
# task request
|
||||
"2049": "This task is already requested ",
|
||||
"2059": "Already exist junior",
|
||||
# task status
|
||||
"2060": "Task does not exist or not in pending state",
|
||||
"2061": "Please insert image or check the image is valid or not.",
|
||||
# email not null
|
||||
"2062": "Please enter email address",
|
||||
"2063": "Unauthorized access.",
|
||||
"2064": "To change your password first request an OTP and get it verify then change your password.",
|
||||
"2065": "Passwords do not match. Please try again."
|
||||
}
|
||||
"""Success message code"""
|
||||
SUCCESS_CODE = {
|
||||
"3000": "ok",
|
||||
# Success code for password
|
||||
"3001": "Sign up successfully",
|
||||
# Success code for Thank you
|
||||
@ -93,9 +127,22 @@ SUCCESS_CODE = {
|
||||
"3019": "Support Email sent successfully",
|
||||
"3020": "Logged out successfully.",
|
||||
"3021": "Add junior successfully",
|
||||
"3022": "Remove junior successfully"
|
||||
"3022": "Remove junior successfully",
|
||||
"3023": "Junior is approved successfully",
|
||||
"3024": "Junior request is rejected successfully",
|
||||
"3025": "Task is approved successfully",
|
||||
"3026": "Task is rejected successfully",
|
||||
"3027": "Article has been created successfully.",
|
||||
"3028": "Article has been updated successfully.",
|
||||
"3029": "Article has been deleted successfully.",
|
||||
"3030": "Article Card has been removed successfully.",
|
||||
"3031": "Article Survey has been removed successfully.",
|
||||
"3032": "Task request sent successfully",
|
||||
"3033": "Valid Referral code",
|
||||
"3034": "Invite guardian successfully",
|
||||
"3035": "Task started successfully"
|
||||
}
|
||||
|
||||
"""status code error"""
|
||||
STATUS_CODE_ERROR = {
|
||||
# Status code for Invalid Input
|
||||
"4001": ["Invalid input."],
|
||||
|
29
base/tasks.py
Normal file
29
base/tasks.py
Normal file
@ -0,0 +1,29 @@
|
||||
"""
|
||||
web_admin tasks file
|
||||
"""
|
||||
# third party imports
|
||||
from celery import shared_task
|
||||
from templated_email import send_templated_mail
|
||||
|
||||
# django imports
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
@shared_task
|
||||
def send_email_otp(email, verification_code):
|
||||
"""
|
||||
used to send otp on email
|
||||
:param email: e-mail
|
||||
:param verification_code: otp
|
||||
"""
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [email]
|
||||
send_templated_mail(
|
||||
template_name='email_reset_verification.email',
|
||||
from_email=from_email,
|
||||
recipient_list=recipient_list,
|
||||
context={
|
||||
'verification_code': verification_code
|
||||
}
|
||||
)
|
||||
return True
|
BIN
celerybeat-schedule
Normal file
BIN
celerybeat-schedule
Normal file
Binary file not shown.
@ -3,6 +3,7 @@ services:
|
||||
nginx:
|
||||
image: nginx:latest
|
||||
container_name: nginx
|
||||
restart: always
|
||||
ports:
|
||||
- "8000:8000"
|
||||
volumes:
|
||||
@ -13,6 +14,26 @@ services:
|
||||
web:
|
||||
build: .
|
||||
container_name: django
|
||||
restart: always
|
||||
command: bash -c "pip install -r requirements.txt && python manage.py collectstatic --noinput && python manage.py migrate && gunicorn zod_bank.wsgi -b 0.0.0.0:8000 -t 300 --log-level=info"
|
||||
volumes:
|
||||
- .:/usr/src/app
|
||||
|
||||
broker:
|
||||
image: rabbitmq:3.7
|
||||
container_name: rabbitmq
|
||||
volumes:
|
||||
- .:/usr/src/app
|
||||
ports:
|
||||
- 5673:5673
|
||||
|
||||
worker:
|
||||
build: .
|
||||
image: celery
|
||||
container_name: dev_celery
|
||||
restart: "always"
|
||||
command: bash -c " celery -A zod_bank.celery worker --concurrency=1 -B -l DEBUG -E"
|
||||
volumes:
|
||||
- .:/usr/src/app
|
||||
depends_on:
|
||||
- broker
|
||||
|
@ -0,0 +1,21 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-14 09:34
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guardian', '0014_guardian_signup_method'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='guardian',
|
||||
options={'verbose_name': 'Guardian', 'verbose_name_plural': 'Guardian'},
|
||||
),
|
||||
migrations.AlterModelOptions(
|
||||
name='juniortask',
|
||||
options={'verbose_name': 'Junior Task', 'verbose_name_plural': 'Junior Task'},
|
||||
),
|
||||
]
|
@ -0,0 +1,28 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-18 07:17
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guardian', '0015_alter_guardian_options_alter_juniortask_options'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='juniortask',
|
||||
name='completed_on',
|
||||
field=models.DateTimeField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='juniortask',
|
||||
name='rejected_on',
|
||||
field=models.DateTimeField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='juniortask',
|
||||
name='requested_on',
|
||||
field=models.DateTimeField(blank=True, null=True),
|
||||
),
|
||||
]
|
@ -0,0 +1,23 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-24 13:21
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guardian', '0016_juniortask_completed_on_juniortask_rejected_on_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='juniortask',
|
||||
name='is_invited',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='juniortask',
|
||||
name='is_password_set',
|
||||
field=models.BooleanField(default=True),
|
||||
),
|
||||
]
|
@ -0,0 +1,21 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-24 13:23
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guardian', '0017_juniortask_is_invited_juniortask_is_password_set'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='juniortask',
|
||||
name='is_invited',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='juniortask',
|
||||
name='is_password_set',
|
||||
),
|
||||
]
|
@ -0,0 +1,23 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-24 13:23
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guardian', '0018_remove_juniortask_is_invited_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='guardian',
|
||||
name='is_invited',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='guardian',
|
||||
name='is_password_set',
|
||||
field=models.BooleanField(default=True),
|
||||
),
|
||||
]
|
18
guardian/migrations/0020_alter_juniortask_task_status.py
Normal file
18
guardian/migrations/0020_alter_juniortask_task_status.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-25 07:44
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guardian', '0019_guardian_is_invited_guardian_is_password_set'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='juniortask',
|
||||
name='task_status',
|
||||
field=models.CharField(choices=[('1', 'pending'), ('2', 'in-progress'), ('3', 'rejected'), ('4', 'requested'), ('5', 'completed'), ('6', 'expired')], default=1, max_length=15),
|
||||
),
|
||||
]
|
@ -4,9 +4,37 @@ from django.db import models
|
||||
from django.contrib.auth import get_user_model
|
||||
"""Import Django app"""
|
||||
from base.constants import GENDERS, TASK_STATUS, PENDING, TASK_POINTS, SIGNUP_METHODS
|
||||
from junior.models import Junior
|
||||
"""import Junior model"""
|
||||
import junior.models
|
||||
"""Add user model"""
|
||||
User = get_user_model()
|
||||
|
||||
# Create your models here.
|
||||
# Define junior model with
|
||||
# various fields like
|
||||
# phone, country code,
|
||||
# country name,
|
||||
# gender,
|
||||
# date of birth,
|
||||
# profile image,
|
||||
# signup method,
|
||||
# guardian code,
|
||||
# referral code,
|
||||
# referral code that used by the guardian
|
||||
# is invited guardian
|
||||
# profile is active or not
|
||||
# profile is complete or not
|
||||
# passcode
|
||||
# guardian is verified or not
|
||||
"""Define junior task model"""
|
||||
# define name of the Task
|
||||
# task description
|
||||
# points of the task
|
||||
# default image of the task
|
||||
# image uploaded by junior
|
||||
# task status
|
||||
# task approved or not
|
||||
|
||||
# Create your models here.
|
||||
|
||||
class Guardian(models.Model):
|
||||
@ -15,23 +43,35 @@ class Guardian(models.Model):
|
||||
"""Contact details"""
|
||||
country_code = models.IntegerField(blank=True, null=True)
|
||||
phone = models.CharField(max_length=31, null=True, blank=True, default=None)
|
||||
"""country name of the guardian"""
|
||||
country_name = models.CharField(max_length=100, null=True, blank=True, default=None)
|
||||
"""Image info"""
|
||||
image = models.URLField(null=True, blank=True, default=None)
|
||||
"""Personal info"""
|
||||
family_name = models.CharField(max_length=50, null=True, blank=True, default=None)
|
||||
"""gender of the guardian"""
|
||||
gender = models.CharField(choices=GENDERS, max_length=15, null=True, blank=True, default=None)
|
||||
"""date of birth of the guardian"""
|
||||
dob = models.DateField(max_length=15, null=True, blank=True, default=None)
|
||||
# invited junior"""
|
||||
is_invited = models.BooleanField(default=False)
|
||||
# Profile activity"""
|
||||
is_password_set = models.BooleanField(default=True)
|
||||
"""Profile activity"""
|
||||
is_active = models.BooleanField(default=True)
|
||||
"""guardian is verified or not"""
|
||||
is_verified = models.BooleanField(default=False)
|
||||
"""guardian profile is complete or not"""
|
||||
is_complete_profile = models.BooleanField(default=False)
|
||||
"""passcode of the guardian profile"""
|
||||
passcode = models.IntegerField(null=True, blank=True, default=None)
|
||||
"""Sign up method"""
|
||||
signup_method = models.CharField(max_length=31, choices=SIGNUP_METHODS, default='1')
|
||||
"""Codes"""
|
||||
"""Guardian Codes"""
|
||||
guardian_code = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
"""Referral code"""
|
||||
referral_code = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
"""Referral code that is used by guardian while signup"""
|
||||
referral_code_used = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
"""Profile created and updated time"""
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
@ -40,7 +80,10 @@ class Guardian(models.Model):
|
||||
class Meta(object):
|
||||
""" Meta class """
|
||||
db_table = 'guardians'
|
||||
"""verbose name of the model"""
|
||||
verbose_name = 'Guardian'
|
||||
"""change another name"""
|
||||
verbose_name_plural = 'Guardian'
|
||||
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
@ -51,20 +94,30 @@ class JuniorTask(models.Model):
|
||||
guardian = models.ForeignKey(Guardian, on_delete=models.CASCADE, related_name='guardian', verbose_name='Guardian')
|
||||
"""task details"""
|
||||
task_name = models.CharField(max_length=100)
|
||||
"""task description"""
|
||||
task_description = models.CharField(max_length=500)
|
||||
"""points of the task"""
|
||||
points = models.IntegerField(default=TASK_POINTS)
|
||||
"""last date of the task"""
|
||||
due_date = models.DateField(auto_now_add=False, null=True, blank=True)
|
||||
"""Images of task"""
|
||||
"""Images of task that is upload by guardian"""
|
||||
default_image = models.URLField(null=True, blank=True, default=None)
|
||||
"""image that is uploaded by junior"""
|
||||
image = models.URLField(null=True, blank=True, default=None)
|
||||
"""associated junior with the task"""
|
||||
junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='junior', verbose_name='Junior')
|
||||
junior = models.ForeignKey('junior.Junior', on_delete=models.CASCADE, related_name='junior', verbose_name='Junior')
|
||||
"""task status"""
|
||||
task_status = models.CharField(choices=TASK_STATUS, max_length=15, default=PENDING)
|
||||
"""task stage"""
|
||||
"""task is active or not"""
|
||||
is_active = models.BooleanField(default=True)
|
||||
"""Task is approved or not"""
|
||||
is_approved = models.BooleanField(default=False)
|
||||
"""request task on particular date"""
|
||||
requested_on = models.DateTimeField(auto_now_add=False, null=True, blank=True)
|
||||
"""reject task on particular date"""
|
||||
rejected_on = models.DateTimeField(auto_now_add=False, null=True, blank=True)
|
||||
"""complete task on particular date"""
|
||||
completed_on = models.DateTimeField(auto_now_add=False, null=True, blank=True)
|
||||
"""Profile created and updated time"""
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
@ -72,7 +125,9 @@ class JuniorTask(models.Model):
|
||||
class Meta(object):
|
||||
""" Meta class """
|
||||
db_table = 'junior_task'
|
||||
"""verbose name of the model"""
|
||||
verbose_name = 'Junior Task'
|
||||
verbose_name_plural = 'Junior Task'
|
||||
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
|
@ -1,20 +1,47 @@
|
||||
"""Serializer of Guardian"""
|
||||
"""Third party Django app"""
|
||||
import logging
|
||||
import random
|
||||
from rest_framework import serializers
|
||||
# Import Refresh token of jwt
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from django.db import transaction
|
||||
from django.contrib.auth.models import User
|
||||
from datetime import datetime, time
|
||||
"""Import Django app"""
|
||||
# Import guardian's model,
|
||||
# Import junior's model,
|
||||
# Import account's model,
|
||||
# Import constant from
|
||||
# base package,
|
||||
# Import messages from
|
||||
# base package,
|
||||
# Import some functions
|
||||
# from utils file"""
|
||||
from .models import Guardian, JuniorTask
|
||||
from account.models import UserProfile, UserEmailOtp, UserNotification
|
||||
from account.utils import generate_alphanumeric_code
|
||||
from account.serializers import JuniorSerializer
|
||||
from account.utils import generate_code
|
||||
from junior.serializers import JuniorDetailSerializer
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import NUMBER
|
||||
from base.constants import NUMBER, JUN, ZOD, GRD
|
||||
from junior.models import Junior, JuniorPoints
|
||||
from .utils import real_time, convert_timedelta_into_datetime, update_referral_points
|
||||
# notification's constant
|
||||
from notifications.constants import TASK_POINTS, TASK_REJECTED
|
||||
# send notification function
|
||||
from notifications.utils import send_notification
|
||||
|
||||
|
||||
# In this serializer file
|
||||
# define user serializer,
|
||||
# create guardian serializer,
|
||||
# task serializer,
|
||||
# guardian serializer,
|
||||
# task details serializer,
|
||||
# top junior serializer,
|
||||
# guardian profile serializer,
|
||||
# approve junior serializer,
|
||||
# approve task serializer,
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
"""User serializer"""
|
||||
auth_token = serializers.SerializerMethodField('get_auth_token')
|
||||
@ -37,13 +64,15 @@ class UserSerializer(serializers.ModelSerializer):
|
||||
try:
|
||||
"""Create user profile"""
|
||||
user = User.objects.create_user(username=email, email=email, password=password)
|
||||
UserNotification.objects.create(user=user)
|
||||
if user_type == '1':
|
||||
Junior.objects.create(auth=user, junior_code=generate_alphanumeric_code(6),
|
||||
referral_code=generate_alphanumeric_code(6))
|
||||
if user_type == '2':
|
||||
Guardian.objects.create(user=user, guardian_code=generate_alphanumeric_code(6),
|
||||
referral_code=generate_alphanumeric_code(6))
|
||||
UserNotification.objects.get_or_create(user=user)
|
||||
if user_type == str(NUMBER['one']):
|
||||
# create junior profile
|
||||
Junior.objects.create(auth=user, junior_code=generate_code(JUN, user.id),
|
||||
referral_code=generate_code(ZOD, user.id))
|
||||
if user_type == str(NUMBER['two']):
|
||||
# create guardian profile
|
||||
Guardian.objects.create(user=user, guardian_code=generate_code(GRD, user.id),
|
||||
referral_code=generate_code(ZOD, user.id))
|
||||
return user
|
||||
except Exception as e:
|
||||
"""Error handling"""
|
||||
@ -56,6 +85,7 @@ class UserSerializer(serializers.ModelSerializer):
|
||||
"code": 400, "status":"failed",
|
||||
})
|
||||
|
||||
# update guardian profile
|
||||
class CreateGuardianSerializer(serializers.ModelSerializer):
|
||||
"""Create guardian serializer"""
|
||||
"""Basic info"""
|
||||
@ -65,9 +95,12 @@ class CreateGuardianSerializer(serializers.ModelSerializer):
|
||||
"""Contact details"""
|
||||
phone = serializers.CharField(max_length=20, required=False)
|
||||
country_code = serializers.IntegerField(required=False)
|
||||
# basic info
|
||||
family_name = serializers.CharField(max_length=100, required=False)
|
||||
dob = serializers.DateField(required=False)
|
||||
# code info
|
||||
referral_code = serializers.CharField(max_length=100, required=False)
|
||||
# image info
|
||||
image = serializers.URLField(required=False)
|
||||
|
||||
class Meta(object):
|
||||
@ -111,7 +144,6 @@ class CreateGuardianSerializer(serializers.ModelSerializer):
|
||||
guardian.country_code = validated_data.get('country_code', guardian.country_code)
|
||||
guardian.passcode = validated_data.get('passcode', guardian.passcode)
|
||||
guardian.country_name = validated_data.get('country_name', guardian.country_name)
|
||||
guardian.referral_code_used = validated_data.get('referral_code_used', guardian.referral_code_used)
|
||||
guardian.image = validated_data.get('image', guardian.image)
|
||||
"""Complete profile of the junior if below all data are filled"""
|
||||
complete_profile_field = all([guardian.gender, guardian.country_name,
|
||||
@ -120,6 +152,9 @@ class CreateGuardianSerializer(serializers.ModelSerializer):
|
||||
guardian.is_complete_profile = False
|
||||
if complete_profile_field:
|
||||
guardian.is_complete_profile = True
|
||||
referral_code_used = validated_data.get('referral_code_used')
|
||||
update_referral_points(guardian.referral_code, referral_code_used)
|
||||
guardian.referral_code_used = validated_data.get('referral_code_used', guardian.referral_code_used)
|
||||
guardian.save()
|
||||
return guardian
|
||||
|
||||
@ -137,9 +172,18 @@ class TaskSerializer(serializers.ModelSerializer):
|
||||
"""Meta info"""
|
||||
model = JuniorTask
|
||||
fields = ['id', 'task_name','task_description','points', 'due_date', 'junior', 'default_image']
|
||||
|
||||
def validate_due_date(self, value):
|
||||
"""validation on due date"""
|
||||
if value < datetime.today().date():
|
||||
raise serializers.ValidationError({"details": ERROR_CODE['2046'],
|
||||
"code": 400, "status": "failed",
|
||||
})
|
||||
return value
|
||||
def create(self, validated_data):
|
||||
"""create default task image data"""
|
||||
validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last()
|
||||
# update image of the task
|
||||
images = self.context['image']
|
||||
validated_data['default_image'] = images
|
||||
instance = JuniorTask.objects.create(**validated_data)
|
||||
@ -173,11 +217,32 @@ class TaskDetailsSerializer(serializers.ModelSerializer):
|
||||
"""Task detail serializer"""
|
||||
|
||||
junior = JuniorDetailSerializer()
|
||||
remaining_time = serializers.SerializerMethodField('get_remaining_time')
|
||||
is_expired = serializers.SerializerMethodField('get_is_expired')
|
||||
|
||||
def get_remaining_time(self, obj):
|
||||
""" remaining time to complete task"""
|
||||
due_date_datetime = datetime.combine(obj.due_date, datetime.max.time())
|
||||
# fetch real time
|
||||
current_datetime = real_time()
|
||||
# Perform the subtraction
|
||||
if due_date_datetime > current_datetime:
|
||||
time_difference = due_date_datetime - current_datetime
|
||||
time_only = convert_timedelta_into_datetime(time_difference)
|
||||
return str(time_difference.days) + ' days ' + str(time_only)
|
||||
return str(NUMBER['zero']) + ' days ' + '00:00:00:00000'
|
||||
|
||||
def get_is_expired(self, obj):
|
||||
""" task expired or not"""
|
||||
if obj.due_date < datetime.today().date():
|
||||
return True
|
||||
return False
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = JuniorTask
|
||||
fields = ['id', 'guardian', 'task_name', 'task_description', 'points', 'due_date','default_image', 'image',
|
||||
'junior', 'task_status', 'is_active', 'created_at','updated_at']
|
||||
'requested_on', 'rejected_on', 'completed_on', 'is_expired',
|
||||
'junior', 'task_status', 'is_active', 'remaining_time', 'created_at','updated_at']
|
||||
|
||||
|
||||
class TopJuniorSerializer(serializers.ModelSerializer):
|
||||
@ -188,7 +253,7 @@ class TopJuniorSerializer(serializers.ModelSerializer):
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = JuniorPoints
|
||||
fields = ['id', 'junior', 'total_task_points', 'position', 'created_at', 'updated_at']
|
||||
fields = ['id', 'junior', 'total_points', 'position', 'created_at', 'updated_at']
|
||||
|
||||
def to_representation(self, instance):
|
||||
"""Convert instance to representation"""
|
||||
@ -227,6 +292,7 @@ class GuardianProfileSerializer(serializers.ModelSerializer):
|
||||
"""total filled fields count"""
|
||||
total_field_list = [obj.user.first_name, obj.country_name,
|
||||
obj.gender, obj.dob, obj.image]
|
||||
# count total complete field
|
||||
total_complete_field = [data for data in total_field_list if data != '' and data is not None]
|
||||
return len(total_complete_field)
|
||||
|
||||
@ -241,3 +307,54 @@ class GuardianProfileSerializer(serializers.ModelSerializer):
|
||||
'guardian_code', 'notification_count', 'total_count', 'complete_field_count', 'referral_code',
|
||||
'is_active', 'is_complete_profile', 'created_at', 'image', 'signup_method',
|
||||
'updated_at', 'passcode']
|
||||
|
||||
class ApproveJuniorSerializer(serializers.ModelSerializer):
|
||||
"""approve junior serializer"""
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Junior
|
||||
fields = ['id', 'guardian_code']
|
||||
|
||||
def create(self, validated_data):
|
||||
"""update guardian code"""
|
||||
instance = self.context['junior']
|
||||
instance.guardian_code = [self.context['guardian_code']]
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
class ApproveTaskSerializer(serializers.ModelSerializer):
|
||||
"""approve task serializer"""
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = JuniorTask
|
||||
fields = ['id', 'task_status', 'is_approved']
|
||||
|
||||
def create(self, validated_data):
|
||||
"""update task status """
|
||||
with transaction.atomic():
|
||||
instance = self.context['task_instance']
|
||||
junior = self.context['junior']
|
||||
junior_details = Junior.objects.filter(id=junior).last()
|
||||
junior_data, created = JuniorPoints.objects.get_or_create(junior=junior_details)
|
||||
if self.context['action'] == str(NUMBER['one']):
|
||||
# approve the task
|
||||
instance.task_status = str(NUMBER['five'])
|
||||
instance.is_approved = True
|
||||
# update total task point
|
||||
junior_data.total_points = junior_data.total_points + instance.points
|
||||
# update complete time of task
|
||||
instance.completed_on = real_time()
|
||||
send_notification.delay(TASK_POINTS, None, junior_details.auth.id, {})
|
||||
else:
|
||||
# reject the task
|
||||
instance.task_status = str(NUMBER['three'])
|
||||
instance.is_approved = False
|
||||
# update total task point
|
||||
junior_data.total_points = junior_data.total_points - instance.points
|
||||
# update reject time of task
|
||||
instance.rejected_on = real_time()
|
||||
send_notification.delay(TASK_REJECTED, None, junior_details.auth.id, {})
|
||||
instance.save()
|
||||
junior_data.save()
|
||||
return junior_details
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
"""task files"""
|
||||
"""Django import"""
|
||||
import random
|
||||
import secrets
|
||||
def generate_otp():
|
||||
"""generate random otp"""
|
||||
return ''.join([str(random.randrange(9)) for _ in range(6)])
|
||||
digits = "0123456789"
|
||||
return "".join(secrets.choice(digits) for _ in range(6))
|
||||
|
@ -2,29 +2,41 @@
|
||||
"""Django import"""
|
||||
from django.urls import path, include
|
||||
from .views import (SignupViewset, UpdateGuardianProfile, AllTaskListAPIView, CreateTaskAPIView, TaskListAPIView,
|
||||
SearchTaskListAPIView, TopJuniorListAPIView)
|
||||
SearchTaskListAPIView, TopJuniorListAPIView, ApproveJuniorAPIView, ApproveTaskAPIView)
|
||||
"""Third party import"""
|
||||
from rest_framework import routers
|
||||
|
||||
"""Define Router"""
|
||||
router = routers.SimpleRouter()
|
||||
|
||||
"""API End points with router"""
|
||||
# API End points with router
|
||||
# in this file
|
||||
# we define various api end point
|
||||
# that is covered in this guardian
|
||||
# section API:- like
|
||||
# sign-up, create guardian profile,
|
||||
# create-task,
|
||||
# all task list, top junior,
|
||||
# filter-task"""
|
||||
"""Sign up API"""
|
||||
router.register('sign-up', SignupViewset, basename='sign-up')
|
||||
"""Create guardian profile API"""
|
||||
# Create guardian profile API"""
|
||||
router.register('create-guardian-profile', UpdateGuardianProfile, basename='update-guardian-profile')
|
||||
"""Create Task API"""
|
||||
# Create Task API"""
|
||||
router.register('create-task', CreateTaskAPIView, basename='create-task')
|
||||
"""All Task list API"""
|
||||
# All Task list API"""
|
||||
router.register('all-task-list', AllTaskListAPIView, basename='all-task-list')
|
||||
"""Task list bases on the status API"""
|
||||
# Task list bases on the status API"""
|
||||
router.register('task-list', TaskListAPIView, basename='task-list')
|
||||
"""Leaderboard API"""
|
||||
# Leaderboard API"""
|
||||
router.register('top-junior', TopJuniorListAPIView, basename='top-junior')
|
||||
"""Search Task list on the bases of status, due date, and task title API"""
|
||||
# Search Task list on the bases of status, due date, and task title API"""
|
||||
router.register('filter-task', SearchTaskListAPIView, basename='filter-task')
|
||||
"""Define Url pattern"""
|
||||
# Approve junior API"""
|
||||
router.register('approve-junior', ApproveJuniorAPIView, basename='approve-junior')
|
||||
# Approve junior API"""
|
||||
router.register('approve-task', ApproveTaskAPIView, basename='approve-task')
|
||||
# Define Url pattern"""
|
||||
urlpatterns = [
|
||||
path('api/v1/', include(router.urls)),
|
||||
]
|
||||
|
@ -3,8 +3,38 @@
|
||||
import oss2
|
||||
"""Import setting"""
|
||||
from django.conf import settings
|
||||
import logging
|
||||
import requests
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
"""Import tempfile"""
|
||||
import tempfile
|
||||
# Import date time module's function
|
||||
from datetime import datetime, time
|
||||
# import Number constant
|
||||
from base.constants import NUMBER, time_url
|
||||
# Import Junior's model
|
||||
from junior.models import Junior, JuniorPoints
|
||||
# Import guardian's model
|
||||
from .models import JuniorTask
|
||||
# Import app from celery
|
||||
from zod_bank.celery import app
|
||||
# notification's constant
|
||||
from notifications.constants import REFERRAL_POINTS
|
||||
# send notification function
|
||||
from notifications.utils import send_notification
|
||||
# Define upload image on
|
||||
# ali baba cloud
|
||||
# firstly save image
|
||||
# in temporary file
|
||||
# then check bucket name
|
||||
# then upload on ali baba
|
||||
# bucket and reform the image url"""
|
||||
# fetch real time data without depend on system time
|
||||
# convert time delta into date time object
|
||||
# update junior total points
|
||||
# update referral points
|
||||
# if any one use referral code of the junior
|
||||
# junior earn 5 points
|
||||
|
||||
def upload_image_to_alibaba(image, filename):
|
||||
"""upload image on oss alibaba bucket"""
|
||||
@ -22,3 +52,55 @@ def upload_image_to_alibaba(image, filename):
|
||||
new_filename = filename.replace(' ', '%20')
|
||||
return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}"
|
||||
|
||||
|
||||
def real_time():
|
||||
"""fetch real time from world time api"""
|
||||
url = time_url
|
||||
response = requests.get(url)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
time_str = data['datetime']
|
||||
realtime = datetime.fromisoformat(time_str.replace('Z', '+00:00')).replace(tzinfo=None)
|
||||
return realtime
|
||||
else:
|
||||
logging.error("Could not fetch error")
|
||||
return None
|
||||
|
||||
|
||||
def convert_timedelta_into_datetime(time_difference):
|
||||
"""convert date time"""
|
||||
# convert timedelta into datetime format
|
||||
hours = time_difference.seconds // NUMBER['thirty_six_hundred']
|
||||
minutes = (time_difference.seconds // NUMBER['sixty']) % NUMBER['sixty']
|
||||
seconds = time_difference.seconds % NUMBER['sixty']
|
||||
microseconds = time_difference.microseconds
|
||||
# Create a new time object with the extracted time components
|
||||
time_only = time(hours, minutes, seconds, microseconds)
|
||||
return time_only
|
||||
|
||||
def update_referral_points(referral_code, referral_code_used):
|
||||
"""Update benefit of referral points"""
|
||||
junior_queryset = Junior.objects.filter(referral_code=referral_code_used).last()
|
||||
if junior_queryset and junior_queryset.referral_code != referral_code:
|
||||
# create data if junior points is not exist
|
||||
junior_query, created = JuniorPoints.objects.get_or_create(junior=junior_queryset)
|
||||
if junior_query:
|
||||
# update junior total points and referral points
|
||||
junior_query.total_points = junior_query.total_points + NUMBER['five']
|
||||
junior_query.referral_points = junior_query.referral_points + NUMBER['five']
|
||||
junior_query.save()
|
||||
send_notification.delay(REFERRAL_POINTS, None, junior_queryset.auth.id, {})
|
||||
|
||||
|
||||
|
||||
@app.task
|
||||
def update_expired_task_status(data=None):
|
||||
"""
|
||||
Update task of the status if due date is in past
|
||||
"""
|
||||
try:
|
||||
task_status = [str(NUMBER['one']), str(NUMBER['two']), str(NUMBER['four'])]
|
||||
JuniorTask.objects.filter(due_date__lt=datetime.today().date(),
|
||||
task_status__in=task_status).update(task_status=str(NUMBER['six']))
|
||||
except ObjectDoesNotExist as e:
|
||||
logging.error(str(e))
|
||||
|
@ -1,49 +1,84 @@
|
||||
"""Views of Guardian"""
|
||||
"""Third party Django app"""
|
||||
|
||||
# django imports
|
||||
# Import IsAuthenticated
|
||||
# Import viewsets and status
|
||||
# Import PageNumberPagination
|
||||
# Import User
|
||||
# Import timezone
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework import viewsets, status
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from django.contrib.auth.models import User
|
||||
from django.utils import timezone
|
||||
from PIL import Image
|
||||
from datetime import datetime, timedelta
|
||||
"""Import Django app"""
|
||||
|
||||
# Import guardian's model,
|
||||
# Import junior's model,
|
||||
# Import account's model,
|
||||
# Import constant from
|
||||
# base package,
|
||||
# Import messages from
|
||||
# base package,
|
||||
# Import some functions
|
||||
# from utils file
|
||||
# Import account's serializer
|
||||
# Import account's task
|
||||
# Import notification constant
|
||||
# Import send_notification function
|
||||
from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer,
|
||||
TopJuniorSerializer)
|
||||
TopJuniorSerializer, ApproveJuniorSerializer, ApproveTaskSerializer)
|
||||
from .models import Guardian, JuniorTask
|
||||
from junior.models import Junior, JuniorPoints
|
||||
from junior.serializers import JuniorDetailSerializer
|
||||
from account.models import UserEmailOtp, UserNotification
|
||||
from .tasks import generate_otp
|
||||
from account.utils import send_otp_email
|
||||
from account.utils import custom_response, custom_error_response
|
||||
from account.utils import custom_response, custom_error_response, OTP_EXPIRY, send_otp_email
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import NUMBER
|
||||
from .utils import upload_image_to_alibaba
|
||||
from django.db.models import Sum
|
||||
from notifications.constants import REGISTRATION, TASK_CREATED, LEADERBOARD_RANKING
|
||||
from notifications.utils import send_notification
|
||||
|
||||
""" Define APIs """
|
||||
# Define Signup API,
|
||||
# update guardian profile,
|
||||
# list of all task
|
||||
# list of task according to the status of the task
|
||||
# create task API
|
||||
# search task by name of the task API
|
||||
# top junior API,
|
||||
# approve junior API
|
||||
# approve task API
|
||||
# Create your views here.
|
||||
# create approve task API
|
||||
class SignupViewset(viewsets.ModelViewSet):
|
||||
"""Signup view set"""
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""Create user profile"""
|
||||
if request.data['user_type'] in ['1', '2']:
|
||||
serializer = UserSerializer(context=request.data['user_type'], data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
"""Generate otp"""
|
||||
otp = generate_otp()
|
||||
expiry = timezone.now() + timezone.timedelta(days=1)
|
||||
UserEmailOtp.objects.create(email=request.data['email'], otp=otp,
|
||||
user_type=str(request.data['user_type']), expired_at=expiry)
|
||||
"""Send email to the register user"""
|
||||
send_otp_email(request.data['email'], otp)
|
||||
return custom_response(SUCCESS_CODE['3001'], {"email_otp": otp},
|
||||
response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE['2028'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
if request.data['user_type'] in [str(NUMBER['one']), str(NUMBER['two'])]:
|
||||
serializer = UserSerializer(context=request.data['user_type'], data=request.data)
|
||||
if serializer.is_valid():
|
||||
user = serializer.save()
|
||||
"""Generate otp"""
|
||||
otp = generate_otp()
|
||||
# expire otp after 1 day
|
||||
expiry = OTP_EXPIRY
|
||||
# create user email otp object
|
||||
UserEmailOtp.objects.create(email=request.data['email'], otp=otp,
|
||||
user_type=str(request.data['user_type']), expired_at=expiry)
|
||||
"""Send email to the register user"""
|
||||
send_otp_email(request.data['email'], otp)
|
||||
# send push notification for registration
|
||||
send_notification.delay(REGISTRATION, None, user.id, {})
|
||||
return custom_response(SUCCESS_CODE['3001'],
|
||||
response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE['2028'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class UpdateGuardianProfile(viewsets.ViewSet):
|
||||
"""Update guardian profile"""
|
||||
@ -53,25 +88,29 @@ class UpdateGuardianProfile(viewsets.ViewSet):
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""Create guardian profile"""
|
||||
data = request.data
|
||||
image = request.data.get('image')
|
||||
image_url = ''
|
||||
if image:
|
||||
if image and image.size == NUMBER['zero']:
|
||||
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
filename = f"images/{image.name}"
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
data = {"image":image_url}
|
||||
serializer = CreateGuardianSerializer(context={"user":request.user,
|
||||
"first_name":request.data.get('first_name'),
|
||||
"last_name": request.data.get('last_name'),
|
||||
"image":image_url},
|
||||
data=data)
|
||||
if serializer.is_valid():
|
||||
"""save serializer"""
|
||||
serializer.save()
|
||||
return custom_response(None, serializer.data,response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
data = request.data
|
||||
image = request.data.get('image')
|
||||
image_url = ''
|
||||
if image:
|
||||
if image and image.size == NUMBER['zero']:
|
||||
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
filename = f"images/{image.name}"
|
||||
# upload image on ali baba
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
data = {"image":image_url}
|
||||
serializer = CreateGuardianSerializer(context={"user":request.user,
|
||||
"first_name":request.data.get('first_name'),
|
||||
"last_name": request.data.get('last_name'),
|
||||
"image":image_url},
|
||||
data=data)
|
||||
if serializer.is_valid():
|
||||
"""save serializer"""
|
||||
serializer.save()
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class AllTaskListAPIView(viewsets.ModelViewSet):
|
||||
@ -83,6 +122,7 @@ class AllTaskListAPIView(viewsets.ModelViewSet):
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""Create guardian profile"""
|
||||
queryset = JuniorTask.objects.filter(guardian__user=request.user)
|
||||
# use TaskDetailsSerializer serializer
|
||||
serializer = TaskDetailsSerializer(queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
|
||||
@ -92,66 +132,97 @@ class TaskListAPIView(viewsets.ModelViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
pagination_class = PageNumberPagination
|
||||
queryset = JuniorTask.objects.all()
|
||||
http_method_names = ('get',)
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""Create guardian profile"""
|
||||
status_value = self.request.GET.get('status')
|
||||
if str(status_value) == '0':
|
||||
queryset = JuniorTask.objects.filter(guardian__user=request.user).order_by('due_date', 'created_at')
|
||||
else:
|
||||
queryset = JuniorTask.objects.filter(guardian__user=request.user,
|
||||
task_status=status_value).order_by('due_date','created_at')
|
||||
paginator = self.pagination_class()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
try:
|
||||
status_value = self.request.GET.get('status')
|
||||
search = self.request.GET.get('search')
|
||||
if search and str(status_value) == '0':
|
||||
queryset = JuniorTask.objects.filter(guardian__user=request.user,
|
||||
task_name__icontains=search).order_by('due_date', 'created_at')
|
||||
elif search and str(status_value) != '0':
|
||||
queryset = JuniorTask.objects.filter(guardian__user=request.user,task_name__icontains=search,
|
||||
task_status=status_value).order_by('due_date', 'created_at')
|
||||
if search is None and str(status_value) == '0':
|
||||
queryset = JuniorTask.objects.filter(guardian__user=request.user).order_by('due_date', 'created_at')
|
||||
elif search is None and str(status_value) != '0':
|
||||
queryset = JuniorTask.objects.filter(guardian__user=request.user,
|
||||
task_status=status_value).order_by('due_date','created_at')
|
||||
paginator = self.pagination_class()
|
||||
# use Pagination
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
# use TaskDetailsSerializer serializer
|
||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class CreateTaskAPIView(viewsets.ModelViewSet):
|
||||
"""create task for junior"""
|
||||
serializer_class = TaskSerializer
|
||||
queryset = JuniorTask.objects.all()
|
||||
http_method_names = ('post', )
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
image = request.data['default_image']
|
||||
data = request.data
|
||||
if 'https' in str(image):
|
||||
image_data = image
|
||||
else:
|
||||
filename = f"images/{image}"
|
||||
if image and image.size == NUMBER['zero']:
|
||||
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
image_data = image_url
|
||||
data.pop('default_image')
|
||||
serializer = TaskSerializer(context={"user":request.user, "image":image_data}, data=data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
image = request.data['default_image']
|
||||
junior = request.data['junior']
|
||||
allowed_extensions = ['.jpg', '.jpeg', '.png']
|
||||
if not any(extension in str(image) for extension in allowed_extensions):
|
||||
return custom_error_response(ERROR_CODE['2048'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
if not junior.isnumeric():
|
||||
"""junior value must be integer"""
|
||||
return custom_error_response(ERROR_CODE['2047'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
data = request.data
|
||||
if 'https' in str(image):
|
||||
image_data = image
|
||||
else:
|
||||
filename = f"images/{image}"
|
||||
if image and image.size == NUMBER['zero']:
|
||||
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
image_data = image_url
|
||||
data.pop('default_image')
|
||||
# use TaskSerializer serializer
|
||||
serializer = TaskSerializer(context={"user":request.user, "image":image_data}, data=data)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
serializer.save()
|
||||
junior_id = Junior.objects.filter(id=junior).last()
|
||||
send_notification.delay(TASK_CREATED, None, junior_id.auth.id, {})
|
||||
return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class SearchTaskListAPIView(viewsets.ModelViewSet):
|
||||
"""Update guardian profile"""
|
||||
serializer_class = TaskDetailsSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
pagination_class = PageNumberPagination
|
||||
queryset = JuniorTask.objects.all()
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get the queryset for the view"""
|
||||
title = self.request.GET.get('title')
|
||||
# fetch junior query
|
||||
junior_queryset = JuniorTask.objects.filter(guardian__user=self.request.user, task_name__icontains=title)\
|
||||
.order_by('due_date', 'created_at')
|
||||
return junior_queryset
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""Create guardian profile"""
|
||||
queryset = self.get_queryset()
|
||||
|
||||
paginator = self.pagination_class()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
|
||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
try:
|
||||
queryset = self.get_queryset()
|
||||
paginator = self.pagination_class()
|
||||
# use pagination
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
# use TaskSerializer serializer
|
||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class TopJuniorListAPIView(viewsets.ModelViewSet):
|
||||
@ -161,20 +232,97 @@ class TopJuniorListAPIView(viewsets.ModelViewSet):
|
||||
queryset = JuniorPoints.objects.all()
|
||||
|
||||
def get_serializer_context(self):
|
||||
# context list
|
||||
context = super().get_serializer_context()
|
||||
context.update({'view': self})
|
||||
return context
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""Fetch junior list of those who complete their tasks"""
|
||||
junior_total_points = self.get_queryset().order_by('-total_task_points')
|
||||
try:
|
||||
junior_total_points = self.get_queryset().order_by('-total_points')
|
||||
|
||||
# Update the position field for each JuniorPoints object
|
||||
for index, junior in enumerate(junior_total_points):
|
||||
junior.position = index + 1
|
||||
junior.save()
|
||||
# Update the position field for each JuniorPoints object
|
||||
for index, junior in enumerate(junior_total_points):
|
||||
junior.position = index + 1
|
||||
send_notification.delay(LEADERBOARD_RANKING, None, junior.junior.auth.id, {})
|
||||
junior.save()
|
||||
|
||||
serializer = self.get_serializer(junior_total_points, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
serializer = self.get_serializer(junior_total_points, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class ApproveJuniorAPIView(viewsets.ViewSet):
|
||||
"""approve junior by guardian"""
|
||||
serializer_class = ApproveJuniorSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get the queryset for the view"""
|
||||
guardian = Guardian.objects.filter(user__email=self.request.user).last()
|
||||
# fetch junior query
|
||||
junior_queryset = Junior.objects.filter(id=self.request.data.get('junior_id')).last()
|
||||
return guardian, junior_queryset
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
""" junior list"""
|
||||
try:
|
||||
queryset = self.get_queryset()
|
||||
# action 1 is use for approve and 2 for reject
|
||||
if request.data['action'] == '1':
|
||||
# use ApproveJuniorSerializer serializer
|
||||
serializer = ApproveJuniorSerializer(context={"guardian_code": queryset[0].guardian_code,
|
||||
"junior": queryset[1], "action": request.data['action']},
|
||||
data=request.data)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3023'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_response(SUCCESS_CODE['3024'], response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class ApproveTaskAPIView(viewsets.ViewSet):
|
||||
"""approve junior by guardian"""
|
||||
serializer_class = ApproveTaskSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get the queryset for the view"""
|
||||
guardian = Guardian.objects.filter(user__email=self.request.user).last()
|
||||
# task query
|
||||
task_queryset = JuniorTask.objects.filter(id=self.request.data.get('task_id'),
|
||||
guardian=guardian,
|
||||
junior=self.request.data.get('junior_id')).last()
|
||||
return guardian, task_queryset
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
""" junior list"""
|
||||
# action 1 is use for approve and 2 for reject
|
||||
try:
|
||||
queryset = self.get_queryset()
|
||||
# use ApproveJuniorSerializer serializer
|
||||
serializer = ApproveTaskSerializer(context={"guardian_code": queryset[0].guardian_code,
|
||||
"task_instance": queryset[1],
|
||||
"action": str(request.data['action']),
|
||||
"junior": self.request.data['junior_id']},
|
||||
data=request.data)
|
||||
unexpected_task_status = [str(NUMBER['five']), str(NUMBER['six'])]
|
||||
if (str(request.data['action']) == str(NUMBER['one']) and serializer.is_valid()
|
||||
and queryset[1] and queryset[1].task_status not in unexpected_task_status):
|
||||
# save serializer
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3025'], response_status=status.HTTP_200_OK)
|
||||
elif (str(request.data['action']) == str(NUMBER['two']) and serializer.is_valid()
|
||||
and queryset[1] and queryset[1].task_status not in unexpected_task_status):
|
||||
# save serializer
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3026'], response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_response(ERROR_CODE['2038'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
"""Third party Django app"""
|
||||
from django.contrib import admin
|
||||
"""Import Django app"""
|
||||
from .models import Junior, JuniorPoints
|
||||
from .models import Junior, JuniorPoints, JuniorGuardianRelationship
|
||||
# Register your models here.
|
||||
@admin.register(Junior)
|
||||
class JuniorAdmin(admin.ModelAdmin):
|
||||
@ -16,8 +16,14 @@ class JuniorAdmin(admin.ModelAdmin):
|
||||
@admin.register(JuniorPoints)
|
||||
class JuniorPointsAdmin(admin.ModelAdmin):
|
||||
"""Junior Points Admin"""
|
||||
list_display = ['junior', 'total_task_points', 'position']
|
||||
list_display = ['junior', 'total_points', 'position']
|
||||
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
return self.junior.auth.email
|
||||
|
||||
@admin.register(JuniorGuardianRelationship)
|
||||
class JuniorGuardianRelationshipAdmin(admin.ModelAdmin):
|
||||
"""Junior Admin"""
|
||||
list_display = ['guardian', 'junior', 'relationship']
|
||||
|
||||
|
@ -0,0 +1,21 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-14 09:34
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('junior', '0012_junior_is_invited'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='junior',
|
||||
options={'verbose_name': 'Junior', 'verbose_name_plural': 'Junior'},
|
||||
),
|
||||
migrations.AlterModelOptions(
|
||||
name='juniorpoints',
|
||||
options={'verbose_name': 'Junior Task Points', 'verbose_name_plural': 'Junior Task Points'},
|
||||
),
|
||||
]
|
18
junior/migrations/0014_junior_is_password_set.py
Normal file
18
junior/migrations/0014_junior_is_password_set.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-18 09:28
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('junior', '0013_alter_junior_options_alter_juniorpoints_options'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='junior',
|
||||
name='is_password_set',
|
||||
field=models.BooleanField(default=True),
|
||||
),
|
||||
]
|
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-19 09:40
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('junior', '0014_junior_is_password_set'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='juniorpoints',
|
||||
old_name='total_task_points',
|
||||
new_name='total_points',
|
||||
),
|
||||
]
|
18
junior/migrations/0016_juniorpoints_referral_points.py
Normal file
18
junior/migrations/0016_juniorpoints_referral_points.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-19 11:00
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('junior', '0015_rename_total_task_points_juniorpoints_total_points'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='juniorpoints',
|
||||
name='referral_points',
|
||||
field=models.IntegerField(blank=True, default=0, null=True),
|
||||
),
|
||||
]
|
31
junior/migrations/0017_juniorguardianrelationship.py
Normal file
31
junior/migrations/0017_juniorguardianrelationship.py
Normal file
@ -0,0 +1,31 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-25 07:44
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guardian', '0020_alter_juniortask_task_status'),
|
||||
('junior', '0016_juniorpoints_referral_points'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='JuniorGuardianRelationship',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('relationship', models.CharField(blank=True, choices=[('1', 'parent'), ('2', 'legal_guardian')], default='1', max_length=31, null=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('guardian', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='guardian_relation', to='guardian.guardian', verbose_name='Guardian')),
|
||||
('junior', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='junior_relation', to='junior.junior', verbose_name='Junior')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Junior Guardian Relation',
|
||||
'verbose_name_plural': 'Junior Guardian Relation',
|
||||
'db_table': 'junior_guardian_relation',
|
||||
},
|
||||
),
|
||||
]
|
@ -1,42 +1,77 @@
|
||||
"""Junior model """
|
||||
"""Import django"""
|
||||
from django.db import models
|
||||
"""Import get_user_model function"""
|
||||
from django.contrib.auth import get_user_model
|
||||
"""Import ArrayField"""
|
||||
from django.contrib.postgres.fields import ArrayField
|
||||
"""Import django app"""
|
||||
from base.constants import GENDERS, SIGNUP_METHODS, RELATIONSHIP
|
||||
# Import guardian's model
|
||||
from guardian.models import Guardian
|
||||
|
||||
"""Define User model"""
|
||||
User = get_user_model()
|
||||
# Create your models here.
|
||||
|
||||
# Define junior model with
|
||||
# various fields like
|
||||
# phone, country code,
|
||||
# country name,
|
||||
# gender,
|
||||
# date of birth,
|
||||
# profile image,
|
||||
# relationship type of the guardian
|
||||
# signup method,
|
||||
# guardian code,
|
||||
# junior code,
|
||||
# referral code,
|
||||
# referral code that used by the junior
|
||||
# is invited junior
|
||||
# profile is active or not
|
||||
# profile is complete or not
|
||||
# passcode
|
||||
# junior is verified or not
|
||||
"""Define junior points model"""
|
||||
# points of the junior
|
||||
# position of the junior
|
||||
# define junior guardian relation model
|
||||
class Junior(models.Model):
|
||||
"""Junior model"""
|
||||
auth = models.ForeignKey(User, on_delete=models.CASCADE, related_name='junior_profile', verbose_name='Email')
|
||||
"""Contact details"""
|
||||
# Contact details"""
|
||||
phone = models.CharField(max_length=31, null=True, blank=True, default=None)
|
||||
country_code = models.IntegerField(blank=True, null=True)
|
||||
# country name of the guardian"""
|
||||
country_name = models.CharField(max_length=100, null=True, blank=True, default=None)
|
||||
"""Personal info"""
|
||||
# Personal info"""
|
||||
gender = models.CharField(max_length=10, choices=GENDERS, null=True, blank=True, default=None)
|
||||
# Date of birth"""
|
||||
dob = models.DateField(max_length=15, null=True, blank=True, default=None)
|
||||
# Image of the junior"""
|
||||
image = models.URLField(null=True, blank=True, default=None)
|
||||
"""relationship"""
|
||||
relationship = models.CharField(max_length=31, choices=RELATIONSHIP, null=True, blank=True,
|
||||
default='1')
|
||||
"""Sign up method"""
|
||||
# Sign up method"""
|
||||
signup_method = models.CharField(max_length=31, choices=SIGNUP_METHODS, default='1')
|
||||
"""Codes"""
|
||||
# Codes"""
|
||||
junior_code = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
# Guardian Codes"""
|
||||
guardian_code = ArrayField(models.CharField(max_length=10, null=True, blank=True, default=None),null=True)
|
||||
# Referral code"""
|
||||
referral_code = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
# Referral code that is used by junior while signup"""
|
||||
referral_code_used = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
"""invited junior"""
|
||||
# invited junior"""
|
||||
is_invited = models.BooleanField(default=False)
|
||||
"""Profile activity"""
|
||||
# Profile activity"""
|
||||
is_active = models.BooleanField(default=True)
|
||||
# check password is set or not
|
||||
is_password_set = models.BooleanField(default=True)
|
||||
# junior profile is complete or not"""
|
||||
is_complete_profile = models.BooleanField(default=False)
|
||||
# passcode of the junior profile"""
|
||||
passcode = models.IntegerField(null=True, blank=True, default=None)
|
||||
# junior is verified or not"""
|
||||
is_verified = models.BooleanField(default=False)
|
||||
"""Profile created and updated time"""
|
||||
# Profile created and updated time"""
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
@ -44,6 +79,8 @@ class Junior(models.Model):
|
||||
""" Meta class """
|
||||
db_table = 'junior'
|
||||
verbose_name = 'Junior'
|
||||
# another name of the model"""
|
||||
verbose_name_plural = 'Junior'
|
||||
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
@ -52,11 +89,13 @@ class Junior(models.Model):
|
||||
class JuniorPoints(models.Model):
|
||||
"""Junior model"""
|
||||
junior = models.OneToOneField(Junior, on_delete=models.CASCADE, related_name='junior_points')
|
||||
"""Contact details"""
|
||||
total_task_points = models.IntegerField(blank=True, null=True, default=0)
|
||||
"""position of the junior"""
|
||||
# Total earned points"""
|
||||
total_points = models.IntegerField(blank=True, null=True, default=0)
|
||||
# referral points"""
|
||||
referral_points = models.IntegerField(blank=True, null=True, default=0)
|
||||
# position of the junior"""
|
||||
position = models.IntegerField(blank=True, null=True, default=99999)
|
||||
"""Profile created and updated time"""
|
||||
# Profile created and updated time"""
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
@ -64,7 +103,35 @@ class JuniorPoints(models.Model):
|
||||
""" Meta class """
|
||||
db_table = 'junior_task_points'
|
||||
verbose_name = 'Junior Task Points'
|
||||
# another name of the model"""
|
||||
verbose_name_plural = 'Junior Task Points'
|
||||
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
return f'{self.junior.auth}'
|
||||
|
||||
class JuniorGuardianRelationship(models.Model):
|
||||
"""Junior Guardian relationship model"""
|
||||
guardian = models.ForeignKey(Guardian, on_delete=models.CASCADE, related_name='guardian_relation',
|
||||
verbose_name='Guardian')
|
||||
# associated junior with the task
|
||||
junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='junior_relation', verbose_name='Junior')
|
||||
# relation between guardian and junior"""
|
||||
relationship = models.CharField(max_length=31, choices=RELATIONSHIP, null=True, blank=True,
|
||||
default='1')
|
||||
"""Profile created and updated time"""
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta(object):
|
||||
""" Meta class """
|
||||
db_table = 'junior_guardian_relation'
|
||||
"""verbose name of the model"""
|
||||
verbose_name = 'Junior Guardian Relation'
|
||||
verbose_name_plural = 'Junior Guardian Relation'
|
||||
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
return f'{self.guardian.user}'
|
||||
|
||||
|
||||
|
@ -3,19 +3,23 @@
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import transaction
|
||||
import random
|
||||
from datetime import datetime
|
||||
from django.utils import timezone
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
|
||||
"""Import django app"""
|
||||
from account.utils import send_otp_email, generate_alphanumeric_code
|
||||
from junior.models import Junior, JuniorPoints
|
||||
from account.utils import send_otp_email, generate_code
|
||||
from junior.models import Junior, JuniorPoints, JuniorGuardianRelationship
|
||||
from guardian.tasks import generate_otp
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED, NUMBER
|
||||
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED, NUMBER, JUN, ZOD
|
||||
from guardian.models import Guardian, JuniorTask
|
||||
from account.models import UserEmailOtp
|
||||
from account.models import UserEmailOtp, UserNotification
|
||||
from junior.utils import junior_notification_email, junior_approval_mail
|
||||
from guardian.utils import real_time, update_referral_points, convert_timedelta_into_datetime
|
||||
from notifications.utils import send_notification
|
||||
from notifications.constants import (INVITED_GUARDIAN, APPROVED_JUNIOR, SKIPPED_PROFILE_SETUP, TASK_ACTION,
|
||||
TASK_SUBMITTED)
|
||||
|
||||
|
||||
class ListCharField(serializers.ListField):
|
||||
@ -91,7 +95,6 @@ class CreateJuniorSerializer(serializers.ModelSerializer):
|
||||
"""Update country code and phone number"""
|
||||
junior.phone = validated_data.get('phone', junior.phone)
|
||||
junior.country_code = validated_data.get('country_code', junior.country_code)
|
||||
junior.referral_code_used = validated_data.get('referral_code_used', junior.referral_code_used)
|
||||
junior.image = validated_data.get('image', junior.image)
|
||||
"""Complete profile of the junior if below all data are filled"""
|
||||
complete_profile_field = all([junior.gender, junior.country_name, junior.image,
|
||||
@ -99,6 +102,9 @@ class CreateJuniorSerializer(serializers.ModelSerializer):
|
||||
junior.is_complete_profile = False
|
||||
if complete_profile_field:
|
||||
junior.is_complete_profile = True
|
||||
referral_code_used = validated_data.get('referral_code_used')
|
||||
update_referral_points(junior.referral_code, referral_code_used)
|
||||
junior.referral_code_used = validated_data.get('referral_code_used', junior.referral_code_used)
|
||||
junior.save()
|
||||
return junior
|
||||
|
||||
@ -244,18 +250,11 @@ class JuniorProfileSerializer(serializers.ModelSerializer):
|
||||
|
||||
class AddJuniorSerializer(serializers.ModelSerializer):
|
||||
"""Add junior serializer"""
|
||||
auth_token = serializers.SerializerMethodField('get_auth_token')
|
||||
|
||||
def get_auth_token(self, obj):
|
||||
"""auth token"""
|
||||
refresh = RefreshToken.for_user(obj)
|
||||
access_token = str(refresh.access_token)
|
||||
return access_token
|
||||
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Junior
|
||||
fields = ['id', 'gender','dob', 'relationship', 'auth_token', 'is_invited']
|
||||
fields = ['id', 'gender','dob', 'is_invited']
|
||||
|
||||
|
||||
def create(self, validated_data):
|
||||
@ -263,31 +262,36 @@ class AddJuniorSerializer(serializers.ModelSerializer):
|
||||
with transaction.atomic():
|
||||
email = self.context['email']
|
||||
guardian = self.context['user']
|
||||
relationship = self.context['relationship']
|
||||
full_name = self.context['first_name'] + ' ' + self.context['last_name']
|
||||
guardian_data = Guardian.objects.filter(user__username=guardian).last()
|
||||
user_data = User.objects.create(username=email, email=email,
|
||||
first_name=self.context['first_name'],
|
||||
last_name=self.context['last_name'])
|
||||
|
||||
password = User.objects.make_random_password()
|
||||
user_data.set_password(password)
|
||||
user_data.save()
|
||||
junior_data = Junior.objects.create(auth=user_data, gender=validated_data.get('gender'),
|
||||
dob=validated_data.get('dob'), is_invited=True,
|
||||
relationship=validated_data.get('relationship'),
|
||||
junior_code=generate_alphanumeric_code(6),
|
||||
referral_code=generate_alphanumeric_code(6),
|
||||
referral_code_used=guardian_data.referral_code)
|
||||
guardian_code=[guardian_data.guardian_code],
|
||||
junior_code=generate_code(JUN, user_data.id),
|
||||
referral_code=generate_code(ZOD, user_data.id),
|
||||
referral_code_used=guardian_data.referral_code,
|
||||
is_password_set=False, is_verified=True)
|
||||
JuniorGuardianRelationship.objects.create(guardian=guardian_data, junior=junior_data,
|
||||
relationship=relationship)
|
||||
"""Generate otp"""
|
||||
otp_value = generate_otp()
|
||||
expiry_time = timezone.now() + timezone.timedelta(days=1)
|
||||
UserEmailOtp.objects.create(email=email, otp=otp_value,
|
||||
user_type='1', expired_at=expiry_time)
|
||||
"""Send email to the register user"""
|
||||
send_otp_email(email, otp_value)
|
||||
user_type='1', expired_at=expiry_time, is_verified=True)
|
||||
# add push notification
|
||||
UserNotification.objects.get_or_create(user=user_data)
|
||||
"""Notification email"""
|
||||
junior_notification_email(email, full_name, email, password)
|
||||
junior_approval_mail(guardian, full_name)
|
||||
# push notification
|
||||
send_notification.delay(SKIPPED_PROFILE_SETUP, None, junior_data.auth.id, {})
|
||||
return junior_data
|
||||
|
||||
|
||||
@ -305,3 +309,148 @@ class RemoveJuniorSerializer(serializers.ModelSerializer):
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
|
||||
class CompleteTaskSerializer(serializers.ModelSerializer):
|
||||
"""User task Serializer"""
|
||||
class Meta(object):
|
||||
"""Meta class"""
|
||||
model = JuniorTask
|
||||
fields = ('id', 'image')
|
||||
def update(self, instance, validated_data):
|
||||
instance.image = validated_data.get('image', instance.image)
|
||||
instance.requested_on = real_time()
|
||||
instance.task_status = str(NUMBER['four'])
|
||||
instance.is_approved = False
|
||||
instance.save()
|
||||
send_notification.delay(TASK_SUBMITTED, None, instance.junior.auth.id, {})
|
||||
send_notification.delay(TASK_ACTION, None, instance.guardian.user.id, {})
|
||||
return instance
|
||||
|
||||
class JuniorPointsSerializer(serializers.ModelSerializer):
|
||||
"""Junior points serializer"""
|
||||
junior_id = serializers.SerializerMethodField('get_junior_id')
|
||||
total_points = serializers.SerializerMethodField('get_points')
|
||||
in_progress_task = serializers.SerializerMethodField('get_in_progress_task')
|
||||
completed_task = serializers.SerializerMethodField('get_completed_task')
|
||||
requested_task = serializers.SerializerMethodField('get_requested_task')
|
||||
rejected_task = serializers.SerializerMethodField('get_rejected_task')
|
||||
pending_task = serializers.SerializerMethodField('get_pending_task')
|
||||
position = serializers.SerializerMethodField('get_position')
|
||||
|
||||
def get_junior_id(self, obj):
|
||||
"""junior id"""
|
||||
return obj.junior.id
|
||||
|
||||
def get_position(self, obj):
|
||||
data = JuniorPoints.objects.filter(junior=obj.junior).last()
|
||||
if data:
|
||||
return data.position
|
||||
return 99999
|
||||
def get_points(self, obj):
|
||||
"""total points"""
|
||||
points = JuniorPoints.objects.filter(junior=obj.junior).last()
|
||||
if points:
|
||||
return points.total_points
|
||||
|
||||
def get_in_progress_task(self, obj):
|
||||
return JuniorTask.objects.filter(junior=obj.junior, task_status=IN_PROGRESS).count()
|
||||
|
||||
def get_completed_task(self, obj):
|
||||
return JuniorTask.objects.filter(junior=obj.junior, task_status=COMPLETED).count()
|
||||
|
||||
|
||||
def get_requested_task(self, obj):
|
||||
return JuniorTask.objects.filter(junior=obj.junior, task_status=REQUESTED).count()
|
||||
|
||||
|
||||
def get_rejected_task(self, obj):
|
||||
return JuniorTask.objects.filter(junior=obj.junior, task_status=REJECTED).count()
|
||||
|
||||
|
||||
def get_pending_task(self, obj):
|
||||
return JuniorTask.objects.filter(junior=obj.junior, task_status=PENDING).count()
|
||||
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Junior
|
||||
fields = ['junior_id', 'total_points', 'position', 'pending_task', 'in_progress_task', 'completed_task',
|
||||
'requested_task', 'rejected_task']
|
||||
|
||||
class AddGuardianSerializer(serializers.ModelSerializer):
|
||||
"""Add guardian serializer"""
|
||||
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Guardian
|
||||
fields = ['id']
|
||||
|
||||
|
||||
def create(self, validated_data):
|
||||
""" invite and create guardian"""
|
||||
with transaction.atomic():
|
||||
email = self.context['email']
|
||||
junior = self.context['user']
|
||||
relationship = self.context['relationship']
|
||||
full_name = self.context['first_name'] + ' ' + self.context['last_name']
|
||||
junior_data = Junior.objects.filter(auth__username=junior).last()
|
||||
instance = User.objects.filter(username=email).last()
|
||||
if instance:
|
||||
guardian_data = Guardian.objects.filter(user=instance).update(is_invited=True,
|
||||
referral_code=generate_code(ZOD,
|
||||
instance.id),
|
||||
referral_code_used=junior_data.referral_code,
|
||||
is_verified=True)
|
||||
UserNotification.objects.get_or_create(user=instance)
|
||||
return guardian_data
|
||||
else:
|
||||
user = User.objects.create(username=email, email=email,
|
||||
first_name=self.context['first_name'],
|
||||
last_name=self.context['last_name'])
|
||||
|
||||
password = User.objects.make_random_password()
|
||||
user.set_password(password)
|
||||
user.save()
|
||||
guardian_data = Guardian.objects.create(user=user, is_invited=True,
|
||||
referral_code=generate_code(ZOD, user.id),
|
||||
referral_code_used=junior_data.referral_code,
|
||||
is_password_set=False, is_verified=True)
|
||||
"""Generate otp"""
|
||||
otp_value = generate_otp()
|
||||
expiry_time = timezone.now() + timezone.timedelta(days=1)
|
||||
UserEmailOtp.objects.create(email=email, otp=otp_value,
|
||||
user_type=str(NUMBER['two']), expired_at=expiry_time,
|
||||
is_verified=True)
|
||||
UserNotification.objects.get_or_create(user=user)
|
||||
JuniorGuardianRelationship.objects.create(guardian=guardian_data, junior=junior_data,
|
||||
relationship=relationship)
|
||||
|
||||
"""Notification email"""
|
||||
junior_notification_email(email, full_name, email, password)
|
||||
junior_approval_mail(email, full_name)
|
||||
send_notification.delay(INVITED_GUARDIAN, None, junior_data.auth.id, {})
|
||||
send_notification.delay(APPROVED_JUNIOR, None, guardian_data.user.id, {})
|
||||
return guardian_data
|
||||
|
||||
class StartTaskSerializer(serializers.ModelSerializer):
|
||||
"""User task Serializer"""
|
||||
task_duration = serializers.SerializerMethodField('get_task_duration')
|
||||
|
||||
def get_task_duration(self, obj):
|
||||
""" remaining time to complete task"""
|
||||
due_date = datetime.combine(obj.due_date, datetime.max.time())
|
||||
# fetch real time
|
||||
real_datetime = real_time()
|
||||
# Perform the subtraction
|
||||
if due_date > real_datetime:
|
||||
time_difference = due_date - real_datetime
|
||||
time_only = convert_timedelta_into_datetime(time_difference)
|
||||
return str(time_difference.days) + ' days ' + str(time_only)
|
||||
return str(NUMBER['zero']) + ' days ' + '00:00:00:00000'
|
||||
class Meta(object):
|
||||
"""Meta class"""
|
||||
model = JuniorTask
|
||||
fields = ('id', 'task_duration')
|
||||
def update(self, instance, validated_data):
|
||||
instance.task_status = str(NUMBER['two'])
|
||||
instance.save()
|
||||
return instance
|
||||
|
@ -2,28 +2,49 @@
|
||||
"""Django import"""
|
||||
from django.urls import path, include
|
||||
from .views import (UpdateJuniorProfile, ValidateGuardianCode, JuniorListAPIView, AddJuniorAPIView,
|
||||
InvitedJuniorAPIView, FilterJuniorAPIView, RemoveJuniorAPIView)
|
||||
InvitedJuniorAPIView, FilterJuniorAPIView, RemoveJuniorAPIView, JuniorTaskListAPIView,
|
||||
CompleteJuniorTaskAPIView, JuniorPointsListAPIView, ValidateReferralCode,
|
||||
InviteGuardianAPIView, StartTaskAPIView)
|
||||
"""Third party import"""
|
||||
from rest_framework import routers
|
||||
|
||||
"""Router"""
|
||||
router = routers.SimpleRouter()
|
||||
|
||||
# API End points with router
|
||||
# in this file
|
||||
# we define various api end point
|
||||
# that is covered in this guardian
|
||||
# section API:- like
|
||||
# Create junior profile API, validate junior profile,
|
||||
# junior list,
|
||||
# add junior list, invited junior,
|
||||
# filter-junior,
|
||||
# remove junior,
|
||||
# junior task list
|
||||
"""API End points with router"""
|
||||
"""Create junior profile API"""
|
||||
router.register('create-junior-profile', UpdateJuniorProfile, basename='profile-update')
|
||||
"""validate guardian code API"""
|
||||
# validate guardian code API"""
|
||||
router.register('validate-guardian-code', ValidateGuardianCode, basename='validate-guardian-code')
|
||||
"""junior list API"""
|
||||
# junior list API"""
|
||||
router.register('junior-list', JuniorListAPIView, basename='junior-list')
|
||||
"""Add junior list API"""
|
||||
# Add junior list API"""
|
||||
router.register('add-junior', AddJuniorAPIView, basename='add-junior')
|
||||
"""Invited junior list API"""
|
||||
# Invited junior list API"""
|
||||
router.register('invited-junior', InvitedJuniorAPIView, basename='invited-junior')
|
||||
"""Filter junior list API"""
|
||||
# Filter junior list API"""
|
||||
router.register('filter-junior', FilterJuniorAPIView, basename='filter-junior')
|
||||
"""Define url pattern"""
|
||||
# junior's task list API"""
|
||||
router.register('junior-task-list', JuniorTaskListAPIView, basename='junior-task-list')
|
||||
# junior's task list API"""
|
||||
router.register('junior-points', JuniorPointsListAPIView, basename='junior-points')
|
||||
# validate referral code API"""
|
||||
router.register('validate-referral-code', ValidateReferralCode, basename='validate-referral-code')
|
||||
# invite guardian API"""
|
||||
router.register('invite-guardian', InviteGuardianAPIView, basename='invite-guardian')
|
||||
# Define url pattern"""
|
||||
urlpatterns = [
|
||||
path('api/v1/', include(router.urls)),
|
||||
path('api/v1/remove-junior/', RemoveJuniorAPIView.as_view())
|
||||
path('api/v1/remove-junior/', RemoveJuniorAPIView.as_view()),
|
||||
path('api/v1/complete-task/', CompleteJuniorTaskAPIView.as_view()),
|
||||
path('api/v1/start-task/', StartTaskAPIView.as_view())
|
||||
]
|
||||
|
@ -1,28 +1,24 @@
|
||||
"""Account utils"""
|
||||
"""Import django"""
|
||||
from django.conf import settings
|
||||
from rest_framework import viewsets, status
|
||||
from rest_framework.response import Response
|
||||
"""Third party Django app"""
|
||||
from templated_email import send_templated_mail
|
||||
import jwt
|
||||
import string
|
||||
from datetime import datetime
|
||||
from calendar import timegm
|
||||
from uuid import uuid4
|
||||
import secrets
|
||||
from rest_framework import serializers
|
||||
from junior.models import Junior
|
||||
from guardian.models import Guardian
|
||||
from account.models import UserDelete
|
||||
from base.messages import ERROR_CODE
|
||||
|
||||
|
||||
|
||||
from .models import JuniorPoints
|
||||
from django.db.models import F
|
||||
# junior notification
|
||||
# email for sending email
|
||||
# when guardian create junior profile
|
||||
# guardian get email when junior send
|
||||
# request for approving the profile and
|
||||
# being part of the zod bank and access the platform
|
||||
# define junior notification email
|
||||
# junior approval email
|
||||
def junior_notification_email(recipient_email, full_name, email, password):
|
||||
"""Notification email"""
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
# recipient email"""
|
||||
recipient_list = [recipient_email]
|
||||
# use send template mail for sending email"""
|
||||
send_templated_mail(
|
||||
template_name='junior_notification_email.email',
|
||||
from_email=from_email,
|
||||
@ -40,6 +36,7 @@ def junior_approval_mail(guardian, full_name):
|
||||
"""junior approval mail"""
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [guardian]
|
||||
# use send template mail for sending email"""
|
||||
send_templated_mail(
|
||||
template_name='junior_approval_mail.email',
|
||||
from_email=from_email,
|
||||
@ -49,3 +46,15 @@ def junior_approval_mail(guardian, full_name):
|
||||
}
|
||||
)
|
||||
return full_name
|
||||
|
||||
def update_positions_based_on_points():
|
||||
"""Update position of the junior"""
|
||||
# First, retrieve all the JuniorPoints instances ordered by total_points in descending order.
|
||||
juniors_points = JuniorPoints.objects.order_by('-total_points')
|
||||
|
||||
# Now, iterate through the queryset and update the position field based on the order.
|
||||
position = 1
|
||||
for junior_point in juniors_points:
|
||||
junior_point.position = position
|
||||
junior_point.save()
|
||||
position += 1
|
||||
|
393
junior/views.py
393
junior/views.py
@ -1,18 +1,59 @@
|
||||
"""Junior view file"""
|
||||
import os
|
||||
|
||||
from rest_framework import viewsets, status, generics,views
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.response import Response
|
||||
from PIL import Image
|
||||
from django.contrib.auth.models import User
|
||||
import datetime
|
||||
import requests
|
||||
"""Django app import"""
|
||||
from junior.models import Junior
|
||||
|
||||
# Import guardian's model,
|
||||
# Import junior's model,
|
||||
# Import account's model,
|
||||
# Import constant from
|
||||
# base package,
|
||||
# Import messages from
|
||||
# base package,
|
||||
# Import some functions
|
||||
# from utils file
|
||||
# Import account's serializer
|
||||
# Import account's task
|
||||
# import junior serializer
|
||||
# Import update_positions_based_on_points
|
||||
# Import upload_image_to_alibaba
|
||||
# Import custom_response, custom_error_response
|
||||
# Import constants
|
||||
from junior.models import Junior, JuniorPoints
|
||||
from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer,\
|
||||
RemoveJuniorSerializer)
|
||||
from guardian.models import Guardian
|
||||
RemoveJuniorSerializer, CompleteTaskSerializer, JuniorPointsSerializer,
|
||||
AddGuardianSerializer, StartTaskSerializer)
|
||||
from guardian.models import Guardian, JuniorTask
|
||||
from guardian.serializers import TaskDetailsSerializer
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import NUMBER
|
||||
from account.utils import custom_response, custom_error_response
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from .utils import update_positions_based_on_points
|
||||
from notifications.utils import send_notification
|
||||
from notifications.constants import REMOVE_JUNIOR
|
||||
|
||||
""" Define APIs """
|
||||
# Define validate guardian code API,
|
||||
# update junior profile,
|
||||
# list of all assosicated junior
|
||||
# Add junior API
|
||||
# invite junior API
|
||||
# search junior API
|
||||
# remove junior API,
|
||||
# approve junior API
|
||||
# create referral code
|
||||
# validation API
|
||||
# invite guardian API
|
||||
# by junior
|
||||
# Start task
|
||||
# by junior API
|
||||
# Create your views here.
|
||||
class UpdateJuniorProfile(viewsets.ViewSet):
|
||||
"""Update junior profile"""
|
||||
@ -22,25 +63,31 @@ class UpdateJuniorProfile(viewsets.ViewSet):
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""Use CreateJuniorSerializer"""
|
||||
request_data = request.data
|
||||
image = request.data.get('image')
|
||||
image_url = ''
|
||||
if image:
|
||||
if image.size == NUMBER['zero']:
|
||||
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
filename = f"images/{image.name}"
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
request_data = {"image": image_url}
|
||||
serializer = CreateJuniorSerializer(context={"user":request.user, "image":image_url,
|
||||
"first_name": request.data.get('first_name'),
|
||||
"last_name": request.data.get('last_name')
|
||||
},
|
||||
data=request_data)
|
||||
if serializer.is_valid():
|
||||
"""save serializer"""
|
||||
serializer.save()
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
request_data = request.data
|
||||
image = request.data.get('image')
|
||||
image_url = ''
|
||||
if image:
|
||||
# check image size
|
||||
if image.size == NUMBER['zero']:
|
||||
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
# convert into file
|
||||
filename = f"images/{image.name}"
|
||||
# upload image on ali baba
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
request_data = {"image": image_url}
|
||||
serializer = CreateJuniorSerializer(context={"user":request.user, "image":image_url,
|
||||
"first_name": request.data.get('first_name'),
|
||||
"last_name": request.data.get('last_name')
|
||||
},
|
||||
data=request_data)
|
||||
if serializer.is_valid():
|
||||
"""save serializer"""
|
||||
serializer.save()
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class ValidateGuardianCode(viewsets.ViewSet):
|
||||
"""Check guardian code exist or not"""
|
||||
@ -49,40 +96,65 @@ class ValidateGuardianCode(viewsets.ViewSet):
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""check guardian code"""
|
||||
guardian_code = self.request.GET.get('guardian_code').split(',')
|
||||
for code in guardian_code:
|
||||
guardian_data = Guardian.objects.filter(guardian_code=code).exists()
|
||||
if guardian_data:
|
||||
return custom_response(SUCCESS_CODE['3013'], response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2022"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
guardian_code = self.request.GET.get('guardian_code').split(',')
|
||||
for code in guardian_code:
|
||||
# fetch guardian object
|
||||
guardian_data = Guardian.objects.filter(guardian_code=code).exists()
|
||||
if guardian_data:
|
||||
# successfully check guardian code
|
||||
return custom_response(SUCCESS_CODE['3013'], response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2022"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class JuniorListAPIView(viewsets.ModelViewSet):
|
||||
"""Junior list of assosicated guardian"""
|
||||
|
||||
serializer_class = JuniorDetailListSerializer
|
||||
queryset = Junior.objects.all()
|
||||
permission_classes = [IsAuthenticated]
|
||||
http_method_names = ('get',)
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" junior list"""
|
||||
guardian_data = Guardian.objects.filter(user__email=request.user).last()
|
||||
queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code))
|
||||
serializer = JuniorDetailListSerializer(queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
try:
|
||||
update_positions_based_on_points()
|
||||
guardian_data = Guardian.objects.filter(user__email=request.user).last()
|
||||
# fetch junior object
|
||||
if guardian_data:
|
||||
queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code))
|
||||
# use JuniorDetailListSerializer serializer
|
||||
serializer = JuniorDetailListSerializer(queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(ERROR_CODE['2045'], response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class AddJuniorAPIView(viewsets.ModelViewSet):
|
||||
"""Add Junior by guardian"""
|
||||
queryset = Junior.objects.all()
|
||||
serializer_class = AddJuniorSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
http_method_names = ('post',)
|
||||
def create(self, request, *args, **kwargs):
|
||||
""" junior list"""
|
||||
info = {'user': request.user, 'email': request.data['email'], 'first_name': request.data['first_name'],
|
||||
'last_name': request.data['last_name']}
|
||||
serializer = AddJuniorSerializer(data=request.data, context=info)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3021'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.error, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
info_data = {'user': request.user, 'relationship': str(request.data['relationship']), 'email': request.data['email'], 'first_name': request.data['first_name'],
|
||||
'last_name': request.data['last_name']}
|
||||
if User.objects.filter(username=request.data['email']):
|
||||
return custom_error_response(ERROR_CODE['2059'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# use AddJuniorSerializer serializer
|
||||
serializer = AddJuniorSerializer(data=request.data, context=info_data)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3021'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.error, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class InvitedJuniorAPIView(viewsets.ModelViewSet):
|
||||
"""Junior list of assosicated guardian"""
|
||||
@ -91,20 +163,26 @@ class InvitedJuniorAPIView(viewsets.ModelViewSet):
|
||||
queryset = Junior.objects.all()
|
||||
permission_classes = [IsAuthenticated]
|
||||
pagination_class = PageNumberPagination
|
||||
http_method_names = ('get',)
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get the queryset for the view"""
|
||||
guardian = Guardian.objects.filter(user__email=self.request.user).last()
|
||||
junior_queryset = Junior.objects.filter(guardian_code__icontains=str(guardian.guardian_code),
|
||||
is_invited=True)
|
||||
is_invited=True)
|
||||
return junior_queryset
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" junior list"""
|
||||
queryset = self.get_queryset()
|
||||
paginator = self.pagination_class()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
serializer = JuniorDetailListSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
try:
|
||||
queryset = self.get_queryset()
|
||||
paginator = self.pagination_class()
|
||||
# pagination
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
# use JuniorDetailListSerializer serializer
|
||||
serializer = JuniorDetailListSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class FilterJuniorAPIView(viewsets.ModelViewSet):
|
||||
@ -113,40 +191,227 @@ class FilterJuniorAPIView(viewsets.ModelViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
pagination_class = PageNumberPagination
|
||||
queryset = Junior.objects.all()
|
||||
http_method_names = ('get',)
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get the queryset for the view"""
|
||||
title = self.request.GET.get('title')
|
||||
guardian_data = Guardian.objects.filter(user__email=self.request.user).last()
|
||||
# fetch junior query
|
||||
queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code),
|
||||
is_invited=True, auth__first_name=title)
|
||||
return queryset
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""Create guardian profile"""
|
||||
queryset = self.get_queryset()
|
||||
paginator = self.pagination_class()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
serializer = JuniorDetailListSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
try:
|
||||
queryset = self.get_queryset()
|
||||
paginator = self.pagination_class()
|
||||
# use Pagination
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
# use JuniorDetailListSerializer serializer
|
||||
serializer = JuniorDetailListSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class RemoveJuniorAPIView(views.APIView):
|
||||
"""Eater Update API"""
|
||||
"""Remove junior API"""
|
||||
serializer_class = RemoveJuniorSerializer
|
||||
model = Junior
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def put(self, request, format=None):
|
||||
junior_id = self.request.GET.get('id')
|
||||
guardian = Guardian.objects.filter(user__email=self.request.user).last()
|
||||
junior_queryset = Junior.objects.filter(id=junior_id, guardian_code__icontains=str(guardian.guardian_code),
|
||||
is_invited=True).last()
|
||||
if junior_queryset:
|
||||
serializer = RemoveJuniorSerializer(junior_queryset, data=request.data, partial=True)
|
||||
try:
|
||||
junior_id = self.request.GET.get('id')
|
||||
guardian = Guardian.objects.filter(user__email=self.request.user).last()
|
||||
# fetch junior query
|
||||
junior_queryset = Junior.objects.filter(id=junior_id, guardian_code__icontains=str(guardian.guardian_code),
|
||||
is_invited=True).last()
|
||||
if junior_queryset:
|
||||
# use RemoveJuniorSerializer serializer
|
||||
serializer = RemoveJuniorSerializer(junior_queryset, data=request.data, partial=True)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
serializer.save()
|
||||
send_notification.delay(REMOVE_JUNIOR, None, junior_queryset.auth.id, {})
|
||||
return custom_response(SUCCESS_CODE['3022'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE['2034'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class JuniorTaskListAPIView(viewsets.ModelViewSet):
|
||||
"""Update guardian profile"""
|
||||
serializer_class = TaskDetailsSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
pagination_class = PageNumberPagination
|
||||
queryset = JuniorTask.objects.all()
|
||||
http_method_names = ('get',)
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""Create guardian profile"""
|
||||
try:
|
||||
status_value = self.request.GET.get('status')
|
||||
search = self.request.GET.get('search')
|
||||
if search and str(status_value) == '0':
|
||||
# search with title and for all task list
|
||||
queryset = JuniorTask.objects.filter(junior__auth=request.user,
|
||||
task_name__icontains=search).order_by('due_date', 'created_at')
|
||||
elif search and str(status_value) != '0':
|
||||
# search with title and fetch task list with status wise
|
||||
queryset = JuniorTask.objects.filter(junior__auth=request.user, task_name__icontains=search,
|
||||
task_status=status_value).order_by('due_date', 'created_at')
|
||||
if search is None and str(status_value) == '0':
|
||||
# fetch all task list
|
||||
queryset = JuniorTask.objects.filter(junior__auth=request.user).order_by('due_date', 'created_at')
|
||||
elif search is None and str(status_value) != '0':
|
||||
# fetch task list with status wise
|
||||
queryset = JuniorTask.objects.filter(junior__auth=request.user,
|
||||
task_status=status_value).order_by('due_date','created_at')
|
||||
paginator = self.pagination_class()
|
||||
# use Pagination
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
# use TaskDetailsSerializer serializer
|
||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class CompleteJuniorTaskAPIView(views.APIView):
|
||||
"""Update junior task API"""
|
||||
serializer_class = CompleteTaskSerializer
|
||||
model = JuniorTask
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def put(self, request, format=None):
|
||||
try:
|
||||
task_id = self.request.data.get('task_id')
|
||||
image = request.data['image']
|
||||
if image and image.size == NUMBER['zero']:
|
||||
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
# create file
|
||||
filename = f"images/{image.name}"
|
||||
# upload image
|
||||
filename = f"images/{image.name}"
|
||||
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
# fetch junior query
|
||||
task_queryset = JuniorTask.objects.filter(id=task_id, junior__auth__email=self.request.user).last()
|
||||
if task_queryset:
|
||||
# use CompleteTaskSerializer serializer
|
||||
if task_queryset.task_status in [str(NUMBER['four']), str(NUMBER['five'])]:
|
||||
"""Already request send """
|
||||
return custom_error_response(ERROR_CODE['2049'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
serializer = CompleteTaskSerializer(task_queryset, data={'image': image_url}, partial=True)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3032'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE['2044'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class JuniorPointsListAPIView(viewsets.ModelViewSet):
|
||||
"""Junior Points viewset"""
|
||||
serializer_class = JuniorPointsSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
http_method_names = ('get',)
|
||||
|
||||
def get_queryset(self):
|
||||
"""get queryset"""
|
||||
return JuniorTask.objects.filter(junior__auth__email=self.request.user).last()
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""profile view"""
|
||||
|
||||
try:
|
||||
queryset = self.get_queryset()
|
||||
# update position of junior
|
||||
update_positions_based_on_points()
|
||||
serializer = JuniorPointsSerializer(queryset)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class ValidateReferralCode(viewsets.ViewSet):
|
||||
"""Check guardian code exist or not"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
http_method_names = ('get',)
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get queryset based on referral_code."""
|
||||
referral_code = self.request.GET.get('referral_code')
|
||||
if referral_code:
|
||||
# search referral code in guardian model
|
||||
guardian_queryset = Guardian.objects.filter(referral_code=referral_code).last()
|
||||
if guardian_queryset:
|
||||
return guardian_queryset
|
||||
else:
|
||||
# search referral code in junior model
|
||||
junior_queryset = Junior.objects.filter(referral_code=referral_code).last()
|
||||
if junior_queryset:
|
||||
return junior_queryset
|
||||
return None
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""check guardian code"""
|
||||
try:
|
||||
if self.get_queryset():
|
||||
return custom_response(SUCCESS_CODE['3033'], response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(ERROR_CODE["2019"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class InviteGuardianAPIView(viewsets.ModelViewSet):
|
||||
"""Invite guardian by junior"""
|
||||
serializer_class = AddGuardianSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
http_method_names = ('post',)
|
||||
def create(self, request, *args, **kwargs):
|
||||
""" junior list"""
|
||||
try:
|
||||
if request.data['email'] == '':
|
||||
return custom_error_response(ERROR_CODE['2062'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
info = {'user': request.user, 'email': request.data['email'], 'first_name': request.data['first_name'],
|
||||
'last_name': request.data['last_name'], 'relationship': str(request.data['relationship'])}
|
||||
# use AddJuniorSerializer serializer
|
||||
serializer = AddGuardianSerializer(data=request.data, context=info)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3022'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE['2034'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
return custom_response(SUCCESS_CODE['3034'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.error, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class StartTaskAPIView(views.APIView):
|
||||
"""Update junior task API"""
|
||||
serializer_class = StartTaskSerializer
|
||||
model = JuniorTask
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def put(self, request, format=None):
|
||||
try:
|
||||
task_id = self.request.data.get('task_id')
|
||||
task_queryset = JuniorTask.objects.filter(id=task_id, junior__auth__email=self.request.user).last()
|
||||
if task_queryset and task_queryset.task_status == str(NUMBER['one']):
|
||||
# use StartTaskSerializer serializer
|
||||
serializer = StartTaskSerializer(task_queryset, data=request.data, partial=True)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3035'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
# task in another state
|
||||
return custom_error_response(ERROR_CODE['2060'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
@ -1,11 +1,15 @@
|
||||
#!/usr/bin/env python
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
"""Django import"""
|
||||
"""Import OS module"""
|
||||
# Import OS module
|
||||
import os
|
||||
"""Import sys module"""
|
||||
# Import sys module"""
|
||||
import sys
|
||||
|
||||
# define all function
|
||||
# execute command line
|
||||
# Import execute from command line
|
||||
# fetch django settings
|
||||
|
||||
def main():
|
||||
"""Main function"""
|
||||
|
0
notifications/__init__.py
Normal file
0
notifications/__init__.py
Normal file
12
notifications/admin.py
Normal file
12
notifications/admin.py
Normal file
@ -0,0 +1,12 @@
|
||||
"""
|
||||
notification admin file
|
||||
"""
|
||||
from django.contrib import admin
|
||||
|
||||
from notifications.models import Notification
|
||||
|
||||
|
||||
@admin.register(Notification)
|
||||
class NotificationAdmin(admin.ModelAdmin):
|
||||
"""Notification Admin"""
|
||||
list_display = ['id', 'notification_type', 'notification_to', 'data', 'is_read']
|
13
notifications/apps.py
Normal file
13
notifications/apps.py
Normal file
@ -0,0 +1,13 @@
|
||||
"""
|
||||
notification app file
|
||||
"""
|
||||
# django imports
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class NotificationsConfig(AppConfig):
|
||||
"""
|
||||
notification app config
|
||||
"""
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'notifications'
|
72
notifications/constants.py
Normal file
72
notifications/constants.py
Normal file
@ -0,0 +1,72 @@
|
||||
"""
|
||||
notification constants file
|
||||
"""
|
||||
from base.constants import NUMBER
|
||||
REGISTRATION = NUMBER['one']
|
||||
TASK_CREATED = NUMBER['two']
|
||||
INVITED_GUARDIAN = NUMBER['three']
|
||||
APPROVED_JUNIOR = NUMBER['four']
|
||||
REFERRAL_POINTS = NUMBER['five']
|
||||
TASK_POINTS = NUMBER['six']
|
||||
TASK_REJECTED = NUMBER['seven']
|
||||
SKIPPED_PROFILE_SETUP = NUMBER['eight']
|
||||
TASK_SUBMITTED = NUMBER['nine']
|
||||
TASK_ACTION = NUMBER['ten']
|
||||
LEADERBOARD_RANKING = NUMBER['eleven']
|
||||
REMOVE_JUNIOR = NUMBER['twelve']
|
||||
TEST_NOTIFICATION = 99
|
||||
|
||||
NOTIFICATION_DICT = {
|
||||
REGISTRATION: {
|
||||
"title": "Successfully registered!",
|
||||
"body": "You have registered successfully. Now login and complete your profile."
|
||||
},
|
||||
TASK_CREATED: {
|
||||
"title": "Task created!",
|
||||
"body": "Task created successfully."
|
||||
},
|
||||
INVITED_GUARDIAN: {
|
||||
"title": "Invite guardian",
|
||||
"body": "Invite guardian successfully"
|
||||
},
|
||||
APPROVED_JUNIOR: {
|
||||
"title": "Approve junior",
|
||||
"body": "You have request from junior to associate with you"
|
||||
},
|
||||
REFERRAL_POINTS: {
|
||||
"title": "Earn Referral points",
|
||||
"body": "You earn 5 points for referral."
|
||||
},
|
||||
TASK_POINTS: {
|
||||
"title": "Earn Task points!",
|
||||
"body": "You earn 5 points for task."
|
||||
},
|
||||
TASK_REJECTED: {
|
||||
"title": "Task rejected!",
|
||||
"body": "Your task has been rejected."
|
||||
},
|
||||
SKIPPED_PROFILE_SETUP: {
|
||||
"title": "Skipped profile setup!",
|
||||
"body": "Your guardian has been setup your profile."
|
||||
},
|
||||
TASK_SUBMITTED: {
|
||||
"title": "Task submitted!",
|
||||
"body": "Your task has been submitted successfully."
|
||||
},
|
||||
TASK_ACTION: {
|
||||
"title": "Task approval!",
|
||||
"body": "You have request for task approval."
|
||||
},
|
||||
LEADERBOARD_RANKING: {
|
||||
"title": "Leader board rank!",
|
||||
"body": "Your rank is ."
|
||||
},
|
||||
REMOVE_JUNIOR: {
|
||||
"title": "Disassociate by guardian!",
|
||||
"body": "Your guardian disassociate you ."
|
||||
},
|
||||
TEST_NOTIFICATION: {
|
||||
"title": "Test Notification",
|
||||
"body": "This notification is for testing purpose"
|
||||
}
|
||||
}
|
30
notifications/migrations/0001_initial.py
Normal file
30
notifications/migrations/0001_initial.py
Normal file
@ -0,0 +1,30 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-19 07:40
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Notification',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('notification_type', models.CharField(blank=True, max_length=50, null=True)),
|
||||
('data', models.JSONField(blank=True, default=dict, null=True)),
|
||||
('is_read', models.BooleanField(default=False)),
|
||||
('created_at', models.DateTimeField(default=django.utils.timezone.now)),
|
||||
('notification_from', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='from_notification', to=settings.AUTH_USER_MODEL)),
|
||||
('notification_to', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='to_notification', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
0
notifications/migrations/__init__.py
Normal file
0
notifications/migrations/__init__.py
Normal file
24
notifications/models.py
Normal file
24
notifications/models.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""
|
||||
notification models file
|
||||
"""
|
||||
# django imports
|
||||
from django.db import models
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.utils import timezone
|
||||
|
||||
USER = get_user_model()
|
||||
|
||||
|
||||
class Notification(models.Model):
|
||||
""" used to save the notifications """
|
||||
notification_type = models.CharField(max_length=50, blank=True, null=True)
|
||||
notification_to = models.ForeignKey(USER, related_name='to_notification', on_delete=models.CASCADE)
|
||||
notification_from = models.ForeignKey(USER, related_name='from_notification', on_delete=models.SET_NULL,
|
||||
blank=True, null=True)
|
||||
data = models.JSONField(default=dict, blank=True, null=True)
|
||||
is_read = models.BooleanField(default=False)
|
||||
created_at = models.DateTimeField(default=timezone.now)
|
||||
|
||||
def __str__(self):
|
||||
""" string representation """
|
||||
return f"{self.notification_to.id} | {self.notification_to.email}"
|
28
notifications/serializers.py
Normal file
28
notifications/serializers.py
Normal file
@ -0,0 +1,28 @@
|
||||
"""
|
||||
notification serializer file
|
||||
"""
|
||||
# third party imports
|
||||
from rest_framework import serializers
|
||||
|
||||
# local imports
|
||||
from notifications.utils import register_fcm_token
|
||||
|
||||
|
||||
class RegisterDevice(serializers.Serializer):
|
||||
"""
|
||||
used to create and validate register device token
|
||||
"""
|
||||
registration_id = serializers.CharField(max_length=250)
|
||||
device_id = serializers.CharField(max_length=250)
|
||||
type = serializers.ChoiceField(choices=["ios", "web", "android"])
|
||||
|
||||
class Meta:
|
||||
""" meta class """
|
||||
fields = ('registration_id', 'type', 'device_id')
|
||||
|
||||
def create(self, validated_data):
|
||||
""" override this method to create device token for users """
|
||||
registration_id = validated_data['registration_id']
|
||||
device_type = validated_data['type']
|
||||
return register_fcm_token(self.context['user_id'], registration_id,
|
||||
validated_data['device_id'], device_type)
|
6
notifications/tests.py
Normal file
6
notifications/tests.py
Normal file
@ -0,0 +1,6 @@
|
||||
"""
|
||||
notification test file
|
||||
"""
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
18
notifications/urls.py
Normal file
18
notifications/urls.py
Normal file
@ -0,0 +1,18 @@
|
||||
"""
|
||||
notifications urls file
|
||||
"""
|
||||
# django imports
|
||||
from django.urls import path, include
|
||||
from rest_framework import routers
|
||||
|
||||
# local imports
|
||||
from notifications.views import NotificationViewSet
|
||||
|
||||
# initiate router
|
||||
router = routers.SimpleRouter()
|
||||
|
||||
router.register('notifications', NotificationViewSet, basename='notifications')
|
||||
|
||||
urlpatterns = [
|
||||
path('api/v1/', include(router.urls)),
|
||||
]
|
68
notifications/utils.py
Normal file
68
notifications/utils.py
Normal file
@ -0,0 +1,68 @@
|
||||
"""
|
||||
notifications utils file
|
||||
"""
|
||||
# third party imports
|
||||
from fcm_django.models import FCMDevice
|
||||
from celery import shared_task
|
||||
from firebase_admin.messaging import Message, Notification as FirebaseNotification
|
||||
|
||||
# django imports
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from account.models import UserNotification
|
||||
from notifications.constants import NOTIFICATION_DICT
|
||||
from notifications.models import Notification
|
||||
|
||||
# local imports
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
def register_fcm_token(user_id, registration_id, device_id, device_type):
|
||||
""" used to register the fcm device token"""
|
||||
device, _ = FCMDevice.objects.update_or_create(device_id=device_id,
|
||||
defaults={'user_id': user_id, 'type': device_type,
|
||||
'active': True,
|
||||
'registration_id': registration_id})
|
||||
return device
|
||||
|
||||
|
||||
def remove_fcm_token(user_id: int, access_token: str, registration_id) -> None:
|
||||
"""
|
||||
remove access_token and registration_token
|
||||
"""
|
||||
try:
|
||||
# remove fcm token for this device
|
||||
FCMDevice.objects.filter(user_id=user_id).delete()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
|
||||
def get_basic_detail(notification_type, from_user_id, to_user_id):
|
||||
""" used to get the basic details """
|
||||
notification_data = NOTIFICATION_DICT[notification_type]
|
||||
from_user = User.objects.get(id=from_user_id) if from_user_id else None
|
||||
to_user = User.objects.get(id=to_user_id)
|
||||
return notification_data, from_user, to_user
|
||||
|
||||
|
||||
@shared_task()
|
||||
def send_notification(notification_type, from_user_id, to_user_id, extra_data):
|
||||
""" used to send the push for the given notification type """
|
||||
(notification_data, from_user, to_user) = get_basic_detail(notification_type, from_user_id, to_user_id)
|
||||
user_notification_type = UserNotification.objects.filter(user=to_user).first()
|
||||
data = notification_data
|
||||
Notification.objects.create(notification_type=notification_type, notification_from=from_user,
|
||||
notification_to=to_user, data=data)
|
||||
if user_notification_type.push_notification:
|
||||
data.update({'badge': Notification.objects.filter(notification_to=to_user, is_read=False).count()})
|
||||
send_push(to_user, data)
|
||||
|
||||
|
||||
def send_push(user, data):
|
||||
""" used to send push notification to specific user """
|
||||
notification_data = data.pop('data', None)
|
||||
user.fcmdevice_set.filter(active=True).send_message(
|
||||
Message(notification=FirebaseNotification(data['title'], data['body']), data=notification_data)
|
||||
)
|
42
notifications/views.py
Normal file
42
notifications/views.py
Normal file
@ -0,0 +1,42 @@
|
||||
"""
|
||||
notifications views file
|
||||
"""
|
||||
# django imports
|
||||
from django.db.models import Q
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
|
||||
# local imports
|
||||
from account.utils import custom_response
|
||||
from base.messages import SUCCESS_CODE
|
||||
from notifications.constants import TEST_NOTIFICATION
|
||||
from notifications.serializers import RegisterDevice
|
||||
from notifications.utils import send_notification
|
||||
|
||||
|
||||
class NotificationViewSet(viewsets.GenericViewSet):
|
||||
""" used to do the notification actions """
|
||||
serializer_class = RegisterDevice
|
||||
permission_classes = [IsAuthenticated, ]
|
||||
|
||||
@action(methods=['post'], detail=False, url_path='device', url_name='device', serializer_class=RegisterDevice)
|
||||
def fcm_registration(self, request):
|
||||
"""
|
||||
used to save the fcm token
|
||||
"""
|
||||
serializer = self.get_serializer_class()(data=request.data,
|
||||
context={'user_id': request.auth.payload['user_id']})
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE["3000"])
|
||||
|
||||
@action(methods=['get'], detail=False, url_path='test', url_name='test')
|
||||
def send_test_notification(self, request):
|
||||
"""
|
||||
to send test notification
|
||||
:return:
|
||||
"""
|
||||
send_notification.delay(TEST_NOTIFICATION, None, request.auth.payload['user_id'], {})
|
||||
return custom_response(SUCCESS_CODE["3000"])
|
@ -8,6 +8,7 @@ async-timeout==4.0.2
|
||||
billiard==4.1.0
|
||||
boto3==1.26.157
|
||||
botocore==1.29.157
|
||||
CacheControl==0.13.1
|
||||
cachetools==5.3.1
|
||||
celery==5.3.1
|
||||
certifi==2023.5.7
|
||||
@ -41,8 +42,22 @@ django-timezone-field==5.1
|
||||
djangorestframework==3.14.0
|
||||
djangorestframework-simplejwt==5.2.2
|
||||
drf-yasg==1.21.6
|
||||
fcm-django==2.0.0
|
||||
firebase-admin==6.2.0
|
||||
google-api-core==2.11.1
|
||||
google-api-python-client==2.93.0
|
||||
google-auth==2.21.0
|
||||
google-auth-httplib2==0.1.0
|
||||
google-cloud-core==2.3.3
|
||||
google-cloud-firestore==2.11.1
|
||||
google-cloud-storage==2.10.0
|
||||
google-crc32c==1.5.0
|
||||
google-resumable-media==2.5.0
|
||||
googleapis-common-protos==1.59.1
|
||||
grpcio==1.56.0
|
||||
grpcio-status==1.56.0
|
||||
gunicorn==20.1.0
|
||||
httplib2==0.22.0
|
||||
idna==3.4
|
||||
inflection==0.5.1
|
||||
itypes==1.2.0
|
||||
@ -51,17 +66,22 @@ jmespath==0.10.0
|
||||
kombu==5.3.1
|
||||
MarkupSafe==2.1.3
|
||||
msgpack==1.0.5
|
||||
ntplib==0.4.0
|
||||
numpy==1.25.1
|
||||
oss2==2.18.0
|
||||
packaging==23.1
|
||||
phonenumbers==8.13.15
|
||||
Pillow==9.5.0
|
||||
prompt-toolkit==3.0.38
|
||||
proto-plus==1.22.3
|
||||
protobuf==4.23.4
|
||||
psycopg==3.1.9
|
||||
pyasn1==0.5.0
|
||||
pyasn1-modules==0.3.0
|
||||
pycparser==2.21
|
||||
pycryptodome==3.18.0
|
||||
PyJWT==2.7.0
|
||||
pyparsing==3.1.0
|
||||
python-crontab==2.7.1
|
||||
python-dateutil==2.8.2
|
||||
python-dotenv==1.0.0
|
||||
|
0
web_admin/__init__.py
Normal file
0
web_admin/__init__.py
Normal file
38
web_admin/admin.py
Normal file
38
web_admin/admin.py
Normal file
@ -0,0 +1,38 @@
|
||||
"""
|
||||
web_admin admin file
|
||||
"""
|
||||
# django imports
|
||||
from django.contrib import admin
|
||||
|
||||
# local imports
|
||||
from web_admin.models import Article, ArticleCard, ArticleSurvey, SurveyOption, DefaultArticleCardImage
|
||||
|
||||
|
||||
@admin.register(Article)
|
||||
class ArticleAdmin(admin.ModelAdmin):
|
||||
"""Article Admin"""
|
||||
list_display = ['id', 'title', 'description', 'is_published', 'is_deleted']
|
||||
|
||||
|
||||
@admin.register(ArticleCard)
|
||||
class ArticleCardAdmin(admin.ModelAdmin):
|
||||
"""Article Card Admin"""
|
||||
list_display = ['id', 'article', 'title', 'description', 'image_url']
|
||||
|
||||
|
||||
@admin.register(ArticleSurvey)
|
||||
class ArticleSurveyAdmin(admin.ModelAdmin):
|
||||
"""Article Survey Admin"""
|
||||
list_display = ['id', 'article', 'question', 'points']
|
||||
|
||||
|
||||
@admin.register(SurveyOption)
|
||||
class SurveyOptionAdmin(admin.ModelAdmin):
|
||||
"""Survey Option Admin"""
|
||||
list_display = ['id', 'survey', 'option', 'is_answer']
|
||||
|
||||
|
||||
@admin.register(DefaultArticleCardImage)
|
||||
class DefaultArticleCardImagesAdmin(admin.ModelAdmin):
|
||||
"""Default Article Card Images Option Admin"""
|
||||
list_display = ['image_name', 'image_url']
|
13
web_admin/apps.py
Normal file
13
web_admin/apps.py
Normal file
@ -0,0 +1,13 @@
|
||||
"""
|
||||
web_admin app file
|
||||
"""
|
||||
# django imports
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class WebAdminConfig(AppConfig):
|
||||
"""
|
||||
web admin app config
|
||||
"""
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'web_admin'
|
61
web_admin/migrations/0001_initial.py
Normal file
61
web_admin/migrations/0001_initial.py
Normal file
@ -0,0 +1,61 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-14 13:15
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Article',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('description', models.TextField()),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('is_published', models.BooleanField(default=True)),
|
||||
('is_deleted', models.BooleanField(default=False)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ArticleSurvey',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('question', models.CharField(max_length=255)),
|
||||
('points', models.IntegerField()),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='article_survey', to='web_admin.article')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SurveyOption',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('option', models.CharField(max_length=255)),
|
||||
('is_answer', models.BooleanField(default=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('survey', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='survey_options', to='web_admin.articlesurvey')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ArticleCard',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('description', models.TextField()),
|
||||
('image', models.ImageField(upload_to='card_images/')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='article_cards', to='web_admin.article')),
|
||||
],
|
||||
),
|
||||
]
|
18
web_admin/migrations/0002_alter_articlecard_image.py
Normal file
18
web_admin/migrations/0002_alter_articlecard_image.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-20 11:51
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('web_admin', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='articlecard',
|
||||
name='image',
|
||||
field=models.URLField(blank=True, default=None, null=True),
|
||||
),
|
||||
]
|
@ -0,0 +1,28 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-24 14:15
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('web_admin', '0002_alter_articlecard_image'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='DefaultArticleCardImage',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('image_name', models.CharField(max_length=20)),
|
||||
('image_url', models.URLField()),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='articlecard',
|
||||
old_name='image',
|
||||
new_name='image_url',
|
||||
),
|
||||
]
|
0
web_admin/migrations/__init__.py
Normal file
0
web_admin/migrations/__init__.py
Normal file
81
web_admin/models.py
Normal file
81
web_admin/models.py
Normal file
@ -0,0 +1,81 @@
|
||||
"""
|
||||
web_admin model file
|
||||
"""
|
||||
# django imports
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Article(models.Model):
|
||||
"""
|
||||
Article model
|
||||
"""
|
||||
title = models.CharField(max_length=255)
|
||||
description = models.TextField()
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
is_published = models.BooleanField(default=True)
|
||||
is_deleted = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
"""Return title"""
|
||||
return f'{self.id} | {self.title}'
|
||||
|
||||
|
||||
class ArticleCard(models.Model):
|
||||
"""
|
||||
Article Card model
|
||||
"""
|
||||
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='article_cards')
|
||||
title = models.CharField(max_length=255)
|
||||
description = models.TextField()
|
||||
image_url = models.URLField(null=True, blank=True, default=None)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
"""Return title"""
|
||||
return f'{self.id} | {self.title}'
|
||||
|
||||
|
||||
class ArticleSurvey(models.Model):
|
||||
"""
|
||||
Article Survey model
|
||||
"""
|
||||
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='article_survey')
|
||||
question = models.CharField(max_length=255)
|
||||
points = models.IntegerField()
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
"""Return title"""
|
||||
return f'{self.id} | {self.article}'
|
||||
|
||||
|
||||
class SurveyOption(models.Model):
|
||||
"""
|
||||
Survey Options model
|
||||
"""
|
||||
survey = models.ForeignKey(ArticleSurvey, on_delete=models.CASCADE, related_name='survey_options')
|
||||
option = models.CharField(max_length=255)
|
||||
is_answer = models.BooleanField(default=False)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
"""Return title"""
|
||||
return f'{self.id} | {self.survey}'
|
||||
|
||||
|
||||
class DefaultArticleCardImage(models.Model):
|
||||
"""
|
||||
Default images upload in oss bucket
|
||||
"""
|
||||
image_name = models.CharField(max_length=20)
|
||||
image_url = models.URLField()
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
"""return image_name as an object"""
|
||||
return self.image_name
|
26
web_admin/permission.py
Normal file
26
web_admin/permission.py
Normal file
@ -0,0 +1,26 @@
|
||||
"""
|
||||
web_admin permission classes
|
||||
"""
|
||||
# django imports
|
||||
from rest_framework import permissions
|
||||
|
||||
|
||||
class AdminPermission(permissions.BasePermission):
|
||||
"""
|
||||
to check for usertype admin only
|
||||
"""
|
||||
def has_permission(self, request, view):
|
||||
"""
|
||||
Return True if user_type is admin
|
||||
"""
|
||||
if request.user.is_superuser:
|
||||
return True
|
||||
return False
|
||||
|
||||
def has_object_permission(self, request, view, obj):
|
||||
"""
|
||||
check for object level permission
|
||||
"""
|
||||
if request.user.is_superuser:
|
||||
return True
|
||||
return False
|
0
web_admin/serializers/__init__.py
Normal file
0
web_admin/serializers/__init__.py
Normal file
278
web_admin/serializers/article_serializer.py
Normal file
278
web_admin/serializers/article_serializer.py
Normal file
@ -0,0 +1,278 @@
|
||||
"""
|
||||
web_admin serializers file
|
||||
"""
|
||||
# django imports
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from base.constants import (ARTICLE_SURVEY_POINTS, MAX_ARTICLE_CARD, MIN_ARTICLE_SURVEY, MAX_ARTICLE_SURVEY, NUMBER,
|
||||
USER_TYPE, ARTICLE_CARD_IMAGE_FOLDER)
|
||||
# local imports
|
||||
from base.messages import ERROR_CODE
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from web_admin.models import Article, ArticleCard, SurveyOption, ArticleSurvey, DefaultArticleCardImage
|
||||
from web_admin.utils import pop_id
|
||||
|
||||
USER = get_user_model()
|
||||
|
||||
|
||||
class ArticleCardSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Article Card serializer
|
||||
"""
|
||||
id = serializers.IntegerField(required=False)
|
||||
image = serializers.FileField(required=False)
|
||||
image_url = serializers.URLField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = ArticleCard
|
||||
fields = ('id', 'title', 'description', 'image', 'image_url')
|
||||
|
||||
def create(self, validated_data):
|
||||
if 'image' in validated_data and validated_data['image'] is not None:
|
||||
image = validated_data.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
# upload image on ali baba
|
||||
validated_data['image_url'] = upload_image_to_alibaba(image, filename)
|
||||
article_card = ArticleCard.objects.create(article_id='1', **validated_data)
|
||||
return article_card
|
||||
|
||||
|
||||
class SurveyOptionSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
survey option serializer
|
||||
"""
|
||||
id = serializers.IntegerField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = SurveyOption
|
||||
fields = ('id', 'option', 'is_answer')
|
||||
|
||||
|
||||
class ArticleSurveySerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
article survey serializer
|
||||
"""
|
||||
id = serializers.IntegerField(required=False)
|
||||
survey_options = SurveyOptionSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = ArticleSurvey
|
||||
fields = ('id', 'question', 'survey_options')
|
||||
|
||||
|
||||
class ArticleSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
serializer for article API
|
||||
"""
|
||||
article_cards = ArticleCardSerializer(many=True)
|
||||
article_survey = ArticleSurveySerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = Article
|
||||
fields = ('id', 'title', 'description', 'article_cards', 'article_survey')
|
||||
|
||||
def validate(self, attrs):
|
||||
"""
|
||||
to validate request data
|
||||
:return: validated attrs
|
||||
"""
|
||||
article_cards = attrs.get('article_cards', None)
|
||||
article_survey = attrs.get('article_survey', None)
|
||||
if article_cards is None or len(article_cards) > int(MAX_ARTICLE_CARD):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2039']})
|
||||
if article_survey is None or len(article_survey) < int(MIN_ARTICLE_SURVEY) or int(
|
||||
MAX_ARTICLE_SURVEY) < len(article_survey):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2040']})
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
"""
|
||||
to create article.
|
||||
ID in post data dict is for update api.
|
||||
:return: article object
|
||||
"""
|
||||
article_cards = validated_data.pop('article_cards')
|
||||
article_survey = validated_data.pop('article_survey')
|
||||
|
||||
article = Article.objects.create(**validated_data)
|
||||
|
||||
for card in article_cards:
|
||||
card = pop_id(card)
|
||||
if 'image' in card and card['image'] is not None:
|
||||
image = card.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
# upload image on ali baba
|
||||
card['image_url'] = upload_image_to_alibaba(image, filename)
|
||||
ArticleCard.objects.create(article=article, **card)
|
||||
|
||||
for survey in article_survey:
|
||||
survey = pop_id(survey)
|
||||
options = survey.pop('survey_options')
|
||||
survey_obj = ArticleSurvey.objects.create(article=article, points=ARTICLE_SURVEY_POINTS, **survey)
|
||||
for option in options:
|
||||
option = pop_id(option)
|
||||
SurveyOption.objects.create(survey=survey_obj, **option)
|
||||
|
||||
return article
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
"""
|
||||
to update article and related table
|
||||
:param instance: article object,
|
||||
:return: article object
|
||||
"""
|
||||
article_cards = validated_data.pop('article_cards')
|
||||
article_survey = validated_data.pop('article_survey')
|
||||
instance.title = validated_data.get('title', instance.title)
|
||||
instance.description = validated_data.get('description', instance.description)
|
||||
instance.save()
|
||||
|
||||
# Update or create cards
|
||||
for card_data in article_cards:
|
||||
card_id = card_data.get('id', None)
|
||||
if card_id:
|
||||
card = ArticleCard.objects.get(id=card_id, article=instance)
|
||||
card.title = card_data.get('title', card.title)
|
||||
card.description = card_data.get('description', card.description)
|
||||
if 'image' in card_data and card_data['image'] is not None:
|
||||
image = card_data.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
# upload image on ali baba
|
||||
card.image_url = upload_image_to_alibaba(image, filename)
|
||||
card.save()
|
||||
else:
|
||||
card_data = pop_id(card_data)
|
||||
if 'image' in card_data and card_data['image'] is not None:
|
||||
image = card_data.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
# upload image on ali baba
|
||||
card_data['image_url'] = upload_image_to_alibaba(image, filename)
|
||||
ArticleCard.objects.create(article=instance, **card_data)
|
||||
|
||||
# Update or create survey sections
|
||||
for survey_data in article_survey:
|
||||
survey_id = survey_data.get('id', None)
|
||||
options_data = survey_data.pop('survey_options')
|
||||
if survey_id:
|
||||
survey = ArticleSurvey.objects.get(id=survey_id, article=instance)
|
||||
survey.question = survey_data.get('question', survey.question)
|
||||
survey.save()
|
||||
else:
|
||||
survey_data = pop_id(survey_data)
|
||||
survey = ArticleSurvey.objects.create(article=instance, **survey_data)
|
||||
|
||||
# Update or create survey options
|
||||
for option_data in options_data:
|
||||
option_id = option_data.get('id', None)
|
||||
if option_id:
|
||||
option = SurveyOption.objects.get(id=option_id, survey=survey)
|
||||
option.option = option_data.get('option', option.option)
|
||||
option.is_answer = option_data.get('is_answer', option.is_answer)
|
||||
option.save()
|
||||
else:
|
||||
option_data = pop_id(option_data)
|
||||
SurveyOption.objects.create(survey=survey, **option_data)
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class DefaultArticleCardImageSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Article Card serializer
|
||||
"""
|
||||
image = serializers.FileField(required=False)
|
||||
image_url = serializers.URLField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = DefaultArticleCardImage
|
||||
fields = ('image_name', 'image', 'image_url')
|
||||
|
||||
def validate(self, attrs):
|
||||
"""
|
||||
to validate data
|
||||
:return: validated data
|
||||
"""
|
||||
if 'image' not in attrs and attrs.get('image') is None:
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2061']})
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
"""
|
||||
to create and upload image
|
||||
:return: card_image object
|
||||
"""
|
||||
image = validated_data.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
if image and image.size == NUMBER['zero']:
|
||||
raise serializers.ValidationError(ERROR_CODE['2035'])
|
||||
# upload image on ali baba
|
||||
validated_data['image_url'] = upload_image_to_alibaba(image, filename)
|
||||
|
||||
card_image = DefaultArticleCardImage.objects.create(**validated_data)
|
||||
return card_image
|
||||
|
||||
|
||||
class UserManagementListSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
user management serializer
|
||||
"""
|
||||
name = serializers.SerializerMethodField()
|
||||
phone_number = serializers.SerializerMethodField()
|
||||
user_type = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = USER
|
||||
fields = ('name', 'email', 'phone_number', 'user_type', 'is_active')
|
||||
|
||||
@staticmethod
|
||||
def get_name(obj):
|
||||
"""
|
||||
:param obj: user object
|
||||
:return: full name
|
||||
"""
|
||||
return (obj.first_name + obj.last_name) if obj.last_name else obj.first_name
|
||||
|
||||
@staticmethod
|
||||
def get_phone_number(obj):
|
||||
"""
|
||||
:param obj: user object
|
||||
:return: user phone number
|
||||
"""
|
||||
if profile := obj.guardian_profile.all().first():
|
||||
return profile.phone
|
||||
elif profile := obj.junior_profile.all().first():
|
||||
return profile.phone
|
||||
else:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_user_type(obj):
|
||||
"""
|
||||
:param obj: user object
|
||||
:return: user type
|
||||
"""
|
||||
if obj.guardian_profile.all().first():
|
||||
return dict(USER_TYPE).get('2')
|
||||
elif obj.junior_profile.all().first():
|
||||
return dict(USER_TYPE).get('1')
|
||||
else:
|
||||
return None
|
136
web_admin/serializers/auth_serializer.py
Normal file
136
web_admin/serializers/auth_serializer.py
Normal file
@ -0,0 +1,136 @@
|
||||
"""
|
||||
web_admin auth serializers file
|
||||
"""
|
||||
# python imports
|
||||
from datetime import datetime
|
||||
|
||||
# django imports
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.utils import timezone
|
||||
|
||||
# local imports
|
||||
from account.models import UserEmailOtp
|
||||
from base.constants import USER_TYPE
|
||||
from base.messages import ERROR_CODE
|
||||
from guardian.tasks import generate_otp
|
||||
from base.tasks import send_email_otp
|
||||
|
||||
USER = get_user_model()
|
||||
|
||||
|
||||
class AdminOTPSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
admin forgot password serializer
|
||||
"""
|
||||
email = serializers.EmailField()
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = USER
|
||||
fields = ('email',)
|
||||
|
||||
def validate(self, attrs):
|
||||
""" used to validate the incoming data """
|
||||
user = USER.objects.filter(email=attrs.get('email')).first()
|
||||
if not user:
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2004']})
|
||||
elif not user.is_superuser:
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2063']})
|
||||
attrs.update({'user': user})
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
"""
|
||||
to send otp
|
||||
:return: user_data
|
||||
"""
|
||||
email = validated_data['email']
|
||||
|
||||
verification_code = generate_otp()
|
||||
|
||||
# Send the verification code to the user's email
|
||||
send_email_otp.delay(email, verification_code)
|
||||
|
||||
expiry = timezone.now() + timezone.timedelta(days=1)
|
||||
user_data, created = UserEmailOtp.objects.update_or_create(email=email,
|
||||
defaults={
|
||||
"otp": verification_code,
|
||||
"expired_at": expiry,
|
||||
"user_type": dict(USER_TYPE).get('3'),
|
||||
})
|
||||
|
||||
return user_data
|
||||
|
||||
|
||||
class AdminVerifyOTPSerializer(serializers.Serializer):
|
||||
"""
|
||||
admin verify otp serializer
|
||||
"""
|
||||
email = serializers.EmailField()
|
||||
otp = serializers.CharField(max_length=6, min_length=6)
|
||||
|
||||
class Meta:
|
||||
""" meta class """
|
||||
fields = ('email', 'otp',)
|
||||
|
||||
def validate(self, attrs):
|
||||
"""
|
||||
to validate data
|
||||
:return: validated data
|
||||
"""
|
||||
email = attrs.get('email')
|
||||
otp = attrs.get('otp')
|
||||
|
||||
# fetch email otp object of the user
|
||||
user_otp_details = UserEmailOtp.objects.filter(email=email, otp=otp).last()
|
||||
if not user_otp_details:
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2064']})
|
||||
if user_otp_details.user_type != dict(USER_TYPE).get('3'):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2063']})
|
||||
if user_otp_details.expired_at.replace(tzinfo=None) < datetime.utcnow():
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2029']})
|
||||
user_otp_details.is_verified = True
|
||||
user_otp_details.save()
|
||||
return attrs
|
||||
|
||||
|
||||
class AdminCreatePasswordSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
admin create new password serializer
|
||||
"""
|
||||
email = serializers.EmailField()
|
||||
new_password = serializers.CharField()
|
||||
confirm_password = serializers.CharField()
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = USER
|
||||
fields = ('email', 'new_password', 'confirm_password')
|
||||
|
||||
def validate(self, attrs):
|
||||
"""
|
||||
to validate data
|
||||
:return: validated data
|
||||
"""
|
||||
email = attrs.get('email')
|
||||
new_password = attrs.get('new_password')
|
||||
confirm_password = attrs.get('confirm_password')
|
||||
|
||||
# matching password
|
||||
if new_password != confirm_password:
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2065']})
|
||||
|
||||
user_otp_details = UserEmailOtp.objects.filter(email=email).last()
|
||||
if not user_otp_details:
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2064']})
|
||||
if user_otp_details.user_type != dict(USER_TYPE).get('3'):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2063']})
|
||||
if not user_otp_details.is_verified:
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2064']})
|
||||
user_otp_details.delete()
|
||||
return attrs
|
6
web_admin/tests.py
Normal file
6
web_admin/tests.py
Normal file
@ -0,0 +1,6 @@
|
||||
"""
|
||||
web_admin test file
|
||||
"""
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
24
web_admin/urls.py
Normal file
24
web_admin/urls.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""
|
||||
web_admin urls file
|
||||
"""
|
||||
# django imports
|
||||
from django.urls import path, include
|
||||
from rest_framework import routers
|
||||
|
||||
# local imports
|
||||
from web_admin.views.article import ArticleViewSet, DefaultArticleCardImagesViewSet, UserManagementViewSet
|
||||
from web_admin.views.auth import ForgotAndResetPasswordViewSet
|
||||
|
||||
# initiate router
|
||||
router = routers.SimpleRouter()
|
||||
|
||||
router.register('article', ArticleViewSet, basename='article')
|
||||
router.register('default-card-images', DefaultArticleCardImagesViewSet, basename='default-card-images')
|
||||
router.register('user', UserManagementViewSet, basename='user')
|
||||
|
||||
# forgot and reset password api for admin
|
||||
router.register('admin', ForgotAndResetPasswordViewSet, basename='admin')
|
||||
|
||||
urlpatterns = [
|
||||
path('api/v1/', include(router.urls)),
|
||||
]
|
13
web_admin/utils.py
Normal file
13
web_admin/utils.py
Normal file
@ -0,0 +1,13 @@
|
||||
"""
|
||||
web_utils file
|
||||
"""
|
||||
|
||||
|
||||
def pop_id(data):
|
||||
"""
|
||||
to pop id, not in use
|
||||
:param data:
|
||||
:return: data
|
||||
"""
|
||||
data.pop('id') if 'id' in data else data
|
||||
return data
|
0
web_admin/views/__init__.py
Normal file
0
web_admin/views/__init__.py
Normal file
224
web_admin/views/article.py
Normal file
224
web_admin/views/article.py
Normal file
@ -0,0 +1,224 @@
|
||||
"""
|
||||
web_admin views file
|
||||
"""
|
||||
# django imports
|
||||
from rest_framework.viewsets import GenericViewSet, mixins
|
||||
from rest_framework.filters import OrderingFilter, SearchFilter
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.permissions import IsAuthenticated, AllowAny
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
# local imports
|
||||
from account.utils import custom_response, custom_error_response
|
||||
from base.constants import USER_TYPE
|
||||
from base.messages import SUCCESS_CODE, ERROR_CODE
|
||||
from web_admin.models import Article, ArticleCard, ArticleSurvey, DefaultArticleCardImage
|
||||
from web_admin.permission import AdminPermission
|
||||
from web_admin.serializers.article_serializer import (ArticleSerializer, ArticleCardSerializer,
|
||||
DefaultArticleCardImageSerializer,
|
||||
UserManagementListSerializer)
|
||||
|
||||
USER = get_user_model()
|
||||
|
||||
|
||||
class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModelMixin,
|
||||
mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.DestroyModelMixin):
|
||||
"""
|
||||
article api
|
||||
"""
|
||||
serializer_class = ArticleSerializer
|
||||
permission_classes = [IsAuthenticated, AdminPermission]
|
||||
queryset = Article
|
||||
filter_backends = (OrderingFilter, SearchFilter,)
|
||||
search_fields = ['title']
|
||||
http_method_names = ['get', 'post', 'put', 'delete']
|
||||
|
||||
def get_queryset(self):
|
||||
article = self.queryset.objects.filter(is_deleted=False).prefetch_related(
|
||||
'article_cards', 'article_survey', 'article_survey__survey_options'
|
||||
).order_by('-created_at')
|
||||
queryset = self.filter_queryset(article)
|
||||
return queryset
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""
|
||||
article create api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE["3027"])
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
"""
|
||||
article update api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return: success message
|
||||
"""
|
||||
article = self.get_object()
|
||||
serializer = self.serializer_class(article, data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE["3028"])
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""
|
||||
article list api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return: list of article
|
||||
"""
|
||||
queryset = self.get_queryset()
|
||||
paginator = self.pagination_class()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
serializer = self.serializer_class(paginated_queryset, many=True)
|
||||
return custom_response(None, data=serializer.data)
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
"""
|
||||
article detail api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return: article detail data
|
||||
"""
|
||||
queryset = self.get_object()
|
||||
serializer = self.serializer_class(queryset, many=False)
|
||||
return custom_response(None, data=serializer.data)
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
"""
|
||||
article delete (soft delete) api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return: success message
|
||||
"""
|
||||
article = self.get_object()
|
||||
article.is_deleted = True
|
||||
article.save()
|
||||
if article:
|
||||
return custom_response(SUCCESS_CODE["3029"])
|
||||
return custom_error_response(ERROR_CODE["2041"], status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(methods=['get'], url_name='remove_card', url_path='remove_card',
|
||||
detail=True)
|
||||
def remove_article_card(self, request, *args, **kwargs):
|
||||
"""
|
||||
article card remove (delete) api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return: success message
|
||||
"""
|
||||
try:
|
||||
ArticleCard.objects.filter(id=kwargs['pk']).first().delete()
|
||||
return custom_response(SUCCESS_CODE["3030"])
|
||||
except AttributeError:
|
||||
return custom_error_response(ERROR_CODE["2042"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(methods=['get'], url_name='remove_survey', url_path='remove_survey',
|
||||
detail=True)
|
||||
def remove_article_survey(self, request, *args, **kwargs):
|
||||
"""
|
||||
article survey remove (delete) api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return: success message
|
||||
"""
|
||||
try:
|
||||
ArticleSurvey.objects.filter(id=kwargs['pk']).first().delete()
|
||||
return custom_response(SUCCESS_CODE["3031"])
|
||||
except AttributeError:
|
||||
return custom_error_response(ERROR_CODE["2043"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(methods=['post'], url_name='test-add-card', url_path='test-add-card',
|
||||
detail=False, serializer_class=ArticleCardSerializer, permission_classes=[AllowAny])
|
||||
def add_card(self, request):
|
||||
"""
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE["3000"])
|
||||
|
||||
@action(methods=['get'], url_name='test-list-card', url_path='test-list-card',
|
||||
detail=False, serializer_class=ArticleCardSerializer, permission_classes=[AllowAny])
|
||||
def list_card(self, request):
|
||||
"""
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
queryset = ArticleCard.objects.all()
|
||||
serializer = self.serializer_class(queryset, many=True)
|
||||
return custom_response(None, serializer.data)
|
||||
|
||||
|
||||
class DefaultArticleCardImagesViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.ListModelMixin):
|
||||
"""
|
||||
api to upload and list default article card images
|
||||
"""
|
||||
serializer_class = DefaultArticleCardImageSerializer
|
||||
permission_classes = [IsAuthenticated, AdminPermission]
|
||||
queryset = DefaultArticleCardImage.objects.all()
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""
|
||||
api method to upload default article card images
|
||||
:param request:
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE["3000"])
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""
|
||||
api method to list default article card images
|
||||
:param request:
|
||||
:return: default article card images
|
||||
"""
|
||||
queryset = self.queryset
|
||||
serializer = self.serializer_class(queryset, many=True)
|
||||
return custom_response(None, data=serializer.data)
|
||||
|
||||
|
||||
class UserManagementViewSet(GenericViewSet, mixins.ListModelMixin):
|
||||
"""
|
||||
api to manage (list, view, edit) user
|
||||
"""
|
||||
serializer_class = UserManagementListSerializer
|
||||
permission_classes = []
|
||||
queryset = USER.objects.prefetch_related(
|
||||
'guardian_profile', 'junior_profile')
|
||||
|
||||
def get_queryset(self):
|
||||
if self.request.query_params.get('user_type') == dict(USER_TYPE).get('2'):
|
||||
return self.queryset.filter(junior_profile__isnull=True)
|
||||
elif self.request.query_params.get('user_type') == dict(USER_TYPE).get('1'):
|
||||
return self.queryset.filter(guardian_profile__isnull=True)
|
||||
else:
|
||||
return self.queryset
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""
|
||||
api method to list all the user
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
queryset = self.get_queryset()
|
||||
serializer = self.serializer_class(queryset, many=True)
|
||||
return custom_response(None, data=serializer.data)
|
||||
|
63
web_admin/views/auth.py
Normal file
63
web_admin/views/auth.py
Normal file
@ -0,0 +1,63 @@
|
||||
"""
|
||||
web_admin auth views file
|
||||
"""
|
||||
# django imports
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
# local imports
|
||||
from account.utils import custom_response, custom_error_response
|
||||
from base.messages import SUCCESS_CODE, ERROR_CODE
|
||||
from web_admin.serializers.auth_serializer import (AdminOTPSerializer, AdminVerifyOTPSerializer,
|
||||
AdminCreatePasswordSerializer)
|
||||
|
||||
USER = get_user_model()
|
||||
|
||||
|
||||
class ForgotAndResetPasswordViewSet(GenericViewSet):
|
||||
"""
|
||||
to reset admin password
|
||||
"""
|
||||
queryset = None
|
||||
|
||||
@action(methods=['post'], url_name='otp', url_path='otp',
|
||||
detail=False, serializer_class=AdminOTPSerializer)
|
||||
def admin_otp(self, request):
|
||||
"""
|
||||
api method to send otp
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3015'])
|
||||
return custom_error_response(ERROR_CODE['2063'], status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(methods=['post'], url_name='verify-otp', url_path='verify-otp',
|
||||
detail=False, serializer_class=AdminVerifyOTPSerializer)
|
||||
def admin_verify_otp(self, request):
|
||||
"""
|
||||
api method to verify otp
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
return custom_response(SUCCESS_CODE['3011'])
|
||||
return custom_error_response(ERROR_CODE['2063'], status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(methods=['post'], url_name='create-password', url_path='create-password',
|
||||
detail=False, serializer_class=AdminCreatePasswordSerializer)
|
||||
def admin_create_password(self, request):
|
||||
"""
|
||||
api method to create new password
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
user = USER.objects.filter(email=serializer.validated_data.get('email')).first()
|
||||
user.set_password(serializer.validated_data.get('new_password'))
|
||||
user.save()
|
||||
return custom_response(SUCCESS_CODE['3007'])
|
||||
return custom_error_response(ERROR_CODE['2064'], status.HTTP_400_BAD_REQUEST)
|
13
zod-bank-fcm-credentials.json
Normal file
13
zod-bank-fcm-credentials.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"type": "service_account",
|
||||
"project_id": "zod-bank-ebb2a",
|
||||
"private_key_id": "f1115f1b1fece77ba7a119b70e2502ce0777a7d4",
|
||||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCvLWEobyVN7D4+\nSZ4NwcugwuVk9MV7CjhQGB8lqzf/1Z0plBytjpjM75+orFk5n2tnOgTxsWCqR1R7\nLry4x2QH3HgJebd/TZTIyfepcAeuzUVhq9prgWVRsvxjpihMPZufA/Q1GEX5TBwX\nEasBW91Qwas2NBhUrzotnUBxOshVB4zCo3Ls9dbAN9O2O6paUMvcofSsRZ9XkB6P\nFFKy6nbQ3Bo+Lw3ntUfG1JQgkkxto2Vudiiq6J2dE6Eih2acEhEezQoJVpkMK+si\nlGp88T3j8nTx3o6ol99ke+3ZejPVE5sUbuhokSV/tS1Goy3whP+ys9lQtIyt3/mJ\nlmkoB9ShAgMBAAECggEAAk3H0GFF08C3EBWyDqH5dbLXbtH92e/UeNAMNcV4mGbY\n5GKGFywmEVmg2N22EPqa/s+Y6QxxrD9u9G1+EhbnIIx7olOBkScJ9C0c9CWztCon\ntPaofd1E1cI7I7UfVTg2ZLAHrBN4H70eadYc4va8bBtHj0EHYONz7S8sEBQ1Qna2\nIQuCEWY6MzhwCNEFIJd8ikd0GnkAJCuInK3F+2c37kugdgjRKxkTIfWmhNIfyWn3\ngoui5wltuuDKETVj9VUMyN6P7kffIJzS4mMRm/9F92scpPRbK+X1TrHpFsO826oP\nUuSBi5oOZYNyAvMBXGdnemoUNtAE+xg4kqwYJu5T0QKBgQDlbO08yH5MSLUxXZfL\nkCYg1mCUC4ySpL/qVFNuyvY+ex9gIg6gj4vEpK8s1lt0yvxrTng/MXxMMg7o9dO8\nmSlv/665twk/7NfkexJPWs1ph+54tud3Sa0TiVw6U5ObPMr1EYc7RnoELDR9Exc1\nUDJB+T3f3SGJicZn4pALyx132wKBgQDDd9lhqWLXJeG7JkInhLywuSVmnk077hY8\nxAfHqlO+/EEkM/YBtUvtC4KNiSkVftQwSM80NRiHlPvlWZ0G8l9RAfM/xxL/XUq6\nOu1fW1uJuukJgXFePV9SQLzfgd1fk1f/dKuiPSNhCzgTD7dFks0Ip6FD6/PCdN7x\neqRUqDccMwKBgQCTk1mW+7CiCTLkKjv2KScdgEhncnZd7bO1W8C/R7bVwgUQpVeb\nWDqjpvs3cDssCVYNAFDA9Wfq61hD6bzlV/AbpvARbfd5MzQ8OB4zBUmUVGfFJoIF\nbVLzeivlKNWNybETqs6+Bjt+a6Dnw1vuY0OwxE5Urb1g50rEkCvwKhsueQKBgQDB\nUt3rG46oX80cPkCbuUquNs/o6JRWu6m+u9s9/RYLBI6g8ctT8S2A6ytaNNgvbFsM\nzlYwunriTdW9Bp6p6jmfcyBUad4+NtTbz8BJ2Z91Xylwv1eS73xBa8nh/R0nlCEq\nhQfj1DgTmPcC0z5eT0z+TFzRQqK6JsEBcFzrZdvrxQKBgQDGEtWElG5XoDwnwO5e\nmfFLRKHfG+Xa6FChl2eeDIGOxv/Y6SMwT68t0gtnDFQGtcIMvC+kC/1Rv1v8yrOD\nCzEToh91Fw1c1eayNnsLq07qYnVWoMa7xFFSDhtBAnepqlU+81qaDvp+yILTIYSP\nwUuk3NpdJs2LkMetm5zJC9PJ2w==\n-----END PRIVATE KEY-----\n",
|
||||
"client_email": "firebase-adminsdk-umcph@zod-bank-ebb2a.iam.gserviceaccount.com",
|
||||
"client_id": "102629742363778142120",
|
||||
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||
"token_uri": "https://oauth2.googleapis.com/token",
|
||||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-umcph%40zod-bank-ebb2a.iam.gserviceaccount.com",
|
||||
"universe_domain": "googleapis.com"
|
||||
}
|
45
zod_bank/celery.py
Normal file
45
zod_bank/celery.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""
|
||||
Celery Basic configuration
|
||||
"""
|
||||
|
||||
# python imports
|
||||
import os
|
||||
|
||||
# third party imports
|
||||
from celery import Celery
|
||||
from dotenv import load_dotenv
|
||||
from celery.schedules import crontab
|
||||
|
||||
# OR, the same with increased verbosity:
|
||||
load_dotenv(verbose=True)
|
||||
|
||||
env_path = os.path.join(os.path.abspath(os.path.join('.env', os.pardir)), '.env')
|
||||
|
||||
load_dotenv(dotenv_path=env_path)
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', os.getenv('SETTINGS'))
|
||||
|
||||
# celery app
|
||||
app = Celery()
|
||||
|
||||
app.config_from_object('django.conf:settings')
|
||||
|
||||
# Load task modules from all registered Django apps.
|
||||
app.autodiscover_tasks()
|
||||
|
||||
|
||||
@app.task(bind=True)
|
||||
def debug_task(self):
|
||||
""" celery debug task """
|
||||
print(f'Request: {self.request!r}')
|
||||
|
||||
|
||||
"""cron task"""
|
||||
|
||||
|
||||
app.conf.beat_schedule = {
|
||||
"expired_task": {
|
||||
"task": "guardian.utils.update_expired_task_status",
|
||||
"schedule": crontab(minute=0, hour=0),
|
||||
},
|
||||
}
|
@ -9,13 +9,16 @@ https://docs.djangoproject.com/en/3.0/topics/settings/
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/3.0/ref/settings/
|
||||
"""
|
||||
|
||||
# Django Import
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from datetime import timedelta
|
||||
from firebase_admin import initialize_app
|
||||
|
||||
load_dotenv()
|
||||
# OR, the same with increased verbosity:
|
||||
load_dotenv(verbose=True)
|
||||
# env path
|
||||
env_path = os.path.join(os.path.abspath(os.path.join('.env', os.pardir)), '.env')
|
||||
load_dotenv(dotenv_path=env_path)
|
||||
|
||||
@ -29,37 +32,49 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = '-pb+8w#)6qsh+w&tr+q$tholf7=54v%05e^9!lneiqqgtddg6q'
|
||||
|
||||
SECRET_KEY = os.getenv('SECRET_KEY')
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
DEBUG = os.getenv('DEBUG')
|
||||
|
||||
# cors allow setting
|
||||
CORS_ORIGIN_ALLOW_ALL = True
|
||||
|
||||
# allow all host
|
||||
ALLOWED_HOSTS = ['*']
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
# Add your installed Django apps here
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
# Add Django rest frame work apps here
|
||||
'django_extensions',
|
||||
'storages',
|
||||
'drf_yasg',
|
||||
'corsheaders',
|
||||
'django.contrib.postgres',
|
||||
'rest_framework',
|
||||
'fcm_django',
|
||||
'django_celery_beat',
|
||||
# Add your custom apps here.
|
||||
'django_ses',
|
||||
'account',
|
||||
'junior',
|
||||
'guardian',
|
||||
'notifications',
|
||||
'web_admin',
|
||||
# 'social_django'
|
||||
]
|
||||
|
||||
# define middle ware here
|
||||
MIDDLEWARE = [
|
||||
# Add your middleware classes here.
|
||||
'corsheaders.middleware.CorsMiddleware',
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
@ -67,10 +82,13 @@ MIDDLEWARE = [
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'account.custom_middleware.CustomMiddleware'
|
||||
]
|
||||
|
||||
# define root
|
||||
ROOT_URLCONF = 'zod_bank.urls'
|
||||
|
||||
# define templates
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
@ -87,18 +105,22 @@ TEMPLATES = [
|
||||
},
|
||||
]
|
||||
|
||||
# define wsgi
|
||||
WSGI_APPLICATION = 'zod_bank.wsgi.application'
|
||||
# define rest frame work
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||||
# 'rest_framework.authentication.SessionAuthentication',
|
||||
'rest_framework.authentication.BasicAuthentication',
|
||||
'rest_framework_simplejwt.authentication.JWTAuthentication',],
|
||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
||||
'PAGE_SIZE': 5,
|
||||
'PAGE_SIZE': 10,
|
||||
}
|
||||
# define jwt token
|
||||
SIMPLE_JWT = {
|
||||
'ACCESS_TOKEN_LIFETIME': timedelta(hours=23, minutes=59, seconds=59, microseconds=999999),
|
||||
'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
|
||||
'ACCESS_TOKEN_LIFETIME': timedelta(hours=2, minutes=59, seconds=59, microseconds=999999),
|
||||
'REFRESH_TOKEN_LIFETIME': timedelta(hours=71, minutes=59, seconds=59, microseconds=999999),
|
||||
|
||||
}
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
|
||||
@ -112,7 +134,7 @@ DATABASES = {
|
||||
'PORT':os.getenv('DB_PORT'),
|
||||
}
|
||||
}
|
||||
|
||||
# define swagger setting
|
||||
SWAGGER_SETTINGS = {
|
||||
"exclude_namespaces": [],
|
||||
"api_version": '0.1',
|
||||
@ -139,6 +161,7 @@ SWAGGER_SETTINGS = {
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
|
||||
|
||||
# password validation
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
@ -158,20 +181,21 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/3.0/topics/i18n/
|
||||
|
||||
# language code
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
# time zone
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
# define I18N
|
||||
USE_I18N = True
|
||||
|
||||
# define L10N
|
||||
USE_L10N = True
|
||||
|
||||
# define TZ
|
||||
USE_TZ = True
|
||||
# cors header settings
|
||||
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
||||
|
||||
CORS_ORIGIN_ALLOW_ALL = True
|
||||
|
||||
# cors allow method
|
||||
CORS_ALLOW_METHODS = (
|
||||
'DELETE',
|
||||
'GET',
|
||||
@ -180,7 +204,7 @@ CORS_ALLOW_METHODS = (
|
||||
'POST',
|
||||
'PUT',
|
||||
)
|
||||
|
||||
# cors allow header
|
||||
CORS_ALLOW_HEADERS = (
|
||||
'accept',
|
||||
'accept-encoding',
|
||||
@ -193,24 +217,48 @@ CORS_ALLOW_HEADERS = (
|
||||
'x-requested-with',
|
||||
)
|
||||
|
||||
CORS_EXPOSE_HEADERS = (
|
||||
'Access-Control-Allow-Origin: *',
|
||||
)
|
||||
|
||||
# Firebase settings
|
||||
FIREBASE_APP = initialize_app()
|
||||
|
||||
# fcm django settings
|
||||
FCM_DJANGO_SETTINGS = {
|
||||
"APP_VERBOSE_NAME": "ZOD_Bank",
|
||||
"ONE_DEVICE_PER_USER": False,
|
||||
"DELETE_INACTIVE_DEVICES": True,
|
||||
"UPDATE_ON_DUPLICATE_REG_ID": True,
|
||||
}
|
||||
|
||||
"""Static files (CSS, JavaScript, Images)
|
||||
https://docs.djangoproject.com/en/3.0/howto/static-files/"""
|
||||
|
||||
|
||||
# google client id
|
||||
GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID')
|
||||
# google client secret key
|
||||
GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET')
|
||||
|
||||
# CELERY SETUP
|
||||
CELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL')
|
||||
CELERY_RESULT_BACKEND = os.getenv('CELERY_RESULT_BACKEND')
|
||||
CELERY_ACCEPT_CONTENT = ['application/json']
|
||||
CELERY_TASK_SERIALIZER = 'json'
|
||||
CELERY_RESULT_SERIALIZER = 'json'
|
||||
|
||||
EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend"
|
||||
EMAIL_HOST="smtp.sendgrid.net"
|
||||
EMAIL_PORT="587"
|
||||
EMAIL_USE_TLS="True"
|
||||
EMAIL_HOST_USER="apikey"
|
||||
EMAIL_HOST_PASSWORD="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"
|
||||
EMAIL_FROM_ADDRESS="support@zodbank.com"
|
||||
DEFAULT_ADDRESS="zodbank@yopmail.com"
|
||||
# email settings
|
||||
EMAIL_BACKEND = os.getenv("MAIL_BACKEND")
|
||||
EMAIL_HOST = os.getenv("MAIL_HOST")
|
||||
EMAIL_PORT = os.getenv("MAIL_PORT")
|
||||
EMAIL_USE_TLS = os.getenv("MAIL_USE_TLS")
|
||||
EMAIL_HOST_USER = os.getenv("MAIL_HOST_USER")
|
||||
EMAIL_HOST_PASSWORD = os.getenv("MAIL_HOST_PASSWORD")
|
||||
EMAIL_FROM_ADDRESS = os.getenv("MAIL_FROM_ADDRESS")
|
||||
DEFAULT_ADDRESS = os.getenv("DEFAULT_ADDRESS")
|
||||
|
||||
|
||||
# ali baba cloud settings
|
||||
|
||||
ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID')
|
||||
ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET')
|
||||
@ -219,6 +267,10 @@ ALIYUN_OSS_ENDPOINT = os.getenv('ALIYUN_OSS_ENDPOINT')
|
||||
ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION')
|
||||
|
||||
|
||||
# define static url
|
||||
STATIC_URL = 'static/'
|
||||
# define static root
|
||||
STATIC_ROOT = 'static'
|
||||
|
||||
MEDIA_URL = "/media/"
|
||||
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')
|
||||
|
@ -30,4 +30,6 @@ urlpatterns = [
|
||||
path('', include(('account.urls', 'account'), namespace='account')),
|
||||
path('', include('guardian.urls')),
|
||||
path('', include(('junior.urls', 'junior'), namespace='junior')),
|
||||
path('', include(('notifications.urls', 'notifications'), namespace='notifications')),
|
||||
path('', include(('web_admin.urls', 'web_admin'), namespace='web_admin')),
|
||||
]
|
||||
|
Reference in New Issue
Block a user