mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Merge pull request #1026 from digitalocean/image-attachments
#152: Image attachments
This commit is contained in:
@@ -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).
|
||||
|
@@ -425,12 +425,13 @@ class BootstrapMixin(forms.BaseForm):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(BootstrapMixin, self).__init__(*args, **kwargs)
|
||||
|
||||
exempt_widgets = [forms.CheckboxInput, forms.ClearableFileInput, forms.FileInput, forms.RadioSelect]
|
||||
|
||||
for field_name, field in self.fields.items():
|
||||
if type(field.widget) not in [type(forms.CheckboxInput()), type(forms.RadioSelect())]:
|
||||
try:
|
||||
field.widget.attrs['class'] += ' form-control'
|
||||
except KeyError:
|
||||
field.widget.attrs['class'] = 'form-control'
|
||||
if field.widget.__class__ not in exempt_widgets:
|
||||
css = field.widget.attrs.get('class', '')
|
||||
field.widget.attrs['class'] = ' '.join([css, 'form-control']).strip()
|
||||
if field.required:
|
||||
field.widget.attrs['required'] = 'required'
|
||||
if 'placeholder' not in field.widget.attrs:
|
||||
|
@@ -174,7 +174,7 @@ class ObjectEditView(View):
|
||||
|
||||
obj = self.get_object(kwargs)
|
||||
obj = self.alter_obj(obj, request, args, kwargs)
|
||||
form = self.form_class(request.POST, instance=obj)
|
||||
form = self.form_class(request.POST, request.FILES, instance=obj)
|
||||
|
||||
if form.is_valid():
|
||||
obj = form.save(commit=False)
|
||||
|
Reference in New Issue
Block a user