Merge branch 'redakcja-api'
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 11 Oct 2011 10:33:07 +0000 (12:33 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 11 Oct 2011 10:33:07 +0000 (12:33 +0200)
Conflicts:
apps/api/handlers.py
apps/api/urls.py

apps/api/handlers.py
apps/api/templates/oauth/challenge.html [new file with mode: 0755]
apps/api/urls.py
apps/catalogue/forms.py
wolnelektury/settings.py
wolnelektury/templates/catalogue/book_detail.html
wolnelektury/templates/piston/authorize_token.html [new file with mode: 0755]
wolnelektury/templates/registration/login.html [new file with mode: 0755]
wolnelektury/urls.py

index 685dfaf..9bb8900 100644 (file)
@@ -3,15 +3,17 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
 from datetime import datetime, timedelta
+import json
 
 from django.conf import settings
 from django.contrib.sites.models import Site
 from django.core.urlresolvers import reverse
-from piston.handler import BaseHandler
+from piston.handler import AnonymousBaseHandler, BaseHandler
 from piston.utils import rc
 
 from api.helpers import timestamp
 from api.models import Deleted
+from catalogue.forms import BookImportForm
 from catalogue.models import Book, Tag, BookMedia, Fragment
 
 
@@ -103,7 +105,7 @@ class BookDetailHandler(BaseHandler):
             return rc.NOT_FOUND
 
 
-class BooksHandler(BaseHandler):
+class AnonymousBooksHandler(AnonymousBaseHandler):
     """ Main handler for Book objects.
 
     Responsible for lists of Book objects
@@ -151,6 +153,9 @@ class BooksHandler(BaseHandler):
         else:
             return rc.NOT_FOUND
 
+    def create(self, request, tags, top_level=False):
+        return 'aaa'
+
     @classmethod
     def media(self, book):
         """ Returns all media for a book. """
@@ -158,6 +163,23 @@ class BooksHandler(BaseHandler):
         return book.media.all()
 
 
+class BooksHandler(BaseHandler):
+    model = Book
+    fields = ('slug', 'title')
+    anonymous = AnonymousBooksHandler
+
+    def create(self, request, tags, top_level=False):
+        if not request.user.has_perm('catalogue.add_book'):
+            return rc.FORBIDDEN
+
+        data = json.loads(request.POST.get('data'))
+        form = BookImportForm(data)
+        if form.is_valid():
+            form.save()
+            return rc.CREATED
+        else:
+            return rc.NOT_FOUND
+
 # add categorized tags fields for Book
 def _tags_getter(category):
     @classmethod
diff --git a/apps/api/templates/oauth/challenge.html b/apps/api/templates/oauth/challenge.html
new file mode 100755 (executable)
index 0000000..e69de29
index 89bb600..2d92eba 100644 (file)
@@ -1,15 +1,19 @@
 # -*- coding: utf-8 -*-
 from django.conf.urls.defaults import *
+from piston.authentication import OAuthAuthentication
 from piston.resource import Resource
 
 from api import handlers
 
 
+auth = OAuthAuthentication(realm="Wolne Lektury")
+
 book_changes_resource = Resource(handler=handlers.BookChangesHandler)
 tag_changes_resource = Resource(handler=handlers.TagChangesHandler)
 changes_resource = Resource(handler=handlers.ChangesHandler)
 
-book_list_resource = Resource(handler=handlers.BooksHandler)
+book_list_resource = Resource(handler=handlers.BooksHandler, authentication=auth)
+#book_list_resource = Resource(handler=handlers.BooksHandler)
 book_resource = Resource(handler=handlers.BookDetailHandler)
 
 tag_list_resource = Resource(handler=handlers.TagsHandler)
@@ -19,7 +23,13 @@ fragment_resource = Resource(handler=handlers.FragmentDetailHandler)
 fragment_list_resource = Resource(handler=handlers.FragmentsHandler)
 
 
-urlpatterns = patterns('',
+urlpatterns = patterns(
+    'piston.authentication',
+    url(r'^oauth/request_token/$', 'oauth_request_token'),
+    url(r'^oauth/authorize/$', 'oauth_user_auth'),
+    url(r'^oauth/access_token/$', 'oauth_access_token'),
+
+) + patterns('',
     url(r'^$', 'django.views.generic.simple.direct_to_template',
             {'template': 'api/main.html'}),
 
index fd75196..2bf974d 100644 (file)
@@ -3,6 +3,7 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django import forms
+from django.core.files.base import ContentFile
 from django.utils.translation import ugettext_lazy as _
 from slughifi import slughifi
 
@@ -12,7 +13,17 @@ from catalogue import utils
 
 
 class BookImportForm(forms.Form):
-    book_xml_file = forms.FileField()
+    book_xml_file = forms.FileField(required=False)
+    book_xml = forms.CharField(required=False)
+
+    def clean(self):
+        if not self.cleaned_data['book_xml_file']:
+            if self.cleaned_data['book_xml']:
+                self.cleaned_data['book_xml_file'] = \
+                        ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
+            else:
+                raise forms.ValidationError(_("Please supply an XML."))
+        return super(BookImportForm, self).clean()
 
     def save(self, commit=True, **kwargs):
         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
index 946a621..5cb556b 100644 (file)
@@ -110,7 +110,7 @@ TEMPLATE_DIRS = [
     path.join(PROJECT_DIR, 'templates'),
 ]
 
-LOGIN_URL = '/uzytkownicy/zaloguj/'
+LOGIN_URL = '/uzytkownicy/login/'
 
 LOGIN_REDIRECT_URL = '/'
 
index 7d0adcb..3d0b92d 100644 (file)
                 {% if extra_info.source_url %}
                 <li><a href="{{ extra_info.source_url }}">{% trans "Source of the book" %}</a></li>
                 {% endif %}
+                {% if extra_info.about %}
+                <li><a href="{{ extra_info.about }}">{% trans "Book on the Editor's Platform" %}</a></li>
+                {% endif %}
                 {% if book.gazeta_link %}
                 <li><a href="{{ book.gazeta_link }}">{% trans "Book description on Lektury.Gazeta.pl" %}</a></li>
                 {% endif %}
diff --git a/wolnelektury/templates/piston/authorize_token.html b/wolnelektury/templates/piston/authorize_token.html
new file mode 100755 (executable)
index 0000000..ba28adc
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+  <head>
+    <title>Authorize Token</title>
+  </head>
+  <body>
+    <h1>Authorize Token</h1>
+
+    <form action="{% url piston.authentication.oauth_user_auth %}" method="POST">
+      {{ form.as_table }}
+      <button type="submit">Confirm</button>
+    </form>
+
+  </body>
+</html>
diff --git a/wolnelektury/templates/registration/login.html b/wolnelektury/templates/registration/login.html
new file mode 100755 (executable)
index 0000000..b88d4e1
--- /dev/null
@@ -0,0 +1,5 @@
+<form method="POST" action="">
+    {% csrf_token %}
+    {{ form }}
+<input type="submit" />
+</form>
index aa9588a..f379c9c 100644 (file)
@@ -35,6 +35,7 @@ urlpatterns = patterns('',
     url(r'^uzytkownicy/zaloguj/$', 'catalogue.views.login', name='login'),
     url(r'^uzytkownicy/wyloguj/$', 'catalogue.views.logout_then_redirect', name='logout'),
     url(r'^uzytkownicy/utworz/$', 'catalogue.views.register', name='register'),
+    url(r'^uzytkownicy/login/$', 'django.contrib.auth.views.login', name='simple_login'),
 
     # API
     (r'^api/', include('api.urls')),