def get_queryset(self, request): # Prefer the Django >= 1.6 interface but maintain # backward compatibility method = getattr( super(GuardedModelAdminMixin, self), 'get_queryset', getattr(super(GuardedModelAdminMixin, self), 'queryset', None)) qs = method(request) if request.user.is_superuser: return qs if self.user_can_access_owned_objects_only: filters = {self.user_owned_objects_field: request.user} qs = qs.filter(**filters) if self.user_can_access_owned_by_group_objects_only: User = get_user_model() user_rel_name = User.groups.field.related_query_name() qs_key = '%s__%s' % (self.group_owned_objects_field, user_rel_name) filters = {qs_key: request.user} qs = qs.filter(**filters) if self.user_can_access_owned_by_organization_objects_only: User = get_user_model() m = OrgManager() qs = m.get_for_user(request.user) return qs
class AbstractBaseOrganization(UnicodeMixin, models.Model): """ The umbrella object with which users can be associated. An organization can have multiple users but only one who can be designated the owner user. """ name = models.CharField(max_length=200, help_text=_("The name of the organization")) is_active = models.BooleanField(default=True) objects = OrgManager() active = ActiveOrgManager() class Meta: abstract = True ordering = ['name'] def __unicode__(self): return self.name @property def user_relation_name(self): """ Returns the string name of the related name to the user. This provides a consistent interface across different organization model classes. """ return "{0}_{1}".format(self._meta.app_label.lower(), self.__class__.__name__.lower()) def is_member(self, user): return True if user in self.users.all() else False
class Organization(TimeStampedModel): """The umbrella object with which users can be associated. An organization can have multiple users but only one who can be designated the owner user. """ name = models.CharField(max_length=100, help_text=_("The name of the organization")) slug = AutoSlugField( max_length=100, blank=False, editable=True, populate_from='name', unique=True, help_text=_( "The name in all lowercase, suitable for URL identification")) users = models.ManyToManyField(USER_MODEL, through="OrganizationUser") is_active = models.BooleanField(default=True) objects = OrgManager() active = ActiveOrgManager() class Meta: ordering = ['name'] verbose_name = _("organization") verbose_name_plural = _("organizations") def __unicode__(self): return self.name @permalink def get_absolute_url(self): return ('organization_detail', (), {'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 org_user = OrganizationUser.objects.create(user=user, organization=self, is_admin=is_admin) if users_count == 0: OrganizationOwner.objects.create(organization=self, organization_user=org_user) return org_user def is_member(self, user): return True if user in self.users.all() else False def is_admin(self, user): return True if self.organization_users.filter(user=user, is_admin=True) else False
class Organization(TimeStampedModel): """ The umbrella object with which users can be associated. An organization can have multiple users but only one who can be designated the owner user. """ name = models.CharField(max_length=200, help_text=_("The name of the organization")) slug = AutoSlugField(max_length=200, blank=False, editable=True, populate_from='name', unique=True, help_text=_("The name in all lowercase, suitable for URL identification")) is_active = models.BooleanField(default=True) description = models.TextField(null=True) street = models.CharField(max_length=200, null=True) city = models.CharField(max_length=200, null=True) state = models.CharField(max_length=25, null=True) zip_code = models.IntegerField(null=True) objects = OrgManager() active = ActiveOrgManager() class Meta: ordering = ['name'] verbose_name = _("organization") verbose_name_plural = _("organizations") def __str__(self): return u"{0}".format(self.name) @permalink def get_absolute_url(self): return ('organization_detail', (), {'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 org_user = User.objects.create(user=user, organization=self, is_admin=is_admin) if users_count == 0: OrganizationOwner.objects.create(organization=self, organization_user=org_user) return org_user def get_or_add_user(self, user, is_admin=False): """ 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. """ users_count = self.users.all().count() if users_count == 0: is_admin = True org_user, created = OrganizationUser.objects.get_or_create( organization=self, user=user, defaults={'is_admin': is_admin}) if users_count == 0: OrganizationOwner.objects.create(organization=self, organization_user=org_user) return org_user, created def is_member(self, user): return True if user in self.users.all() else False def is_admin(self, user): return True if self.organization_users.filter(user=user, is_admin=True) else False
class Organization(models.Model): """ The umbrella object with which users can be associated. An organization can have multiple users but only one who can be designated the owner user. """ name = models.CharField(max_length=200,verbose_name="Όνομα") description = models.CharField(max_length=256,verbose_name="Περιγραφή") image = models.ImageField(max_length=256, default="/static/main/img/group.png",verbose_name="Εικόνα",upload_to=MEDIA_ROOT) slug = AutoSlugField(max_length=200, blank=True, editable=True, populate_from='name', unique=True,) users = models.ManyToManyField(USER_MODEL, through="OrganizationUser") is_active = models.BooleanField(default=True) objects = OrgManager() active = ActiveOrgManager() class Meta: ordering = ['name'] verbose_name = _("organization") verbose_name_plural = _("organizations") def __str__(self): return u"{0}".format(self.name) @permalink def get_absolute_url(self): return ('organization_detail', (), {'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 org_user = OrganizationUser.objects.create(user=user, organization=self, is_admin=is_admin) if users_count == 0: OrganizationOwner.objects.create(organization=self, organization_user=org_user) return org_user def get_or_add_user(self, user, is_admin=False): """ 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. """ users_count = self.users.all().count() if users_count == 0: is_admin = True org_user, created = OrganizationUser.objects.get_or_create( organization=self, user=user, defaults={'is_admin': is_admin}) if users_count == 0: OrganizationOwner.objects.create(organization=self, organization_user=org_user) return org_user, created def is_member(self, user): return True if user in self.users.all() else False def is_admin(self, user): return True if self.organization_users.filter(user=user, is_admin=True) else False
class Organization(TimeStampedModel): """ The umbrella object with which users can be associated. An organization can have multiple users but only one who can be designated the owner user. """ name = models.CharField(max_length=200, help_text=_("The name of the organization")) slug = AutoSlugField( max_length=200, blank=False, editable=True, populate_from='name', unique=True, help_text=_( "The name in all lowercase, suitable for URL identification")) users = models.ManyToManyField(settings.AUTH_USER_MODEL, through="OrganizationUser") is_active = models.BooleanField(default=True) custom_data = JSONField(blank=True, default={}) custom_settings = JSONField(blank=True, default={}) objects = OrgManager() active = ActiveOrgManager() class Meta: ordering = ['name'] verbose_name = _("organization") verbose_name_plural = _("organizations") def __str__(self): return self.name def get_absolute_url(self): url = reverse('organization_detail', args=[], kwargs={'organization_pk': str(self.pk)}) return url + "?org=%s" % self.slug 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 org_user = OrganizationUser.objects.create(user=user, organization=self, is_admin=is_admin) if users_count == 0: OrganizationOwner.objects.create(organization=self, organization_user=org_user) return org_user def get_or_add_user(self, user, is_admin=False): """ 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. """ users_count = self.users.all().count() if users_count == 0: is_admin = True org_user, created = OrganizationUser.objects.get_or_create( organization=self, user=user, defaults={'is_admin': is_admin}) if users_count == 0: OrganizationOwner.objects.create(organization=self, organization_user=org_user) return org_user, created def is_member(self, user): return True if user in self.users.all() else False def is_admin(self, user): return True if self.organization_users.filter(user=user, is_admin=True) else False