diff options
| author | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-08-23 18:25:58 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-08-23 18:25:58 +0200 | 
| commit | 7be596f41305b4be26796625bd800d58a04bb0a7 (patch) | |
| tree | 9743bec37b2c651fafecfc373a1d5bb8bc2aadc3 /ishtar_common/models.py | |
| parent | ed74b751839d42d30a74b0d67a47ae138a537377 (diff) | |
| download | Ishtar-7be596f41305b4be26796625bd800d58a04bb0a7.tar.bz2 Ishtar-7be596f41305b4be26796625bd800d58a04bb0a7.zip | |
PersonType from single value to multi value for Person
Diffstat (limited to 'ishtar_common/models.py')
| -rw-r--r-- | ishtar_common/models.py | 45 | 
1 files changed, 29 insertions, 16 deletions
| diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 0155896f4..12b1b79f9 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -47,10 +47,9 @@ def post_save_user(sender, **kwargs):      else:          ishtaruser = q.all()[0]      if ishtaruser.is_superuser \ -       and ishtaruser.person.person_type.txt_idx != 'administrator': -        ishtaruser.person.person_type = PersonType.objects.get( -                                                    txt_idx='administrator') -        ishtaruser.person.save() +       and not ishtaruser.has_right('administrator'): +        ishtaruser.person.person_types.add(PersonType.objects.get( +                                                   txt_idx='administrator'))  post_save.connect(post_save_user, sender=User)  # HistoricalRecords enhancement: don't save identical versions @@ -193,16 +192,18 @@ class GeneralType(models.Model):          return u""      @classmethod -    def get_types(cls, dct={}, instances=False, exclude=[]): +    def get_types(cls, dct={}, instances=False, exclude=[], empty_first=True):          base_dct = dct.copy()          if hasattr(cls, 'parent'): -            return cls._get_parent_types(base_dct, instances, exclude=exclude) -        return cls._get_types(base_dct, instances, exclude=exclude) +            return cls._get_parent_types(base_dct, instances, exclude=exclude, +                                         empty_first=empty_first) +        return cls._get_types(base_dct, instances, exclude=exclude, +                              empty_first=empty_first)      @classmethod -    def _get_types(cls, dct={}, instances=False, exclude=[]): +    def _get_types(cls, dct={}, instances=False, exclude=[], empty_first=True):          dct['available'] = True -        if not instances: +        if not instances and empty_first:              yield ('', '--')          items = cls.objects.filter(**dct)          if exclude: @@ -237,9 +238,10 @@ class GeneralType(models.Model):                  yield sub_child      @classmethod -    def _get_parent_types(cls, dct={}, instances=False, exclude=[]): +    def _get_parent_types(cls, dct={}, instances=False, exclude=[], +                          empty_first=True):          dct['available'] = True -        if not instances: +        if not instances and empty_first:              yield ('', '--')          dct['parent'] = None          items = cls.objects.filter(**dct) @@ -395,10 +397,10 @@ class WizardStep(models.Model):  class UserDashboard:      def __init__(self): -        types = IshtarUser.objects.values('person__person_type', -                                          'person__person_type__label') +        types = IshtarUser.objects.values('person__person_types', +                                          'person__person_types__label')          self.types = types.annotate(number=Count('pk'))\ -                          .order_by('person__person_type') +                          .order_by('person__person_types')  class Dashboard:      def __init__(self, model): @@ -563,7 +565,7 @@ class Person(Address, OwnPerms) :                                 null=True)      name = models.CharField(_(u"Name"), max_length=30)      email = models.CharField(_(u"Email"), max_length=40, blank=True, null=True) -    person_type = models.ForeignKey(PersonType, verbose_name=_(u"Type")) +    person_types = models.ManyToManyField(PersonType, verbose_name=_(u"Types"))      attached_to = models.ForeignKey('Organization',                         verbose_name=_(u"Is attached to"), blank=True, null=True) @@ -584,6 +586,14 @@ class Person(Address, OwnPerms) :                                 if getattr(self, attr)]          return u" ".join(values) +    def has_right(self, right_name): +        if type(right_name) in (list, tuple): +            return bool( +              self.person_types.filter(txt_idx__in=right_name).count() or +              self.person_types.filter(wizard__url_name__in=right_name).count()) +        return bool(self.person_types.filter(txt_idx=right_name).count() or +                self.person_types.filter(wizard__url_name=right_name).count()) +      def full_label(self):          values = []          if self.title: @@ -613,10 +623,13 @@ class IshtarUser(User):              person_type = PersonType.objects.get(txt_idx='public_access')          person = Person.objects.create(title='Mr', surname=surname,                                         name=name, email=email, -                                       person_type=person_type,                                         history_modifier=user) +        person.person_types.add(person_type)          return IshtarUser.objects.create(user_ptr=user, person=person) +    def has_right(self, right_name): +       return self.person.has_right('administrator') +  class AuthorType(GeneralType):      class Meta:          verbose_name = _(u"Author type") | 
