Skip to content

Commit

Permalink
Major update on User Profile.
Browse files Browse the repository at this point in the history
User profile update feature with URL encryption and User authorization.
  • Loading branch information
tamaraiselvan committed Jan 25, 2023
1 parent f1c3fba commit 6b2f287
Show file tree
Hide file tree
Showing 15 changed files with 476 additions and 38 deletions.
Binary file modified Base_Master/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file modified Base_Master/__pycache__/urls.cpython-311.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion Base_Master/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-k6k-x^q$jjp*@bs#_7yw-!&cyn4g^byvr*lseiok=h=t#!)bw4'

ENCRYPT_KEY = b'Py6zhVP-eFxkfq0kHUN0ZmIePwwaOeQ12ZmrFAVLbI8='
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

Expand Down
1 change: 1 addition & 0 deletions Base_Master/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
path('home', views.home, name="home"),
path('register/', views.signup_view, name="sign_up"),
path('profile/', views.profile, name="profile"),
path('update/<str:id>/', views.update, name="update"),
]
Binary file not shown.
Binary file modified firstapp/__pycache__/forms.cpython-311.pyc
Binary file not shown.
Binary file modified firstapp/__pycache__/views.cpython-311.pyc
Binary file not shown.
38 changes: 38 additions & 0 deletions firstapp/encryption_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from cryptography.fernet import Fernet
import base64
import logging
import traceback
from django.conf import settings

#this is your "password/ENCRYPT_KEY". keep it in settings.py file
key = Fernet.generate_key()

def encrypt(txt):
try:
# convert integer etc to string first
txt = str(txt)
# get the key from settings
cipher_suite = Fernet(settings.ENCRYPT_KEY) # key should be byte
# #input should be byte, so convert the text to byte
encrypted_text = cipher_suite.encrypt(txt.encode('ascii'))
# encode to urlsafe base64 format
encrypted_text = base64.urlsafe_b64encode(encrypted_text).decode("ascii")
return encrypted_text
except Exception as e:
# log the error if any
print(e)
logging.getLogger("error_logger").error(traceback.format_exc())
return None


def decrypt(txt):
try:
# base64 decode
txt = base64.urlsafe_b64decode(txt)
cipher_suite = Fernet(settings.ENCRYPT_KEY)
decoded_text = cipher_suite.decrypt(txt).decode("ascii")
return decoded_text
except Exception as e:
# log the error
logging.getLogger("error_logger").error(traceback.format_exc())
return None
6 changes: 5 additions & 1 deletion firstapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def clean_email(self):
return email

class profile_edit(forms.ModelForm):
first_name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'placeholder': 'Enter First name'}), required=True)
last_name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'placeholder': 'Enter last name'}), required=True)
email = forms.EmailField(widget=forms.EmailInput(attrs={'class':'form-control', 'placeholder': 'Enter Email address'}), required=True)
username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'placeholder': 'Enter username'}), required=True)
class Meta:
model = User
fields=['username','email','first_name','last_name', "is_superuser"]
fields=['username','email','first_name','last_name',]
48 changes: 47 additions & 1 deletion firstapp/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth import login, authenticate
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
# Create your views here.

def home(request):
if request.user.is_authenticated:
return render(request, 'home.html')
Expand All @@ -26,4 +29,47 @@ def signup_view(request):


def profile(request):
return render(request, 'profile.html')
if request.user.is_authenticated:
lst = User.objects.filter(username=request.user).values('id')
l=[]
for i in lst:
i['encrypt_key']=encrypt(i['id'])
i['id']=i['id']
l.append(i)
return render(request, 'profile.html', {'lst':l})
else:
messages.error(request, 'Please Provide the credentials to Login to your account.')
return redirect("login")


from django.shortcuts import redirect, render, get_object_or_404
from firstapp.encryption_util import *
@login_required
def update(request,id):
id=decrypt(id)

# dictionary for initial data with
# field names as keys
context ={}

# fetch the object related to passed id
obj = get_object_or_404(User, id = id)
if request.user == obj:

# pass the object as instance in form
form = profile_edit(request.POST or None, instance = obj)

# save the data from the form and
# redirect to detail_view
if form.is_valid():
form.save()
messages.success(request, 'Profile updated successfully!')
return redirect('profile')

# add form dictionary to context
context["form"] = form

return render(request, "edit_profile.html", context)
else:
messages.error(request,"You cannot access other user profile")
return redirect("profile")
9 changes: 3 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
asgiref==3.5.2
cffi==1.15.1
cryptography==39.0.0
Django==4.0.4
django-crispy-forms==1.14.0
django-jazzmin==2.5.0
sqlparse==0.4.2
tzdata==2022.1
asgiref==3.5.2
Django==4.0.4
django-crispy-forms==1.14.0
django-jazzmin==2.5.0
pycparser==2.21
sqlparse==0.4.2
tzdata==2022.1
Loading

0 comments on commit 6b2f287

Please sign in to comment.