Advertisement
YomoMan

Untitled

Dec 15th, 2021
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. def add_staff(request):
  2.     form = StaffForm(request.POST or None, request.FILES or None)
  3.     context = {'form': form, 'page_title': 'Add Staff'}
  4.     if request.method == 'POST':
  5.         if form.is_valid():
  6.             first_name = form.cleaned_data.get('first_name')
  7.             last_name = form.cleaned_data.get('last_name')
  8.             email = form.cleaned_data.get('email')
  9.             gender = form.cleaned_data.get('gender')
  10.             password = form.cleaned_data.get('password')
  11.             course = form.cleaned_data.get('course')
  12.  
  13.  
  14.             try:
  15.                 passport = request.FILES.get('profile_pic')
  16.                 fs = FileSystemStorage()
  17.                 filename = fs.save(passport.name, passport)
  18.                 passport_url = fs.url(filename)
  19.             except:
  20.                 passport = None
  21.                 passport_url = None
  22.  
  23.             address = form.cleaned_data.get('address')
  24.  
  25.  
  26.             try:
  27.                 user = CustomUser.objects.create_user(
  28.                     email=email, password=password, user_type=2, first_name=first_name, last_name=last_name, profile_pic=passport_url)
  29.                 user.gender = gender
  30.                 user.address = address
  31.                 user.staff.course = course
  32.                 user.save()
  33.                 messages.success(request, "Successfully Added")
  34.                 return redirect(reverse('add_staff'))
  35.  
  36.             except Exception as e:
  37.                 messages.error(request, "Could Not Add " + str(e))
  38.         else:
  39.             messages.error(request, "Please fulfil all requirements")
  40.  
  41.     return render(request, 'hod_template/add_staff_template.html', context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement