django-orchestra-test/orchestra/api/options.py

93 lines
3.7 KiB
Python
Raw Normal View History

2015-04-05 18:02:36 +00:00
from django.contrib.admin.options import get_content_type_for_model
2015-02-24 09:34:26 +00:00
from django.conf import settings as django_settings
2015-04-05 18:02:36 +00:00
from django.utils.encoding import force_text
2014-10-07 13:08:59 +00:00
from django.utils.module_loading import autodiscover_modules
2015-04-05 18:02:36 +00:00
from django.utils.translation import ugettext as _
2015-04-04 18:10:39 +00:00
from rest_framework.routers import DefaultRouter
2014-05-08 16:59:35 +00:00
2014-05-13 13:46:40 +00:00
from orchestra import settings
from orchestra.utils.python import import_class
2015-04-04 17:44:07 +00:00
from .helpers import insert_links
2014-05-08 16:59:35 +00:00
2015-04-05 18:02:36 +00:00
class LogApiMixin(object):
def post(self, request, *args, **kwargs):
from django.contrib.admin.models import ADDITION
response = super(LogApiMixin, self).post(request, *args, **kwargs)
message = _('Added.')
self.log_addition(request, message, ADDITION)
return response
def put(self, request, *args, **kwargs):
from django.contrib.admin.models import CHANGE
response = super(LogApiMixin, self).put(request, *args, **kwargs)
message = _('Changed')
self.log(request, message, CHANGE)
return response
def patch(self, request, *args, **kwargs):
from django.contrib.admin.models import CHANGE
response = super(LogApiMixin, self).put(request, *args, **kwargs)
message = _('Changed %s') % str(response.data)
self.log(request, message, CHANGE)
return response
def delete(self, request, *args, **kwargs):
from django.contrib.admin.models import DELETION
message = _('Deleted')
self.log(request, message, DELETION)
response = super(LogApiMixin, self).put(request, *args, **kwargs)
return response
def log(self, request, message, action):
from django.contrib.admin.models import LogEntry
instance = self.get_object()
LogEntry.objects.log_action(
user_id=request.user.pk,
content_type_id=get_content_type_for_model(instance).pk,
object_id=instance.pk,
object_repr=force_text(instance),
action_flag=action,
change_message=message,
)
2014-05-08 16:59:35 +00:00
class LinkHeaderRouter(DefaultRouter):
def get_api_root_view(self):
""" returns the root view, with all the linked collections """
APIRoot = import_class(settings.ORCHESTRA_API_ROOT_VIEW)
2014-05-13 13:46:40 +00:00
APIRoot.router = self
2014-05-08 16:59:35 +00:00
return APIRoot.as_view()
def register(self, prefix, viewset, base_name=None):
""" inserts link headers on every viewset """
if base_name is None:
base_name = self.get_default_base_name(viewset)
insert_links(viewset, base_name)
self.registry.append((prefix, viewset, base_name))
2014-07-10 17:34:23 +00:00
def get_viewset(self, prefix_or_model):
for _prefix, viewset, __ in self.registry:
if _prefix == prefix_or_model or viewset.model == prefix_or_model:
return viewset
2014-07-21 15:43:36 +00:00
msg = "%s does not have a regiestered viewset" % prefix_or_model
raise KeyError(msg)
2014-07-10 17:34:23 +00:00
2014-07-08 16:37:38 +00:00
def insert(self, prefix_or_model, name, field, **kwargs):
2014-05-08 16:59:35 +00:00
""" Dynamically add new fields to an existing serializer """
2014-07-10 17:34:23 +00:00
viewset = self.get_viewset(prefix_or_model)
2014-10-07 13:08:59 +00:00
# setattr(viewset, 'inserted', getattr(viewset, 'inserted', []))
2014-07-10 17:34:23 +00:00
if viewset.serializer_class is None:
viewset.serializer_class = viewset().get_serializer_class()
viewset.serializer_class.base_fields.update({name: field(**kwargs)})
2014-10-07 13:08:59 +00:00
# if not name in viewset.inserted:
viewset.serializer_class.Meta.fields += (name,)
# viewset.inserted.append(name)
2014-05-08 16:59:35 +00:00
# Create a router and register our viewsets with it.
2015-02-24 09:34:26 +00:00
router = LinkHeaderRouter(trailing_slash=django_settings.APPEND_SLASH)
2014-05-08 16:59:35 +00:00
2014-10-07 13:08:59 +00:00
autodiscover = lambda: (autodiscover_modules('api'), autodiscover_modules('serializers'))