Add event list API view
This commit is contained in:
@@ -29,6 +29,7 @@ from events.permissions import permission_events_view
|
||||
from mayan.celery import app
|
||||
from navigation import SourceColumn
|
||||
from rest_api.classes import APIEndPoint
|
||||
from rest_api.fields import DynamicSerializerField
|
||||
from statistics.classes import StatisticNamespace, CharJSLine
|
||||
|
||||
from .handlers import create_default_document_type
|
||||
@@ -94,6 +95,10 @@ class DocumentsApp(MayanAppConfig):
|
||||
DocumentTypeFilename = self.get_model('DocumentTypeFilename')
|
||||
DocumentVersion = self.get_model('DocumentVersion')
|
||||
|
||||
DynamicSerializerField.add_serializer(
|
||||
klass=Document,
|
||||
serializer_class='documents.serializers.DocumentSerializer'
|
||||
)
|
||||
MissingItem(
|
||||
label=_('Create a document type'),
|
||||
description=_(
|
||||
|
||||
@@ -1,15 +1,30 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from actstream.models import Action
|
||||
from rest_framework import generics
|
||||
|
||||
from rest_api.permissions import MayanPermission
|
||||
|
||||
from .classes import Event
|
||||
from .serializers import EventSerializer
|
||||
from .permissions import permission_events_view
|
||||
from .serializers import EventSerializer, EventTypeSerializer
|
||||
|
||||
|
||||
class APIEventTypeList(generics.ListAPIView):
|
||||
class APIEventTypeListView(generics.ListAPIView):
|
||||
"""
|
||||
Returns a list of all the available event types.
|
||||
"""
|
||||
|
||||
serializer_class = EventSerializer
|
||||
serializer_class = EventTypeSerializer
|
||||
queryset = sorted(Event.all(), key=lambda event: event.name)
|
||||
|
||||
|
||||
class APIEventListView(generics.ListAPIView):
|
||||
"""
|
||||
Returns a list of all the available events.
|
||||
"""
|
||||
|
||||
mayan_view_permissions = {'GET': (permission_events_view,)}
|
||||
permission_classes = (MayanPermission,)
|
||||
queryset = Action.objects.all()
|
||||
serializer_class = EventSerializer
|
||||
|
||||
@@ -35,11 +35,20 @@ class Event(object):
|
||||
self.event_type = None
|
||||
self.__class__._registry[name] = self
|
||||
|
||||
def commit(self, actor=None, action_object=None, target=None):
|
||||
model = apps.get_model('events', 'EventType')
|
||||
|
||||
def get_type(self):
|
||||
if not self.event_type:
|
||||
self.event_type, created = model.objects.get_or_create(
|
||||
EventType = apps.get_model('events', 'EventType')
|
||||
|
||||
self.event_type, created = EventType.objects.get_or_create(
|
||||
name=self.name
|
||||
)
|
||||
|
||||
return self.event_type
|
||||
|
||||
def commit(self, actor=None, action_object=None, target=None):
|
||||
if not self.event_type:
|
||||
EventType = apps.get_model('events', 'EventType')
|
||||
self.event_type, created = EventType.objects.get_or_create(
|
||||
name=self.name
|
||||
)
|
||||
|
||||
|
||||
@@ -13,9 +13,12 @@ class EventType(models.Model):
|
||||
max_length=64, unique=True, verbose_name=_('Name')
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return unicode(Event.get_label(self.name))
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Event type')
|
||||
verbose_name_plural = _('Event types')
|
||||
|
||||
def __str__(self):
|
||||
return self.get_class().label
|
||||
|
||||
def get_class(self):
|
||||
return Event.get_label(self.name)
|
||||
|
||||
@@ -1,8 +1,45 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils.six import string_types
|
||||
|
||||
from actstream.models import Action
|
||||
from rest_framework import serializers
|
||||
|
||||
from common.serializers import ContentTypeSerializer
|
||||
from rest_api.fields import DynamicSerializerField
|
||||
|
||||
class EventSerializer(serializers.Serializer):
|
||||
from .classes import Event
|
||||
from .models import EventType
|
||||
|
||||
|
||||
class EventTypeSerializer(serializers.Serializer):
|
||||
label = serializers.CharField()
|
||||
name = serializers.CharField()
|
||||
|
||||
def to_representation(self, instance):
|
||||
if isinstance(instance, Event):
|
||||
return super(EventTypeSerializer, self).to_representation(
|
||||
instance
|
||||
)
|
||||
elif isinstance(instance, EventType):
|
||||
return super(EventTypeSerializer, self).to_representation(
|
||||
instance.get_class()
|
||||
)
|
||||
elif isinstance(instance, string_types):
|
||||
return super(EventTypeSerializer, self).to_representation(
|
||||
Event.get(name=instance)
|
||||
)
|
||||
|
||||
|
||||
class EventSerializer(serializers.ModelSerializer):
|
||||
actor = DynamicSerializerField(read_only=True)
|
||||
target = DynamicSerializerField(read_only=True)
|
||||
actor_content_type = ContentTypeSerializer(read_only=True)
|
||||
target_content_type = ContentTypeSerializer(read_only=True)
|
||||
verb = EventTypeSerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
exclude = (
|
||||
'action_object_content_type', 'action_object_object_id'
|
||||
)
|
||||
model = Action
|
||||
|
||||
@@ -2,7 +2,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
from .api_views import APIEventTypeList
|
||||
from .api_views import APIEventListView, APIEventTypeListView
|
||||
from .views import EventListView, ObjectEventListView, VerbEventListView
|
||||
|
||||
urlpatterns = patterns(
|
||||
@@ -19,5 +19,6 @@ urlpatterns = patterns(
|
||||
)
|
||||
|
||||
api_urls = [
|
||||
url(r'^$', APIEventTypeList.as_view(), name='event-type-list'),
|
||||
url(r'^types/$', APIEventTypeListView.as_view(), name='event-type-list'),
|
||||
url(r'^events/$', APIEventListView.as_view(), name='event-list'),
|
||||
]
|
||||
|
||||
30
mayan/apps/rest_api/fields.py
Normal file
30
mayan/apps/rest_api/fields.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils.module_loading import import_string
|
||||
from django.utils.six import string_types
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class DynamicSerializerField(serializers.ReadOnlyField):
|
||||
serializers = {}
|
||||
|
||||
@classmethod
|
||||
def add_serializer(cls, klass, serializer_class):
|
||||
if isinstance(klass, string_types):
|
||||
klass = import_string(klass)
|
||||
|
||||
if isinstance(serializer_class, string_types):
|
||||
serializer_class = import_string(serializer_class)
|
||||
|
||||
cls.serializers[klass] = serializer_class
|
||||
|
||||
def to_representation(self, value):
|
||||
for klass, serializer_class in self.serializers.items():
|
||||
if isinstance(value, klass):
|
||||
return serializer_class(
|
||||
context={'request': self.context['request']}
|
||||
).to_representation(instance=value)
|
||||
|
||||
return _('Unable to find serializer class for: %s') % value
|
||||
@@ -12,6 +12,7 @@ from common.widgets import two_state_template
|
||||
from metadata import MetadataLookup
|
||||
from navigation import SourceColumn
|
||||
from rest_api.classes import APIEndPoint
|
||||
from rest_api.fields import DynamicSerializerField
|
||||
|
||||
from .links import (
|
||||
link_group_add, link_group_delete, link_group_edit, link_group_list,
|
||||
@@ -42,6 +43,10 @@ class UserManagementApp(MayanAppConfig):
|
||||
User = get_user_model()
|
||||
|
||||
APIEndPoint(app=self, version_string='1')
|
||||
DynamicSerializerField.add_serializer(
|
||||
klass=get_user_model(),
|
||||
serializer_class='user_management.serializers.UserSerializer'
|
||||
)
|
||||
|
||||
MetadataLookup(
|
||||
description=_('All the groups.'), name='groups',
|
||||
|
||||
Reference in New Issue
Block a user