mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-27 09:04:54 +00:00
Merge branch 'dev' into ZBKADM-67
This commit is contained in:
@ -61,7 +61,8 @@ TASK_STATUS = (
|
|||||||
('2', 'in-progress'),
|
('2', 'in-progress'),
|
||||||
('3', 'rejected'),
|
('3', 'rejected'),
|
||||||
('4', 'requested'),
|
('4', 'requested'),
|
||||||
('5', 'completed')
|
('5', 'completed'),
|
||||||
|
('6', 'expired')
|
||||||
)
|
)
|
||||||
# sign up method
|
# sign up method
|
||||||
SIGNUP_METHODS = (
|
SIGNUP_METHODS = (
|
||||||
@ -103,3 +104,6 @@ MAX_ARTICLE_CARD = 6
|
|||||||
# min and max survey
|
# min and max survey
|
||||||
MIN_ARTICLE_SURVEY = 5
|
MIN_ARTICLE_SURVEY = 5
|
||||||
MAX_ARTICLE_SURVEY = 10
|
MAX_ARTICLE_SURVEY = 10
|
||||||
|
|
||||||
|
# real time url
|
||||||
|
time_url = "http://worldtimeapi.org/api/timezone/Asia/Riyadh"
|
||||||
|
|||||||
@ -74,7 +74,16 @@ ERROR_CODE = {
|
|||||||
"2041": "Article with given id doesn't exist.",
|
"2041": "Article with given id doesn't exist.",
|
||||||
"2042": "Article Card with given id doesn't exist.",
|
"2042": "Article Card with given id doesn't exist.",
|
||||||
"2043": "Article Survey with given id doesn't exist.",
|
"2043": "Article Survey with given id doesn't exist.",
|
||||||
"2044": "Task does not exist"
|
"2044": "Task does not exist",
|
||||||
|
"2045": "Invalid guardian",
|
||||||
|
"2046": "Due date must be future date",
|
||||||
|
# invalid junior id msg
|
||||||
|
"2047": "Invalid Junior ID ",
|
||||||
|
"2048": "Choose right file for image",
|
||||||
|
"2049": "This task is already requested ",
|
||||||
|
"2059": "Already exist junior",
|
||||||
|
"2060": "Task does not exist or not in pending state",
|
||||||
|
"2061": "Please insert image or check the image is valid or not."
|
||||||
}
|
}
|
||||||
"""Success message code"""
|
"""Success message code"""
|
||||||
SUCCESS_CODE = {
|
SUCCESS_CODE = {
|
||||||
@ -122,6 +131,8 @@ SUCCESS_CODE = {
|
|||||||
"3031": "Article Survey has been removed successfully.",
|
"3031": "Article Survey has been removed successfully.",
|
||||||
"3032": "Task request sent successfully",
|
"3032": "Task request sent successfully",
|
||||||
"3033": "Valid Referral code",
|
"3033": "Valid Referral code",
|
||||||
|
"3034": "Invite guardian successfully",
|
||||||
|
"3035": "Task started successfully"
|
||||||
}
|
}
|
||||||
"""status code error"""
|
"""status code error"""
|
||||||
STATUS_CODE_ERROR = {
|
STATUS_CODE_ERROR = {
|
||||||
|
|||||||
BIN
celerybeat-schedule
Normal file
BIN
celerybeat-schedule
Normal file
Binary file not shown.
@ -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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -5,7 +5,7 @@ from django.contrib.auth import get_user_model
|
|||||||
"""Import Django app"""
|
"""Import Django app"""
|
||||||
from base.constants import GENDERS, TASK_STATUS, PENDING, TASK_POINTS, SIGNUP_METHODS
|
from base.constants import GENDERS, TASK_STATUS, PENDING, TASK_POINTS, SIGNUP_METHODS
|
||||||
"""import Junior model"""
|
"""import Junior model"""
|
||||||
from junior.models import Junior
|
import junior.models
|
||||||
"""Add user model"""
|
"""Add user model"""
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
@ -53,6 +53,10 @@ class Guardian(models.Model):
|
|||||||
gender = models.CharField(choices=GENDERS, max_length=15, null=True, blank=True, default=None)
|
gender = models.CharField(choices=GENDERS, max_length=15, null=True, blank=True, default=None)
|
||||||
"""date of birth of the guardian"""
|
"""date of birth of the guardian"""
|
||||||
dob = models.DateField(max_length=15, null=True, blank=True, default=None)
|
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"""
|
"""Profile activity"""
|
||||||
is_active = models.BooleanField(default=True)
|
is_active = models.BooleanField(default=True)
|
||||||
"""guardian is verified or not"""
|
"""guardian is verified or not"""
|
||||||
@ -101,7 +105,7 @@ class JuniorTask(models.Model):
|
|||||||
"""image that is uploaded by junior"""
|
"""image that is uploaded by junior"""
|
||||||
image = models.URLField(null=True, blank=True, default=None)
|
image = models.URLField(null=True, blank=True, default=None)
|
||||||
"""associated junior with the task"""
|
"""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"""
|
||||||
task_status = models.CharField(choices=TASK_STATUS, max_length=15, default=PENDING)
|
task_status = models.CharField(choices=TASK_STATUS, max_length=15, default=PENDING)
|
||||||
"""task is active or not"""
|
"""task is active or not"""
|
||||||
|
|||||||
@ -169,6 +169,14 @@ class TaskSerializer(serializers.ModelSerializer):
|
|||||||
"""Meta info"""
|
"""Meta info"""
|
||||||
model = JuniorTask
|
model = JuniorTask
|
||||||
fields = ['id', 'task_name','task_description','points', 'due_date', 'junior', 'default_image']
|
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):
|
def create(self, validated_data):
|
||||||
"""create default task image data"""
|
"""create default task image data"""
|
||||||
validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last()
|
validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last()
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import tempfile
|
|||||||
# Import date time module's function
|
# Import date time module's function
|
||||||
from datetime import datetime, time
|
from datetime import datetime, time
|
||||||
# import Number constant
|
# import Number constant
|
||||||
from base.constants import NUMBER
|
from base.constants import NUMBER, time_url
|
||||||
# Import Junior's model
|
# Import Junior's model
|
||||||
from junior.models import Junior, JuniorPoints
|
from junior.models import Junior, JuniorPoints
|
||||||
|
|
||||||
@ -45,8 +45,9 @@ def upload_image_to_alibaba(image, filename):
|
|||||||
return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}"
|
return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}"
|
||||||
|
|
||||||
|
|
||||||
def real_time(timezone='Asia/Riyadh'):
|
def real_time():
|
||||||
url = f'http://worldtimeapi.org/api/timezone/{timezone}'
|
"""fetch real time from world time api"""
|
||||||
|
url = time_url
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|||||||
@ -30,7 +30,7 @@ from account.utils import custom_response, custom_error_response
|
|||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
from base.constants import NUMBER
|
from base.constants import NUMBER
|
||||||
from .utils import upload_image_to_alibaba
|
from .utils import upload_image_to_alibaba
|
||||||
from notifications.constants import REGISTRATION
|
from notifications.constants import REGISTRATION, TASK_CREATED
|
||||||
from notifications.utils import send_notification
|
from notifications.utils import send_notification
|
||||||
|
|
||||||
""" Define APIs """
|
""" Define APIs """
|
||||||
@ -51,7 +51,8 @@ class SignupViewset(viewsets.ModelViewSet):
|
|||||||
serializer_class = UserSerializer
|
serializer_class = UserSerializer
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
"""Create user profile"""
|
"""Create user profile"""
|
||||||
if request.data['user_type'] in ['1', '2']:
|
try:
|
||||||
|
if request.data['user_type'] in [str(NUMBER['one']), str(NUMBER['two'])]:
|
||||||
serializer = UserSerializer(context=request.data['user_type'], data=request.data)
|
serializer = UserSerializer(context=request.data['user_type'], data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
user = serializer.save()
|
user = serializer.save()
|
||||||
@ -69,6 +70,8 @@ class SignupViewset(viewsets.ModelViewSet):
|
|||||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
else:
|
else:
|
||||||
return custom_error_response(ERROR_CODE['2028'], response_status=status.HTTP_400_BAD_REQUEST)
|
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):
|
class UpdateGuardianProfile(viewsets.ViewSet):
|
||||||
"""Update guardian profile"""
|
"""Update guardian profile"""
|
||||||
@ -78,6 +81,7 @@ class UpdateGuardianProfile(viewsets.ViewSet):
|
|||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
"""Create guardian profile"""
|
"""Create guardian profile"""
|
||||||
|
try:
|
||||||
data = request.data
|
data = request.data
|
||||||
image = request.data.get('image')
|
image = request.data.get('image')
|
||||||
image_url = ''
|
image_url = ''
|
||||||
@ -98,6 +102,8 @@ class UpdateGuardianProfile(viewsets.ViewSet):
|
|||||||
serializer.save()
|
serializer.save()
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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)
|
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):
|
class AllTaskListAPIView(viewsets.ModelViewSet):
|
||||||
@ -119,9 +125,11 @@ class TaskListAPIView(viewsets.ModelViewSet):
|
|||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
pagination_class = PageNumberPagination
|
pagination_class = PageNumberPagination
|
||||||
queryset = JuniorTask.objects.all()
|
queryset = JuniorTask.objects.all()
|
||||||
|
http_method_names = ('get',)
|
||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
"""Create guardian profile"""
|
"""Create guardian profile"""
|
||||||
|
try:
|
||||||
status_value = self.request.GET.get('status')
|
status_value = self.request.GET.get('status')
|
||||||
search = self.request.GET.get('search')
|
search = self.request.GET.get('search')
|
||||||
if search and str(status_value) == '0':
|
if search and str(status_value) == '0':
|
||||||
@ -141,14 +149,25 @@ class TaskListAPIView(viewsets.ModelViewSet):
|
|||||||
# use TaskDetailsSerializer serializer
|
# use TaskDetailsSerializer serializer
|
||||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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):
|
class CreateTaskAPIView(viewsets.ModelViewSet):
|
||||||
"""create task for junior"""
|
"""create task for junior"""
|
||||||
serializer_class = TaskSerializer
|
serializer_class = TaskSerializer
|
||||||
queryset = JuniorTask.objects.all()
|
queryset = JuniorTask.objects.all()
|
||||||
|
http_method_names = ('post', )
|
||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
|
try:
|
||||||
image = request.data['default_image']
|
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
|
data = request.data
|
||||||
if 'https' in str(image):
|
if 'https' in str(image):
|
||||||
image_data = image
|
image_data = image
|
||||||
@ -164,15 +183,18 @@ class CreateTaskAPIView(viewsets.ModelViewSet):
|
|||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
# save serializer
|
# save serializer
|
||||||
serializer.save()
|
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_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)
|
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):
|
class SearchTaskListAPIView(viewsets.ModelViewSet):
|
||||||
"""Update guardian profile"""
|
"""Update guardian profile"""
|
||||||
serializer_class = TaskDetailsSerializer
|
serializer_class = TaskDetailsSerializer
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
pagination_class = PageNumberPagination
|
pagination_class = PageNumberPagination
|
||||||
queryset = JuniorTask.objects.all()
|
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""Get the queryset for the view"""
|
"""Get the queryset for the view"""
|
||||||
@ -184,14 +206,16 @@ class SearchTaskListAPIView(viewsets.ModelViewSet):
|
|||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
"""Create guardian profile"""
|
"""Create guardian profile"""
|
||||||
|
try:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
|
|
||||||
paginator = self.pagination_class()
|
paginator = self.pagination_class()
|
||||||
# use pagination
|
# use pagination
|
||||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||||
# use TaskSerializer serializer
|
# use TaskSerializer serializer
|
||||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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):
|
class TopJuniorListAPIView(viewsets.ModelViewSet):
|
||||||
@ -208,6 +232,7 @@ class TopJuniorListAPIView(viewsets.ModelViewSet):
|
|||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
"""Fetch junior list of those who complete their tasks"""
|
"""Fetch junior list of those who complete their tasks"""
|
||||||
|
try:
|
||||||
junior_total_points = self.get_queryset().order_by('-total_points')
|
junior_total_points = self.get_queryset().order_by('-total_points')
|
||||||
|
|
||||||
# Update the position field for each JuniorPoints object
|
# Update the position field for each JuniorPoints object
|
||||||
@ -217,6 +242,8 @@ class TopJuniorListAPIView(viewsets.ModelViewSet):
|
|||||||
|
|
||||||
serializer = self.get_serializer(junior_total_points, many=True)
|
serializer = self.get_serializer(junior_total_points, many=True)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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):
|
class ApproveJuniorAPIView(viewsets.ViewSet):
|
||||||
@ -233,6 +260,7 @@ class ApproveJuniorAPIView(viewsets.ViewSet):
|
|||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
""" junior list"""
|
""" junior list"""
|
||||||
|
try:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
# action 1 is use for approve and 2 for reject
|
# action 1 is use for approve and 2 for reject
|
||||||
if request.data['action'] == '1':
|
if request.data['action'] == '1':
|
||||||
@ -246,6 +274,8 @@ class ApproveJuniorAPIView(viewsets.ViewSet):
|
|||||||
return custom_response(SUCCESS_CODE['3023'], serializer.data, response_status=status.HTTP_200_OK)
|
return custom_response(SUCCESS_CODE['3023'], serializer.data, response_status=status.HTTP_200_OK)
|
||||||
else:
|
else:
|
||||||
return custom_response(SUCCESS_CODE['3024'], response_status=status.HTTP_200_OK)
|
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):
|
class ApproveTaskAPIView(viewsets.ViewSet):
|
||||||
"""approve junior by guardian"""
|
"""approve junior by guardian"""
|
||||||
@ -264,6 +294,7 @@ class ApproveTaskAPIView(viewsets.ViewSet):
|
|||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
""" junior list"""
|
""" junior list"""
|
||||||
# action 1 is use for approve and 2 for reject
|
# action 1 is use for approve and 2 for reject
|
||||||
|
try:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
# use ApproveJuniorSerializer serializer
|
# use ApproveJuniorSerializer serializer
|
||||||
serializer = ApproveTaskSerializer(context={"guardian_code": queryset[0].guardian_code,
|
serializer = ApproveTaskSerializer(context={"guardian_code": queryset[0].guardian_code,
|
||||||
@ -281,3 +312,6 @@ class ApproveTaskAPIView(viewsets.ViewSet):
|
|||||||
return custom_response(SUCCESS_CODE['3026'], response_status=status.HTTP_200_OK)
|
return custom_response(SUCCESS_CODE['3026'], response_status=status.HTTP_200_OK)
|
||||||
else:
|
else:
|
||||||
return custom_response(ERROR_CODE['2038'], response_status=status.HTTP_400_BAD_REQUEST)
|
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)
|
||||||
|
|
||||||
|
|||||||
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',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -7,6 +7,9 @@ from django.contrib.auth import get_user_model
|
|||||||
from django.contrib.postgres.fields import ArrayField
|
from django.contrib.postgres.fields import ArrayField
|
||||||
"""Import django app"""
|
"""Import django app"""
|
||||||
from base.constants import GENDERS, SIGNUP_METHODS, RELATIONSHIP
|
from base.constants import GENDERS, SIGNUP_METHODS, RELATIONSHIP
|
||||||
|
# Import guardian's model
|
||||||
|
from guardian.models import Guardian
|
||||||
|
|
||||||
"""Define User model"""
|
"""Define User model"""
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
@ -31,7 +34,7 @@ User = get_user_model()
|
|||||||
"""Define junior points model"""
|
"""Define junior points model"""
|
||||||
# points of the junior
|
# points of the junior
|
||||||
# position of the junior
|
# position of the junior
|
||||||
|
# define junior guardian relation model
|
||||||
class Junior(models.Model):
|
class Junior(models.Model):
|
||||||
"""Junior model"""
|
"""Junior model"""
|
||||||
auth = models.ForeignKey(User, on_delete=models.CASCADE, related_name='junior_profile', verbose_name='Email')
|
auth = models.ForeignKey(User, on_delete=models.CASCADE, related_name='junior_profile', verbose_name='Email')
|
||||||
@ -109,3 +112,28 @@ class JuniorPoints(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Return email id"""
|
"""Return email id"""
|
||||||
return f'{self.junior.auth}'
|
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}'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,8 @@ from guardian.models import Guardian, JuniorTask
|
|||||||
from account.models import UserEmailOtp
|
from account.models import UserEmailOtp
|
||||||
from junior.utils import junior_notification_email, junior_approval_mail
|
from junior.utils import junior_notification_email, junior_approval_mail
|
||||||
from guardian.utils import real_time, update_referral_points
|
from guardian.utils import real_time, update_referral_points
|
||||||
|
from notifications.utils import send_notification
|
||||||
|
from notifications.constants import INVITED_GUARDIAN, APPROVED_JUNIOR
|
||||||
|
|
||||||
|
|
||||||
class ListCharField(serializers.ListField):
|
class ListCharField(serializers.ListField):
|
||||||
@ -272,7 +273,6 @@ class AddJuniorSerializer(serializers.ModelSerializer):
|
|||||||
user_data = User.objects.create(username=email, email=email,
|
user_data = User.objects.create(username=email, email=email,
|
||||||
first_name=self.context['first_name'],
|
first_name=self.context['first_name'],
|
||||||
last_name=self.context['last_name'])
|
last_name=self.context['last_name'])
|
||||||
|
|
||||||
password = User.objects.make_random_password()
|
password = User.objects.make_random_password()
|
||||||
user_data.set_password(password)
|
user_data.set_password(password)
|
||||||
user_data.save()
|
user_data.save()
|
||||||
@ -288,8 +288,6 @@ class AddJuniorSerializer(serializers.ModelSerializer):
|
|||||||
expiry_time = timezone.now() + timezone.timedelta(days=1)
|
expiry_time = timezone.now() + timezone.timedelta(days=1)
|
||||||
UserEmailOtp.objects.create(email=email, otp=otp_value,
|
UserEmailOtp.objects.create(email=email, otp=otp_value,
|
||||||
user_type='1', expired_at=expiry_time, is_verified=True)
|
user_type='1', expired_at=expiry_time, is_verified=True)
|
||||||
"""Send email to the register user"""
|
|
||||||
send_otp_email(email, otp_value)
|
|
||||||
"""Notification email"""
|
"""Notification email"""
|
||||||
junior_notification_email(email, full_name, email, password)
|
junior_notification_email(email, full_name, email, password)
|
||||||
junior_approval_mail(guardian, full_name)
|
junior_approval_mail(guardian, full_name)
|
||||||
@ -374,3 +372,70 @@ class JuniorPointsSerializer(serializers.ModelSerializer):
|
|||||||
model = Junior
|
model = Junior
|
||||||
fields = ['junior_id', 'total_points', 'position', 'pending_task', 'in_progress_task', 'completed_task',
|
fields = ['junior_id', 'total_points', 'position', 'pending_task', 'in_progress_task', 'completed_task',
|
||||||
'requested_task', 'rejected_task']
|
'requested_task', 'rejected_task']
|
||||||
|
|
||||||
|
class AddGuardianSerializer(serializers.ModelSerializer):
|
||||||
|
"""Add guardian 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 = Guardian
|
||||||
|
fields = ['id', 'auth_token']
|
||||||
|
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
""" invite and create guardian"""
|
||||||
|
with transaction.atomic():
|
||||||
|
email = self.context['email']
|
||||||
|
junior = self.context['user']
|
||||||
|
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=user).update(is_invited=True,
|
||||||
|
referral_code=generate_code(ZOD,
|
||||||
|
instance.id),
|
||||||
|
referral_code_used=junior_data.referral_code,
|
||||||
|
is_verified=True)
|
||||||
|
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.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)
|
||||||
|
"""Notification email"""
|
||||||
|
junior_notification_email(email, full_name, email, password)
|
||||||
|
junior_approval_mail(email, full_name)
|
||||||
|
send_notification(INVITED_GUARDIAN, None, junior_data.auth.id, {})
|
||||||
|
send_notification(APPROVED_JUNIOR, None, email, {})
|
||||||
|
return instance
|
||||||
|
|
||||||
|
class StartTaskSerializer(serializers.ModelSerializer):
|
||||||
|
"""User task Serializer"""
|
||||||
|
class Meta(object):
|
||||||
|
"""Meta class"""
|
||||||
|
model = JuniorTask
|
||||||
|
fields = ('id', 'task_status')
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
instance.task_status = str(NUMBER['two'])
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|||||||
@ -3,7 +3,8 @@
|
|||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from .views import (UpdateJuniorProfile, ValidateGuardianCode, JuniorListAPIView, AddJuniorAPIView,
|
from .views import (UpdateJuniorProfile, ValidateGuardianCode, JuniorListAPIView, AddJuniorAPIView,
|
||||||
InvitedJuniorAPIView, FilterJuniorAPIView, RemoveJuniorAPIView, JuniorTaskListAPIView,
|
InvitedJuniorAPIView, FilterJuniorAPIView, RemoveJuniorAPIView, JuniorTaskListAPIView,
|
||||||
CompleteJuniorTaskAPIView, JuniorPointsListAPIView, ValidateReferralCode)
|
CompleteJuniorTaskAPIView, JuniorPointsListAPIView, ValidateReferralCode,
|
||||||
|
InviteGuardianAPIView, StartTaskAPIView)
|
||||||
"""Third party import"""
|
"""Third party import"""
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
@ -38,9 +39,12 @@ router.register('junior-task-list', JuniorTaskListAPIView, basename='junior-task
|
|||||||
router.register('junior-points', JuniorPointsListAPIView, basename='junior-points')
|
router.register('junior-points', JuniorPointsListAPIView, basename='junior-points')
|
||||||
# validate referral code API"""
|
# validate referral code API"""
|
||||||
router.register('validate-referral-code', ValidateReferralCode, basename='validate-referral-code')
|
router.register('validate-referral-code', ValidateReferralCode, basename='validate-referral-code')
|
||||||
|
# invite guardian API"""
|
||||||
|
router.register('invite-guardian', InviteGuardianAPIView, basename='invite-guardian')
|
||||||
# Define url pattern"""
|
# Define url pattern"""
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/v1/', include(router.urls)),
|
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/complete-task/', CompleteJuniorTaskAPIView.as_view()),
|
||||||
|
path('api/v1/start-task/', StartTaskAPIView.as_view())
|
||||||
]
|
]
|
||||||
|
|||||||
@ -3,7 +3,8 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
"""Third party Django app"""
|
"""Third party Django app"""
|
||||||
from templated_email import send_templated_mail
|
from templated_email import send_templated_mail
|
||||||
|
from .models import JuniorPoints
|
||||||
|
from django.db.models import F
|
||||||
# junior notification
|
# junior notification
|
||||||
# email for sending email
|
# email for sending email
|
||||||
# when guardian create junior profile
|
# when guardian create junior profile
|
||||||
@ -45,3 +46,15 @@ def junior_approval_mail(guardian, full_name):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
return 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
|
||||||
|
|||||||
122
junior/views.py
122
junior/views.py
@ -4,6 +4,7 @@ import os
|
|||||||
from rest_framework import viewsets, status, generics,views
|
from rest_framework import viewsets, status, generics,views
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.pagination import PageNumberPagination
|
from rest_framework.pagination import PageNumberPagination
|
||||||
|
from django.contrib.auth.models import User
|
||||||
import requests
|
import requests
|
||||||
"""Django app import"""
|
"""Django app import"""
|
||||||
|
|
||||||
@ -19,15 +20,17 @@ import requests
|
|||||||
# Import account's serializer
|
# Import account's serializer
|
||||||
# Import account's task
|
# Import account's task
|
||||||
# import junior serializer
|
# import junior serializer
|
||||||
from junior.models import Junior
|
from junior.models import Junior, JuniorPoints
|
||||||
from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer,\
|
from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer,\
|
||||||
RemoveJuniorSerializer, CompleteTaskSerializer, JuniorPointsSerializer)
|
RemoveJuniorSerializer, CompleteTaskSerializer, JuniorPointsSerializer,
|
||||||
|
AddGuardianSerializer, StartTaskSerializer)
|
||||||
from guardian.models import Guardian, JuniorTask
|
from guardian.models import Guardian, JuniorTask
|
||||||
from guardian.serializers import TaskDetailsSerializer
|
from guardian.serializers import TaskDetailsSerializer
|
||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
from base.constants import NUMBER
|
from base.constants import NUMBER
|
||||||
from account.utils import custom_response, custom_error_response
|
from account.utils import custom_response, custom_error_response
|
||||||
from guardian.utils import upload_image_to_alibaba
|
from guardian.utils import upload_image_to_alibaba
|
||||||
|
from .utils import update_positions_based_on_points
|
||||||
|
|
||||||
""" Define APIs """
|
""" Define APIs """
|
||||||
# Define validate guardian code API,
|
# Define validate guardian code API,
|
||||||
@ -50,6 +53,7 @@ class UpdateJuniorProfile(viewsets.ViewSet):
|
|||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
"""Use CreateJuniorSerializer"""
|
"""Use CreateJuniorSerializer"""
|
||||||
|
try:
|
||||||
request_data = request.data
|
request_data = request.data
|
||||||
image = request.data.get('image')
|
image = request.data.get('image')
|
||||||
image_url = ''
|
image_url = ''
|
||||||
@ -71,6 +75,8 @@ class UpdateJuniorProfile(viewsets.ViewSet):
|
|||||||
serializer.save()
|
serializer.save()
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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)
|
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):
|
class ValidateGuardianCode(viewsets.ViewSet):
|
||||||
"""Check guardian code exist or not"""
|
"""Check guardian code exist or not"""
|
||||||
@ -79,6 +85,7 @@ class ValidateGuardianCode(viewsets.ViewSet):
|
|||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
"""check guardian code"""
|
"""check guardian code"""
|
||||||
|
try:
|
||||||
guardian_code = self.request.GET.get('guardian_code').split(',')
|
guardian_code = self.request.GET.get('guardian_code').split(',')
|
||||||
for code in guardian_code:
|
for code in guardian_code:
|
||||||
# fetch guardian object
|
# fetch guardian object
|
||||||
@ -87,6 +94,8 @@ class ValidateGuardianCode(viewsets.ViewSet):
|
|||||||
return custom_response(SUCCESS_CODE['3013'], response_status=status.HTTP_200_OK)
|
return custom_response(SUCCESS_CODE['3013'], response_status=status.HTTP_200_OK)
|
||||||
else:
|
else:
|
||||||
return custom_error_response(ERROR_CODE["2022"], response_status=status.HTTP_400_BAD_REQUEST)
|
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):
|
class JuniorListAPIView(viewsets.ModelViewSet):
|
||||||
"""Junior list of assosicated guardian"""
|
"""Junior list of assosicated guardian"""
|
||||||
@ -94,39 +103,37 @@ class JuniorListAPIView(viewsets.ModelViewSet):
|
|||||||
serializer_class = JuniorDetailListSerializer
|
serializer_class = JuniorDetailListSerializer
|
||||||
queryset = Junior.objects.all()
|
queryset = Junior.objects.all()
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
http_method_names = ('get',)
|
||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
""" junior list"""
|
""" junior list"""
|
||||||
print("self.request.META====>",self.request.META)
|
try:
|
||||||
print()
|
update_positions_based_on_points()
|
||||||
auth_token = self.request.META['HTTP_AUTHORIZATION']
|
|
||||||
headers_token = {
|
|
||||||
'Authorization': auth_token
|
|
||||||
}
|
|
||||||
print("auth_token====>", auth_token)
|
|
||||||
print("headers_token====>", headers_token)
|
|
||||||
print("os.getenv('BASE_URL')===>", os.getenv('BASE_URL'))
|
|
||||||
print("url====>", os.getenv('BASE_URL') + '/api/v1/top-junior/')
|
|
||||||
# url = requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token)
|
|
||||||
# print("url data====>",url)
|
|
||||||
|
|
||||||
# requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers_token)
|
|
||||||
guardian_data = Guardian.objects.filter(user__email=request.user).last()
|
guardian_data = Guardian.objects.filter(user__email=request.user).last()
|
||||||
# fetch junior object
|
# fetch junior object
|
||||||
|
if guardian_data:
|
||||||
queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code))
|
queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code))
|
||||||
# use JuniorDetailListSerializer serializer
|
# use JuniorDetailListSerializer serializer
|
||||||
serializer = JuniorDetailListSerializer(queryset, many=True)
|
serializer = JuniorDetailListSerializer(queryset, many=True)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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):
|
class AddJuniorAPIView(viewsets.ModelViewSet):
|
||||||
"""Add Junior by guardian"""
|
"""Add Junior by guardian"""
|
||||||
queryset = Junior.objects.all()
|
queryset = Junior.objects.all()
|
||||||
serializer_class = AddJuniorSerializer
|
serializer_class = AddJuniorSerializer
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
http_method_names = ('post',)
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
""" junior list"""
|
""" junior list"""
|
||||||
|
try:
|
||||||
info = {'user': request.user, 'email': request.data['email'], 'first_name': request.data['first_name'],
|
info = {'user': request.user, 'email': request.data['email'], 'first_name': request.data['first_name'],
|
||||||
'last_name': request.data['last_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
|
# use AddJuniorSerializer serializer
|
||||||
serializer = AddJuniorSerializer(data=request.data, context=info)
|
serializer = AddJuniorSerializer(data=request.data, context=info)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
@ -134,6 +141,8 @@ class AddJuniorAPIView(viewsets.ModelViewSet):
|
|||||||
serializer.save()
|
serializer.save()
|
||||||
return custom_response(SUCCESS_CODE['3021'], serializer.data, response_status=status.HTTP_200_OK)
|
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)
|
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):
|
class InvitedJuniorAPIView(viewsets.ModelViewSet):
|
||||||
"""Junior list of assosicated guardian"""
|
"""Junior list of assosicated guardian"""
|
||||||
@ -142,6 +151,7 @@ class InvitedJuniorAPIView(viewsets.ModelViewSet):
|
|||||||
queryset = Junior.objects.all()
|
queryset = Junior.objects.all()
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
pagination_class = PageNumberPagination
|
pagination_class = PageNumberPagination
|
||||||
|
http_method_names = ('get',)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""Get the queryset for the view"""
|
"""Get the queryset for the view"""
|
||||||
@ -151,6 +161,7 @@ class InvitedJuniorAPIView(viewsets.ModelViewSet):
|
|||||||
return junior_queryset
|
return junior_queryset
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
""" junior list"""
|
""" junior list"""
|
||||||
|
try:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
paginator = self.pagination_class()
|
paginator = self.pagination_class()
|
||||||
# pagination
|
# pagination
|
||||||
@ -158,6 +169,8 @@ class InvitedJuniorAPIView(viewsets.ModelViewSet):
|
|||||||
# use JuniorDetailListSerializer serializer
|
# use JuniorDetailListSerializer serializer
|
||||||
serializer = JuniorDetailListSerializer(paginated_queryset, many=True)
|
serializer = JuniorDetailListSerializer(paginated_queryset, many=True)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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):
|
class FilterJuniorAPIView(viewsets.ModelViewSet):
|
||||||
@ -166,6 +179,7 @@ class FilterJuniorAPIView(viewsets.ModelViewSet):
|
|||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
pagination_class = PageNumberPagination
|
pagination_class = PageNumberPagination
|
||||||
queryset = Junior.objects.all()
|
queryset = Junior.objects.all()
|
||||||
|
http_method_names = ('get',)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""Get the queryset for the view"""
|
"""Get the queryset for the view"""
|
||||||
@ -178,6 +192,7 @@ class FilterJuniorAPIView(viewsets.ModelViewSet):
|
|||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
"""Create guardian profile"""
|
"""Create guardian profile"""
|
||||||
|
try:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
paginator = self.pagination_class()
|
paginator = self.pagination_class()
|
||||||
# use Pagination
|
# use Pagination
|
||||||
@ -185,6 +200,8 @@ class FilterJuniorAPIView(viewsets.ModelViewSet):
|
|||||||
# use JuniorDetailListSerializer serializer
|
# use JuniorDetailListSerializer serializer
|
||||||
serializer = JuniorDetailListSerializer(paginated_queryset, many=True)
|
serializer = JuniorDetailListSerializer(paginated_queryset, many=True)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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):
|
class RemoveJuniorAPIView(views.APIView):
|
||||||
@ -194,6 +211,7 @@ class RemoveJuniorAPIView(views.APIView):
|
|||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
def put(self, request, format=None):
|
def put(self, request, format=None):
|
||||||
|
try:
|
||||||
junior_id = self.request.GET.get('id')
|
junior_id = self.request.GET.get('id')
|
||||||
guardian = Guardian.objects.filter(user__email=self.request.user).last()
|
guardian = Guardian.objects.filter(user__email=self.request.user).last()
|
||||||
# fetch junior query
|
# fetch junior query
|
||||||
@ -209,6 +227,8 @@ class RemoveJuniorAPIView(views.APIView):
|
|||||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
else:
|
else:
|
||||||
return custom_error_response(ERROR_CODE['2034'], response_status=status.HTTP_400_BAD_REQUEST)
|
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):
|
class JuniorTaskListAPIView(viewsets.ModelViewSet):
|
||||||
@ -217,9 +237,11 @@ class JuniorTaskListAPIView(viewsets.ModelViewSet):
|
|||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
pagination_class = PageNumberPagination
|
pagination_class = PageNumberPagination
|
||||||
queryset = JuniorTask.objects.all()
|
queryset = JuniorTask.objects.all()
|
||||||
|
http_method_names = ('get',)
|
||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
"""Create guardian profile"""
|
"""Create guardian profile"""
|
||||||
|
try:
|
||||||
status_value = self.request.GET.get('status')
|
status_value = self.request.GET.get('status')
|
||||||
search = self.request.GET.get('search')
|
search = self.request.GET.get('search')
|
||||||
if search and str(status_value) == '0':
|
if search and str(status_value) == '0':
|
||||||
@ -243,6 +265,8 @@ class JuniorTaskListAPIView(viewsets.ModelViewSet):
|
|||||||
# use TaskDetailsSerializer serializer
|
# use TaskDetailsSerializer serializer
|
||||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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):
|
class CompleteJuniorTaskAPIView(views.APIView):
|
||||||
@ -266,7 +290,10 @@ class CompleteJuniorTaskAPIView(views.APIView):
|
|||||||
# fetch junior query
|
# fetch junior query
|
||||||
task_queryset = JuniorTask.objects.filter(id=task_id, junior__auth__email=self.request.user).last()
|
task_queryset = JuniorTask.objects.filter(id=task_id, junior__auth__email=self.request.user).last()
|
||||||
if task_queryset:
|
if task_queryset:
|
||||||
# use RemoveJuniorSerializer serializer
|
# 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)
|
serializer = CompleteTaskSerializer(task_queryset, data={'image': image_url}, partial=True)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
# save serializer
|
# save serializer
|
||||||
@ -282,26 +309,28 @@ class JuniorPointsListAPIView(viewsets.ModelViewSet):
|
|||||||
"""Junior Points viewset"""
|
"""Junior Points viewset"""
|
||||||
serializer_class = JuniorPointsSerializer
|
serializer_class = JuniorPointsSerializer
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
http_method_names = ('get',)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""get queryset"""
|
"""get queryset"""
|
||||||
return JuniorTask.objects.filter(junior__auth__email=self.request.user).last()
|
return JuniorTask.objects.filter(junior__auth__email=self.request.user).last()
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
"""profile view"""
|
"""profile view"""
|
||||||
|
|
||||||
|
try:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
# update position of junior
|
# update position of junior
|
||||||
token = self.request.META['HTTP_AUTHORIZATION']
|
update_positions_based_on_points()
|
||||||
headers = {
|
|
||||||
'Authorization': token
|
|
||||||
}
|
|
||||||
# requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers)
|
|
||||||
# requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers)
|
|
||||||
serializer = JuniorPointsSerializer(queryset)
|
serializer = JuniorPointsSerializer(queryset)
|
||||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
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):
|
class ValidateReferralCode(viewsets.ViewSet):
|
||||||
"""Check guardian code exist or not"""
|
"""Check guardian code exist or not"""
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
http_method_names = ('get',)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""Get queryset based on referral_code."""
|
"""Get queryset based on referral_code."""
|
||||||
@ -320,6 +349,53 @@ class ValidateReferralCode(viewsets.ViewSet):
|
|||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
"""check guardian code"""
|
"""check guardian code"""
|
||||||
|
try:
|
||||||
if self.get_queryset():
|
if self.get_queryset():
|
||||||
return custom_response(SUCCESS_CODE['3033'], response_status=status.HTTP_200_OK)
|
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)
|
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:
|
||||||
|
info = {'user': request.user, 'email': request.data['email'], 'first_name': request.data['first_name'],
|
||||||
|
'last_name': request.data['last_name']}
|
||||||
|
# use AddJuniorSerializer serializer
|
||||||
|
serializer = AddGuardianSerializer(data=request.data, context=info)
|
||||||
|
if serializer.is_valid():
|
||||||
|
# save serializer
|
||||||
|
serializer.save()
|
||||||
|
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'], None, 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['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)
|
||||||
|
|||||||
@ -2,6 +2,9 @@
|
|||||||
notification constants file
|
notification constants file
|
||||||
"""
|
"""
|
||||||
REGISTRATION = 1
|
REGISTRATION = 1
|
||||||
|
TASK_CREATED = 2
|
||||||
|
INVITED_GUARDIAN = 3
|
||||||
|
APPROVED_JUNIOR = 4
|
||||||
TEST_NOTIFICATION = 99
|
TEST_NOTIFICATION = 99
|
||||||
|
|
||||||
NOTIFICATION_DICT = {
|
NOTIFICATION_DICT = {
|
||||||
@ -9,6 +12,18 @@ NOTIFICATION_DICT = {
|
|||||||
"title": "Successfully registered!",
|
"title": "Successfully registered!",
|
||||||
"body": "You have registered successfully. Now login and complete your profile."
|
"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 for associate the junior"
|
||||||
|
},
|
||||||
TEST_NOTIFICATION: {
|
TEST_NOTIFICATION: {
|
||||||
"title": "Test Notification",
|
"title": "Test Notification",
|
||||||
"body": "This notification is for testing purpose"
|
"body": "This notification is for testing purpose"
|
||||||
|
|||||||
@ -209,7 +209,7 @@ class DefaultArticleCardImageSerializer(serializers.ModelSerializer):
|
|||||||
:return: validated data
|
:return: validated data
|
||||||
"""
|
"""
|
||||||
if 'image' not in attrs and attrs.get('image') is None:
|
if 'image' not in attrs and attrs.get('image') is None:
|
||||||
raise serializers.ValidationError({'details': 'insert image'})
|
raise serializers.ValidationError({'details': ERROR_CODE['2061']})
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
|
|||||||
Reference in New Issue
Block a user