1 from django.contrib import messages
2 from django.contrib.auth.decorators import login_required
3 from django.core.exceptions import ObjectDoesNotExist
4 from django.urls import reverse
5 from django.utils.decorators import method_decorator
6 from django.views.generic import FormView
7 from registration.backends.simple.views import RegistrationView as BaseRegistrationView
9 from account.forms import AccountSettingsForm
10 from account.models import UserProfile
13 class RegistrationView(BaseRegistrationView):
14 template_name = 'registration/registration_form.html'
16 def get_context_data(self, **kwargs):
17 context = super(RegistrationView, self).get_context_data(**kwargs)
18 context.update({'title': "Registration"})
21 def register(self, form):
22 new_user = super(RegistrationView, self).register(form)
23 UserProfile.objects.create(user=new_user)
24 messages.add_message(self.request, messages.INFO, 'Please complete your user profile.')
27 def get_success_url(self, user):
28 return reverse('account:settings')
31 @method_decorator(login_required, name='dispatch')
32 class AccountSettingsView(FormView):
33 form_class = AccountSettingsForm
34 template_name = 'registration/registration_form.html'
37 def dispatch(self, request, *args, **kwargs):
39 request.user.userprofile
40 except ObjectDoesNotExist:
41 UserProfile.objects.create(user=request.user)
42 messages.add_message(self.request, messages.INFO,
43 'Please complete your user profile to proceed.')
44 return super(AccountSettingsView, self).dispatch(request, *args, **kwargs)
46 def get_context_data(self, **kwargs):
47 context = super(AccountSettingsView, self).get_context_data(**kwargs)
48 context.update({'title': "Settings"})
51 def get_initial(self):
52 user = self.request.user
53 initial = super(AccountSettingsView, self).get_initial()
54 initial['first_name'] = user.first_name
55 initial['last_name'] = user.last_name
56 initial['email'] = user.email
57 initial['company'] = user.userprofile.company
58 initial['ssh_public_key'] = user.userprofile.sshkey
59 initial['pgp_public_key'] = user.userprofile.pgpkey
60 initial['timezone'] = user.userprofile.timezone
63 def form_valid(self, form):
64 user = self.request.user
65 user.first_name = form.cleaned_data['first_name']
66 user.last_name = form.cleaned_data['last_name']
67 user.email = form.cleaned_data['email']
68 user.userprofile.company = form.cleaned_data['company']
69 user.userprofile.sshkey = form.cleaned_data['ssh_public_key']
70 user.userprofile.pgpkey = form.cleaned_data['pgp_public_key']
71 user.userprofile.timezone = form.cleaned_data['timezone']
72 user.userprofile.save()
73 if not user.is_active:
76 messages.add_message(self.request, messages.INFO,
78 return super(AccountSettingsView, self).form_valid(form)