From f67de73a6cabbef2d84dc79706cbeb4441800860 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Tue, 22 May 2018 11:20:11 +0200 Subject: [PATCH] add publication options: delay, beta --- apps/apiclient/__init__.py | 9 +++++---- apps/apiclient/settings.py | 9 +++++++-- apps/apiclient/views.py | 16 +++++++-------- apps/catalogue/forms.py | 5 +++++ apps/catalogue/models/book.py | 11 ++++++---- .../templates/catalogue/book_detail.html | 1 + apps/catalogue/views.py | 20 ++++++++++++++----- 7 files changed, 48 insertions(+), 23 deletions(-) diff --git a/apps/apiclient/__init__.py b/apps/apiclient/__init__.py index 38ce26e4..2709ca06 100644 --- a/apps/apiclient/__init__.py +++ b/apps/apiclient/__init__.py @@ -3,7 +3,7 @@ import urllib import json import oauth2 -from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL +from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL, BETA_API_URL if WL_CONSUMER_KEY and WL_CONSUMER_SECRET: @@ -20,8 +20,9 @@ class NotAuthorizedError(BaseException): pass -def api_call(user, path, data=None): +def api_call(user, path, data=None, beta=False): from .models import OAuthConnection + api_url = BETA_API_URL if beta else WL_API_URL conn = OAuthConnection.get(user) if not conn.access: raise NotAuthorizedError("No WL authorization for user %s." % user) @@ -31,12 +32,12 @@ def api_call(user, path, data=None): data = json.dumps(data) data = urllib.urlencode({"data": data}) resp, content = client.request( - "%s%s" % (WL_API_URL, path), + "%s%s" % (api_url, path), method="POST", body=data) else: resp, content = client.request( - "%s%s" % (WL_API_URL, path)) + "%s%s" % (api_url, path)) status = resp['status'] if status == '200': diff --git a/apps/apiclient/settings.py b/apps/apiclient/settings.py index f1eb34a4..51b49069 100755 --- a/apps/apiclient/settings.py +++ b/apps/apiclient/settings.py @@ -4,8 +4,9 @@ from django.conf import settings WL_CONSUMER_KEY = getattr(settings, 'APICLIENT_WL_CONSUMER_KEY', None) WL_CONSUMER_SECRET = getattr(settings, 'APICLIENT_WL_CONSUMER_SECRET', None) -WL_API_URL = getattr(settings, 'APICLIENT_WL_API_URL', - 'http://wolnelektury.pl/api/') +WL_API_URL = getattr(settings, 'APICLIENT_WL_API_URL', 'https://wolnelektury.pl/api/') + +BETA_API_URL = getattr(settings, 'APICLIENT_BETA_API_URL', 'http://dev.wolnelektury.pl/api/') WL_REQUEST_TOKEN_URL = getattr(settings, 'APICLIENT_WL_REQUEST_TOKEN_URL', WL_API_URL + 'oauth/request_token/') @@ -13,3 +14,7 @@ WL_ACCESS_TOKEN_URL = getattr(settings, 'APICLIENT_WL_ACCESS_TOKEN_URL', WL_API_URL + 'oauth/access_token/') WL_AUTHORIZE_URL = getattr(settings, 'APICLIENT_WL_AUTHORIZE_URL', WL_API_URL + 'oauth/authorize/') + +BETA_REQUEST_TOKEN_URL = BETA_API_URL + 'oauth/request_token/' +BETA_ACCESS_TOKEN_URL = BETA_API_URL + 'oauth/access_token/' +BETA_AUTHORIZE_URL = BETA_API_URL + 'oauth/authorize/' diff --git a/apps/apiclient/views.py b/apps/apiclient/views.py index d4960148..fb3f5077 100644 --- a/apps/apiclient/views.py +++ b/apps/apiclient/views.py @@ -7,17 +7,17 @@ import oauth2 from apiclient.models import OAuthConnection from apiclient import wl_consumer -from apiclient.settings import (WL_REQUEST_TOKEN_URL, WL_ACCESS_TOKEN_URL, - WL_AUTHORIZE_URL) +from apiclient.settings import WL_REQUEST_TOKEN_URL, WL_ACCESS_TOKEN_URL, WL_AUTHORIZE_URL +from apiclient.settings import BETA_REQUEST_TOKEN_URL, BETA_ACCESS_TOKEN_URL, BETA_AUTHORIZE_URL @login_required -def oauth(request): +def oauth(request, beta=False): if wl_consumer is None: return HttpResponse("OAuth consumer not configured.") client = oauth2.Client(wl_consumer) - resp, content = client.request(WL_REQUEST_TOKEN_URL) + resp, content = client.request(WL_REQUEST_TOKEN_URL if not beta else BETA_REQUEST_TOKEN_URL) if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) @@ -31,16 +31,16 @@ def oauth(request): conn.save() url = "%s?oauth_token=%s&oauth_callback=%s" % ( - WL_AUTHORIZE_URL, + WL_AUTHORIZE_URL if not beta else BETA_AUTHORIZE_URL, request_token['oauth_token'], - request.build_absolute_uri(reverse("apiclient_oauth_callback")), + request.build_absolute_uri(reverse("apiclient_oauth_callback" if not beta else "apiclient_beta_callback")), ) return HttpResponseRedirect(url) @login_required -def oauth_callback(request): +def oauth_callback(request, beta=False): if wl_consumer is None: return HttpResponse("OAuth consumer not configured.") @@ -49,7 +49,7 @@ def oauth_callback(request): token = oauth2.Token(conn.token, conn.token_secret) token.set_verifier(oauth_verifier) client = oauth2.Client(wl_consumer, token) - resp, content = client.request(WL_ACCESS_TOKEN_URL, method="POST") + resp, content = client.request(WL_ACCESS_TOKEN_URL if not beta else BETA_ACCESS_TOKEN_URL, method="POST") access_token = dict(cgi.parse_qsl(content)) conn.access = True diff --git a/apps/catalogue/forms.py b/apps/catalogue/forms.py index 5a0b2d5a..a256dfa1 100644 --- a/apps/catalogue/forms.py +++ b/apps/catalogue/forms.py @@ -227,3 +227,8 @@ class MarkFinalForm(forms.Form): tags=[Chunk.tag_model.objects.get(slug='editor-proofreading')], publishable=True ) + + +class PublishOptionsForm(forms.Form): + days = forms.IntegerField(label=u'po ilu dniach udostępnienić (0 = od razu)', min_value=0, initial=0) + beta = forms.BooleanField(label=u'Opublikuj na wersji testowej', required=False) diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index 42a4d1fc..7acd8706 100755 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -422,7 +422,7 @@ class Book(models.Model): parse_dublincore=parse_dublincore, strict=strict) - def publish(self, user, fake=False, host=None): + def publish(self, user, fake=False, host=None, days=0, beta=False): """ Publishes a book on behalf of a (local) user. """ @@ -430,17 +430,20 @@ class Book(models.Model): changes = self.get_current_changes(publishable=True) if not fake: book_xml = self.materialize(changes=changes) - data = {"book_xml": book_xml} + data = {"book_xml": book_xml, "days": days} if host: data['gallery_url'] = host + self.gallery_url() - apiclient.api_call(user, "books/", data) + apiclient.api_call(user, "books/", data, beta=beta) # record the publish br = BookPublishRecord.objects.create(book=self, user=user) for c in changes: ChunkPublishRecord.objects.create(book_record=br, change=c) - if not self.public: + if not self.public and days == 0: self.public = True self.save() + if self.public and days > 0: + self.public = False + self.save() post_publish.send(sender=br) def latex_dir(self): diff --git a/apps/catalogue/templates/catalogue/book_detail.html b/apps/catalogue/templates/catalogue/book_detail.html index 5f1f7902..65a22876 100755 --- a/apps/catalogue/templates/catalogue/book_detail.html +++ b/apps/catalogue/templates/catalogue/book_detail.html @@ -81,6 +81,7 @@ CC BY 2.0 (http://creativecommons.org/licenses/by/2.0/) -->
{% csrf_token %} + {{ publish_options_form.as_p }} diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index fc572c25..76001f9e 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from collections import defaultdict from datetime import datetime, date, timedelta import logging @@ -26,7 +27,6 @@ from django_cas.decorators import user_passes_test from apiclient import NotAuthorizedError from catalogue import forms from catalogue import helpers -from catalogue.forms import MarkFinalForm from catalogue.helpers import active_tab from catalogue.models import (Book, Chunk, Image, BookPublishRecord, ChunkPublishRecord, ImagePublishRecord, Project) @@ -334,9 +334,11 @@ def book(request, slug): return http.HttpResponseRedirect(book.get_absolute_url()) else: form = forms.BookForm(instance=book) + publish_options_form = forms.PublishOptionsForm() editable = True else: form = forms.ReadonlyBookForm(instance=book) + publish_options_form = forms.PublishOptionsForm() editable = False publish_error = book.publishable_error() @@ -347,6 +349,7 @@ def book(request, slug): "publishable": publishable, "publishable_error": publish_error, "form": form, + "publish_options_form": publish_options_form, "editable": editable, }) @@ -564,15 +567,22 @@ def book_append(request, slug): @require_POST @login_required def publish(request, slug): + form = forms.PublishOptionsForm(request.POST) + if form.is_valid(): + days = form.cleaned_data['days'] + beta = form.cleaned_data['beta'] + else: + days = 0 + beta = False book = get_object_or_404(Book, slug=slug) if not book.accessible(request): return HttpResponseForbidden("Not authorized.") try: protocol = 'https://' if request.is_secure() else 'http://' - book.publish(request.user, host=protocol + request.get_host()) + book.publish(request.user, host=protocol + request.get_host(), days=days, beta=beta) except NotAuthorizedError: - return http.HttpResponseRedirect(reverse('apiclient_oauth')) + return http.HttpResponseRedirect(reverse('apiclient_oauth' if not beta else 'apiclient_beta_oauth')) except BaseException, e: return http.HttpResponse(repr(e)) else: @@ -648,12 +658,12 @@ def active_users_list(request): @user_passes_test(lambda u: u.is_superuser) def mark_final(request): if request.method == 'POST': - form = MarkFinalForm(data=request.POST) + form = forms.MarkFinalForm(data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('mark_final_completed')) else: - form = MarkFinalForm() + form = forms.MarkFinalForm() return render(request, 'catalogue/mark_final.html', {'form': form}) -- 2.20.1