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
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.encoding import force_str
|
2014-10-07 13:08:59 +00:00
|
|
|
from django.utils.module_loading import autodiscover_modules
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import gettext 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):
|
2015-04-23 14:34:04 +00:00
|
|
|
def create(self, request, *args, **kwargs):
|
2015-04-05 18:02:36 +00:00
|
|
|
from django.contrib.admin.models import ADDITION
|
2015-04-23 14:34:04 +00:00
|
|
|
response = super(LogApiMixin, self).create(request, *args, **kwargs)
|
2015-04-05 18:02:36 +00:00
|
|
|
message = _('Added.')
|
2015-04-23 14:34:04 +00:00
|
|
|
self.log(request, message, ADDITION, instance=self.serializer.instance)
|
2015-04-05 18:02:36 +00:00
|
|
|
return response
|
2021-07-08 10:46:55 +00:00
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
""" stores serializer for accessing instance on create() """
|
|
|
|
super(LogApiMixin, self).perform_create(serializer)
|
|
|
|
self.serializer = serializer
|
2021-07-08 10:46:55 +00:00
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
def update(self, request, *args, **kwargs):
|
2015-04-05 18:02:36 +00:00
|
|
|
from django.contrib.admin.models import CHANGE
|
2015-04-23 14:34:04 +00:00
|
|
|
response = super(LogApiMixin, self).update(request, *args, **kwargs)
|
|
|
|
message = _('Changed data')
|
2015-04-05 18:02:36 +00:00
|
|
|
self.log(request, message, CHANGE)
|
|
|
|
return response
|
2021-07-08 10:46:55 +00:00
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
def partial_update(self, request, *args, **kwargs):
|
2015-04-05 18:02:36 +00:00
|
|
|
from django.contrib.admin.models import CHANGE
|
2015-04-23 14:34:04 +00:00
|
|
|
response = super(LogApiMixin, self).partial_update(request, *args, **kwargs)
|
2015-10-05 13:31:08 +00:00
|
|
|
message = _('Changed %s') % response.data
|
2015-04-05 18:02:36 +00:00
|
|
|
self.log(request, message, CHANGE)
|
|
|
|
return response
|
2021-07-08 10:46:55 +00:00
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
def destroy(self, request, *args, **kwargs):
|
2015-04-05 18:02:36 +00:00
|
|
|
from django.contrib.admin.models import DELETION
|
|
|
|
message = _('Deleted')
|
|
|
|
self.log(request, message, DELETION)
|
2015-04-23 14:34:04 +00:00
|
|
|
response = super(LogApiMixin, self).destroy(request, *args, **kwargs)
|
2015-04-05 18:02:36 +00:00
|
|
|
return response
|
2021-07-08 10:46:55 +00:00
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
def log(self, request, message, action, instance=None):
|
2015-04-05 18:02:36 +00:00
|
|
|
from django.contrib.admin.models import LogEntry
|
2015-04-23 14:34:04 +00:00
|
|
|
instance = instance or self.get_object()
|
2015-04-05 18:02:36 +00:00
|
|
|
LogEntry.objects.log_action(
|
|
|
|
user_id=request.user.pk,
|
|
|
|
content_type_id=get_content_type_for_model(instance).pk,
|
|
|
|
object_id=instance.pk,
|
2023-11-17 12:25:13 +00:00
|
|
|
object_repr=force_str(instance),
|
2015-04-05 18:02:36 +00:00
|
|
|
action_flag=action,
|
|
|
|
change_message=message,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class LinkHeaderRouter(DefaultRouter):
|
2016-10-11 10:01:56 +00:00
|
|
|
def get_api_root_view(self, api_urls=None):
|
2014-05-08 16:59:35 +00:00
|
|
|
""" returns the root view, with all the linked collections """
|
2015-04-05 22:34:47 +00:00
|
|
|
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()
|
2021-07-08 10:46:55 +00:00
|
|
|
|
|
|
|
def register(self, prefix, viewset, basename=None):
|
2014-05-08 16:59:35 +00:00
|
|
|
""" inserts link headers on every viewset """
|
2021-07-08 10:46:55 +00:00
|
|
|
if basename is None:
|
|
|
|
basename = self.get_default_basename(viewset)
|
|
|
|
insert_links(viewset, basename)
|
|
|
|
self.registry.append((prefix, viewset, basename))
|
|
|
|
|
2014-07-10 17:34:23 +00:00
|
|
|
def get_viewset(self, prefix_or_model):
|
|
|
|
for _prefix, viewset, __ in self.registry:
|
2015-04-23 14:34:04 +00:00
|
|
|
if _prefix == prefix_or_model or viewset.queryset.model == prefix_or_model:
|
2014-07-10 17:34:23 +00:00
|
|
|
return viewset
|
2014-07-21 15:43:36 +00:00
|
|
|
msg = "%s does not have a regiestered viewset" % prefix_or_model
|
|
|
|
raise KeyError(msg)
|
2021-07-08 10:46:55 +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)
|
|
|
|
if viewset.serializer_class is None:
|
|
|
|
viewset.serializer_class = viewset().get_serializer_class()
|
2015-04-23 14:34:04 +00:00
|
|
|
viewset.serializer_class._declared_fields.update({name: field(**kwargs)})
|
2014-10-07 13:08:59 +00:00
|
|
|
viewset.serializer_class.Meta.fields += (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'))
|