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
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
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)
)
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']:
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):
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:
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()
--- /dev/null
+{% 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
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
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)
</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 %}
</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
</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
</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 %}
</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 %}
{{ 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 %}
# 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)),