class OrganizationInvitationBase( six.with_metaclass(OrgMeta, AbstractBaseInvitation)): class Meta(AbstractBaseInvitation.Meta): abstract = True
class AbstractOrganization( six.with_metaclass(OrgMeta, SharedBaseModel, AbstractBaseOrganization) ): """ Abstract Organization model. """ slug = SlugField( max_length=200, blank=False, editable=True, populate_from="name", unique=True, help_text=_("The name in all lowercase, suitable for URL identification"), ) class Meta(AbstractBaseOrganization.Meta): abstract = True verbose_name = _("organization") verbose_name_plural = _("organizations") def __unicode__(self): return self.name def get_absolute_url(self): return reverse("organization_detail", kwargs={"organization_pk": self.pk}) def add_user(self, user, is_admin=False): """ Adds a new user and if the first user makes the user an admin and the owner. """ users_count = self.users.all().count() if users_count == 0: is_admin = True # TODO get specific org user? org_user = self._org_user_model.objects.create( user=user, organization=self, is_admin=is_admin ) if users_count == 0: # TODO get specific org user? self._org_owner_model.objects.create( organization=self, organization_user=org_user ) # User added signal user_added.send(sender=self, user=user) return org_user def remove_user(self, user): """ Deletes a user from an organization. """ org_user = self._org_user_model.objects.get(user=user, organization=self) org_user.delete() # User removed signal user_removed.send(sender=self, user=user) def get_or_add_user(self, user, **kwargs): """ Adds a new user to the organization, and if it's the first user makes the user an admin and the owner. Uses the `get_or_create` method to create or return the existing user. `user` should be a user instance, e.g. `auth.User`. Returns the same tuple as the `get_or_create` method, the `OrganizationUser` and a boolean value indicating whether the OrganizationUser was created or not. """ is_admin = kwargs.pop("is_admin", False) users_count = self.users.all().count() if users_count == 0: is_admin = True org_user, created = self._org_user_model.objects.get_or_create( organization=self, user=user, defaults={"is_admin": is_admin} ) if users_count == 0: self._org_owner_model.objects.create( organization=self, organization_user=org_user ) if created: # User added signal user_added.send(sender=self, user=user) return org_user, created def change_owner(self, new_owner): """ Changes ownership of an organization. """ old_owner = self.owner.organization_user self.owner.organization_user = new_owner self.owner.save() # Owner changed signal owner_changed.send(sender=self, old=old_owner, new=new_owner) def is_admin(self, user): """ Returns True is user is an admin in the organization, otherwise false """ return True if self.organization_users.filter( user=user, is_admin=True ) else False def is_owner(self, user): """ Returns True is user is the organization's owner, otherwise false """ return self.owner.organization_user.user == user
class OrganizationOwnerBase( six.with_metaclass(OrgMeta, AbstractBaseOrganizationOwner)): class Meta(AbstractBaseOrganizationOwner.Meta): abstract = True