From 282654ea252af7e2d30740b40bccfc9be61dd3a8 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 7 Oct 2022 15:05:57 +0200 Subject: [PATCH] Publishing collections. --- src/apiclient/__init__.py | 21 ++++++++++----- src/catalogue/admin.py | 2 +- .../migrations/0040_collection_description.py | 18 +++++++++++++ src/catalogue/models.py | 1 + src/catalogue/urls.py | 1 + src/catalogue/views.py | 26 +++++++++++++++++-- 6 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/catalogue/migrations/0040_collection_description.py diff --git a/src/apiclient/__init__.py b/src/apiclient/__init__.py index 36cd7ec7..44546d33 100644 --- a/src/apiclient/__init__.py +++ b/src/apiclient/__init__.py @@ -21,7 +21,7 @@ class NotAuthorizedError(BaseException): pass -def api_call(user, path, data=None, beta=False): +def api_call(user, path, data=None, beta=False, method=None, as_json=False): from .models import OAuthConnection api_url = BETA_API_URL if beta else WL_API_URL conn = OAuthConnection.get(user=user, beta=beta) @@ -31,21 +31,30 @@ def api_call(user, path, data=None, beta=False): client = oauth2.Client(wl_consumer, token) if data is not None: data = json.dumps(data) - data = urlencode({"data": data}) + headers = {} + if as_json: + headers["Content-Type"] = 'application/json' + else: + data = urlencode({"data": data}) + data = data.encode('utf-8') + resp, content = client.request( "%s%s" % (api_url, path), - method="POST", + method=method or 'POST', + headers=headers, body=data) else: resp, content = client.request( - "%s%s" % (api_url, path)) + "%s%s" % (api_url, path), + method=method or 'GET' + ) status = resp['status'] - if status == '200': + if status in ('200', '201'): return json.loads(content) elif status.startswith('2'): return elif status == '401': raise ApiError('User not authorized for publishing.') else: - raise ApiError("WL API call error %s, path: %s" % (status, path)) + raise ApiError("WL API call error %s, path: %s, body: %s" % (status, path, content)) diff --git a/src/catalogue/admin.py b/src/catalogue/admin.py index f00e7fc9..958230b4 100644 --- a/src/catalogue/admin.py +++ b/src/catalogue/admin.py @@ -312,7 +312,7 @@ class CollectionAdmin(admin.ModelAdmin): autocomplete_fields = [] prepopulated_fields = {"slug": ("name",)} search_fields = ["name"] - fields = ['name', 'slug', 'category', 'notes', 'estimated_costs'] + fields = ['name', 'slug', 'category', 'description', 'notes', 'estimated_costs'] readonly_fields = ['estimated_costs'] inlines = [AuthorInline, BookInline] diff --git a/src/catalogue/migrations/0040_collection_description.py b/src/catalogue/migrations/0040_collection_description.py new file mode 100644 index 00000000..ba8f3bf7 --- /dev/null +++ b/src/catalogue/migrations/0040_collection_description.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.6 on 2022-10-07 12:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('catalogue', '0039_author_photo_author_photo_attribution_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='collection', + name='description', + field=models.TextField(blank=True, verbose_name='description'), + ), + ] diff --git a/src/catalogue/models.py b/src/catalogue/models.py index db840da5..4544fc68 100644 --- a/src/catalogue/models.py +++ b/src/catalogue/models.py @@ -265,6 +265,7 @@ class Collection(models.Model): slug = models.SlugField(max_length=255, unique=True) category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category")) notes = models.TextField(_("notes"), blank=True) + description = models.TextField(_("description"), blank=True) class Meta: ordering = ('category', 'name') diff --git a/src/catalogue/urls.py b/src/catalogue/urls.py index 4eaf7b07..065c4f92 100644 --- a/src/catalogue/urls.py +++ b/src/catalogue/urls.py @@ -23,4 +23,5 @@ urlpatterns = [ path('wikidata//', views.WikidataView.as_view()), path('publish/author//', views.publish_author, name='catalogue_publish_author'), + path('publish/collection//', views.publish_collection, name='catalogue_publish_collection'), ] diff --git a/src/catalogue/views.py b/src/catalogue/views.py index 376d3287..95f33fd0 100644 --- a/src/catalogue/views.py +++ b/src/catalogue/views.py @@ -206,5 +206,27 @@ def publish_author(request, pk): } apiclient.api_call(request.user, f"authors/{author.slug}/", data) return redirect(reverse('admin:catalogue_author_change', args=[author.pk])) - - + + +@require_POST +@login_required +def publish_collection(request, pk): + collection = get_object_or_404(models.Collection, pk=pk) + data = { + "title": collection.name, + "description": collection.description, + "book_slugs": "\n".join( + book.slug + for book in collection.book_set.exclude(slug=None).exclude(slug='') + ) + } + apiclient.api_call( + request.user, + f"collections/{collection.slug}/", + data, + method='PUT', + as_json=True, + ) + return redirect(reverse( + 'admin:catalogue_collection_change', args=[collection.pk] + )) -- 2.20.1