From 5df7943ca24d893a3db02a1d12f494acf8dd549b Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 9 Apr 2018 20:22:12 +0200 Subject: Add a profile page to manage preferences (refs #4046) --- ishtar_common/forms_common.py | 34 ++++++++++++++++++++++++++++++++++ ishtar_common/models.py | 14 ++++++++++++++ ishtar_common/templates/navbar.html | 12 +++++++++--- ishtar_common/urls.py | 1 + ishtar_common/views.py | 19 ++++++++++++++++++- 5 files changed, 76 insertions(+), 4 deletions(-) (limited to 'ishtar_common') diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index c7cea9bdc..06debe9ce 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -731,6 +731,39 @@ class FinalAccountForm(forms.Form): return super(FinalAccountForm, self).__init__(*args, **kwargs) +class ProfilePersonForm(forms.Form): + current_profile = forms.ChoiceField(label=_(u"Current profile"), + choices=[]) + + def __init__(self, *args, **kwargs): + self.user = kwargs.pop('user') + choices, initial = [], kwargs.get('initial', {}) + for profile in self.user.ishtaruser.person.profiles.all(): + if profile.current: + initial['current_profile'] = profile.pk + choices.append((profile.pk, unicode(profile))) + kwargs['initial'] = initial + super(ProfilePersonForm, self).__init__(*args, **kwargs) + self.fields['current_profile'].choices = choices + + def save(self, *args, **kwargs): + q = models.UserProfile.objects.filter( + person__ishtaruser=self.user.ishtaruser, current=True) + for profile in q.all(): + profile.current = False + profile.save() + + q = models.UserProfile.objects.filter( + person__ishtaruser=self.user.ishtaruser, + pk=int(self.cleaned_data['current_profile']) + ) + if not q.count(): + return + profile = q.all()[0] + profile.current = True + profile.save() + + class TownForm(forms.Form): form_label = _("Towns") base_model = 'town' @@ -1009,6 +1042,7 @@ class AuthorFormSet(FormSet): return self.check_duplicate(('author',), _("There are identical authors.")) + AuthorFormset = formset_factory(AuthorFormSelection, can_delete=True, formset=AuthorFormSet) AuthorFormset.form_label = _("Authors") diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 9aaf7bab1..a6fc8722d 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -2670,6 +2670,9 @@ class UserProfile(models.Model): verbose_name = _(u"User profile") verbose_name_plural = _(u"User profiles") + def __unicode__(self): + return unicode(self.profile_type) + class IshtarUser(FullSearch): TABLE_COLS = ('username', 'person__name', 'person__surname', @@ -2706,6 +2709,17 @@ class IshtarUser(FullSearch): def __unicode__(self): return unicode(self.person) + @property + def current_profile_name(self): + q = UserProfile.objects.filter(current=True, person__ishtaruser=self) + if q.count(): + return q.values('profile_type__label').all()[0][ + 'profile_type__label'] + profile = self.person.current_profile + if not profile: + return u"" + return unicode(profile) + @classmethod def set_superuser(cls, user): q = cls.objects.filter(user_ptr=user) diff --git a/ishtar_common/templates/navbar.html b/ishtar_common/templates/navbar.html index a5c135a59..0ea06bff8 100644 --- a/ishtar_common/templates/navbar.html +++ b/ishtar_common/templates/navbar.html @@ -22,14 +22,20 @@ {% if user.is_authenticated %} {% else %} diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index 538041b82..04d6acfb2 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -122,6 +122,7 @@ urlpatterns = [ kwargs={'all_pages': True}), url(r'^import-step-by-step/(?P[0-9]+)/(?P[0-9]+)/$', views.ImportStepByStepView.as_view(), name='import_step_by_step'), + url(r'^profile/', views.ProfileEdit.as_view(), name='profile'), ] actions = [] diff --git a/ishtar_common/views.py b/ishtar_common/views.py index decbef589..0e53e7d1f 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -51,7 +51,7 @@ from django.shortcuts import redirect, render from django.template import loader from django.utils.decorators import method_decorator from django.utils.translation import ugettext, ugettext_lazy as _ -from django.views.generic import ListView, UpdateView, TemplateView +from django.views.generic import ListView, UpdateView, TemplateView, FormView from django.views.generic.edit import CreateView, DeleteView, FormView from xhtml2odt import xhtml2odt @@ -1743,6 +1743,23 @@ class AdminLoginRequiredMixin(LoginRequiredMixin): request, *args, **kwargs) +class ProfileEdit(LoginRequiredMixin, FormView): + template_name = 'ishtar/form.html' + form_class = forms.ProfilePersonForm + + def get_success_url(self): + return reverse('profile') + + def get_form_kwargs(self): + kwargs = super(ProfileEdit, self).get_form_kwargs() + kwargs['user'] = self.request.user + return kwargs + + def form_valid(self, form): + form.save() + return HttpResponseRedirect(self.get_success_url()) + + class DisplayItemView(IshtarMixin, LoginRequiredMixin, TemplateView): template_name = 'ishtar/display_item.html' -- cgit v1.2.3