Skip to content

Commit

Permalink
Merge pull request #1542 from gtech-mulearn:dev
Browse files Browse the repository at this point in the history
[PATCH] Remove more instances of user active
  • Loading branch information
MZaFaRM authored Nov 7, 2023
2 parents 9c1aff9 + 19e432e commit 5df9c10
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 103 deletions.
1 change: 0 additions & 1 deletion api/dashboard/user/dash_user_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class Meta:
"mobile",
"gender",
"dob",
"active",
"exist_in_guild",
"joined",
"roles",
Expand Down
152 changes: 50 additions & 102 deletions api/dashboard/user/dash_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from . import dash_user_serializer
from django.core.files.storage import FileSystemStorage


class UserInfoAPI(APIView):
authentication_classes = [CustomizePermission]

Expand All @@ -25,14 +26,9 @@ def get(self, request):
general_message="No user data available"
).get_failure_response()

response = dash_user_serializer.UserSerializer(
user,
many=False
).data
response = dash_user_serializer.UserSerializer(user, many=False).data

return CustomResponse(
response=response
).get_success_response()
return CustomResponse(response=response).get_success_response()


class UserGetPatchDeleteAPI(APIView):
Expand All @@ -49,9 +45,7 @@ def get(self, request, user_id):

serializer = dash_user_serializer.UserDetailsSerializer(user)

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

@role_required([RoleType.ADMIN.value])
def delete(self, request, user_id):
Expand All @@ -62,7 +56,8 @@ def delete(self, request, user_id):
general_message="User Not Available"
).get_failure_response()

user.active = False
user.deleted_by = User.objects.get(pk=JWTUtils.fetch_user_id(request))
user.deleted_at = DateTimeUtils.get_current_utc_time()
user.save()

return CustomResponse(
Expand All @@ -76,24 +71,19 @@ def patch(self, request, user_id):
request.data["admin"] = JWTUtils.fetch_user_id(request)

serializer = dash_user_serializer.UserDetailsEditSerializer(
user, data=request.data,
partial=True
user, data=request.data, partial=True
)
if serializer.is_valid():
serializer.save()

DiscordWebhooks.general_updates(
WebHookCategory.USER.value,
WebHookActions.UPDATE.value,
user_id
WebHookCategory.USER.value, WebHookActions.UPDATE.value, user_id
)
return CustomResponse(
general_message="User Edited Successfully"
).get_success_response()

return CustomResponse(
general_message=serializer.errors
).get_failure_response()
return CustomResponse(general_message=serializer.errors).get_failure_response()


class UserAPI(APIView):
Expand All @@ -102,9 +92,7 @@ class UserAPI(APIView):
@role_required([RoleType.ADMIN.value])
def get(self, request):
user_queryset = User.objects.select_related(
"wallet_user",
"user_lvl_link_user",
"user_lvl_link_user__level"
"wallet_user", "user_lvl_link_user", "user_lvl_link_user__level"
).all()

queryset = CommonUtils.get_paginated_queryset(
Expand All @@ -126,13 +114,11 @@ def get(self, request):
},
)
serializer = dash_user_serializer.UserDashboardSerializer(
queryset.get("queryset"),
many=True
queryset.get("queryset"), many=True
)

return CustomResponse().paginated_response(
data=serializer.data,
pagination=queryset.get("pagination")
data=serializer.data, pagination=queryset.get("pagination")
)


Expand All @@ -142,34 +128,25 @@ class UserManagementCSV(APIView):
@role_required([RoleType.ADMIN.value])
def get(self, request):
user_queryset = User.objects.select_related(
"wallet_user",
"user_lvl_link_user",
"user_lvl_link_user__level"
"wallet_user", "user_lvl_link_user", "user_lvl_link_user__level"
).all()

serializer = dash_user_serializer.UserDashboardSerializer(
user_queryset,
many=True
user_queryset, many=True
)

return CommonUtils.generate_csv(
serializer.data,
"User"
)
return CommonUtils.generate_csv(serializer.data, "User")


class UserVerificationAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.ADMIN.value])
def get(self, request):
user_queryset = UserRoleLink.objects.select_related(
"user",
"role"
).filter(
user_queryset = UserRoleLink.objects.select_related("user", "role").filter(
verified=False
)

queryset = CommonUtils.get_paginated_queryset(
user_queryset,
request,
Expand All @@ -190,25 +167,19 @@ def get(self, request):
},
)
serializer = dash_user_serializer.UserVerificationSerializer(
queryset.get(
"queryset"
),
many=True
queryset.get("queryset"), many=True
)

return CustomResponse().paginated_response(
data=serializer.data,
pagination=queryset.get("pagination")
data=serializer.data, pagination=queryset.get("pagination")
)

@role_required([RoleType.ADMIN.value])
def patch(self, request, link_id):
user = UserRoleLink.objects.get(id=link_id)

user_serializer = dash_user_serializer.UserVerificationSerializer(
user,
data=request.data,
partial=True
user, data=request.data, partial=True
)

if not user_serializer.is_valid():
Expand Down Expand Up @@ -250,19 +221,14 @@ class UserVerificationCSV(APIView):

@role_required([RoleType.ADMIN.value])
def get(self, request):
user_queryset = UserRoleLink.objects.select_related(
"user",
"role"
).filter(verified=False)

serializer = dash_user_serializer.UserVerificationSerializer(
user_queryset,
many=True
user_queryset = UserRoleLink.objects.select_related("user", "role").filter(
verified=False
)
return CommonUtils.generate_csv(
serializer.data,
"User"

serializer = dash_user_serializer.UserVerificationSerializer(
user_queryset, many=True
)
return CommonUtils.generate_csv(serializer.data, "User")


class ForgotPasswordAPI(APIView):
Expand All @@ -271,8 +237,7 @@ def post(self, request):

if not (
user := User.objects.filter(
Q(muid=email_muid) |
Q(email=email_muid)
Q(muid=email_muid) | Q(email=email_muid)
).first()
):
return CustomResponse(
Expand All @@ -283,10 +248,7 @@ def post(self, request):
expiry = created_at + timedelta(seconds=900) # 15 minutes

forget_user = ForgotPassword.objects.create(
id=uuid.uuid4(),
user=user,
expiry=expiry,
created_at=created_at
id=uuid.uuid4(), user=user, expiry=expiry, created_at=created_at
)

receiver_mail = user.email
Expand All @@ -310,7 +272,6 @@ def post(self, request):
class ResetPasswordVerifyTokenAPI(APIView):
def post(self, request, token):
if not (forget_user := ForgotPassword.objects.filter(id=token).first()):

return CustomResponse(
general_message="Invalid Token"
).get_failure_response()
Expand All @@ -319,9 +280,7 @@ def post(self, request, token):

if forget_user.expiry > current_time:
muid = forget_user.user.muid
return CustomResponse(
response={"muid": muid}
).get_success_response()
return CustomResponse(response={"muid": muid}).get_success_response()
else:
forget_user.delete()
return CustomResponse(
Expand All @@ -332,24 +291,17 @@ def post(self, request, token):
class ResetPasswordConfirmAPI(APIView):
def post(self, request, token):
if not (forget_user := ForgotPassword.objects.filter(id=token).first()):

return CustomResponse(
general_message="Invalid Token"
).get_failure_response()

current_time = DateTimeUtils.get_current_utc_time()
if forget_user.expiry > current_time:

return self.save_password(
request,
forget_user
)
return self.save_password(request, forget_user)

forget_user.delete()

return CustomResponse(
general_message="Link is expired"
).get_failure_response()
return CustomResponse(general_message="Link is expired").get_failure_response()

def save_password(self, request, forget_user):
new_password = request.data.get("password")
Expand All @@ -363,11 +315,11 @@ def save_password(self, request, forget_user):
general_message="New Password Saved Successfully"
).get_success_response()

class UserProfilePictureView(APIView):

class UserProfilePictureView(APIView):
# def get(self, request, user_id):
# user = User.objects.filter(id=user_id).first()

# if user is None:
# return CustomResponse(
# general_message="No user data available"
Expand All @@ -381,50 +333,46 @@ class UserProfilePictureView(APIView):
# general_message="No Profile picture available"
# ).get_failure_response()

def patch(self,request):
def patch(self, request):
DiscordWebhooks.general_updates(
WebHookCategory.USER_PROFILE.value,
WebHookActions.UPDATE.value,
JWTUtils.fetch_user_id(request)
)
WebHookCategory.USER_PROFILE.value,
WebHookActions.UPDATE.value,
JWTUtils.fetch_user_id(request),
)
return CustomResponse(
general_message="Successfully updated"
).get_success_response()
general_message="Successfully updated"
).get_success_response()

def post(self, request):
user_id = request.data.get('user_id')
user_id = request.data.get("user_id")
user = User.objects.filter(id=user_id).first()

if user is None:
return CustomResponse(
general_message="No user data available"
).get_failure_response()
pic = request.FILES.get('profile')

pic = request.FILES.get("profile")

if pic is None:
return CustomResponse(
general_message="No profile picture"
).get_failure_response()

if not pic.content_type.startswith("image/"):
return CustomResponse(
general_message="Expected an image"
).get_failure_response()
extention = '.png' # os.path.splitext(pic.name)[1]

extention = ".png" # os.path.splitext(pic.name)[1]
fs = FileSystemStorage()
filename = f"user/profile/{user_id}{extention}"
if fs.exists(filename):
fs.delete(filename)
filename = fs.save(filename, pic)
file_url = fs.url(filename)
uploaded_file_url = f"{request.build_absolute_uri('/')}{file_url[1:]}"

return CustomResponse(
response={
'user_id':user.id,
'profile_pic':uploaded_file_url
}
response={"user_id": user.id, "profile_pic": uploaded_file_url}
).get_success_response()

0 comments on commit 5df9c10

Please sign in to comment.