core: add custom group model with hierarchy , add tree admin
This commit is contained in:
36
passbook/admin/api/v1/groups.py
Normal file
36
passbook/admin/api/v1/groups.py
Normal file
@ -0,0 +1,36 @@
|
||||
"""passbook admin gorup API"""
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.serializers import ModelSerializer, Serializer
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
|
||||
from passbook.core.models import Group
|
||||
|
||||
|
||||
class RecursiveField(Serializer):
|
||||
"""Recursive field for manytomanyfield"""
|
||||
|
||||
def to_representation(self, value):
|
||||
serializer = self.parent.parent.__class__(value, context=self.context)
|
||||
return serializer.data
|
||||
|
||||
def create(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
def update(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
class GroupSerializer(ModelSerializer):
|
||||
"""Group Serializer"""
|
||||
|
||||
children = RecursiveField(many=True)
|
||||
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = '__all__'
|
||||
|
||||
class GroupViewSet(ModelViewSet):
|
||||
"""Group Viewset"""
|
||||
|
||||
permission_classes = [IsAdminUser]
|
||||
serializer_class = GroupSerializer
|
||||
queryset = Group.objects.filter(parent__isnull=True)
|
@ -1,8 +0,0 @@
|
||||
# from django.conf.urls import url, include
|
||||
|
||||
# # Add this!
|
||||
# from passbook.admin.api.v1.source import SourceResource
|
||||
|
||||
# urlpatterns = [
|
||||
# url(r'source/', include(SourceResource.urls())),
|
||||
# ]
|
@ -1,26 +0,0 @@
|
||||
# from rest_framework.serializers import HyperlinkedModelSerializer
|
||||
# from passbook.admin.api.v1.utils import LookupSerializer
|
||||
# from passbook.core.models import Source
|
||||
# from passbook.oauth_client.models import OAuthSource
|
||||
|
||||
# from rest_framework.viewsets import ModelViewSet
|
||||
|
||||
# class LookupSourceSerializer(HyperlinkedModelSerializer):
|
||||
|
||||
# def to_representation(self, instance):
|
||||
# if isinstance(instance, Source):
|
||||
# return SourceSerializer(instance=instance).data
|
||||
# elif isinstance(instance, OAuthSource):
|
||||
# return OAuthSourceSerializer(instance=instance).data
|
||||
# else:
|
||||
# return LookupSourceSerializer(instance=instance).data
|
||||
|
||||
# class Meta:
|
||||
# model = Source
|
||||
# fields = '__all__'
|
||||
|
||||
|
||||
# class SourceViewSet(ModelViewSet):
|
||||
|
||||
# serializer_class = LookupSourceSerializer
|
||||
# queryset = Source.objects.select_subclasses()
|
9
passbook/admin/api/v1/urls.py
Normal file
9
passbook/admin/api/v1/urls.py
Normal file
@ -0,0 +1,9 @@
|
||||
"""passbook admin API URLs"""
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from passbook.admin.api.v1.groups import GroupViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'groups', GroupViewSet)
|
||||
|
||||
urlpatterns = router.urls
|
@ -1,18 +0,0 @@
|
||||
"""passbook admin api utils"""
|
||||
# from django.db.models import Model
|
||||
# from rest_framework.serializers import ModelSerializer
|
||||
|
||||
|
||||
# class LookupSerializer(ModelSerializer):
|
||||
|
||||
# mapping = {}
|
||||
|
||||
# def to_representation(self, instance):
|
||||
# for __model, __serializer in self.mapping.items():
|
||||
# if isinstance(instance, __model):
|
||||
# return __serializer(instance=instance).to_representation(instance)
|
||||
# raise KeyError(instance.__class__.__name__)
|
||||
|
||||
# class Meta:
|
||||
# model = Model
|
||||
# fields = '__all__'
|
Reference in New Issue
Block a user