publishing + some template changes
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 21 Dec 2011 16:00:40 +0000 (17:00 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 21 Dec 2011 16:06:34 +0000 (17:06 +0100)
13 files changed:
apps/api/handlers.py
apps/api/urls.py
apps/picture/forms.py
apps/picture/models.py
apps/picture/templates/admin/picture/picture/change_list.html [new file with mode: 0755]
apps/picture/views.py
wolnelektury/templates/catalogue/book_detail.html
wolnelektury/templates/catalogue/book_fragments.html
wolnelektury/templates/catalogue/differentiate_tags.html
wolnelektury/templates/catalogue/picture_detail.html
wolnelektury/templates/catalogue/tagged_object_list.html
wolnelektury/templates/main_page.html
wolnelektury/urls.py

index f99dd90..d34806f 100644 (file)
@@ -16,6 +16,7 @@ from api.models import Deleted
 from catalogue.forms import BookImportForm
 from catalogue.models import Book, Tag, BookMedia, Fragment
 from picture.models import Picture
+from picture.forms import PictureImportForm
 
 from stats.utils import piwik_track
 
@@ -589,11 +590,11 @@ class PictureHandler(BaseHandler):
     allowed_methods = ('POST',)
 
     def create(self, request):
-        if not request.user.has_perm('catalogue.add_book'):
+        if not request.user.has_perm('picture.add_picture'):
             return rc.FORBIDDEN
 
         data = json.loads(request.POST.get('data'))
-        form = BookImportForm(data)
+        form = PictureImportForm(data)
         if form.is_valid():
             form.save()
             return rc.CREATED
index 60b2064..8f7bed3 100644 (file)
@@ -57,13 +57,12 @@ urlpatterns = patterns(
     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})books/$', book_list_resource),
     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})parent_books/$', book_list_resource, {"top_level": True}),
 
+    url(r'^pictures/$', picture_resource),
+
     # fragments by book, tags, themes
     # this should be paged
     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),
 
     # tags by category
     url(r'^(?P<category>[a-z0-9-]+)/$', tag_list_resource),
-
-    # picture by slug
-    url(r'^pictures/$', picture_resource)
 )
index ad5096b..9d0c5a5 100644 (file)
@@ -6,9 +6,11 @@ from picture.models import Picture
 class PictureImportForm(forms.Form):
     picture_xml_file = forms.FileField(required=False)
     picture_xml = forms.CharField(required=False)
-    picture_image_file = forms.FileField(required=True)
+    picture_image_file = forms.FileField(required=False)
+    picture_image_data = forms.CharField(required=False)
 
     def clean(self):
+        from base64 import b64decode
         from django.core.files.base import ContentFile
 
         if not self.cleaned_data['picture_xml_file']:
@@ -17,6 +19,15 @@ class PictureImportForm(forms.Form):
                         ContentFile(self.cleaned_data['picture_xml'].encode('utf-8'))
             else:
                 raise forms.ValidationError(_("Please supply an XML."))
+
+        if not self.cleaned_data['picture_image_file']:
+            if self.cleaned_data['picture_image_data']:
+                self.cleaned_data['picture_image_file'] = \
+                        ContentFile(b64decode(
+                                self.cleaned_data['picture_image_data']))
+            else:
+                raise forms.ValidationError(_("Please supply an image."))
+
         return super(PictureImportForm, self).clean()
 
     def save(self, commit=True, **kwargs):
index 1dfd596..f87b2bf 100644 (file)
@@ -80,12 +80,18 @@ class Picture(models.Model):
         from librarian.picture import WLPicture
         close_xml_file = False
 
+        class SimpleImageStore(object):
+            def path(self_, slug, mime_type):
+                """Returns the image file. Ignores slug ad mime_type."""
+                return image_file
+
         if not isinstance(xml_file, File):
             xml_file = File(open(xml_file))
             close_xml_file = True
         try:
             # use librarian to parse meta-data
-            picture_xml = WLPicture.from_file(xml_file)
+            picture_xml = WLPicture.from_file(xml_file,
+                    image_store=SimpleImageStore)
 
             pict, created = Picture.objects.get_or_create(slug=picture_xml.slug)
             if not created and not overwrite:
@@ -112,7 +118,8 @@ class Picture(models.Model):
             else:
                 img = picture_xml.image_file()
 
-            pict.image_file.save(path.basename(picture_xml.image_path), File(img))
+            # FIXME: hardcoded extension
+            picture.image_file.save("%s.jpg" % picture.slug, File(img))
 
             pict.xml_file.save("%s.xml" % pict.slug, File(xml_file))
             pict.save()
diff --git a/apps/picture/templates/admin/picture/picture/change_list.html b/apps/picture/templates/admin/picture/picture/change_list.html
new file mode 100755 (executable)
index 0000000..e150da5
--- /dev/null
@@ -0,0 +1,11 @@
+{% extends "admin/change_list.html" %}
+{% load i18n %}
+
+{% block content %}
+    <form action="{% url import_picture %}" method="post" enctype="multipart/form-data">
+        <p>XML: <input type="file" id="id_picture_xml_file" name="picture_xml_file" /><br/>
+            {% trans "Image" %}: <input type="file" id="id_picture_image_file" name="picture_image_file" /><br/>
+            <input type="submit" value="{% trans "Import picture" %}"/></p>
+    </form>
+    {{ block.super }}
+{% endblock content %}
\ No newline at end of file
index c5be3be..24457e2 100644 (file)
@@ -1,4 +1,5 @@
 from picture.models import Picture
+from django.contrib.auth.decorators import permission_required
 from django.utils.datastructures import SortedDict
 from django.shortcuts import render_to_response, get_object_or_404
 from django.template import RequestContext
@@ -29,3 +30,29 @@ def picture_detail(request, picture):
 
     return render_to_response("catalogue/picture_detail.html", locals(),
                               context_instance=RequestContext(request))
+
+# =========
+# = Admin =
+# =========
+@permission_required('picture.add_picture')
+def import_picture(request):
+    """docstring for import_book"""
+    from django.http import HttpResponse
+    from picture.forms import PictureImportForm
+    from django.utils.translation import ugettext as _
+
+    import_form = PictureImportForm(request.POST, request.FILES)
+    if import_form.is_valid():
+        try:
+            import_form.save()
+        except:
+            import sys
+            import pprint
+            import traceback
+            info = sys.exc_info()
+            exception = pprint.pformat(info[1])
+            tb = '\n'.join(traceback.format_tb(info[2]))
+            return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
+        return HttpResponse(_("Picture imported successfully"))
+    else:
+        return HttpResponse(_("Error importing file: %r") % import_form.errors)
index 5c0a047..4b4ee2f 100644 (file)
         </div>
         <div class="clearboth"></div>
     </div>
-    <div id="set-window">
-        <div class="header"><a href="#" class="jqmClose">{% trans "Close" %}</a></div>
-        <div class="target">
-            <p><img src="{{ STATIC_URL }}img/indicator.gif" alt="*"/> {% trans "Loading" %}</p>
-        </div>
-    </div>
 {% endblock %}
index 6f5f8c8..3ec3ad2 100644 (file)
         </div>
         <div class="clearboth"></div>
     </div>
-    <div id="set-window">
-        <div class="header"><a href="#" class="jqmClose">{% trans "Close" %}</a></div>
-        <div class="target">
-            <p><img src="{{ STATIC_URL }}img/indicator.gif" alt="*"/> {% trans "Loading" %}</p>
-        </div>
-    </div>
 {% endblock %}
\ No newline at end of file
index b40d3fe..f3bddd3 100644 (file)
                </div>
         {% endfor %}
     </div>
-
-    <div id="set-window">
-        <div class="header"><a href="#" class="jqmClose">{% trans "Close" %}</a></div>
-        <div class="target">
-            <p><img src="{{ STATIC_URL }}img/indicator.gif" alt="*"/> {% trans "Loading" %}</p>
-        </div>
-    </div>
 {% endblock %}
\ No newline at end of file
index b059709..b8b70c6 100644 (file)
         </div>
         <div class="clearboth"></div>
     </div>
-    <div id="set-window">
-        <div class="header"><a href="#" class="jqmClose">{% trans "Close" %}</a></div>
-        <div class="target">
-            <p><img src="{{ STATIC_URL }}img/indicator.gif" alt="*"/> {% trans "Loading" %}</p>
-        </div>
-    </div>
 {% endblock %}
index 7cae9eb..1744c33 100644 (file)
     </div>
        {% endif %}
     {% endif %}
-    <div id="set-window">
-        <div class="header"><a href="#" class="jqmClose">{% trans "Close" %}</a></div>
-        <div class="target">
-            <p><img src="{{ STATIC_URL }}img/indicator.gif" alt="*"/> {% trans "Loading" %}</p>
-        </div>
-    </div>
 {% endblock %}
index ddb90fa..3e0692a 100755 (executable)
@@ -37,6 +37,8 @@
             {{ book.mini_box }}
         {% endfor %}
 
+    <div class="clearboth"></div>
+
     <div class="infopages-box">
         <h2 class="grid-line"><span class='mono'>Aktualnoƛci</span></h2>
         {% cache 1800 latest-blog-posts %}
index 05d8e23..4ec1b37 100644 (file)
@@ -32,6 +32,7 @@ urlpatterns += patterns('',
 
     # Admin panel
     url(r'^admin/catalogue/book/import$', 'catalogue.views.import_book', name='import_book'),
+    url(r'^admin/catalogue/picture/import$', 'picture.views.import_picture', name='import_picture'),
     url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
     url(r'^admin/', include(admin.site.urls)),