Skip to content

Commit

Permalink
Merge pull request #1609 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
[fix] reset password
  • Loading branch information
adnankattekaden authored Nov 24, 2023
2 parents 0328c45 + 96a6833 commit 0dd8265
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions api/dashboard/profile/profile_view.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
from django.db.models import Prefetch
from rest_framework.views import APIView
from io import BytesIO

import decouple
import qrcode
import requests
import decouple
from django.http import HttpResponse
from PIL import Image
from io import BytesIO
from django.contrib.auth.hashers import make_password
from django.core.files.base import ContentFile
from django.core.files.storage import FileSystemStorage
from django.db.models import Prefetch
from rest_framework.views import APIView

from db.organization import UserOrganizationLink
from db.task import InterestGroup, KarmaActivityLog, Level
from db.user import Role
from db.user import User, UserSettings, UserRoleLink, Socials
from utils.exception import CustomException
from utils.permission import CustomizePermission, JWTUtils
from utils.response import CustomResponse
from utils.types import WebHookActions, WebHookCategory
from utils.utils import DiscordWebhooks
from . import profile_serializer
from django.core.files.base import ContentFile
from .profile_serializer import LinkSocials
from django.core.files.storage import FileSystemStorage


class UserProfileEditView(APIView):
Expand Down Expand Up @@ -98,7 +99,6 @@ def patch(self, request):
)

if not serializer.is_valid():

return CustomResponse(
response=serializer.errors
).get_failure_response()
Expand Down Expand Up @@ -168,7 +168,6 @@ def get(self, request, muid=None):
).first()

if user is None:

return CustomResponse(
general_message="Invalid muid"
).get_failure_response()
Expand All @@ -178,7 +177,6 @@ def get(self, request, muid=None):
).first()

if not user_settings.is_public:

return CustomResponse(
general_message="Private Profile"
).get_failure_response()
Expand Down Expand Up @@ -208,6 +206,8 @@ def get(self, request, muid=None):
return CustomResponse(
response=serializer.data
).get_success_response()


class ShareUserProfileAPI(APIView):
authentication_classes = [CustomizePermission]

Expand All @@ -225,7 +225,7 @@ def put(self, request):
data=request.data,
context={"request": request}
)

if serializer.is_valid():
serializer.save()

Expand Down Expand Up @@ -296,7 +296,7 @@ def get(self, request, uuid=None):
QRcode = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_H
)

QRcode.add_data(data)
QRcolor = 'black'
QRimg = QRcode.make_image(fill_color=QRcolor, back_color="white").convert('RGB')
Expand All @@ -307,16 +307,15 @@ def get(self, request, uuid=None):
image_io = BytesIO()
QRimg.save(image_io, format='PNG')
image_io.seek(0)
image_data: bytes =image_io.getvalue()
image_data: bytes = image_io.getvalue()
file_path = f'user/qr/{user_uuid}.png'
fs.exists(file_path) and fs.delete(file_path)
file = fs.save(file_path, ContentFile(image_io.read()))

return CustomResponse(
general_message="QR code image with logo saved locally"
).get_success_response()



class UserLevelsAPI(APIView):
def get(self, request, muid=None):
Expand Down Expand Up @@ -390,7 +389,6 @@ def get(self, request, muid=None):
muid=muid
).first()
if user is None:

return CustomResponse(
general_message="Invalid muid"
).get_failure_response()
Expand All @@ -400,7 +398,6 @@ def get(self, request, muid=None):
).first()

if not user_settings.is_public:

return CustomResponse(
general_message="Private Profile"
).get_failure_response()
Expand Down Expand Up @@ -451,11 +448,37 @@ def put(self, request):
response=serializer.errors
).get_failure_response()


class ResetPasswordAPI(APIView):
authentication_classes = [CustomizePermission]

def post(self, request):
user_muid = JWTUtils.fetch_muid(request)
user = User.objects.filter(muid=user_muid).first()

if user is None:
return CustomResponse(
general_message="No user data available"
).get_failure_response()

self.save_password(user)

def save_password(self, request, user_obj):
new_password = request.data.get("password")
hashed_pwd = make_password(new_password)

user_obj.password = hashed_pwd
user_obj.save()
return CustomResponse(
general_message="New Password Saved Successfully"
).get_success_response()


class QrcodeRetrieveAPI(APIView):
def get(self, request, uuid):
def get(self, request, uuid):
try:
user = User.objects.prefetch_related().get(id=uuid or JWTUtils.fetch_user_id(request))

user_settings = UserSettings.objects.filter(
user_id=user
).first()
Expand All @@ -465,14 +488,12 @@ def get(self, request, uuid):
general_message="Private Profile"
).get_failure_response()



serializer = profile_serializer.UserShareQrcode(
user,
many=False,
context={'request':request}
context={'request': request}
)

return CustomResponse(
response=serializer.data
).get_success_response()
Expand Down

0 comments on commit 0dd8265

Please sign in to comment.