diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-05-28 16:58:42 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-12 08:49:36 +0200 | 
| commit | 1dc2eb2ebc5a837d745358620f1dfd35544632ac (patch) | |
| tree | c616f2686db785b7b1103beb85636d1891cc354c /ishtar_common/models.py | |
| parent | eba5908a065ba3461fdc1337f617c1af7f2abc60 (diff) | |
| download | Ishtar-1dc2eb2ebc5a837d745358620f1dfd35544632ac.tar.bz2 Ishtar-1dc2eb2ebc5a837d745358620f1dfd35544632ac.zip | |
User profile form: duplicate, delete and edit
Diffstat (limited to 'ishtar_common/models.py')
| -rw-r--r-- | ishtar_common/models.py | 39 | 
1 files changed, 36 insertions, 3 deletions
| diff --git a/ishtar_common/models.py b/ishtar_common/models.py index a5bf6cf3a..b31338a2d 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -2792,6 +2792,7 @@ post_delete.connect(post_save_cache, sender=ProfileType)  class UserProfile(models.Model): +    name = models.CharField(_(u"Name"), blank=True, default=u"", max_length=100)      profile_type = models.ForeignKey(          ProfileType, verbose_name=_(u"Profile type"))      areas = models.ManyToManyField("Area", verbose_name=_(u"Areas"), @@ -2803,9 +2804,10 @@ class UserProfile(models.Model):      class Meta:          verbose_name = _(u"User profile")          verbose_name_plural = _(u"User profiles") +        unique_together = (('name', 'profile_type', 'person'),)      def __unicode__(self): -        lbl = unicode(self.profile_type) +        lbl = self.name or unicode(self.profile_type)          if not self.areas.count():              return lbl          return u"{} ({})".format(lbl, u", ".join( @@ -2824,6 +2826,34 @@ class UserProfile(models.Model):      def area_labels(self):          return u", ".join([unicode(area) for area in self.areas.all()]) +    def duplicate(self, **kwargs): +        areas = [area for area in self.areas.all()] +        new_item = self +        new_item.pk = None +        for key in kwargs: +            setattr(new_item, key, kwargs[key]) +        new_item.save() +        for area in areas: +            new_item.areas.add(area) +        return new_item + +    def save(self, force_insert=False, force_update=False, using=None, +             update_fields=None): +        super(UserProfile, self).save( +            force_insert=force_insert, force_update=force_update, using=using, +            update_fields=update_fields) + +        # only one current profile per user +        if not self.current: +            return +        q = UserProfile.objects.filter( +            person=self.person, current=True).exclude(pk=self.pk) +        if not q.count(): +            return +        for p in q.all(): +            p.current = False +            p.save() +  class IshtarUser(FullSearch):      TABLE_COLS = ('username', 'person__name', 'person__surname', @@ -2864,8 +2894,8 @@ class IshtarUser(FullSearch):      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'] +            vals = q.values('profile_type__label', 'name').all()[0] +            return vals['name'] or vals['profile_type__label']          profile = self.person.current_profile          if not profile:              return u"" @@ -2885,6 +2915,9 @@ class IshtarUser(FullSearch):          admin, created = ProfileType.objects.get_or_create(              txt_idx='administrator')          if user.is_superuser: +            if UserProfile.objects.filter( +                    profile_type=admin, person=person).count(): +                return              UserProfile.objects.get_or_create(                  profile_type=admin, person=person,                  defaults={'current': True}) | 
