mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Add GraphQL for users and groups
This commit is contained in:
@ -5,6 +5,7 @@ from dcim.graphql.schema import DCIMQuery
|
|||||||
from extras.graphql.schema import ExtrasQuery
|
from extras.graphql.schema import ExtrasQuery
|
||||||
from ipam.graphql.schema import IPAMQuery
|
from ipam.graphql.schema import IPAMQuery
|
||||||
from tenancy.graphql.schema import TenancyQuery
|
from tenancy.graphql.schema import TenancyQuery
|
||||||
|
from users.graphql.schema import UsersQuery
|
||||||
from virtualization.graphql.schema import VirtualizationQuery
|
from virtualization.graphql.schema import VirtualizationQuery
|
||||||
|
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ class Query(
|
|||||||
ExtrasQuery,
|
ExtrasQuery,
|
||||||
IPAMQuery,
|
IPAMQuery,
|
||||||
TenancyQuery,
|
TenancyQuery,
|
||||||
|
UsersQuery,
|
||||||
VirtualizationQuery,
|
VirtualizationQuery,
|
||||||
graphene.ObjectType
|
graphene.ObjectType
|
||||||
):
|
):
|
||||||
|
0
netbox/users/graphql/__init__.py
Normal file
0
netbox/users/graphql/__init__.py
Normal file
12
netbox/users/graphql/schema.py
Normal file
12
netbox/users/graphql/schema.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import graphene
|
||||||
|
|
||||||
|
from netbox.graphql.fields import ObjectField, ObjectListField
|
||||||
|
from .types import *
|
||||||
|
|
||||||
|
|
||||||
|
class UsersQuery(graphene.ObjectType):
|
||||||
|
group = ObjectField(GroupType)
|
||||||
|
groups = ObjectListField(GroupType)
|
||||||
|
|
||||||
|
user = ObjectField(UserType)
|
||||||
|
users = ObjectListField(UserType)
|
37
netbox/users/graphql/types.py
Normal file
37
netbox/users/graphql/types.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from django.contrib.auth.models import Group, User
|
||||||
|
from graphene_django import DjangoObjectType
|
||||||
|
|
||||||
|
from users import filtersets
|
||||||
|
from utilities.querysets import RestrictedQuerySet
|
||||||
|
|
||||||
|
__all__ = (
|
||||||
|
'GroupType',
|
||||||
|
'UserType',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class GroupType(DjangoObjectType):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Group
|
||||||
|
fields = ('id', 'name')
|
||||||
|
filterset_class = filtersets.GroupFilterSet
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_queryset(cls, queryset, info):
|
||||||
|
return RestrictedQuerySet(model=Group)
|
||||||
|
|
||||||
|
|
||||||
|
class UserType(DjangoObjectType):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = (
|
||||||
|
'id', 'username', 'password', 'first_name', 'last_name', 'email', 'is_staff', 'is_active', 'date_joined',
|
||||||
|
'groups',
|
||||||
|
)
|
||||||
|
filterset_class = filtersets.UserFilterSet
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_queryset(cls, queryset, info):
|
||||||
|
return RestrictedQuerySet(model=User)
|
@ -17,7 +17,7 @@ class AppTest(APITestCase):
|
|||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
class UserTest(APIViewTestCases.APIViewTestCase):
|
class UserTest(APIViewTestCases.GraphQLTestCase, APIViewTestCases.APIViewTestCase):
|
||||||
model = User
|
model = User
|
||||||
view_namespace = 'users'
|
view_namespace = 'users'
|
||||||
brief_fields = ['display', 'id', 'url', 'username']
|
brief_fields = ['display', 'id', 'url', 'username']
|
||||||
@ -48,7 +48,7 @@ class UserTest(APIViewTestCases.APIViewTestCase):
|
|||||||
User.objects.bulk_create(users)
|
User.objects.bulk_create(users)
|
||||||
|
|
||||||
|
|
||||||
class GroupTest(APIViewTestCases.APIViewTestCase):
|
class GroupTest(APIViewTestCases.GraphQLTestCase, APIViewTestCases.APIViewTestCase):
|
||||||
model = Group
|
model = Group
|
||||||
view_namespace = 'users'
|
view_namespace = 'users'
|
||||||
brief_fields = ['display', 'id', 'name', 'url']
|
brief_fields = ['display', 'id', 'name', 'url']
|
||||||
|
@ -33,6 +33,9 @@ def get_graphql_type_for_model(model):
|
|||||||
Return the GraphQL type class for the given model.
|
Return the GraphQL type class for the given model.
|
||||||
"""
|
"""
|
||||||
app_name, model_name = model._meta.label.split('.')
|
app_name, model_name = model._meta.label.split('.')
|
||||||
|
# Object types for Django's auth models are in the users app
|
||||||
|
if app_name == 'auth':
|
||||||
|
app_name = 'users'
|
||||||
class_name = f'{app_name}.graphql.types.{model_name}Type'
|
class_name = f'{app_name}.graphql.types.{model_name}Type'
|
||||||
try:
|
try:
|
||||||
return dynamic_import(class_name)
|
return dynamic_import(class_name)
|
||||||
|
Reference in New Issue
Block a user