Skip to content

Commit

Permalink
Merge pull request #1607 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
Devserv
  • Loading branch information
Aashish Vinu authored Nov 24, 2023
2 parents 934a97a + 61714c7 commit 0328c45
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 47 deletions.
2 changes: 1 addition & 1 deletion api/dashboard/lc/dash_lc_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def validate_attendees(self, attendees):
id=uuid.uuid4(),
user_id=user,
karma=Lc.KARMA.value,
task_id=task.id,
task=task,
updated_by_id=user_id,
created_by_id=user_id,
)
Expand Down
5 changes: 2 additions & 3 deletions api/dashboard/profile/profile_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ class Meta:


def get_profile_pic(self,obj):
# Here the media url in settings.py is /home/mishal/../../uid.png
fs = FileSystemStorage()
path = f'user/qr/{obj.id}.png'
if fs.exists(path):
qrcode_image = f"{self.context.get('request').build_absolute_uri('/')}{fs.url(path)[1:]}"
qrcode_image = f"{self.context.get('request').build_absolute_uri(BE_DOMAIN_NAME)}{fs.url(path)[1:]}"
else:
return None
return None
return qrcode_image

class UserProfileSerializer(serializers.ModelSerializer):
Expand Down
68 changes: 25 additions & 43 deletions api/dashboard/profile/profile_view.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
from io import BytesIO

import decouple
from django.db.models import Prefetch
from rest_framework.views import APIView
import qrcode
import requests
import decouple
from django.http import HttpResponse
from PIL import Image
from io import BytesIO
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 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 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,6 +98,7 @@ def patch(self, request):
)

if not serializer.is_valid():

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

if user is None:

return CustomResponse(
general_message="Invalid muid"
).get_failure_response()
Expand All @@ -176,6 +178,7 @@ 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 @@ -205,8 +208,6 @@ def get(self, request, muid=None):
return CustomResponse(
response=serializer.data
).get_success_response()


class ShareUserProfileAPI(APIView):
authentication_classes = [CustomizePermission]

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

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

Expand Down Expand Up @@ -290,12 +291,12 @@ def get(self, request, uuid=None):
basewidth = 100
wpercent = (basewidth / float(logo_width))
hsize = int((float(logo_height) * float(wpercent)))
resized_logo = logo_image.resize((basewidth, hsize), Image.ANTIALIAS)
resized_logo = logo_image.resize((basewidth, hsize))

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 @@ -306,15 +307,16 @@ 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 @@ -388,6 +390,7 @@ def get(self, request, muid=None):
muid=muid
).first()
if user is None:

return CustomResponse(
general_message="Invalid muid"
).get_failure_response()
Expand All @@ -397,6 +400,7 @@ 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 @@ -447,12 +451,11 @@ def put(self, request):
response=serializer.errors
).get_failure_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 @@ -462,12 +465,14 @@ 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 All @@ -476,26 +481,3 @@ def get(self, request, uuid):
return CustomResponse(
response="The given UUID seems to be invalid"
).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()

return self.save_password(request, 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()

0 comments on commit 0328c45

Please sign in to comment.