diff --git a/mayan/apps/documents/apps.py b/mayan/apps/documents/apps.py index 1abfd005e5..9c7ff6c36d 100644 --- a/mayan/apps/documents/apps.py +++ b/mayan/apps/documents/apps.py @@ -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=_( diff --git a/mayan/apps/events/api_views.py b/mayan/apps/events/api_views.py index a317c64c15..9882cc3ce6 100644 --- a/mayan/apps/events/api_views.py +++ b/mayan/apps/events/api_views.py @@ -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 diff --git a/mayan/apps/events/classes.py b/mayan/apps/events/classes.py index d986bfb52b..1cc0ad7df5 100644 --- a/mayan/apps/events/classes.py +++ b/mayan/apps/events/classes.py @@ -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 ) diff --git a/mayan/apps/events/models.py b/mayan/apps/events/models.py index bae47fe833..659236cfe8 100644 --- a/mayan/apps/events/models.py +++ b/mayan/apps/events/models.py @@ -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) diff --git a/mayan/apps/events/serializers.py b/mayan/apps/events/serializers.py index 6223363413..aa3926eb5e 100644 --- a/mayan/apps/events/serializers.py +++ b/mayan/apps/events/serializers.py @@ -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 diff --git a/mayan/apps/events/urls.py b/mayan/apps/events/urls.py index b256e1585e..0641d17c31 100644 --- a/mayan/apps/events/urls.py +++ b/mayan/apps/events/urls.py @@ -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'), ] diff --git a/mayan/apps/rest_api/fields.py b/mayan/apps/rest_api/fields.py new file mode 100644 index 0000000000..df2a5853d0 --- /dev/null +++ b/mayan/apps/rest_api/fields.py @@ -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 diff --git a/mayan/apps/user_management/apps.py b/mayan/apps/user_management/apps.py index 992b11dc9c..3bf3f437ab 100644 --- a/mayan/apps/user_management/apps.py +++ b/mayan/apps/user_management/apps.py @@ -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',