diff options
Diffstat (limited to 'ishtar_common/forms_common.py')
| -rw-r--r-- | ishtar_common/forms_common.py | 72 | 
1 files changed, 62 insertions, 10 deletions
| diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index af16e6deb..edea0cde2 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -771,6 +771,7 @@ class ProfileForm(ManageOldType):      profile_type = forms.ChoiceField(label=_(u"Type"), choices=[],                                       required=False)      area = widgets.Select2MultipleField(label=_(u"Areas"), required=False) +    name = forms.CharField(label=_(u"Name"), required=False)      pk = forms.IntegerField(label=" ", widget=forms.HiddenInput, required=False)      TYPES = [ @@ -798,37 +799,88 @@ class FinalAccountForm(forms.Form):  class ProfilePersonForm(forms.Form): +    """ +    Edit the current profile +    """      current_profile = forms.ChoiceField(label=_(u"Current profile"),                                          choices=[]) +    name = forms.CharField(label=_(u"Name"), required=False) +    profile_type = forms.ChoiceField(label=_(u"Profile type"), required=False, +                                     disabled=True, choices=[]) +    duplicate_profile = forms.BooleanField( +        label=_(u"Duplicate this profile"), required=False) +    delete_profile = forms.BooleanField( +        label=_(u"Delete this profile"), required=False, +    )      def __init__(self, *args, **kwargs):          self.user = kwargs.pop('user')          choices, initial = [], kwargs.get('initial', {}) -        for profile in self.user.ishtaruser.person.profiles.all(): +        current_profile = None +        for profile in self.user.ishtaruser.person.profiles.order_by( +                'name', 'profile_type__label').all():              if profile.current: +                current_profile = profile                  initial['current_profile'] = profile.pk              choices.append((profile.pk, unicode(profile))) +        if current_profile: +            initial['name'] = current_profile.name or \ +                              current_profile.profile_type +            initial['profile_type'] = current_profile.profile_type.pk          kwargs['initial'] = initial          super(ProfilePersonForm, self).__init__(*args, **kwargs)          self.fields['current_profile'].choices = choices -    def save(self, session): -        q = models.UserProfile.objects.filter( -            person__ishtaruser=self.user.ishtaruser, current=True) -        for profile in q.all(): -            profile.current = False -            profile.save() +        if not current_profile or \ +                not self.user.ishtaruser.person.profiles.filter( +                    profile_type=current_profile.profile_type).exclude( +                    pk=current_profile.pk).count(): +            # cannot delete the current profile if no profile of this type is +            # available +            self.fields.pop('delete_profile') + +        if not current_profile: +            return +        self.fields['profile_type'].choices = [ +            (current_profile.profile_type.pk, current_profile.profile_type.name) +        ] +    def save(self, session):          q = models.UserProfile.objects.filter(              person__ishtaruser=self.user.ishtaruser, -            pk=int(self.cleaned_data['current_profile']) -        ) +            pk=self.cleaned_data['current_profile'])          if not q.count():              return          profile = q.all()[0] + +        # manage deletion +        if self.cleaned_data.get('delete_profile', None): +            q = self.user.ishtaruser.person.profiles.filter( +                    profile_type=profile.profile_type).exclude( +                    pk=profile.pk) +            if not q.count(): +                # cannot delete the current profile if no profile of this type +                # is available +                return +            new_current = q.all()[0] +            new_current.current = True +            new_current.save() +            profile.delete() +            return + +        name = self.cleaned_data['name'] + +        # manage duplication +        if self.cleaned_data.get('duplicate_profile', None): +            profile_name = profile.name or profile.profile_type.label +            if name == profile_name: +                name += unicode(_(u" (duplicate)")) +            profile.duplicate(name=name) +            return +          profile.current = True +        profile.name = name          profile.save() -          clean_session_cache(session) | 
