From 70aa7ae3a3f5cdb74148eed6095a87b5f7b9c8ae Mon Sep 17 00:00:00 2001 From: Tris Forster Date: Wed, 1 Mar 2023 11:18:35 +1100 Subject: [PATCH] Fixed reference to section type --- app/interface/views.py | 3 +-- app/library/models.py | 2 +- app/library/tests.py | 20 +++++++++++++++++--- app/library/urls.py | 8 ++++---- app/library/views/api.py | 9 ++++----- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/interface/views.py b/app/interface/views.py index af94149..0f4a20a 100644 --- a/app/interface/views.py +++ b/app/interface/views.py @@ -232,12 +232,11 @@ class EnsembleDetailView(EnsembleMixin, DetailView): if inactive: projects = projects.order_by('-pk') else: - projects = projects.filter(active=True, event_date__gte=timezone.now()-timezone.timedelta(7)) + projects = projects.active().current() data['inactive'] = inactive data['object_list'] = projects if self.request.is_admin: - #data['ensemble_link'] = signed_url(f'{self.request.path}?nonce={self.ensemble.nonce}') data['ensemble_link'] = self.request.path + "?auth=" + self.ensemble.auth() return data diff --git a/app/library/models.py b/app/library/models.py index bb2b001..361e878 100644 --- a/app/library/models.py +++ b/app/library/models.py @@ -276,7 +276,7 @@ class Work(models.Model): return f"{composer[:4]}-{work}-{self.pk:05d}".upper() def assigned_instruments(self): - return Section.objects.filter(doc__work_id=self.pk, type=Section.TYPE_INSTRUMENT).values_list('tag', flat=True) + return Section.objects.filter(doc__work_id=self.pk).values_list('tag', flat=True) def unassigned_instruments(self): assigned = set(self.assigned_instruments()) diff --git a/app/library/tests.py b/app/library/tests.py index 1d961bd..af91d42 100644 --- a/app/library/tests.py +++ b/app/library/tests.py @@ -3,6 +3,7 @@ from interface.tests import AccessTestCase from django.contrib.auth.models import User from interface.models import Ensemble, Project from . import models +from .views.api import WorkSerializer class LibraryTestCase(AccessTestCase): @@ -30,7 +31,7 @@ class LibraryTestCase(AccessTestCase): ) WORKS = ( - {'name': 'Baby on Board', 'collection': 'ned'}, + {'name': 'Baby on Board', 'collection': 'ned', 'docs': [{'upload': 'local:baby_on_board.pdf'}]}, ) PROTECTED_URLS = ( @@ -41,6 +42,7 @@ class LibraryTestCase(AccessTestCase): '/collections/2/works/1/partset', '/collections/2/works/1/add_to_project', '/collections/2/works/1/upload', + '/collections/2/docs/1/annotate', ) @classmethod @@ -57,8 +59,20 @@ class LibraryTestCase(AccessTestCase): cls.works = {} for details in cls.WORKS: - collection = details.pop('collection') - cls.works[details['name']] = models.Work.objects.create(collection=cls.collections[collection], **details) + collection = cls.collections[details.pop('collection')] + #details.setdefault('docs', []) + #details.setdefault('meta_info', []) + #s = WorkSerializer(data=details) + #assert s.is_valid(), s.errors + #s.save(collection_id=collection.pk) + docs = details.pop('docs', []) + obj = models.Work.objects.create(collection=collection, **details) + for doc in docs: + obj.docs.create(**doc) + cls.works[details['name']] = obj + + + def oldSetUp(self): self.homer = User.objects.create(username='homer') diff --git a/app/library/urls.py b/app/library/urls.py index 9e213cc..aec1277 100644 --- a/app/library/urls.py +++ b/app/library/urls.py @@ -35,8 +35,8 @@ urlpatterns = [ path('collections//download//', views.PartDownloadView.as_view(), name="part_download"), #path('api/', include(router.urls)) - path('api/library/collections//export', api.CollectionExportView.as_view(), name="collection_export"), - path('api/library/works//export', api.WorkExportView.as_view(), name="work_export"), - path('api/library/collections//import', api.WorkImportView.as_view(), name="work_import"), - path('api/library/collections//bulk_import', api.CollectionImportView.as_view(), name="collection_import"), + path('api/collections/', api.CollectionExportView.as_view(), name="collection_export"), + path('api/collections//works/', api.WorkExportView.as_view(), name="work_export"), + path('api/collections//import', api.WorkImportView.as_view(), name="work_import"), + path('api/collections//bulk_import', api.CollectionImportView.as_view(), name="collection_import"), ] diff --git a/app/library/views/api.py b/app/library/views/api.py index b549fa7..e5d9394 100644 --- a/app/library/views/api.py +++ b/app/library/views/api.py @@ -83,9 +83,8 @@ class SectionSerializer(serializers.ModelSerializer): class DocumentSerializer(serializers.ModelSerializer): - upload = serializers.URLField() + upload = serializers.CharField() sections = SectionSerializer(many=True) - #doctype = serializers.CharField(source='get_doctype_display') #def to_internal_value(self, data): # r = requests.get(data['upload'], stream=True) @@ -97,8 +96,7 @@ class DocumentSerializer(serializers.ModelSerializer): def to_representation(self, instance): data = super().to_representation(instance) - if data['upload'][0] == '/': - data['upload'] = 'http://localhost:8000' + (data['upload']) + data['upload'] = instance.upload.url return data def create(self, validated_data): @@ -169,6 +167,7 @@ from rest_framework import generics class CollectionExportView(AuthorizedResourceMixin, generics.RetrieveAPIView): serializer_class = CollectionSerializer + lookup_url_kwarg = 'collection' def get_queryset(self): return Collection.objects.filter(administrators=self.request.user) @@ -177,7 +176,7 @@ class WorkExportView(AuthorizedResourceMixin, generics.RetrieveAPIView): serializer_class = WorkSerializer def get_queryset(self): - return Work.objects.filter(collection__administrators=self.request.user) + return Work.objects.filter(collection__administrators=self.request.user, collection=self.kwargs['collection']) class WorkImportView(AuthorizedResourceMixin, generics.CreateAPIView): serializer_class = WorkSerializer