From f715832d8af615462b881bff83a37237bf44ca22 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 12 Sep 2011 05:30:51 +0200 Subject: [PATCH 1/1] api info page, some api fixes --- apps/api/handlers.py | 26 ++++- apps/api/locale/pl/LC_MESSAGES/django.mo | Bin 0 -> 3418 bytes apps/api/locale/pl/LC_MESSAGES/django.po | 125 +++++++++++++++++++++++ apps/api/templates/api/main.html | 82 +++++++++++++++ apps/api/tests.py | 3 + apps/api/urls.py | 4 + 6 files changed, 235 insertions(+), 5 deletions(-) create mode 100644 apps/api/locale/pl/LC_MESSAGES/django.mo create mode 100644 apps/api/locale/pl/LC_MESSAGES/django.po create mode 100755 apps/api/templates/api/main.html diff --git a/apps/api/handlers.py b/apps/api/handlers.py index 32a3ce300..73ca07c58 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -135,11 +135,17 @@ class BooksHandler(BaseHandler): tags = read_tags(tags, allowed=self.categories) if tags: if top_level: - return Book.tagged_top_level(tags) + books = Book.tagged_top_level(tags) + return books if books else rc.NOT_FOUND else: - return Book.tagged.with_all(tags) + books = Book.tagged.with_all(tags) else: - return Book.objects.all() + books = Book.objects.all() + + if books.exists(): + return books + else: + return rc.NOT_FOUND # add categorized tags fields for Book @@ -201,7 +207,13 @@ class TagsHandler(BaseHandler): except KeyError, e: return rc.NOT_FOUND - return Tag.objects.filter(category=category_sng) + tags = Tag.objects.filter(category=category_sng) + tags = [t for t in tags if t.get_count() > 0] + if tags: + return tags + else: + return rc.NOT_FOUND + @classmethod def href(cls, tag): @@ -240,7 +252,11 @@ class FragmentsHandler(BaseHandler): """ tags = read_tags(tags, allowed=self.categories) - return Fragment.tagged.with_all(tags).select_related('book') + fragmets = Fragment.tagged.with_all(tags).select_related('book') + if fragments.exists(): + return fragments + else: + return rc.NOT_FOUND @classmethod def href(cls, fragment): diff --git a/apps/api/locale/pl/LC_MESSAGES/django.mo b/apps/api/locale/pl/LC_MESSAGES/django.mo new file mode 100644 index 0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QJrqIvu>|}Od z^y$9Um-p6hFGKnT)xHs`T9JL%VHAQG$nVlM+5 zBTp&k(d>RUq_WsklJ$Yz7WvspzVHI_nP#!b9WC=FRCGU0_A+X^ERqaN$B5$C%LJbS zD^;=Z%YVc`fJ-s}@hTN2^K!t$97yJ+u~gAl^gzcVKKF9B!R2xb-HoGcjQkYm?|NZ+ zD*5Dd_QfADWT4l_M1@H8xdy!Rpss3k, YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2011-09-12 05:20+0200\n" +"PO-Revision-Date: 2011-09-12 05:22+0100\n" +"Last-Translator: Radek Czajka \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" + +#: templates/api/main.html:4 +msgid "WolneLektury.pl API" +msgstr "API serwisu WolneLektury.pl" + +#: templates/api/main.html:13 +msgid "" +"\n" +"WolneLektury.pl API resides under http://www.wolnelektury.pl/api/.\n" +"You can use it to access information about books, their fragments and\n" +"their metadata.\n" +msgstr "" +"\n" +"API serwisu WolneLektury.pl znajduje się pod adresem http://www.wolnelektury.pl/api/.\n" +"Za jego pomocą można uzyskać informacje o utworach, ich fragmentach i metadanych.\n" + +#: templates/api/main.html:21 +msgid "" +"\n" +"Default data serialization format is\n" +"JSON,\n" +"but you can also use XML by appending ?format=xml\n" +"query parameter to each URL.\n" +msgstr "" +"\n" +"Dane domyślnie są serializowane w formacie JSON,ale dostępny jest też format XML – wystarczy dodać parametr ?format=xml\n" +"do każdego zapytania.\n" + +#: templates/api/main.html:30 +msgid "" +"\n" +"The URLs in WolneLektury.pl API are:\n" +msgstr "" +"\n" +"API Wolnych Lektur zawiera następujące adresy URL:\n" + +#: templates/api/main.html:36 +msgid "List of all books" +msgstr "Lista utworów" + +#: templates/api/main.html:38 +msgid "List of all authors" +msgstr "Lista autorów" + +#: templates/api/main.html:39 +msgid "List of all epochs" +msgstr "Lista epok" + +#: templates/api/main.html:40 +msgid "List of all genres" +msgstr "Lista gatunków literackich" + +#: templates/api/main.html:41 +msgid "List of all kinds" +msgstr "Lista rodzajów literackich" + +#: templates/api/main.html:43 +msgid "List of all themes" +msgstr "Lista motywów i tematów literackich" + +#: templates/api/main.html:49 +#, python-format +msgid "" +"\n" +"Each element of those lists contains a link (in a \"href\") attibute\n" +"which points to individual resource's details, i.e.:\n" +"%(e1)s or\n" +"%(e2)s.\n" +msgstr "" +"\n" +"Każdy element na tych listach zawiera adres (w atrybucie „href”), pod którym można znaleźć szczegółowe dane, np. %(e1)s czy %(e2)s.\n" + +#: templates/api/main.html:58 +#, python-format +msgid "" +"\n" +"You can combine authors, epochs, genres and kinds to find only books matching\n" +"those criteria. For instance:\n" +"%(e)s.\n" +msgstr "" +"\n" +"Można łączyć autorów, epoki, gatunki i rodzaje, aby wybrać tylko utwory odpowiadające zadanym kryteriom. Na przykład: %(e)s.\n" + +#: templates/api/main.html:66 +#, python-format +msgid "" +"\n" +"If you only want top-level books and not all the children, you can use /parent_books/, as in:\n" +"%(e)s.\n" +msgstr "" +"\n" +"Aby spośród wszystkich pasujących wybrać tylko utwory najwyższego poziomu (pomijając ich podutwory), można użyć zapytania /parent_books/, np.: %(e)s.\n" + +#: templates/api/main.html:74 +#, python-format +msgid "" +"\n" +"The same way, using also books and themes, you can search for a list of fragments:\n" +"%(e)s. \n" +"Again, each entry has a \"href\" attribute which links to the fragment's details, i.e.:\n" +"%(f)s. \n" +msgstr "" +"\n" +"W ten sam sposób, filtrując dodatkowo według lektur lub motywów, można wyszukiwać fragmenty:%(e)s. \n" +"Każdy element uzyskanej listy w atrybucie „href” zawiera link do szczegółowego opisu danego fragmentu, np.:\n" +"%(f)s. \n" + diff --git a/apps/api/templates/api/main.html b/apps/api/templates/api/main.html new file mode 100755 index 000000000..ada509342 --- /dev/null +++ b/apps/api/templates/api/main.html @@ -0,0 +1,82 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block title %}{% trans "WolneLektury.pl API" %}{% endblock %} + +{% block bodyid %}api{% endblock %} + +{% block body %} + +

API

+ +

+{% blocktrans %} +WolneLektury.pl API resides under http://www.wolnelektury.pl/api/. +You can use it to access information about books, their fragments and +their metadata. +{% endblocktrans %} +

+ +

+{% blocktrans %} +Default data serialization format is +JSON, +but you can also use XML by appending ?format=xml +query parameter to each URL. +{% endblocktrans %} +

+ +

+{% blocktrans %} +The URLs in WolneLektury.pl API are: +{% endblocktrans %} +

+ + + +

+{% url api_book "studnia-i-wahadlo" as e1 %} +{% url api_tag "authors" "edgar-allan-poe" as e2 %} +{% blocktrans %} +Each element of those lists contains a link (in a "href") attibute +which points to individual resource's details, i.e.: +{{e1}} or +{{e2}}. +{% endblocktrans %} +

+ +

+{% blocktrans with "/api/authors/adam-mickiewicz/kinds/liryka/books/" as e %} +You can combine authors, epochs, genres and kinds to find only books matching +those criteria. For instance: +{{e}}. +{% endblocktrans %} +

+ +

+{% blocktrans with "/api/authors/adam-mickiewicz/kinds/liryka/parent_books/" as e %} +If you only want top-level books and not all the children, you can use /parent_books/, as in: +{{e}}. +{% endblocktrans %} +

+ +

+{% url api_fragment "sen-nocy-letniej" "1290526312912-3814598192" as f %} +{% blocktrans with "/api/authors/william-shakespeare/themes/zabawa/fragments/" as e %} +The same way, using also books and themes, you can search for a list of fragments: +{{e}}. +Again, each entry has a "href" attribute which links to the fragment's details, i.e.: +{{f}}. +{% endblocktrans %} +

+ +{% endblock %} diff --git a/apps/api/tests.py b/apps/api/tests.py index 12c71260b..2c2e51ce8 100644 --- a/apps/api/tests.py +++ b/apps/api/tests.py @@ -122,6 +122,9 @@ class TagTests(TestCase): def setUp(self): self.tag = Tag.objects.create(category='author', slug='joe', name='Joe') + self.book = Book.objects.create(title='A Book', slug='a-book') + self.book.tags = [self.tag] + self.book.save() def test_tag_list(self): tags = json.loads(self.client.get('/api/authors/').content) diff --git a/apps/api/urls.py b/apps/api/urls.py index ec2c2e74a..89bb6001c 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -20,6 +20,10 @@ fragment_list_resource = Resource(handler=handlers.FragmentsHandler) urlpatterns = patterns('', + url(r'^$', 'django.views.generic.simple.direct_to_template', + {'template': 'api/main.html'}), + + # changes handlers url(r'^book_changes/(?P\d*?)\.(?Pxml|json|yaml)$', book_changes_resource), url(r'^tag_changes/(?P\d*?)\.(?Pxml|json|yaml)$', tag_changes_resource), -- 2.20.1