bgarcial

Account Profile View to many type of users

Apr 3rd, 2017
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. class AccountProfilesView(LoginRequiredMixin, UpdateView):
  2.     # All users can access this view
  3.     model = get_user_model()
  4.     #success_url = reverse_lazy('dashboard')
  5.     template_name = 'accounts/profile_form.html'
  6.     fields = '__all__'
  7.  
  8.     def get_context_data(self, **kwargs):
  9.         context = super(AccountProfilesView, self).get_context_data(**kwargs)
  10.         user = self.request.user
  11.  
  12.         if not self.request.POST:
  13.             if user.is_student:
  14.                 profile = user.get_student_profile()
  15.                 context['userprofile'] = profile
  16.                 context['form_student'] = forms.StudentProfileForm()
  17.             elif user.is_professor:
  18.                 profile = user.get_professor_profile()
  19.                 context['userprofile'] = profile
  20.                 context['form_professor'] = forms.ProfessorProfileForm()
  21.             elif user.is_executive:
  22.                 profile = user.get_executive_profile()
  23.                 context['userprofile'] = profile
  24.                 context['form_executive'] = forms.ExecutiveProfileForm()
  25.         return context
  26.  
  27.     def post(self, request, *args, **kwargs):
  28.         self.object = self.get_object()
  29.         user = self.request.user
  30.         if user.is_student:
  31.             context['form_student'] = forms.StudentProfileForm(self.request.POST)
  32.         elif user.is_professor:
  33.             context['form_professor'] = forms.ProfessorProfileForm(self.request.POST)
  34.         elif user.is_executive:
  35.             context['form_executive'] = forms.ExecutiveProfileForm(self.request.POST)
  36.         return super(AccountProfilesView, self).post(request, *args, **kwargs)
  37.  
  38.     def form_valid(self, form):
  39.         context = self.get_context_data(form=form)
  40.         user = self.request.user
  41.         user = form.save()
  42.         if user.is_student:
  43.             student = context['form_student'].save(commit=False)
  44.             student.user = user
  45.             student.save()
  46.         elif user.is_professor:
  47.             professor = context['form_professor'].save(commit=False)
  48.             professor.user = user
  49.             professor.save()
  50.         elif user.is_executive:
  51.             executive = context['form_executive'].save(commit=False)
  52.             executive.user = user
  53.             executive.save()
  54.         return super(AccountProfilesView, self).form_valid(form)
  55.  
  56.     def get_success_url(self):
  57.         return reverse('dashboard')
Advertisement
Add Comment
Please, Sign In to add comment