amr_aly

Hamdy_decorator

Jul 15th, 2021
417
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Prevent user from changing the user id in this url(localhost:8000/accounts/profile/<user_id>/)
  2. def prevent_changing_user_id(function):
  3.     def wrap(request, *args, **kwargs):
  4.         from apps.accounts.views import edit_user_profile
  5.         from apps.accounts.models import UserProfile
  6.        
  7.         if request.LANGUAGE_CODE == 'en-us':
  8.             lang = 'en'
  9.         else:
  10.             lang = (request.LANGUAGE_CODE)
  11.         # print(lang)
  12.         user = request.user
  13.         user_id = request.user.id
  14.         match = UserProfile.objects.filter(user_id=user_id).exists()
  15.         if match:
  16.             uuid = UserProfile.objects.get(user_id=user_id)
  17.             url_path = '/'+ lang +'/accounts/profile/user/id/'+ str(user_id) +'/unique/id/'+ str(uuid.profile_uuid) +'/'
  18.         else:
  19.             uuid = None
  20.             url_path = '/'+ lang +'/accounts/profile/user/id/'+ str(user_id) + '/' # +'/unique/id/'+ str(uuid.profile_uuid) +'/'
  21.        
  22.         if user_id is not None:
  23.             if request.path != url_path:
  24.                 # print(url_path)
  25.                 from django.http import HttpResponseRedirect
  26.                 return HttpResponseRedirect(url_path)
  27.             else:
  28.                 return function(request, *args, **kwargs)
  29.                
  30.         else:
  31.             raise PermissionDenied
  32.  
  33.     wrap.__doc__ = function.__doc__
  34.     wrap.__name__ = function.__name__
  35.     return wrap
RAW Paste Data