Skip to content

Commit

Permalink
feat: view to update user data (#441)
Browse files Browse the repository at this point in the history
* view to update user data

* fix formatting

* initiate test suites for authv1

* check for urls mapped with correct functions

* raw test-suite for views

* test suite for views

* update doc string for user profile update

* move authv1 test-case to common test

* formatting fix

* creating log folder if not exists

* neat implimentation of log folder creation

* EMAIL_HOST_USER import fix

* remove urls testcases

* fix: update update_user_profile logic

Signed-off-by: Aditya Srivastava <adityasrivastava301199@gmail.com>

Co-authored-by: Aditya Srivastava <adityasrivastava301199@gmail.com>
  • Loading branch information
techonerd and ADI10HERO committed Mar 1, 2022
1 parent 32ecfb5 commit c6113f1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 4 deletions.
5 changes: 5 additions & 0 deletions BackEndApp/BackEndApp/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os

BASE_LOG_DIRECTORY = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "logs")
)
7 changes: 7 additions & 0 deletions BackEndApp/BackEndApp/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import logging
import os
from .constants import BASE_LOG_DIRECTORY


class AutoreloadLogFilter(logging.Filter):

# ensure log folder exists
if not os.path.exists(BASE_LOG_DIRECTORY):
os.mkdir(BASE_LOG_DIRECTORY)

def filter(self, record: logging.LogRecord) -> bool:
if record.name.find("django.utils.autoreload") != -1:
return False
Expand Down
2 changes: 1 addition & 1 deletion BackEndApp/authv1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def find(self, by_email=False):

def update(self, field_name, new_value, **kwargs):
try:
self.collection.update(
self.collection.update_one(
{"username": self.username},
{"$set": {field_name: new_value}},
upsert=False,
Expand Down
Empty file.
1 change: 1 addition & 0 deletions BackEndApp/authv1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
path("password/forgot/", views.forgot_password, name="forgot-password"),
path("password/update/", views.update_password, name="update-password"),
path("email/verify/", views.verify_email, name="verify-email"),
path("profile/update/", views.update_profile, name="update-profile"),
]
60 changes: 57 additions & 3 deletions BackEndApp/authv1/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import email
import bcrypt
from BackEndApp.settings import EMAIL_HOST_USER
from django.conf import settings
from django.http import JsonResponse
from rest_framework.decorators import api_view
from rest_framework.response import Response
Expand Down Expand Up @@ -146,7 +147,7 @@ def forgot_password(request):
user_email = this_user.get("email")
email = EmailTemplates(this_user)
subject, msg = email.forgot_password(username, generated_otp)
send_mail(subject, msg, EMAIL_HOST_USER, [user_email])
send_mail(subject, msg, settings.EMAIL_HOST_USER, [user_email])

message = "Email sent successfully."
status = 200
Expand Down Expand Up @@ -174,7 +175,7 @@ def verify_email(request):
user_email = this_user.get("email")
email = EmailTemplates(this_user)
subject, msg = email.verify_email(username, generated_otp)
send_mail(subject, msg, EMAIL_HOST_USER, [user_email])
send_mail(subject, msg, settings.EMAIL_HOST_USER, [user_email])

message = "Email sent successfully"
status = 200
Expand Down Expand Up @@ -245,3 +246,56 @@ def update_password(request):
status = 500

return JsonResponse({"message": message}, status=status)


@api_view(["POST"])
def update_profile(request):
""".. http:post:: /auth/profile/update/
Update existing user details.
:form username: Username of existing user for which changes to happen.
:form new_username: (optional) new username for the user.
:form new_email: (optional) new email for the user.
"""
current_user = request.data.get("username")
new_username = request.data.get("new_username", None)
new_email = request.data.get("new_email", None)
user = User(username=current_user, password=None)
this_user = user.find()

if this_user is None:
message = "No user found. Please register if visiting first time"
status = 500

elif this_user is not None:
if new_username is None and new_email is None:
status = 200
message = "No change provided"
else:
exist_user_obj = User(username=new_username, email=new_email)
exist_user = exist_user_obj.find()
exist_user_by_email = exist_user_obj.find(by_email=True)

if exist_user or exist_user_by_email:
message = "user with this username or email already exists"
status = 200
else:
if new_username:
err, _ = user.update("username", new_username)
if err:
status = 500
message = "Error updating user profile."

if new_email:
err, _ = user.update("email", new_email)
if err:
status = 500
message = "Error updating user profile."
status = 200
message = "User profile info updated successfully"
else:
message = "Some error occurred! Please try again."
status = 500

return JsonResponse({"message": message}, status=status)

0 comments on commit c6113f1

Please sign in to comment.