mirror of
https://github.com/ansible/awx.git
synced 2026-02-28 08:18:43 -03:30
Listing tags attached to an organization, and basic model/view things around tag details
This commit is contained in:
@@ -26,8 +26,12 @@ class BaseList(generics.ListCreateAPIView):
|
|||||||
raise exceptions.NotImplementedError
|
raise exceptions.NotImplementedError
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
if self.__class__.model == User:
|
base = self._get_queryset()
|
||||||
return self._get_queryset().filter(is_active=True)
|
model = self.__class__.model
|
||||||
|
if model == User:
|
||||||
|
return base.filter(is_active=True)
|
||||||
|
elif model in [ Tag, AuditTrail ]:
|
||||||
|
return base
|
||||||
else:
|
else:
|
||||||
return self._get_queryset().filter(active=True)
|
return self._get_queryset().filter(active=True)
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ class CommonModel(models.Model):
|
|||||||
description = models.TextField(blank=True, default='')
|
description = models.TextField(blank=True, default='')
|
||||||
created_by = models.ForeignKey('auth.User', on_delete=SET_NULL, null=True, related_name='%s(class)s_created') # not blank=False on purpose for admin!
|
created_by = models.ForeignKey('auth.User', on_delete=SET_NULL, null=True, related_name='%s(class)s_created') # not blank=False on purpose for admin!
|
||||||
creation_date = models.DateField(auto_now_add=True)
|
creation_date = models.DateField(auto_now_add=True)
|
||||||
tags = models.ManyToManyField('Tag', related_name='%(class)s_tags', blank=True)
|
tags = models.ManyToManyField('Tag', related_name='%(class)s_by_tag', blank=True)
|
||||||
audit_trail = models.ManyToManyField('AuditTrail', related_name='%(class)s_audit_trails', blank=True)
|
audit_trail = models.ManyToManyField('AuditTrail', related_name='%(class)s_by_audit_trail', blank=True)
|
||||||
active = models.BooleanField(default=True)
|
active = models.BooleanField(default=True)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
@@ -76,6 +76,9 @@ class Tag(models.Model):
|
|||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return unicode(self.name)
|
return unicode(self.name)
|
||||||
|
|
||||||
|
def get_absolute_url(self):
|
||||||
|
import lib.urls
|
||||||
|
return reverse(lib.urls.views_TagsDetail, args=(self.pk,))
|
||||||
|
|
||||||
class AuditTrail(CommonModel):
|
class AuditTrail(CommonModel):
|
||||||
'''
|
'''
|
||||||
|
|||||||
@@ -257,7 +257,22 @@ class OrganizationsTest(BaseTest):
|
|||||||
self.assertEquals(org1_users['count'], 1)
|
self.assertEquals(org1_users['count'], 1)
|
||||||
|
|
||||||
def test_get_item_subobjects_tags(self):
|
def test_get_item_subobjects_tags(self):
|
||||||
pass
|
|
||||||
|
# put some tags on the org
|
||||||
|
org1 = Organization.objects.get(pk=2)
|
||||||
|
tag1 = Tag.objects.create(name='atag')
|
||||||
|
tag2 = Tag.objects.create(name='btag')
|
||||||
|
org1.tags.add(tag1)
|
||||||
|
org1.tags.add(tag2)
|
||||||
|
|
||||||
|
# see if we can list the users added to the organization
|
||||||
|
orgs = self.get(self.collection(), expect=200, auth=self.get_super_credentials())
|
||||||
|
org1_tags_url = orgs['results'][1]['related']['tags']
|
||||||
|
org1_tags = self.get(org1_tags_url, expect=200, auth=self.get_normal_credentials())
|
||||||
|
self.assertEquals(org1_tags['count'], 2)
|
||||||
|
org1_tags = self.get(org1_tags_url, expect=200, auth=self.get_super_credentials())
|
||||||
|
self.assertEquals(org1_tags['count'], 2)
|
||||||
|
org1_tags = self.get(org1_tags_url, expect=403, auth=self.get_other_credentials())
|
||||||
|
|
||||||
def test_get_item_subobjects_audit_trail(self):
|
def test_get_item_subobjects_audit_trail(self):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -48,12 +48,15 @@ class OrganizationsAuditTrailList(BaseList):
|
|||||||
serializer_class = AuditTrailSerializer
|
serializer_class = AuditTrailSerializer
|
||||||
permission_classes = (CustomRbac,)
|
permission_classes = (CustomRbac,)
|
||||||
|
|
||||||
class OrganizationsUsersList(BaseList):
|
class OrganizationsUsersList(BaseSubList):
|
||||||
|
|
||||||
model = User
|
model = User
|
||||||
serializer_class = UserSerializer
|
serializer_class = UserSerializer
|
||||||
permission_classes = (CustomRbac,)
|
permission_classes = (CustomRbac,)
|
||||||
|
|
||||||
|
parent_model = Organization
|
||||||
|
relationship = 'users'
|
||||||
|
|
||||||
def _get_queryset(self):
|
def _get_queryset(self):
|
||||||
''' to list users in the organization, I must be a superuser or org admin '''
|
''' to list users in the organization, I must be a superuser or org admin '''
|
||||||
organization = Organization.objects.get(pk=self.kwargs['pk'])
|
organization = Organization.objects.get(pk=self.kwargs['pk'])
|
||||||
@@ -61,12 +64,15 @@ class OrganizationsUsersList(BaseList):
|
|||||||
raise PermissionDenied()
|
raise PermissionDenied()
|
||||||
return User.objects.filter(organizations__in = [ organization ])
|
return User.objects.filter(organizations__in = [ organization ])
|
||||||
|
|
||||||
class OrganizationsAdminsList(BaseList):
|
class OrganizationsAdminsList(BaseSubList):
|
||||||
|
|
||||||
model = User
|
model = User
|
||||||
serializer_class = UserSerializer
|
serializer_class = UserSerializer
|
||||||
permission_classes = (CustomRbac,)
|
permission_classes = (CustomRbac,)
|
||||||
|
|
||||||
|
parent_model = Organization
|
||||||
|
relationship = 'admins'
|
||||||
|
|
||||||
def _get_queryset(self):
|
def _get_queryset(self):
|
||||||
''' to list admins in the organization, I must be a superuser or org admin '''
|
''' to list admins in the organization, I must be a superuser or org admin '''
|
||||||
organization = Organization.objects.get(pk=self.kwargs['pk'])
|
organization = Organization.objects.get(pk=self.kwargs['pk'])
|
||||||
@@ -91,8 +97,21 @@ class OrganizationsProjectsList(BaseSubList):
|
|||||||
return Project.objects.filter(organizations__in = [ organization ])
|
return Project.objects.filter(organizations__in = [ organization ])
|
||||||
|
|
||||||
class OrganizationsTagsList(BaseList):
|
class OrganizationsTagsList(BaseList):
|
||||||
# FIXME: guts & tests
|
|
||||||
pass
|
model = Tag
|
||||||
|
serializer_class = TagSerializer
|
||||||
|
permission_classes = (CustomRbac,)
|
||||||
|
|
||||||
|
parent_model = Organization # for sub list
|
||||||
|
relationship = 'tags' # " "
|
||||||
|
|
||||||
|
def _get_queryset(self):
|
||||||
|
''' to list tags in the organization, I must be a superuser or org admin '''
|
||||||
|
organization = Organization.objects.get(pk=self.kwargs['pk'])
|
||||||
|
if not (self.request.user.is_superuser or self.request.user in organization.admins.all()):
|
||||||
|
# FIXME: use: organization.can_user_administrate(self.request.user)
|
||||||
|
raise PermissionDenied()
|
||||||
|
return Tag.objects.filter(organization_by_tag__in = [ organization ])
|
||||||
|
|
||||||
class ProjectsDetail(BaseDetail):
|
class ProjectsDetail(BaseDetail):
|
||||||
|
|
||||||
@@ -100,5 +119,10 @@ class ProjectsDetail(BaseDetail):
|
|||||||
serializer_class = ProjectSerializer
|
serializer_class = ProjectSerializer
|
||||||
permission_classes = (CustomRbac,)
|
permission_classes = (CustomRbac,)
|
||||||
|
|
||||||
|
class TagsDetail(BaseDetail):
|
||||||
|
|
||||||
|
model = Tag
|
||||||
|
serializer_class = TagSerializer
|
||||||
|
permission_classes = (CustomRbac,)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ views_ProjectsDetail = views.OrganizationsDetail.as_view()
|
|||||||
# events services
|
# events services
|
||||||
# jobs services
|
# jobs services
|
||||||
# tags service
|
# tags service
|
||||||
|
views_TagsDetail = views.TagsDetail.as_view()
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
@@ -64,6 +65,8 @@ urlpatterns = patterns('',
|
|||||||
# jobs services
|
# jobs services
|
||||||
|
|
||||||
# tags service
|
# tags service
|
||||||
|
url(r'^api/v1/tags/(?P<pk>[0-9]+)/$', views_TagsDetail),
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if 'django.contrib.admin' in settings.INSTALLED_APPS:
|
if 'django.contrib.admin' in settings.INSTALLED_APPS:
|
||||||
|
|||||||
Reference in New Issue
Block a user