1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Implemented API endpoints for ImageAttachments

This commit is contained in:
Jeremy Stretch
2017-04-03 14:00:15 -04:00
parent 6bbdc2bae1
commit 2c1fa628a2
6 changed files with 83 additions and 7 deletions

View File

@@ -1,9 +1,10 @@
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from rest_framework import authentication, exceptions
from rest_framework.exceptions import APIException
from rest_framework.permissions import DjangoModelPermissions, SAFE_METHODS
from rest_framework.serializers import Field
from rest_framework.serializers import Field, ValidationError
from users.models import Token
@@ -79,6 +80,21 @@ class ChoiceFieldSerializer(Field):
return self._choices.get(data)
class ContentTypeFieldSerializer(Field):
"""
Represent a ContentType as '<app_label>.<model>'
"""
def to_representation(self, obj):
return "{}.{}".format(obj.app_label, obj.model)
def to_internal_value(self, data):
app_label, model = data.split('.')
try:
return ContentType.objects.get_by_natural_key(app_label=app_label, model=model)
except ContentType.DoesNotExist:
raise ValidationError("Invalid content type")
class WritableSerializerMixin(object):
"""
Allow for the use of an alternate, writable serializer class for write operations (e.g. POST, PUT).