mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add a special serializer class for FileField which has a custom to_representation function
- This was solving a very subtle bug which will probably only ever apply to a single installation instance
- Future me will most likely not remember what this was for or how it works
- In any case, there we go
- Ref: http://www.cdrf.co/3.9/rest_framework.fields/Field.html
(cherry picked from commit 7305094854
)
This commit is contained in:
parent
3678c940eb
commit
861e30e8d6
@ -8,6 +8,9 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
|
||||||
@ -50,3 +53,32 @@ class InvenTreeModelSerializer(serializers.ModelSerializer):
|
|||||||
instance.clean()
|
instance.clean()
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
class InvenTreeAttachmentSerializerField(serializers.FileField):
|
||||||
|
"""
|
||||||
|
Override the DRF native FileField serializer,
|
||||||
|
to remove the leading server path.
|
||||||
|
|
||||||
|
For example, the FileField might supply something like:
|
||||||
|
|
||||||
|
http://127.0.0.1:8000/media/foo/bar.jpg
|
||||||
|
|
||||||
|
Whereas we wish to return:
|
||||||
|
|
||||||
|
/media/foo/bar.jpg
|
||||||
|
|
||||||
|
Why? You can't handle the why!
|
||||||
|
|
||||||
|
Actually, if the server process is serving the data at 127.0.0.1,
|
||||||
|
but a proxy service (e.g. nginx) is then providing DNS lookup to the outside world,
|
||||||
|
then an attachment which prefixes the "address" of the internal server
|
||||||
|
will not be accessible from the outside world.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def to_representation(self, value):
|
||||||
|
|
||||||
|
if not value:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return os.path.join(str(settings.MEDIA_URL), str(value))
|
||||||
|
@ -15,6 +15,7 @@ from django.db.models.functions import Coalesce
|
|||||||
from company.serializers import SupplierPartSerializer
|
from company.serializers import SupplierPartSerializer
|
||||||
from part.serializers import PartBriefSerializer
|
from part.serializers import PartBriefSerializer
|
||||||
from InvenTree.serializers import UserSerializerBrief, InvenTreeModelSerializer
|
from InvenTree.serializers import UserSerializerBrief, InvenTreeModelSerializer
|
||||||
|
from InvenTree.serializers import InvenTreeAttachmentSerializerField
|
||||||
|
|
||||||
|
|
||||||
class LocationBriefSerializer(InvenTreeModelSerializer):
|
class LocationBriefSerializer(InvenTreeModelSerializer):
|
||||||
@ -232,6 +233,8 @@ class StockItemTestResultSerializer(InvenTreeModelSerializer):
|
|||||||
|
|
||||||
key = serializers.CharField(read_only=True)
|
key = serializers.CharField(read_only=True)
|
||||||
|
|
||||||
|
attachment = InvenTreeAttachmentSerializerField()
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
user_detail = kwargs.pop('user_detail', False)
|
user_detail = kwargs.pop('user_detail', False)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user