add publication options: delay, beta
authorJan Szejko <janek37@gmail.com>
Tue, 22 May 2018 09:20:11 +0000 (11:20 +0200)
committerJan Szejko <janek37@gmail.com>
Tue, 22 May 2018 09:20:11 +0000 (11:20 +0200)
apps/apiclient/__init__.py
apps/apiclient/settings.py
apps/apiclient/views.py
apps/catalogue/forms.py
apps/catalogue/models/book.py
apps/catalogue/templates/catalogue/book_detail.html
apps/catalogue/views.py

index 38ce26e..2709ca0 100644 (file)
@@ -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':
index f1eb34a..51b4906 100755 (executable)
@@ -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/'
index d496014..fb3f507 100644 (file)
@@ -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
index 5a0b2d5..a256dfa 100644 (file)
@@ -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)
index 42a4d1f..7acd870 100755 (executable)
@@ -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):
index 5f1f790..65a2287 100755 (executable)
@@ -81,6 +81,7 @@
         CC BY 2.0 (http://creativecommons.org/licenses/by/2.0/)
         -->
         <form method="POST" action="{% url 'catalogue_publish' book.slug %}">{% csrf_token %}
+            {{ publish_options_form.as_p }}
             <img src="{{ STATIC_URL }}img/angel-left.png" style="vertical-align: middle" />
             <button id="publish-button" type="submit">
                 <span>{% trans "Publish" %}</span></button>
index fc572c2..76001f9 100644 (file)
@@ -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})