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:
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)
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':
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/')
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/'
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'])
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.")
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
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)
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.
"""
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):
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>
+# -*- coding: utf-8 -*-
from collections import defaultdict
from datetime import datetime, date, timedelta
import logging
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)
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()
"publishable": publishable,
"publishable_error": publish_error,
"form": form,
+ "publish_options_form": publish_options_form,
"editable": editable,
})
@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:
@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})