Skip to content

Instantly share code, notes, and snippets.

@Zadigo
Last active June 7, 2018 23:21
Show Gist options
  • Save Zadigo/bc58266e5721a7bbf4309b4c6882012c to your computer and use it in GitHub Desktop.
Save Zadigo/bc58266e5721a7bbf4309b4c6882012c to your computer and use it in GitHub Desktop.
Code for profile view in user account
class ProfileView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
context = {}
# Redirect user if is_admin
if request.user.is_admin or request.user.is_staff:
return redirect('/admin/')
# Create context with factory
context = self.profile_factory(request)
return render(request, 'shared/content_profile.html', context)
def post(self, request, *args, **kwargs):
context = self.profile_factory(request)
return render(request, 'shared/content_profile.html', context)
def profile_factory(self, request):
factory = {}
# Get user profile details
detail = MyUserProfile.objects.get(id=request.user.id)
if request.method == 'POST':
# POST with the correct form
# whether the user is a
# learner or a teacher
if request.user.is_learner:
factory['form'] = form = MyUserLearnerProfileForm(request.POST, instance=detail)
else:
factory['form'] = form = MyUserTeacherProfileForm(request.POST, instance=detail)
if form.is_valid():
form.save()
else:
initial_dict = {
'addresse': detail.addresse,
'code_postal': detail.code_postal,
}
if request.user.is_learner:
factory['form'] = MyUserLearnerProfileForm(initial=initial_dict)
else:
factory['form'] = MyUserTeacherProfileForm(initial=initial_dict)
return factory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment