Library cleanup
This commit is contained in:
+40
-27
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Views relating to importing and exporting collection items
|
||||
"""
|
||||
|
||||
"""
|
||||
from interface.views import EnsembleMixin
|
||||
from library.views import WorkMixin
|
||||
@@ -49,22 +50,24 @@ import os.path
|
||||
from django.db import transaction
|
||||
from django.core.files.uploadedfile import TemporaryUploadedFile
|
||||
|
||||
|
||||
class WorkMetaSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = WorkMeta
|
||||
exclude = ['id', 'work']
|
||||
exclude = ["id", "work"]
|
||||
|
||||
def to_representation(self, instance):
|
||||
return f"{instance.name}:{instance.value}"
|
||||
|
||||
|
||||
def to_internal_value(self, data):
|
||||
name, _, value = data.partition(':')
|
||||
return super().to_internal_value({'name': name, 'value': value})
|
||||
name, _, value = data.partition(":")
|
||||
return super().to_internal_value({"name": name, "value": value})
|
||||
|
||||
|
||||
class SectionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Section
|
||||
exclude = ['id', 'doc']
|
||||
exclude = ["id", "doc"]
|
||||
|
||||
def to_representation(self, instance):
|
||||
start = instance.start or 0
|
||||
@@ -79,14 +82,14 @@ class SectionSerializer(serializers.ModelSerializer):
|
||||
start = None
|
||||
if end < 1:
|
||||
end = None
|
||||
return super().to_internal_value({'tag': tag, 'start': start, 'end': end})
|
||||
return super().to_internal_value({"tag": tag, "start": start, "end": end})
|
||||
|
||||
|
||||
class DocumentSerializer(serializers.ModelSerializer):
|
||||
|
||||
upload = serializers.URLField()
|
||||
sections = SectionSerializer(many=True)
|
||||
|
||||
#def to_internal_value(self, data):
|
||||
# def to_internal_value(self, data):
|
||||
# r = requests.get(data['upload'], stream=True)
|
||||
# with tempfile.NamedTemporaryFile('wb') as f:
|
||||
# shutil.copyfileobj(r.raw, f)
|
||||
@@ -96,7 +99,7 @@ class DocumentSerializer(serializers.ModelSerializer):
|
||||
|
||||
def to_representation(self, instance):
|
||||
data = super().to_representation(instance)
|
||||
data['upload'] = instance.upload.url
|
||||
data["upload"] = instance.upload.url
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
@@ -115,35 +118,40 @@ class DocumentSerializer(serializers.ModelSerializer):
|
||||
model = Document
|
||||
exclude = ["id", "work", "version", "created"]
|
||||
|
||||
|
||||
# Serializers define the API representation.
|
||||
class WorkSerializer(serializers.ModelSerializer):
|
||||
|
||||
docs = DocumentSerializer(many=True)
|
||||
meta_info = WorkMetaSerializer(many=True)
|
||||
|
||||
|
||||
class Meta:
|
||||
model = Work
|
||||
exclude = ['id', 'collection', 'projects', 'parent']
|
||||
exclude = ["id", "collection", "projects", "parent"]
|
||||
|
||||
def create(self, validated):
|
||||
with transaction.atomic():
|
||||
docs = validated.pop('docs', [])
|
||||
meta = validated.pop('meta_info', [])
|
||||
docs = validated.pop("docs", [])
|
||||
meta = validated.pop("meta_info", [])
|
||||
work = Work.objects.create(**validated)
|
||||
|
||||
for d in docs:
|
||||
sections = d.pop('sections', [])
|
||||
sections = d.pop("sections", [])
|
||||
|
||||
url = urllib.parse.urlparse(d['upload'])
|
||||
url = urllib.parse.urlparse(d["upload"])
|
||||
filename = os.path.basename(url.path)
|
||||
|
||||
r = requests.get(d['upload'], stream=True)
|
||||
r = requests.get(d["upload"], stream=True)
|
||||
if r.status_code != 200:
|
||||
raise APIException("Failed to download file")
|
||||
f = TemporaryUploadedFile(filename, r.headers['content-type'], r.headers.get('content-length'), r.encoding)
|
||||
f = TemporaryUploadedFile(
|
||||
filename,
|
||||
r.headers["content-type"],
|
||||
r.headers.get("content-length"),
|
||||
r.encoding,
|
||||
)
|
||||
shutil.copyfileobj(r.raw, f.file)
|
||||
r.close()
|
||||
d['upload'] = f
|
||||
d["upload"] = f
|
||||
doc = Document.objects.create(work_id=work.pk, **d)
|
||||
|
||||
for s in sections:
|
||||
@@ -154,22 +162,24 @@ class WorkSerializer(serializers.ModelSerializer):
|
||||
|
||||
return work
|
||||
|
||||
|
||||
class CollectionSerializer(serializers.Serializer):
|
||||
works = WorkSerializer(many=True)
|
||||
|
||||
def create(self, validated):
|
||||
s = WorkSerializer()
|
||||
print(validated)
|
||||
collection = validated['collection_id']
|
||||
collection = validated["collection_id"]
|
||||
with transaction.atomic():
|
||||
for work in validated['works']:
|
||||
work['collection_id'] = collection
|
||||
for work in validated["works"]:
|
||||
work["collection_id"] = collection
|
||||
s.create(work)
|
||||
return Collection.objects.get(pk=collection)
|
||||
|
||||
|
||||
|
||||
from rest_framework import generics
|
||||
|
||||
|
||||
class CollectionExportView(AuthorizedResourceMixin, generics.RetrieveAPIView):
|
||||
serializer_class = CollectionSerializer
|
||||
|
||||
@@ -178,23 +188,26 @@ class CollectionExportView(AuthorizedResourceMixin, generics.RetrieveAPIView):
|
||||
return Collection.objects.all()
|
||||
return Collection.objects.filter(administrators=self.request.user)
|
||||
|
||||
|
||||
class WorkExportView(AuthorizedResourceMixin, generics.RetrieveAPIView):
|
||||
serializer_class = WorkSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
works = Work.objects.filter(collection=self.kwargs['collection'])
|
||||
works = Work.objects.filter(collection=self.kwargs["collection"])
|
||||
if self.request.user.is_superuser:
|
||||
return works
|
||||
return works
|
||||
return works.filter(collection__administrators=self.request.user)
|
||||
|
||||
|
||||
class WorkImportView(AuthorizedResourceMixin, generics.CreateAPIView):
|
||||
serializer_class = WorkSerializer
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(collection_id=self.kwargs['collection'])
|
||||
serializer.save(collection_id=self.kwargs["collection"])
|
||||
|
||||
|
||||
class CollectionImportView(AuthorizedResourceMixin, generics.CreateAPIView):
|
||||
serializer_class = CollectionSerializer
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(collection_id=self.kwargs['pk'])
|
||||
serializer.save(collection_id=self.kwargs["pk"])
|
||||
|
||||
Reference in New Issue
Block a user