Skip to content

Commit

Permalink
[change] Improved or removed empty label for organization field #171
Browse files Browse the repository at this point in the history
Closes #171

Co-authored-by: Federico Capoano <federico.capoano@gmail.com>
  • Loading branch information
purhan and nemesifier authored Dec 30, 2020
1 parent 69cd57a commit a23e05f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
16 changes: 10 additions & 6 deletions openwisp_users/multitenancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ def _edit_form(self, request, form):
else show everything
"""
fields = form.base_fields
if not request.user.is_superuser:
orgs_pk = request.user.organizations_managed
user = request.user
org_field = fields.get('organization')
if user.is_superuser and org_field and not org_field.required:
org_field.empty_label = _('Shared systemwide (no organization)')
elif not user.is_superuser:
orgs_pk = user.organizations_managed
# organizations relation;
# may be readonly and not present in field list
if 'organization' in fields:
org_field = fields['organization']
if org_field:
org_field.queryset = org_field.queryset.filter(pk__in=orgs_pk)
org_field.empty_label = None
# other relations
q = Q(organization__in=orgs_pk) | Q(organization=None)
for field_name in self.multitenant_shared_relations:
Expand All @@ -98,8 +102,8 @@ def multitenant_behaviour_for_user_admin(self, request):
from same organization and hide superusers
if superuser is logged in - show all users
"""
if not request.user.is_superuser:
user = request.user
user = request.user
if not user.is_superuser:
org_users = OrganizationUser.objects.filter(user=user).select_related(
'organization'
)
Expand Down
23 changes: 23 additions & 0 deletions openwisp_users/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,29 @@ def test_admin_add_user_with_invalid_email(self):
self.client.post(reverse(f'admin:{self.app_label}_user_add'), params)
mocked.assert_called_once()

def test_organization_default_label(self):
admin = self._create_admin()
self.client.force_login(admin)
with self.subTest('Test required organization label'):
r = self.client.get(reverse('admin:testapp_book_add'))
self.assertContains(r, '<option value="" selected>---------</option>')

with self.subTest('Test optional organization label for superuser'):
r = self.client.get(reverse('admin:testapp_template_add'))
self.assertContains(
r, '<option value="" selected>Shared systemwide (no organization)',
)

with self.subTest('Test optional organization label for non-superuser'):
operator = self._create_operator()
template_permissions = Permission.objects.filter(codename='add_template')
operator.user_permissions.add(*template_permissions)
self.client.force_login(operator)
r = self.client.get(reverse('admin:testapp_template_add'))
self.assertNotContains(
r, 'Shared systemwide (no organization)',
)

@classmethod
def tearDownClass(cls):
super().tearDownClass()
Expand Down
7 changes: 6 additions & 1 deletion tests/testapp/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
MultitenantRelatedOrgFilter,
)

from .models import Book, Shelf
from .models import Book, Shelf, Template


class BaseAdmin(MultitenantAdminMixin, admin.ModelAdmin):
Expand Down Expand Up @@ -51,5 +51,10 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
return super().change_view(request, object_id, form_url, extra_context)


class TemplateAdmin(BaseAdmin):
pass


admin.site.register(Shelf, ShelfAdmin)
admin.site.register(Book, BookAdmin)
admin.site.register(Template, TemplateAdmin)
3 changes: 3 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
class Template(ShareableOrgMixin):
name = models.CharField(max_length=16)

def __str__(self):
return self.name

def clean(self):
self._validate_org_reverse_relation('config_set')

Expand Down

0 comments on commit a23e05f

Please sign in to comment.