From: Radek Czajka Date: Wed, 21 Dec 2011 16:00:40 +0000 (+0100) Subject: publishing + some template changes X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/e9327dee290c524806c89e1c3cb546f95ee51e52?ds=sidebyside;hp=--cc publishing + some template changes --- e9327dee290c524806c89e1c3cb546f95ee51e52 diff --git a/apps/api/handlers.py b/apps/api/handlers.py index f99dd90ab..d34806f0a 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -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 diff --git a/apps/api/urls.py b/apps/api/urls.py index 60b20647c..8f7bed3fe 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -57,13 +57,12 @@ urlpatterns = patterns( url(r'^(?P(?:(?:[a-z0-9-]+/){2}){0,6})books/$', book_list_resource), url(r'^(?P(?:(?:[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(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource), # tags by category url(r'^(?P[a-z0-9-]+)/$', tag_list_resource), - - # picture by slug - url(r'^pictures/$', picture_resource) ) diff --git a/apps/picture/forms.py b/apps/picture/forms.py index ad5096bf5..9d0c5a54b 100644 --- a/apps/picture/forms.py +++ b/apps/picture/forms.py @@ -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): diff --git a/apps/picture/models.py b/apps/picture/models.py index 1dfd59618..f87b2bf90 100644 --- a/apps/picture/models.py +++ b/apps/picture/models.py @@ -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 index 000000000..e150da51e --- /dev/null +++ b/apps/picture/templates/admin/picture/picture/change_list.html @@ -0,0 +1,11 @@ +{% extends "admin/change_list.html" %} +{% load i18n %} + +{% block content %} +
+

XML:
+ {% trans "Image" %}:
+

+
+ {{ block.super }} +{% endblock content %} \ No newline at end of file diff --git a/apps/picture/views.py b/apps/picture/views.py index c5be3bea3..24457e297 100644 --- a/apps/picture/views.py +++ b/apps/picture/views.py @@ -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) diff --git a/wolnelektury/templates/catalogue/book_detail.html b/wolnelektury/templates/catalogue/book_detail.html index 5c0a047f4..4b4ee2f18 100644 --- a/wolnelektury/templates/catalogue/book_detail.html +++ b/wolnelektury/templates/catalogue/book_detail.html @@ -211,10 +211,4 @@
-
- -
-

* {% trans "Loading" %}

-
-
{% endblock %} diff --git a/wolnelektury/templates/catalogue/book_fragments.html b/wolnelektury/templates/catalogue/book_fragments.html index 6f5f8c86d..3ec3ad284 100644 --- a/wolnelektury/templates/catalogue/book_fragments.html +++ b/wolnelektury/templates/catalogue/book_fragments.html @@ -26,10 +26,4 @@
-
- -
-

* {% trans "Loading" %}

-
-
{% endblock %} \ No newline at end of file diff --git a/wolnelektury/templates/catalogue/differentiate_tags.html b/wolnelektury/templates/catalogue/differentiate_tags.html index b40d3fe97..f3bddd397 100644 --- a/wolnelektury/templates/catalogue/differentiate_tags.html +++ b/wolnelektury/templates/catalogue/differentiate_tags.html @@ -17,11 +17,4 @@ {% endfor %} - -
- -
-

* {% trans "Loading" %}

-
-
{% endblock %} \ No newline at end of file diff --git a/wolnelektury/templates/catalogue/picture_detail.html b/wolnelektury/templates/catalogue/picture_detail.html index b0597097f..b8b70c6be 100644 --- a/wolnelektury/templates/catalogue/picture_detail.html +++ b/wolnelektury/templates/catalogue/picture_detail.html @@ -91,10 +91,4 @@
-
- -
-

* {% trans "Loading" %}

-
-
{% endblock %} diff --git a/wolnelektury/templates/catalogue/tagged_object_list.html b/wolnelektury/templates/catalogue/tagged_object_list.html index 7cae9ebed..1744c3361 100644 --- a/wolnelektury/templates/catalogue/tagged_object_list.html +++ b/wolnelektury/templates/catalogue/tagged_object_list.html @@ -140,10 +140,4 @@ {% endif %} {% endif %} -
- -
-

* {% trans "Loading" %}

-
-
{% endblock %} diff --git a/wolnelektury/templates/main_page.html b/wolnelektury/templates/main_page.html index ddb90facb..3e0692af1 100755 --- a/wolnelektury/templates/main_page.html +++ b/wolnelektury/templates/main_page.html @@ -37,6 +37,8 @@ {{ book.mini_box }} {% endfor %} +
+

Aktualności

{% cache 1800 latest-blog-posts %} diff --git a/wolnelektury/urls.py b/wolnelektury/urls.py index 05d8e238f..4ec1b3713 100644 --- a/wolnelektury/urls.py +++ b/wolnelektury/urls.py @@ -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)),