From 026bfb6da71a74b64102b3f5712320c6a6cf8e71 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 12:10:15 +0530 Subject: [PATCH 1/8] sonar fixes --- account/apps.py | 3 +++ account/models.py | 4 +++- account/serializers.py | 22 +++++++++------------- account/tests.py | 2 ++ account/urls.py | 14 +++++++++++--- account/utils.py | 15 ++++++++++++--- account/views.py | 11 ++++++++--- guardian/models.py | 9 ++++++++- guardian/serializers.py | 25 ++++++++++++++++--------- guardian/utils.py | 7 +++++++ guardian/views.py | 2 +- junior/serializers.py | 13 +++++++------ manage.py | 3 +++ zod_bank/settings.py | 15 ++++----------- 14 files changed, 94 insertions(+), 51 deletions(-) diff --git a/account/apps.py b/account/apps.py index 2b08f1a..dc67efe 100644 --- a/account/apps.py +++ b/account/apps.py @@ -1,6 +1,9 @@ +"""Account app file""" +"""Import Django""" from django.apps import AppConfig class AccountConfig(AppConfig): + """default configurations""" default_auto_field = 'django.db.models.BigAutoField' name = 'account' diff --git a/account/models.py b/account/models.py index 3eedfde..ac23e79 100644 --- a/account/models.py +++ b/account/models.py @@ -1,6 +1,8 @@ +"""Account module file""" +"""Django import""" from django.db import models -import random from django.contrib.auth.models import User +"""App import""" from base.constants import USER_TYPE # Create your models here. diff --git a/account/serializers.py b/account/serializers.py index b04df3e..db11233 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -1,25 +1,21 @@ +"""Account serializer""" +"""Django Imoprt""" from rest_framework import serializers from django.contrib.auth.models import User +from rest_framework_simplejwt.tokens import RefreshToken +"""App import""" from guardian.models import Guardian from junior.models import Junior from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages from base.constants import GUARDIAN, JUNIOR, SUPERUSER -from django.db import transaction from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR -from django.core.exceptions import ObjectDoesNotExist -from django.contrib.auth import authenticate -from rest_framework import viewsets, status -from rest_framework.decorators import action -from django.contrib.auth import authenticate, login -from rest_framework_simplejwt.tokens import RefreshToken -from guardian.utils import upload_image_to_alibaba -from .utils import get_token class GoogleLoginSerializer(serializers.Serializer): + """google login serializer""" access_token = serializers.CharField(max_length=5000, required=True) - class Meta: + class Meta(object): """meta class""" fields = ('access_token',) @@ -86,9 +82,8 @@ class ChangePasswordSerializer(serializers.Serializer): def validate_current_password(self, value): user = self.context - if self.context.password not in ('', None): - if user.check_password(value): - return value + 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') @@ -109,6 +104,7 @@ class ForgotPasswordSerializer(serializers.Serializer): email = serializers.EmailField() class SuperUserSerializer(serializers.ModelSerializer): + """Super admin serializer""" user_type = serializers.SerializerMethodField('get_user_type') def get_user_type(self, obj): diff --git a/account/tests.py b/account/tests.py index 7ce503c..f2435b9 100644 --- a/account/tests.py +++ b/account/tests.py @@ -1,3 +1,5 @@ +"""Test cases file of account""" +"""Django import""" from django.test import TestCase # Create your tests here. diff --git a/account/urls.py b/account/urls.py index c9929be..ffb2db6 100644 --- a/account/urls.py +++ b/account/urls.py @@ -1,9 +1,9 @@ """ Urls files""" """Django import""" from django.urls import path, include -from rest_framework.decorators import api_view """Third party import""" from rest_framework import routers +"""Import view functions""" from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, GoogleLoginViewSet, SigninWithApple, ProfileAPIViewSet, UploadImageAPIViewSet, @@ -13,15 +13,23 @@ router = routers.SimpleRouter() """API End points with router""" router.register('user', UserLogin, basename='user') +"""super admin login""" router.register('admin', UserLogin, basename='admin') +"""google login end point""" router.register('google-login', GoogleLoginViewSet, basename='admin') -# router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp') -# router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') +router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp') +router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') +"""email verification end point""" router.register('user-email-verification', UserEmailVerification, basename='user-email-verification') +"""Resend email otp end point""" router.register('resend-email-otp', ReSendEmailOtp, basename='resend-email-otp') +"""Profile end point""" router.register('profile', ProfileAPIViewSet, basename='profile') +"""Upload default task image end point""" router.register('upload-default-task-image', UploadImageAPIViewSet, basename='upload-default-task-image') +"""Fetch default task image end point""" router.register('default-task-image', DefaultImageAPIViewSet, basename='default-task-image') +"""Define url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), path('api/v1/forgot-password/', ForgotPasswordAPIView.as_view()), diff --git a/account/utils.py b/account/utils.py index 0848c03..4412e62 100644 --- a/account/utils.py +++ b/account/utils.py @@ -1,8 +1,9 @@ """Account utils""" -"""Third party Django app""" +"""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 from datetime import datetime @@ -11,6 +12,7 @@ from uuid import uuid4 import secrets def send_otp_email(recipient_email, otp): + """Send otp on email with template""" from_email = settings.EMAIL_FROM_ADDRESS recipient_list = [recipient_email] send_templated_mail( @@ -26,8 +28,8 @@ def send_otp_email(recipient_email, otp): def custom_response(detail, data=None, response_status=status.HTTP_200_OK): """Custom response code""" if not data: + """when data is none""" data = None - return Response({"data": data, "message": detail, "status": "success", "code": response_status}) @@ -39,6 +41,7 @@ def custom_error_response(detail, response_status): :return: Json response """ if not detail: + """when details is empty""" detail = {} return Response({"error": detail, "status": "failed", "code": response_status}) @@ -58,16 +61,20 @@ def generate_jwt_token(token_type: str, now_time: int, data: dict = dict): """ if type(data) == type: data = {} + """Update data dictionary""" data.update({ 'token_type': token_type, 'iss': 'your_site_url', 'iat': timegm(datetime.utcnow().utctimetuple()), 'jti': uuid4().hex }) + """Access and Refresh token""" TOKEN_TYPE = ["access", "refresh"] if token_type == TOKEN_TYPE[1]: + """Refresh token""" exp = now_time + settings.SIMPLE_JWT['REFRESH_TOKEN_LIFETIME'] else: + """access token""" exp = now_time + settings.SIMPLE_JWT['ACCESS_TOKEN_LIFETIME'] data.update({ @@ -84,10 +91,12 @@ def generate_jwt_token(token_type: str, now_time: int, data: dict = dict): def get_token(data: dict = dict): """ create access and refresh token """ now_time = datetime.utcnow() + """generate access token""" access = generate_jwt_token('access', now_time, data) + """generate refresh token""" refresh = generate_jwt_token('refresh', now_time, data) return { 'access': access, 'refresh': refresh - } \ No newline at end of file + } diff --git a/account/views.py b/account/views.py index 15b8cff..1324869 100644 --- a/account/views.py +++ b/account/views.py @@ -1,17 +1,20 @@ +"""Account view """ +"""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 django.utils import timezone import jwt +"""App Import""" from guardian.utils import upload_image_to_alibaba from django.contrib.auth import authenticate, login from guardian.models import Guardian from junior.models import Junior from account.models import UserProfile, UserPhoneOtp, UserEmailOtp, DefaultTaskImages from django.contrib.auth.models import User +"""Account serializer""" from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, @@ -29,12 +32,13 @@ from rest_framework import status from rest_framework.response import Response import requests from django.conf import settings -from .utils import get_token from junior.serializers import JuniorProfileSerializer from guardian.serializers import GuardianProfileSerializer class GoogleLoginMixin: + """google login mixin""" def google_login(self, request): + """google login function""" access_token = request.data.get('access_token') user_type = request.data.get('user_type') if not access_token: @@ -341,7 +345,8 @@ 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}, response_status=status.HTTP_200_OK) + return custom_response(SUCCESS_CODE['3011'], {"auth_token":access_token}, + response_status=status.HTTP_200_OK) else: return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST) except Exception as e: diff --git a/guardian/models.py b/guardian/models.py index 00c5d42..1bb4d2c 100644 --- a/guardian/models.py +++ b/guardian/models.py @@ -5,6 +5,7 @@ from django.contrib.auth import get_user_model """Import Django app""" from base.constants import GENDERS, TASK_STATUS, PENDING, TASK_POINTS from junior.models import Junior +"""Add user model""" User = get_user_model() # Create your models here. @@ -44,16 +45,22 @@ class Guardian(models.Model): return f'{self.user}' class JuniorTask(models.Model): - """Guardian model""" + """Junior Task details 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 = models.CharField(max_length=500) + """points of the task""" points = models.IntegerField(default=TASK_POINTS) due_date = models.DateField(auto_now_add=False, null=True, blank=True) + """Images of task""" default_image = models.URLField(null=True, blank=True, default=None) 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') + """task status""" task_status = models.CharField(choices=TASK_STATUS, max_length=15, default=PENDING) + """task stage""" is_active = models.BooleanField(default=True) is_approved = models.BooleanField(default=False) """Profile created and updated time""" diff --git a/guardian/serializers.py b/guardian/serializers.py index 845f331..08c2861 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -114,17 +114,18 @@ class CreateGuardianSerializer(serializers.ModelSerializer): 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) - """Complete profile of the junior if below all data are filled""" - complete_profile_field = all([guardian.phone, guardian.gender, guardian.family_name, guardian.country_name, - guardian.dob, guardian.country_code, user.first_name, user.last_name]) - guardian.is_complete_profile = False - if complete_profile_field: - guardian.is_complete_profile = True image = validated_data.pop('image', None) if image: filename = f"images/{image.name}" image_url = upload_image_to_alibaba(image, filename) guardian.image = image_url + """Complete profile of the junior if below all data are filled""" + complete_profile_field = all([guardian.phone, guardian.gender, guardian.family_name, guardian.country_name, + guardian.dob, guardian.country_code, user.first_name, user.last_name, + user.email, guardian.image]) + guardian.is_complete_profile = False + if complete_profile_field: + guardian.is_complete_profile = True guardian.save() return guardian @@ -137,10 +138,13 @@ class CreateGuardianSerializer(serializers.ModelSerializer): class TaskSerializer(serializers.ModelSerializer): + """Task serializer""" class Meta(object): + """Meta info""" model = JuniorTask fields = ['task_name','task_description','points', 'due_date', 'junior', 'default_image'] def create(self, validated_data): + """create default task image data""" validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last() images = self.context['image'] validated_data['default_image'] = images @@ -173,23 +177,26 @@ class GuardianDetailSerializer(serializers.ModelSerializer): 'updated_at'] class TaskDetailsSerializer(serializers.ModelSerializer): - # guardian = GuardianDetailSerializer() junior = JuniorDetailSerializer() 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'] class TopJuniorSerializer(serializers.ModelSerializer): + """Top junior serializer""" junior = JuniorDetailSerializer() position = serializers.SerializerMethodField() - class Meta: + class Meta(object): + """Meta info""" model = JuniorPoints fields = ['id', 'junior', 'total_task_points', 'position', 'created_at', 'updated_at'] def get_position(self, obj): + """get position of junior""" queryset = self.context['view'].get_queryset() position = list(queryset).index(obj) + 1 return position @@ -238,4 +245,4 @@ class GuardianProfileSerializer(serializers.ModelSerializer): fields = ['id', 'email', 'first_name', 'last_name', 'country_name','country_code', 'phone', 'gender', 'dob', 'guardian_code', 'notification_count', 'total_count', 'complete_field_count', 'referral_code', 'is_active', 'is_complete_profile', 'created_at', 'image', - 'updated_at'] \ No newline at end of file + 'updated_at'] diff --git a/guardian/utils.py b/guardian/utils.py index 66a36ca..ff8472c 100644 --- a/guardian/utils.py +++ b/guardian/utils.py @@ -1,15 +1,22 @@ +"""Utiles file of guardian""" +"""Django import""" import oss2 from django.conf import settings import tempfile def upload_image_to_alibaba(image, filename): + """upload image on oss alibaba bucket""" # Save the image object to a temporary file with tempfile.NamedTemporaryFile(delete=False) as temp_file: + """write image in temporary file""" temp_file.write(image.read()) + """auth of bucket""" auth = oss2.Auth(settings.ALIYUN_OSS_ACCESS_KEY_ID, settings.ALIYUN_OSS_ACCESS_KEY_SECRET) + """fetch bucket details""" bucket = oss2.Bucket(auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME) # Upload the temporary file to Alibaba OSS bucket.put_object_from_file(filename, temp_file.name) + """create perfect url for image""" new_filename = filename.replace(' ', '%20') return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}" diff --git a/guardian/views.py b/guardian/views.py index ff6e332..9c43628 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -143,7 +143,7 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer - # permission_classes = [IsAuthenticated] + permission_classes = [IsAuthenticated] queryset = JuniorPoints.objects.all() def get_serializer_context(self): diff --git a/junior/serializers.py b/junior/serializers.py index b87c799..81ea0cb 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -89,16 +89,17 @@ class CreateJuniorSerializer(serializers.ModelSerializer): 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) - """Complete profile of the junior if below all data are filled""" - complete_profile_field = all([junior.phone, junior.gender, junior.country_name, - junior.dob, junior.country_code, user.first_name, user.last_name]) - junior.is_complete_profile = False - if complete_profile_field: - junior.is_complete_profile = True if image: filename = f"images/{image.name}" image_url = upload_image_to_alibaba(image, filename) junior.image = image_url + """Complete profile of the junior if below all data are filled""" + complete_profile_field = all([junior.phone, junior.gender, junior.country_name, junior.image, + junior.dob, junior.country_code, user.first_name, user.last_name, + user.email]) + junior.is_complete_profile = False + if complete_profile_field: + junior.is_complete_profile = True junior.save() return junior diff --git a/manage.py b/manage.py index fe9e065..a19d33b 100755 --- a/manage.py +++ b/manage.py @@ -1,7 +1,9 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" """Django import""" +"""Import OS module""" import os +"""Import sys module""" import sys @@ -18,6 +20,7 @@ def main(): "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc + """execute command line function""" execute_from_command_line(sys.argv) diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 6960233..6f43fdc 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -93,8 +93,8 @@ REST_FRAMEWORK = { # 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', 'rest_framework_simplejwt.authentication.JWTAuthentication',], - 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', - 'PAGE_SIZE': 5, # Set the default pagination size + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 5, } SIMPLE_JWT = { 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=50), @@ -201,22 +201,15 @@ https://docs.djangoproject.com/en/3.0/howto/static-files/""" GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID') GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET') -# EMAIL_BACKEND = os.getenv('EMAIL_BACKEND') -# EMAIL_HOST = os.getenv('EMAIL_HOST') -# EMAIL_PORT = os.getenv('EMAIL_PORT') -# EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS') -# EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER') # Replace with your Gmail email address -# EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD') # Replace with your Gmail email password or App password -# EMAIL_FROM_ADDRESS = os.getenv('EMAIL_FROM_ADDRESS') EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend" EMAIL_HOST="smtp.sendgrid.net" EMAIL_PORT="587" EMAIL_USE_TLS="True" -EMAIL_HOST_USER="apikey" # Replace with your Gmail email address +EMAIL_HOST_USER="apikey" EMAIL_HOST_PASSWORD="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" EMAIL_FROM_ADDRESS="support@zodbank.com" -# EMAIL_FROM_ADDRESS="zodbank@yopmail.com" + ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID') ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET') From c8925aad8eff38c70bd5aaa239b76a11faf073cb Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 14:53:43 +0530 Subject: [PATCH 2/8] jira-15 leader board API and profile API --- guardian/serializers.py | 16 ++++++++-------- guardian/views.py | 10 ++++++++-- junior/admin.py | 2 +- .../migrations/0009_juniorpoints_position.py | 18 ++++++++++++++++++ junior/models.py | 2 ++ junior/serializers.py | 10 ++++++++-- 6 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 junior/migrations/0009_juniorpoints_position.py diff --git a/guardian/serializers.py b/guardian/serializers.py index 08c2861..f5f8588 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -120,7 +120,7 @@ class CreateGuardianSerializer(serializers.ModelSerializer): image_url = upload_image_to_alibaba(image, filename) guardian.image = image_url """Complete profile of the junior if below all data are filled""" - complete_profile_field = all([guardian.phone, guardian.gender, guardian.family_name, guardian.country_name, + complete_profile_field = all([guardian.phone, guardian.gender, guardian.country_name, guardian.dob, guardian.country_code, user.first_name, user.last_name, user.email, guardian.image]) guardian.is_complete_profile = False @@ -188,18 +188,18 @@ class TaskDetailsSerializer(serializers.ModelSerializer): class TopJuniorSerializer(serializers.ModelSerializer): """Top junior serializer""" junior = JuniorDetailSerializer() - position = serializers.SerializerMethodField() + position = serializers.IntegerField() - class Meta(object): + class Meta: """Meta info""" model = JuniorPoints fields = ['id', 'junior', 'total_task_points', 'position', 'created_at', 'updated_at'] - def get_position(self, obj): - """get position of junior""" - queryset = self.context['view'].get_queryset() - position = list(queryset).index(obj) + 1 - return position + def to_representation(self, instance): + """Convert instance to representation""" + representation = super().to_representation(instance) + representation['position'] = instance.position + return representation class GuardianProfileSerializer(serializers.ModelSerializer): diff --git a/guardian/views.py b/guardian/views.py index 9c43628..360f77d 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -143,7 +143,7 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer - permission_classes = [IsAuthenticated] + # permission_classes = [IsAuthenticated] queryset = JuniorPoints.objects.all() def get_serializer_context(self): @@ -152,8 +152,14 @@ class TopJuniorListAPIView(viewsets.ModelViewSet): return context def list(self, request, *args, **kwargs): - """fetch junior list those complete their task""" + """Fetch junior list of those who complete their tasks""" junior_total_points = self.get_queryset().order_by('-total_task_points') + + # Update the position field for each JuniorPoints object + for index, junior in enumerate(junior_total_points): + junior.position = index + 1 + junior.save() + serializer = self.get_serializer(junior_total_points, many=True) return custom_response(serializer.data, response_status=status.HTTP_200_OK) diff --git a/junior/admin.py b/junior/admin.py index bb9ea49..a2acfb6 100644 --- a/junior/admin.py +++ b/junior/admin.py @@ -16,7 +16,7 @@ class JuniorAdmin(admin.ModelAdmin): @admin.register(JuniorPoints) class JuniorPointsAdmin(admin.ModelAdmin): """Junior Points Admin""" - list_display = ['junior', 'total_task_points'] + list_display = ['junior', 'total_task_points', 'position'] def __str__(self): """Return email id""" diff --git a/junior/migrations/0009_juniorpoints_position.py b/junior/migrations/0009_juniorpoints_position.py new file mode 100644 index 0000000..2f5b31d --- /dev/null +++ b/junior/migrations/0009_juniorpoints_position.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-07-10 07:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('junior', '0008_juniorpoints'), + ] + + operations = [ + migrations.AddField( + model_name='juniorpoints', + name='position', + field=models.IntegerField(blank=True, default=99999, null=True), + ), + ] diff --git a/junior/models.py b/junior/models.py index 167983f..505a144 100644 --- a/junior/models.py +++ b/junior/models.py @@ -47,6 +47,8 @@ class JuniorPoints(models.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""" + position = models.IntegerField(blank=True, null=True, default=99999) """Profile created and updated time""" created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) diff --git a/junior/serializers.py b/junior/serializers.py index 81ea0cb..12e522f 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -5,7 +5,7 @@ from django.contrib.auth.models import User from django.db import transaction import random """Import django app""" -from junior.models import Junior +from junior.models import Junior, JuniorPoints from guardian.utils import upload_image_to_alibaba from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.models import Guardian, JuniorTask @@ -147,6 +147,7 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): 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_auth(self, obj): @@ -162,6 +163,11 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): data = JuniorTask.objects.filter(junior=obj).count() return data + def get_position(self, obj): + data = JuniorPoints.objects.filter(junior=obj).last() + if data: + return data.position + return 99999 def get_points(self, obj): data = sum(JuniorTask.objects.filter(junior=obj, task_status=COMPLETED).values_list('points', flat=True)) return data @@ -192,7 +198,7 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', 'updated_at', 'assigned_task','points', 'pending_task', 'in_progress_task', 'completed_task', - 'requested_task', 'rejected_task'] + 'requested_task', 'rejected_task', 'position'] class JuniorProfileSerializer(serializers.ModelSerializer): """junior serializer""" From 0d4c9aae78afd1035f976d8ebc99db1e6cbb394e Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 16:50:50 +0530 Subject: [PATCH 3/8] delete user account API --- account/migrations/0004_userdelete.py | 31 ++++++++++++++++++ account/models.py | 21 ++++++++++++ account/serializers.py | 47 ++++++++++++++++++++++++++- account/urls.py | 4 ++- account/views.py | 19 ++++++++++- base/messages.py | 5 +-- junior/admin.py | 2 +- 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 account/migrations/0004_userdelete.py diff --git a/account/migrations/0004_userdelete.py b/account/migrations/0004_userdelete.py new file mode 100644 index 0000000..10bd2aa --- /dev/null +++ b/account/migrations/0004_userdelete.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.2 on 2023-07-10 09:24 + +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', '0003_defaulttaskimages'), + ] + + operations = [ + migrations.CreateModel( + name='UserDelete', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('old_email', models.EmailField(blank=True, max_length=254, null=True, verbose_name='Original Email')), + ('d_email', models.EmailField(blank=True, max_length=254, null=True, verbose_name='Dummy Email')), + ('is_active', models.BooleanField(default=True)), + ('reason', models.TextField(blank=True, max_length=500, null=True, verbose_name='Reason for Leaving')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='delete_information_set', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'db_table': 'user_delete_information', + }, + ), + ] diff --git a/account/models.py b/account/models.py index ac23e79..31c592a 100644 --- a/account/models.py +++ b/account/models.py @@ -92,3 +92,24 @@ class DefaultTaskImages(models.Model): def __str__(self): """return phone as an object""" return self.task_name + +class UserDelete(models.Model): + """ + User delete information + """ + user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='delete_information_set') + """Old email""" + old_email = models.EmailField(blank=True, null=True, verbose_name='Original Email') + """Dummy email""" + d_email = models.EmailField(blank=True, null=True, verbose_name='Dummy Email') + is_active = models.BooleanField(default=True) + """reason for leaving""" + reason = models.TextField(max_length=500, blank=True, null=True, verbose_name='Reason for Leaving') + created_at = models.DateTimeField(auto_now_add=True) + + class Meta(object): + """ Meta information """ + db_table = 'user_delete_information' + + def __str__(self): + return self.user.email diff --git a/account/serializers.py b/account/serializers.py index db11233..4007e98 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -1,12 +1,13 @@ """Account serializer""" """Django Imoprt""" +import random from rest_framework import serializers from django.contrib.auth.models import User from rest_framework_simplejwt.tokens import RefreshToken """App import""" from guardian.models import Guardian from junior.models import Junior -from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages +from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserDelete from base.constants import GUARDIAN, JUNIOR, SUPERUSER from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR @@ -218,3 +219,47 @@ class DefaultTaskImagesDetailsSerializer(serializers.ModelSerializer): """Meta info""" model = DefaultTaskImages fields = '__all__' + +class UserDeleteSerializer(serializers.ModelSerializer): + """User Delete Serializer""" + class Meta(object): + """Meta Information""" + model = UserDelete + fields = ['reason'] + def create(self, validated_data): + user = self.context['user'] + user_type = str(self.context['user_type']) + data = validated_data.get('reason') + random_num = random.randint(0,10000) + user_tb = User.objects.filter(id=user.id).last() + user_type_data = UserEmailOtp.objects.filter(email=user.email).last() + if user_type == '1' and user_type_data.user_type == '1': + junior_data = Junior.objects.filter(auth__email=user_tb.email).first() + if junior_data: + junior_data.is_active = False + junior_data.is_verified = False + junior_data.guardian_code = '{}' + junior_data.save() + elif user_type == '2' and user_type_data.user_type == '2': + guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() + if guardian_data: + 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)) + for data in jun_data: + data.guardian_code.remove(guardian_data.guardian_code) + data.save() + else: + 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 + instance = UserDelete.objects.create(user=user_tb, d_email=d_email, old_email=o_mail, + is_active=True, reason=data) + user_tb.save() + + return instance diff --git a/account/urls.py b/account/urls.py index ffb2db6..b64f99d 100644 --- a/account/urls.py +++ b/account/urls.py @@ -7,7 +7,7 @@ from rest_framework import routers from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, GoogleLoginViewSet, SigninWithApple, ProfileAPIViewSet, UploadImageAPIViewSet, - DefaultImageAPIViewSet) + DefaultImageAPIViewSet, DeleteUserProfileAPIViewSet) """Router""" router = routers.SimpleRouter() @@ -29,6 +29,8 @@ router.register('profile', ProfileAPIViewSet, basename='profile') router.register('upload-default-task-image', UploadImageAPIViewSet, basename='upload-default-task-image') """Fetch default task image end point""" router.register('default-task-image', DefaultImageAPIViewSet, basename='default-task-image') +"""Delete user account""" +router.register('delete', DeleteUserProfileAPIViewSet, basename='delete') """Define url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), diff --git a/account/views.py b/account/views.py index 1324869..5657b4a 100644 --- a/account/views.py +++ b/account/views.py @@ -18,7 +18,7 @@ from django.contrib.auth.models import User from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, - DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer) + DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer, UserDeleteSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -417,3 +417,20 @@ class DefaultImageAPIViewSet(viewsets.ModelViewSet): queryset = DefaultTaskImages.objects.all() serializer = DefaultTaskImagesSerializer(queryset, many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + + +class DeleteUserProfileAPIViewSet(viewsets.GenericViewSet): + """ Delete user API view set """ + + @action(detail=False, methods=['POST'], url_path='user-account',serializer_class=UserDeleteSerializer, + permission_classes=[IsAuthenticated]) + def account(self, request): + print("request.data===>",request.data) + print("request.user===>", request.user) + user_type = str(request.data['user_type']) + serializer = self.get_serializer(data=request.data, context={'request': request, 'user': request.user, + 'user_type':user_type}) + if serializer.is_valid(): + serializer.save() + return custom_response(SUCCESS_CODE['3005'], response_status=status.HTTP_200_OK) + return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) diff --git a/base/messages.py b/base/messages.py index f024a3d..b00bfbf 100644 --- a/base/messages.py +++ b/base/messages.py @@ -52,7 +52,8 @@ ERROR_CODE = { "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" + "2029": "Validity of otp verification is expired", + "2030": "Use correct user type and token" } SUCCESS_CODE = { # Success code for password @@ -64,7 +65,7 @@ SUCCESS_CODE = { # Success code for password reset "3004": "Password reset link has been sent to your email address", # Success code for link verified - "3005": "Your link has been verified, it's valid", + "3005": "Your account is deleted successfully.", # Success code for password reset "3006": "Your password has been reset successfully.", # Success code for password update diff --git a/junior/admin.py b/junior/admin.py index a2acfb6..3764f48 100644 --- a/junior/admin.py +++ b/junior/admin.py @@ -7,7 +7,7 @@ from .models import Junior, JuniorPoints @admin.register(Junior) class JuniorAdmin(admin.ModelAdmin): """Junior Admin""" - list_display = ['auth'] + list_display = ['auth', 'guardian_code'] def __str__(self): """Return email id""" From 4fb7a2fc6c6e369f455c2fcc8ee4d6aac3e81934 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 17:51:30 +0530 Subject: [PATCH 4/8] jira-14 dashboard API --- guardian/serializers.py | 21 +++++++++++---------- guardian/views.py | 14 +++++++++++--- junior/serializers.py | 20 +++++++++++--------- junior/views.py | 17 +++++++++++++---- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/guardian/serializers.py b/guardian/serializers.py index f5f8588..14c4a6d 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -67,7 +67,7 @@ class CreateGuardianSerializer(serializers.ModelSerializer): family_name = serializers.CharField(max_length=100, required=False) dob = serializers.DateField(required=False) referral_code = serializers.CharField(max_length=100, required=False) - image = serializers.ImageField(required=False) + image = serializers.URLField(required=False) class Meta(object): """Meta info""" @@ -93,10 +93,15 @@ class CreateGuardianSerializer(serializers.ModelSerializer): user = User.objects.filter(username=self.context['user']).last() if user: """Save first and last name of guardian""" - if self.context.get('first_name') != '' and self.context.get('last_name') != '': - user.first_name = self.context.get('first_name', user.first_name) - user.last_name = self.context.get('last_name', user.last_name) - user.save() + if self.context.get('first_name') != '' and self.context.get('first_name') is not None: + user.first_name = self.context.get('first_name') + else: + user.first_name = user.first_name + if self.context.get('last_name') != '' and self.context.get('last_name') is not None: + user.last_name = self.context.get('last_name') + else: + user.last_name = user.last_name + user.save() """Create guardian data""" guardian, created = Guardian.objects.get_or_create(user=self.context['user']) if created: @@ -114,11 +119,7 @@ class CreateGuardianSerializer(serializers.ModelSerializer): 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) - image = validated_data.pop('image', None) - if image: - filename = f"images/{image.name}" - image_url = upload_image_to_alibaba(image, filename) - guardian.image = image_url + guardian.image = validated_data.get('image', guardian.image) """Complete profile of the junior if below all data are filled""" complete_profile_field = all([guardian.phone, guardian.gender, guardian.country_name, guardian.dob, guardian.country_code, user.first_name, user.last_name, diff --git a/guardian/views.py b/guardian/views.py index 360f77d..4d9517f 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -51,10 +51,18 @@ 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: + 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',' ')}, - data=request.data) + "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() diff --git a/junior/serializers.py b/junior/serializers.py index 12e522f..28bbdc4 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -36,7 +36,7 @@ class CreateJuniorSerializer(serializers.ModelSerializer): dob = serializers.DateField(required=False) referral_code = serializers.CharField(max_length=100, required=False) guardian_code = ListCharField(required=False) - image = serializers.ImageField(required=False) + image = serializers.URLField(required=False) class Meta(object): """Meta info""" @@ -64,10 +64,15 @@ class CreateJuniorSerializer(serializers.ModelSerializer): user = User.objects.filter(username=self.context['user']).last() if user: """Save first and last name of junior""" - if self.context.get('first_name') != '' and self.context.get('last_name') != '': - user.first_name = self.context.get('first_name', user.first_name) - user.last_name = self.context.get('last_name', user.last_name) - user.save() + if self.context.get('first_name') != '' and self.context.get('first_name') is not None: + user.first_name = self.context.get('first_name') + else: + user.first_name = user.first_name + if self.context.get('last_name') != '' and self.context.get('last_name') is not None: + user.last_name = self.context.get('last_name') + else: + user.last_name = user.last_name + user.save() """Create junior data""" junior, created = Junior.objects.get_or_create(auth=self.context['user']) if created: @@ -89,10 +94,7 @@ class CreateJuniorSerializer(serializers.ModelSerializer): 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) - if image: - filename = f"images/{image.name}" - image_url = upload_image_to_alibaba(image, filename) - junior.image = image_url + junior.image = validated_data.get('image', junior.image) """Complete profile of the junior if below all data are filled""" complete_profile_field = all([junior.phone, junior.gender, junior.country_name, junior.image, junior.dob, junior.country_code, user.first_name, user.last_name, diff --git a/junior/views.py b/junior/views.py index 0658b2c..fa3b57c 100644 --- a/junior/views.py +++ b/junior/views.py @@ -7,6 +7,7 @@ from .serializers import CreateJuniorSerializer, JuniorDetailListSerializer from guardian.models import Guardian from base.messages import ERROR_CODE, SUCCESS_CODE from account.utils import custom_response, custom_error_response +from guardian.utils import upload_image_to_alibaba # Create your views here. class UpdateJuniorProfile(viewsets.ViewSet): """Update junior profile""" @@ -16,10 +17,18 @@ class UpdateJuniorProfile(viewsets.ViewSet): def create(self, request, *args, **kwargs): """Use CreateJuniorSerializer""" - serializer = CreateJuniorSerializer(context={"user":request.user, - "first_name":request.data.get('first_name', ''), - "last_name": request.data.get('last_name',' ')}, - data=request.data) + request_data = request.data + image = request.data.get('image') + image_url = '' + if image: + 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() From 03fb774a7b41dc51131f634ab2289113d5dc52de Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 18:01:14 +0530 Subject: [PATCH 5/8] jira-25 setting API --- guardian/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/guardian/views.py b/guardian/views.py index 4d9517f..94ff8f4 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -117,6 +117,7 @@ class CreateTaskAPIView(viewsets.ModelViewSet): filename = f"images/{image}" 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() From 1ae81d41349d3a234a20863794a51af9ec03ae4f Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 19:16:45 +0530 Subject: [PATCH 6/8] jira-25 setting API --- account/admin.py | 17 ++--- account/migrations/0005_usernotification.py | 31 ++++++++ account/models.py | 27 +++++++ account/serializers.py | 84 ++++++++++++++------- account/urls.py | 7 +- account/views.py | 36 +++++++-- base/messages.py | 3 +- guardian/serializers.py | 4 +- guardian/views.py | 3 +- 9 files changed, 164 insertions(+), 48 deletions(-) create mode 100644 account/migrations/0005_usernotification.py diff --git a/account/admin.py b/account/admin.py index 96c2e4d..2614bdb 100644 --- a/account/admin.py +++ b/account/admin.py @@ -2,9 +2,16 @@ from django.contrib import admin """Import django app""" -from .models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages +from .models import UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserNotification # Register your models here. +@admin.register(UserNotification) +class UserNotificationAdmin(admin.ModelAdmin): + """User profile admin""" + list_display = ['user', 'push_notification', 'email_notification', 'sms_notification'] + + def __str__(self): + return self.image_url @admin.register(DefaultTaskImages) class DefaultTaskImagesAdmin(admin.ModelAdmin): """User profile admin""" @@ -13,14 +20,6 @@ class DefaultTaskImagesAdmin(admin.ModelAdmin): def __str__(self): return self.image_url -@admin.register(UserProfile) -class UserProfileAdmin(admin.ModelAdmin): - """User profile admin""" - list_display = ['user'] - - def __str__(self): - return self.user__email - @admin.register(UserEmailOtp) class UserEmailOtpAdmin(admin.ModelAdmin): """User Email otp admin""" diff --git a/account/migrations/0005_usernotification.py b/account/migrations/0005_usernotification.py new file mode 100644 index 0000000..397b633 --- /dev/null +++ b/account/migrations/0005_usernotification.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.2 on 2023-07-10 12:40 + +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', '0004_userdelete'), + ] + + operations = [ + migrations.CreateModel( + name='UserNotification', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('push_notification', models.BooleanField(default=True)), + ('email_notification', models.BooleanField(default=True)), + ('sms_notification', models.BooleanField(default=True)), + ('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_notification', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'db_table': 'user_notification', + }, + ), + ] diff --git a/account/models.py b/account/models.py index 31c592a..36b2286 100644 --- a/account/models.py +++ b/account/models.py @@ -72,6 +72,8 @@ class UserEmailOtp(models.Model): class Meta(object): """ Meta information """ db_table = 'user_email_otp' + verbose_name = 'User Email OTP' + verbose_name_plural = 'User Email OTP' def __str__(self): """return phone as an object""" @@ -113,3 +115,28 @@ class UserDelete(models.Model): def __str__(self): return self.user.email + + +class UserNotification(models.Model): + """ + User notification details + """ + user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_notification') + """Push Notification""" + push_notification = models.BooleanField(default=True) + """Email Notification""" + email_notification = models.BooleanField(default=True) + """SMS Notification""" + sms_notification = models.BooleanField(default=True) + + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta(object): + """ Meta information """ + db_table = 'user_notification' + verbose_name = 'User Notification' + verbose_name_plural = 'User Notification' + + def __str__(self): + return self.user.email diff --git a/account/serializers.py b/account/serializers.py index 4007e98..e4ad809 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -7,7 +7,7 @@ from rest_framework_simplejwt.tokens import RefreshToken """App import""" from guardian.models import Guardian from junior.models import Junior -from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserDelete +from account.models import UserEmailOtp, DefaultTaskImages, UserDelete, UserNotification from base.constants import GUARDIAN, JUNIOR, SUPERUSER from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR @@ -230,36 +230,64 @@ class UserDeleteSerializer(serializers.ModelSerializer): user = self.context['user'] user_type = str(self.context['user_type']) data = validated_data.get('reason') + passwd = self.context['password'] random_num = random.randint(0,10000) user_tb = User.objects.filter(id=user.id).last() - user_type_data = UserEmailOtp.objects.filter(email=user.email).last() - if user_type == '1' and user_type_data.user_type == '1': - junior_data = Junior.objects.filter(auth__email=user_tb.email).first() - if junior_data: - junior_data.is_active = False - junior_data.is_verified = False - junior_data.guardian_code = '{}' - junior_data.save() - elif user_type == '2' and user_type_data.user_type == '2': - guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() - if guardian_data: - 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)) - for data in jun_data: - data.guardian_code.remove(guardian_data.guardian_code) - data.save() + if user_tb.check_password(passwd): + user_type_data = UserEmailOtp.objects.filter(email=user.email).last() + if user_type == '1' and user_type_data.user_type == '1': + junior_data = Junior.objects.filter(auth__email=user_tb.email).first() + if junior_data: + junior_data.is_active = False + junior_data.is_verified = False + junior_data.guardian_code = '{}' + junior_data.save() + elif user_type == '2' and user_type_data.user_type == '2': + guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() + if guardian_data: + 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)) + for data in jun_data: + data.guardian_code.remove(guardian_data.guardian_code) + data.save() + else: + 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 + instance = UserDelete.objects.create(user=user_tb, d_email=d_email, old_email=o_mail, + is_active=True, reason=data) + user_tb.save() + + return instance else: - raise serializers.ValidationError({"details":ERROR_CODE['2030'],"code":"400", "status":"failed"}) + raise serializers.ValidationError({"details": ERROR_CODE['2031'], "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 - instance = UserDelete.objects.create(user=user_tb, d_email=d_email, old_email=o_mail, - is_active=True, reason=data) - user_tb.save() +class UserNotificationSerializer(serializers.ModelSerializer): + """User Notification serializer""" + class Meta(object): + """Meta info""" + model = UserNotification + fields = '__all__' + + +class UpdateUserNotificationSerializer(serializers.ModelSerializer): + """Update User Notification serializer""" + class Meta(object): + """Meta info""" + model = UserNotification + fields = ['push_notification', 'email_notification', 'sms_notification'] + + def create(self, validated_data): + instance = UserNotification.objects.filter(user=self.context).last() + 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) + instance.save() return instance diff --git a/account/urls.py b/account/urls.py index b64f99d..6bfc8dd 100644 --- a/account/urls.py +++ b/account/urls.py @@ -7,7 +7,8 @@ from rest_framework import routers from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, GoogleLoginViewSet, SigninWithApple, ProfileAPIViewSet, UploadImageAPIViewSet, - DefaultImageAPIViewSet, DeleteUserProfileAPIViewSet) + DefaultImageAPIViewSet, DeleteUserProfileAPIViewSet, UserNotificationAPIViewSet, + UpdateUserNotificationAPIViewSet) """Router""" router = routers.SimpleRouter() @@ -31,6 +32,10 @@ router.register('upload-default-task-image', UploadImageAPIViewSet, basename='up router.register('default-task-image', DefaultImageAPIViewSet, basename='default-task-image') """Delete user account""" router.register('delete', DeleteUserProfileAPIViewSet, basename='delete') +"""user account notification""" +router.register('user-notification', UserNotificationAPIViewSet, basename='user-notification') +"""update user account notification""" +router.register('update-user-notification', UpdateUserNotificationAPIViewSet, basename='update-user-notification') """Define url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), diff --git a/account/views.py b/account/views.py index 5657b4a..1ad142d 100644 --- a/account/views.py +++ b/account/views.py @@ -12,13 +12,14 @@ from guardian.utils import upload_image_to_alibaba from django.contrib.auth import authenticate, login from guardian.models import Guardian from junior.models import Junior -from account.models import UserProfile, UserPhoneOtp, UserEmailOtp, DefaultTaskImages +from account.models import UserProfile, UserPhoneOtp, UserEmailOtp, DefaultTaskImages, UserNotification from django.contrib.auth.models import User """Account serializer""" from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, - DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer, UserDeleteSerializer) + DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer, UserDeleteSerializer, + UserNotificationSerializer, UpdateUserNotificationSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -425,12 +426,37 @@ class DeleteUserProfileAPIViewSet(viewsets.GenericViewSet): @action(detail=False, methods=['POST'], url_path='user-account',serializer_class=UserDeleteSerializer, permission_classes=[IsAuthenticated]) def account(self, request): - print("request.data===>",request.data) - print("request.user===>", request.user) user_type = str(request.data['user_type']) + password = request.data['password'] serializer = self.get_serializer(data=request.data, context={'request': request, 'user': request.user, - 'user_type':user_type}) + 'user_type':user_type, + 'password':password}) if serializer.is_valid(): serializer.save() return custom_response(SUCCESS_CODE['3005'], response_status=status.HTTP_200_OK) return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) + + +class UserNotificationAPIViewSet(viewsets.ModelViewSet): + """Profile viewset""" + queryset = UserNotification.objects.all() + serializer_class = UserNotificationSerializer + def list(self, request, *args, **kwargs): + """profile view""" + queryset = self.queryset.filter(user=request.user) + serializer = UserNotificationSerializer(queryset, many=True) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + + +class UpdateUserNotificationAPIViewSet(viewsets.ModelViewSet): + """Profile viewset""" + serializer_class = UpdateUserNotificationSerializer + + def create(self, request, *args, **kwargs): + """profile view""" + serializer = UpdateUserNotificationSerializer(data=request.data, + context=request.user) + if serializer.is_valid(): + serializer.save() + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + return custom_error_response(serializer.error, response_status=status.HTTP_400_BAD_REQUEST) diff --git a/base/messages.py b/base/messages.py index b00bfbf..788c7bc 100644 --- a/base/messages.py +++ b/base/messages.py @@ -53,7 +53,8 @@ ERROR_CODE = { "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" + "2030": "Use correct user type and token", + "2031":"Invalid password" } SUCCESS_CODE = { # Success code for password diff --git a/guardian/serializers.py b/guardian/serializers.py index 14c4a6d..dd79ed7 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -8,7 +8,7 @@ from django.db import transaction from django.contrib.auth.models import User """Import Django app""" from .models import Guardian, JuniorTask -from account.models import UserProfile, UserEmailOtp +from account.models import UserProfile, UserEmailOtp, UserNotification from account.serializers import JuniorSerializer from junior.serializers import JuniorDetailSerializer from base.messages import ERROR_CODE, SUCCESS_CODE @@ -36,7 +36,7 @@ class UserSerializer(serializers.ModelSerializer): try: """Create user profile""" user = User.objects.create_user(username=email, email=email, password=password) - UserProfile.objects.create(user=user, user_type=user_type) + UserNotification.objects.create(user=user) if user_type == '1': Junior.objects.create(auth=user, junior_code=''.join([str(random.randrange(9)) for _ in range(6)]), referral_code=''.join([str(random.randrange(9)) for _ in range(6)])) diff --git a/guardian/views.py b/guardian/views.py index 94ff8f4..e7f10bf 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -12,7 +12,7 @@ from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializ from .models import Guardian, JuniorTask from junior.models import Junior, JuniorPoints from junior.serializers import JuniorDetailSerializer -from account.models import UserEmailOtp +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 @@ -92,7 +92,6 @@ class TaskListAPIView(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): """Create guardian profile""" status_value = self.request.GET.get('status') - print("status_value==>",status_value,'===>',type(status_value)) if str(status_value) == '0': queryset = JuniorTask.objects.filter(guardian__user=request.user).order_by('created_at') else: From c0f20f732dae06848da21a4cd72892f715b42d02 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 21:18:22 +0530 Subject: [PATCH 7/8] swagger points --- account/serializers.py | 10 ++++++++-- account/views.py | 11 +++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index e4ad809..47472c0 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -7,7 +7,7 @@ from rest_framework_simplejwt.tokens import RefreshToken """App import""" from guardian.models import Guardian from junior.models import Junior -from account.models import UserEmailOtp, DefaultTaskImages, UserDelete, UserNotification +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 @@ -233,7 +233,7 @@ class UserDeleteSerializer(serializers.ModelSerializer): passwd = self.context['password'] random_num = random.randint(0,10000) user_tb = User.objects.filter(id=user.id).last() - if user_tb.check_password(passwd): + if user_tb and user_tb.check_password(passwd): user_type_data = UserEmailOtp.objects.filter(email=user.email).last() if user_type == '1' and user_type_data.user_type == '1': junior_data = Junior.objects.filter(auth__email=user_tb.email).first() @@ -291,3 +291,9 @@ class UpdateUserNotificationSerializer(serializers.ModelSerializer): instance.sms_notification = validated_data.get('sms_notification', instance.sms_notification) instance.save() return instance + +class UserPhoneOtpSerializer(serializers.ModelSerializer): + """User Phone serializers""" + class Meta(object): + model = UserPhoneOtp + fields = '__all__' diff --git a/account/views.py b/account/views.py index 1ad142d..ded9a74 100644 --- a/account/views.py +++ b/account/views.py @@ -19,7 +19,7 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer, UserDeleteSerializer, - UserNotificationSerializer, UpdateUserNotificationSerializer) + UserNotificationSerializer, UpdateUserNotificationSerializer, UserPhoneOtpSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -221,6 +221,7 @@ class ForgotPasswordAPIView(views.APIView): class SendPhoneOtp(viewsets.ModelViewSet): """Send otp on phone""" queryset = UserPhoneOtp.objects.all() + serializer_class = UserPhoneOtpSerializer def create(self, request, *args, **kwargs): otp = generate_otp() phone_number = self.request.data['phone'] @@ -237,6 +238,7 @@ class SendPhoneOtp(viewsets.ModelViewSet): class UserPhoneVerification(viewsets.ModelViewSet): """Send otp on phone""" queryset = UserPhoneOtp.objects.all() + serializer_class = UserPhoneOtpSerializer def list(self, request, *args, **kwargs): try: phone_data = UserPhoneOtp.objects.filter(phone=self.request.GET.get('phone'), @@ -378,7 +380,7 @@ class ReSendEmailOtp(viewsets.ModelViewSet): class ProfileAPIViewSet(viewsets.ModelViewSet): """Profile viewset""" queryset = User.objects.all() - serializer_class = [JuniorProfileSerializer, GuardianProfileSerializer] + serializer_class = JuniorProfileSerializer def list(self, request, *args, **kwargs): """profile view""" if str(self.request.GET.get('user_type')) == '1': @@ -438,7 +440,7 @@ class DeleteUserProfileAPIViewSet(viewsets.GenericViewSet): class UserNotificationAPIViewSet(viewsets.ModelViewSet): - """Profile viewset""" + """notification viewset""" queryset = UserNotification.objects.all() serializer_class = UserNotificationSerializer def list(self, request, *args, **kwargs): @@ -449,7 +451,8 @@ class UserNotificationAPIViewSet(viewsets.ModelViewSet): class UpdateUserNotificationAPIViewSet(viewsets.ModelViewSet): - """Profile viewset""" + """Update notification viewset""" + queryset = UserNotification.objects.all() serializer_class = UpdateUserNotificationSerializer def create(self, request, *args, **kwargs): From 41761f22e82c2ba9a205450aaa62340f30229cef Mon Sep 17 00:00:00 2001 From: jain Date: Tue, 11 Jul 2023 11:52:07 +0530 Subject: [PATCH 8/8] sonar issue --- account/admin.py | 9 ++++++++- account/serializers.py | 21 ++++----------------- account/utils.py | 21 +++++++++++++++++++++ account/views.py | 2 -- guardian/serializers.py | 4 ---- junior/serializers.py | 5 ----- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/account/admin.py b/account/admin.py index 2614bdb..e1decdd 100644 --- a/account/admin.py +++ b/account/admin.py @@ -2,9 +2,16 @@ from django.contrib import admin """Import django app""" -from .models import UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserNotification +from .models import UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserNotification, UserDelete # Register your models here. +@admin.register(UserDelete) +class UserDeleteAdmin(admin.ModelAdmin): + """User profile admin""" + list_display = ['user', 'old_email', 'd_email'] + + def __str__(self): + return self.user @admin.register(UserNotification) class UserNotificationAdmin(admin.ModelAdmin): """User profile admin""" diff --git a/account/serializers.py b/account/serializers.py index 47472c0..21a8d41 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -10,7 +10,7 @@ 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 .utils import junior_account_update, guardian_account_update class GoogleLoginSerializer(serializers.Serializer): """google login serializer""" @@ -236,25 +236,11 @@ class UserDeleteSerializer(serializers.ModelSerializer): if user_tb and user_tb.check_password(passwd): user_type_data = UserEmailOtp.objects.filter(email=user.email).last() if user_type == '1' and user_type_data.user_type == '1': - junior_data = Junior.objects.filter(auth__email=user_tb.email).first() - if junior_data: - junior_data.is_active = False - junior_data.is_verified = False - junior_data.guardian_code = '{}' - junior_data.save() + junior_account_update(user_tb) elif user_type == '2' and user_type_data.user_type == '2': - guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() - if guardian_data: - 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)) - for data in jun_data: - data.guardian_code.remove(guardian_data.guardian_code) - data.save() + guardian_account_update(user_tb) else: 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' @@ -295,5 +281,6 @@ class UpdateUserNotificationSerializer(serializers.ModelSerializer): class UserPhoneOtpSerializer(serializers.ModelSerializer): """User Phone serializers""" class Meta(object): + """Meta info""" model = UserPhoneOtp fields = '__all__' diff --git a/account/utils.py b/account/utils.py index 4412e62..af9f86b 100644 --- a/account/utils.py +++ b/account/utils.py @@ -10,7 +10,28 @@ from datetime import datetime from calendar import timegm from uuid import uuid4 import secrets +from junior.models import Junior +from guardian.models import Guardian +def junior_account_update(user_tb): + """junior account delete""" + junior_data = Junior.objects.filter(auth__email=user_tb.email).first() + if junior_data: + junior_data.is_active = False + junior_data.is_verified = False + junior_data.guardian_code = '{}' + junior_data.save() +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: + 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)) + for data in jun_data: + data.guardian_code.remove(guardian_data.guardian_code) + data.save() def send_otp_email(recipient_email, otp): """Send otp on email with template""" from_email = settings.EMAIL_FROM_ADDRESS diff --git a/account/views.py b/account/views.py index ded9a74..b5c0d26 100644 --- a/account/views.py +++ b/account/views.py @@ -109,8 +109,6 @@ class SigninWithApple(views.APIView): def post(self, request): token = request.data.get("access_token") user_type = request.data.get("user_type") - if not token: - return custom_error_response(ERROR_CODE['2027'], response_status=status.HTTP_400_BAD_REQUEST) try: decoded_data = jwt.decode(token, options={"verify_signature": False}) user_data = {"email": decoded_data.get('email'), "username": decoded_data.get('email'), "is_active": True} diff --git a/guardian/serializers.py b/guardian/serializers.py index dd79ed7..2c1c3dc 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -95,12 +95,8 @@ class CreateGuardianSerializer(serializers.ModelSerializer): """Save first and last name of guardian""" if self.context.get('first_name') != '' and self.context.get('first_name') is not None: user.first_name = self.context.get('first_name') - else: - user.first_name = user.first_name if self.context.get('last_name') != '' and self.context.get('last_name') is not None: user.last_name = self.context.get('last_name') - else: - user.last_name = user.last_name user.save() """Create guardian data""" guardian, created = Guardian.objects.get_or_create(user=self.context['user']) diff --git a/junior/serializers.py b/junior/serializers.py index 28bbdc4..df7a40a 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -59,19 +59,14 @@ class CreateJuniorSerializer(serializers.ModelSerializer): def create(self, validated_data): """Create junior profile""" - image = validated_data.get('image', None) guardian_code = validated_data.get('guardian_code',None) user = User.objects.filter(username=self.context['user']).last() if user: """Save first and last name of junior""" if self.context.get('first_name') != '' and self.context.get('first_name') is not None: user.first_name = self.context.get('first_name') - else: - user.first_name = user.first_name if self.context.get('last_name') != '' and self.context.get('last_name') is not None: user.last_name = self.context.get('last_name') - else: - user.last_name = user.last_name user.save() """Create junior data""" junior, created = Junior.objects.get_or_create(auth=self.context['user'])