From: Radek Czajka Date: Fri, 15 Jun 2012 15:05:19 +0000 (+0200) Subject: cover image repo X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/05f67adb27390c7a9cf214e83550d3bbca88c8f2?hp=9ce71c396eabe3142c5f587c1d590c2b3352d480 cover image repo --- diff --git a/apps/catalogue/constants.py b/apps/catalogue/constants.py index d75d6b4b..0c842324 100644 --- a/apps/catalogue/constants.py +++ b/apps/catalogue/constants.py @@ -1,5 +1,8 @@ # -*- coding: utf-8 -*- - +# +# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# TRIM_BEGIN = " TRIM_BEGIN " TRIM_END = " TRIM_END " diff --git a/apps/catalogue/templatetags/catalogue.py b/apps/catalogue/templatetags/catalogue.py index 3cc7210c..8d5ff65b 100644 --- a/apps/catalogue/templatetags/catalogue.py +++ b/apps/catalogue/templatetags/catalogue.py @@ -34,6 +34,8 @@ def main_tabs(context): tabs.append(Tab('create', _('Add'), reverse("catalogue_create_missing"))) tabs.append(Tab('upload', _('Upload'), reverse("catalogue_upload"))) + tabs.append(Tab('cover', _('Covers'), reverse("cover_image_list"))) + return {"tabs": tabs, "active_tab": active} diff --git a/apps/catalogue/templatetags/common_tags.py b/apps/catalogue/templatetags/common_tags.py new file mode 100755 index 00000000..ccaf03bf --- /dev/null +++ b/apps/catalogue/templatetags/common_tags.py @@ -0,0 +1,7 @@ +from django import template +register = template.Library() + + +@register.filter +def build_absolute_uri(uri, request): + return request.build_absolute_uri(uri) diff --git a/apps/cover/forms.py b/apps/cover/forms.py new file mode 100755 index 00000000..754d697c --- /dev/null +++ b/apps/cover/forms.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +# +# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +import re +from urllib2 import urlopen +from django import forms +from cover.models import Image + +class ImageAddForm(forms.ModelForm): + class Meta: + model = Image + +class ImageEditForm(forms.ModelForm): + """Form used for editing a Book.""" + class Meta: + model = Image + exclude = ['download_url'] + + +class ReadonlyImageEditForm(ImageEditForm): + """Form used for not editing a Book.""" + + def __init__(self, *args, **kwargs): + ret = super(ReadonlyImageEditForm, self).__init__(*args, **kwargs) + for field in self.fields.values(): + field.widget.attrs.update({"readonly": True}) + return ret + + def save(self, *args, **kwargs): + raise AssertionError, "ReadonlyImageEditForm should not be saved." + + +class FlickrForm(forms.Form): + source_url = forms.URLField() + + def clean_source_url(self): + url = self.cleaned_data['source_url'] + m = re.match(r'(https?://)?(www.)?flickr.com/photos/(?P[^/]+)/(?P\d+)', url) + if not m: + raise forms.ValidationError("It doesn't look like Flickr URL.") + author_slug, img_id = m.group('author'), m.group('img') + base_url = "https://www.flickr.com/photos/%s/%s/" % (author_slug, img_id) + + try: + html = urlopen(url).read().decode('utf-8') + except: + raise forms.ValidationError('Error reading page.') + match = re.search(r'Some rights reserved', html) + try: + assert match + license_url = match.group(1) + self.cleaned_data['license_url'] = license_url + re_license = re.compile(r'http://creativecommons.org/licenses/([^/]*)/([^/]*)/.*') + m = re_license.match(license_url) + assert m + self.cleaned_data['license_name'] = 'CC %s %s' % (m.group(1).upper(), m.group(2)) + except AssertionError: + raise forms.ValidationError('Error reading license name.') + + m = re.search(r'By ([^<]*)', html) + if m: + self.cleaned_data['author'] = "%s@Flickr" % m.group(1) + else: + raise forms.ValidationError('Error reading author name.') + + m = re.search(r']*>(.*?)', html) + if not m: + raise forms.ValidationError('Error reading image title.') + self.cleaned_data['title'] = m.group(1) + + url_size = base_url + "sizes/o/" + html = urlopen(url_size).read().decode('utf-8') + m = re.search(r'
\s*{% trans "Add image" %} + +
+{% csrf_token %} + +{{ ff.as_p }} + +
+ + +
+{% csrf_token %} +{{ form.as_p }} + +
+{% endblock %} diff --git a/apps/cover/templates/cover/image_detail.html b/apps/cover/templates/cover/image_detail.html new file mode 100755 index 00000000..8ebf988e --- /dev/null +++ b/apps/cover/templates/cover/image_detail.html @@ -0,0 +1,29 @@ +{% extends "catalogue/base.html" %} +{% load i18n %} +{% load build_absolute_uri from common_tags %} + +{% block content %} +

{% trans "Cover image" %}

+ +
+ +
{{ object.title }} by {{ object.author }}, + {% if object.license_url %}{% endif %} + {{ object.license_name }} + {% if object.license_url %}{% endif %} + +
{% trans "source" %}: {{ object.download_url }} +
+ +
+{% csrf_token %} +{{ form.as_p }} + +
+ + +{% endblock %} diff --git a/apps/cover/templates/cover/image_list.html b/apps/cover/templates/cover/image_list.html new file mode 100755 index 00000000..5b2b0006 --- /dev/null +++ b/apps/cover/templates/cover/image_list.html @@ -0,0 +1,30 @@ +{% extends "catalogue/base.html" %} +{% load i18n %} +{% load url from future %} +{% load thumbnail pagination_tags %} + + +{% block content %} +

{% trans "Cover images" %}

+ +{% if can_add %} + {% trans "Add new" %} +{% endif %} + +
    +{% autopaginate object_list 100 %} +{% for image in object_list %} + + + + {{ image }} +{% endfor %} +{% paginate %} +
+ +{% endblock %} diff --git a/apps/cover/tests.py b/apps/cover/tests.py new file mode 100644 index 00000000..d6a1ad7d --- /dev/null +++ b/apps/cover/tests.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# +# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from nose.tools import * +from django.test import TestCase +from cover.forms import FlickrForm + + +class FlickrTests(TestCase): + def test_flickr(self): + form = FlickrForm({"source_url": "https://www.flickr.com/photos/rczajka/6941928577/in/photostream"}) + self.assertTrue(form.is_valid()) + self.assertEqual(form.cleaned_data['source_url'], "https://www.flickr.com/photos/rczajka/6941928577/") + self.assertEqual(form.cleaned_data['author'], "rczajka@Flickr") + self.assertEqual(form.cleaned_data['title'], u"Pirate Stańczyk") + self.assertEqual(form.cleaned_data['license_name'], "CC BY 2.0") + self.assertEqual(form.cleaned_data['license_url'], "http://creativecommons.org/licenses/by/2.0/deed.en") + self.assertEqual(form.cleaned_data['download_url'], "http://farm8.staticflickr.com/7069/6941928577_2a0306f6a6_b.jpg") diff --git a/apps/cover/urls.py b/apps/cover/urls.py index 5e6ffc54..2337d47a 100644 --- a/apps/cover/urls.py +++ b/apps/cover/urls.py @@ -1,4 +1,8 @@ -# -*- coding: utf-8 +# -*- coding: utf-8 -*- +# +# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# from django.conf.urls.defaults import patterns, url @@ -10,5 +14,7 @@ urlpatterns = patterns('cover.views', url(r'^preview/(?P[^/]+)/(?P[^/]+)/(?P\d+)/$', 'preview', name='cover_preview'), - url(r'^flickr/$', 'flickr'), + url(r'^image/$', 'image_list', name='cover_image_list'), + url(r'^image/(?P\d+)/?$', 'image', name='cover_image'), + url(r'^add_image/$', 'add_image', name='cover_add_image'), ) diff --git a/apps/cover/views.py b/apps/cover/views.py index 0f341ad7..0c29209c 100644 --- a/apps/cover/views.py +++ b/apps/cover/views.py @@ -1,11 +1,19 @@ -# Create your views here. +# -*- coding: utf-8 -*- +# +# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# import os.path from django.conf import settings +from django.contrib.auth.decorators import permission_required from django.http import HttpResponse, HttpResponseRedirect, Http404 -from catalogue.models import Chunk -from django.views.decorators.http import require_POST +from django.shortcuts import get_object_or_404, render from django.views.decorators.csrf import csrf_exempt -from django.shortcuts import render +from django.views.decorators.http import require_POST +from catalogue.helpers import active_tab +from catalogue.models import Chunk +from cover.models import Image +from cover import forms PREVIEW_SIZE = (216, 300) @@ -73,36 +81,59 @@ def preview_from_xml(request): return HttpResponse(os.path.join(settings.MEDIA_URL, fname)) -def flickr(request): - url = request.POST.get('flickr_url') - if url: - import re - from urllib2 import urlopen +@active_tab('cover') +def image(request, pk): + image = get_object_or_404(Image, pk=pk) - html = urlopen(url).read() - match = re.search(r'Some rights reserved', html) - try: - assert match - license_url = match.group(1) - re_license = re.compile(r'http://creativecommons.org/licenses/([^/]*)/([^/]*)/.*') - m = re_license.match(license_url) - assert m - license_name = 'CC %s %s' % (m.group(1).upper(), m.group(2)) - except AssertionError: - license_name = 'NIEZNANA LICENCJA' - - m = re.search(r'By ([^<]*)', html) - if m: - author = m.group(1) + if request.user.has_perm('cover.change_image'): + if request.method == "POST": + form = forms.ImageEditForm(request.POST, instance=image) + if form.is_valid(): + form.save() + return HttpResponseRedirect(image.get_absolute_url()) else: - author = "NIEZNANY AUTOR" - - url_size = url.rstrip('/') + '/sizes/o/' - html = urlopen(url_size).read() - m = re.search(r'
\s*=1.3,<1.4 -sorl-thumbnail>=3.2 +sorl-thumbnail>=11.09,<12 django-maintenancemode>=0.9 django-pagination django-gravatar