From 75349c5b71dfbc94bc28319c86794b56da36a08b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20St=C4=99pniowski?= Date: Sat, 30 Jan 2010 23:00:34 +0100 Subject: [PATCH] =?utf8?q?Dodanie=20mo=C5=BCliwo=C5=9Bci=20wyboru=20format?= =?utf8?q?=C3=B3w=20przy=20=C5=9Bci=C4=85ganiu=20p=C3=B3=C5=82ki.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- apps/catalogue/forms.py | 16 ++++ apps/catalogue/urls.py | 1 + apps/catalogue/views.py | 75 +++++++++++++++---- wolnelektury/static/css/master.css | 61 ++++++++++++++- wolnelektury/static/js/catalogue.js | 38 ++++++++++ .../catalogue/tagged_object_list.html | 12 +++ 6 files changed, 187 insertions(+), 16 deletions(-) diff --git a/apps/catalogue/forms.py b/apps/catalogue/forms.py index 48faeb15b..bf6b0df35 100644 --- a/apps/catalogue/forms.py +++ b/apps/catalogue/forms.py @@ -60,3 +60,19 @@ class NewSetForm(forms.Form): new_set.save() return new_set + +FORMATS = ( + ('mp3', 'MP3'), + ('ogg', 'OGG'), + ('pdf', 'PDF'), + ('odt', 'ODT'), + ('txt', 'TXT'), +) + + +class DownloadFormatsForm(forms.Form): + formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple) + + def __init__(self, *args, **kwargs): + super(DownloadFormatsForm, self).__init__(*args, **kwargs) + diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index dc8cc9bdf..327e0fc72 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -4,6 +4,7 @@ from django.conf.urls.defaults import * urlpatterns = patterns('catalogue.views', url(r'^$', 'main_page', name='main_page'), + url(r'^polki/(?P[a-zA-Z0-9-]+)/formaty/$', 'shelf_book_formats', name='shelf_book_formats'), url(r'^polki/(?P[a-zA-Z0-9-]+)/(?P[a-zA-Z0-9-0-]+)/usun$', 'remove_from_shelf', name='remove_from_shelf'), url(r'^polki/$', 'user_shelves', name='user_shelves'), url(r'^polki/(?P[a-zA-Z0-9-]+)/usun/$', 'delete_shelf', name='delete_shelf'), diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index cc7669b06..92aa3e054 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -98,7 +98,12 @@ def tagged_object_list(request, tags=''): queryset_or_model=model, tags=tags, template_name='catalogue/tagged_object_list.html', - extra_context = {'categories': categories, 'shelf_is_set': shelf_is_set, 'user_is_owner': user_is_owner }, + extra_context = { + 'categories': categories, + 'shelf_is_set': shelf_is_set, + 'user_is_owner': user_is_owner, + 'formats_form': forms.DownloadFormatsForm(), + }, ) @@ -251,6 +256,19 @@ def remove_from_shelf(request, shelf, book): return HttpResponse('Usunieto') +def collect_books(books): + """ + Returns all real books in collection. + """ + result = [] + for book in books: + if len(book.children.all()) == 0: + result.append(book) + else: + result += collect_books(book.children.all()) + return result + + @cache.never_cache def download_shelf(request, slug): """" @@ -259,31 +277,34 @@ def download_shelf(request, slug): be used for large dynamic PDF files. """ shelf = get_object_or_404(models.Tag, slug=slug, category='set') - + + formats = [] + form = forms.DownloadFormatsForm(request.GET) + if form.is_valid(): + formats = form.cleaned_data['formats'] + if len(formats) == 0: + formats = ['pdf', 'odt', 'txt', 'mp3', 'ogg'] + # Create a ZIP archive temp = temp = tempfile.TemporaryFile() archive = zipfile.ZipFile(temp, 'w') - # Collect all books to include in ZIP archive - def collect_books(books): - result = [] - for book in books: - if len(book.children.all()) == 0: - result.append(book) - else: - result += collect_books(book.children.all()) - return result - for book in collect_books(models.Book.tagged.with_all(shelf)): - if book.pdf_file: + if 'pdf' in formats and book.pdf_file: filename = book.pdf_file.path archive.write(filename, str('%s.pdf' % book.slug)) - if book.odt_file: + if 'odt' in formats and book.odt_file: filename = book.odt_file.path archive.write(filename, str('%s.odt' % book.slug)) - if book.txt_file: + if 'txt' in formats and book.txt_file: filename = book.txt_file.path archive.write(filename, str('%s.txt' % book.slug)) + if 'mp3' in formats and book.mp3_file: + filename = book.mp3_file.path + archive.write(filename, str('%s.mp3' % book.slug)) + if 'ogg' in formats and book.ogg_file: + filename = book.ogg_file.path + archive.write(filename, str('%s.ogg' % book.slug)) archive.close() response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed') @@ -295,6 +316,30 @@ def download_shelf(request, slug): return response +@cache.never_cache +def shelf_book_formats(request, shelf): + """" + Returns a list of formats of books in shelf. + """ + shelf = get_object_or_404(models.Tag, slug=shelf, category='set') + + formats = {'pdf': False, 'odt': False, 'txt': False, 'mp3': False, 'ogg': False} + + for book in collect_books(models.Book.tagged.with_all(shelf)): + if book.pdf_file: + formats['pdf'] = True + if book.odt_file: + formats['odt'] = True + if book.txt_file: + formats['txt'] = True + if book.mp3_file: + formats['mp3'] = True + if book.ogg_file: + formats['ogg'] = True + + return HttpResponse(LazyEncoder().encode(formats)) + + @login_required @require_POST @cache.never_cache diff --git a/wolnelektury/static/css/master.css b/wolnelektury/static/css/master.css index ec5c76552..ba6e0aaaf 100644 --- a/wolnelektury/static/css/master.css +++ b/wolnelektury/static/css/master.css @@ -397,7 +397,66 @@ div.shown-tags p, div.all-tags p { -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; - text-align: center; + text-align: center; + outline: none; +} + +#download-shelf-menu { + -moz-border-radius: 4px; + -webkit-border-radius: 4px; + border-radius: 4px; + border: 3px solid #EEE; + padding: 5px; + margin-top: -5px; +} + +#download-formats-form li { + float: left; + height: 2em; +} + +#download-formats-form input { + float: left; +} + +#download-formats-form label { + display: block; + background-color: #EEE; + float: left; + width: 8em; + margin-right: 0.5em; + margin-top: -0.5em; + padding-top: 0.5em; + padding-bottom: 0.75em; +} + +#download-formats-form li { + margin: 0; + padding: 0; + list-style: none; +} + +#download-formats-form em { + color: #999; + background-color: #FFF; +} + +#download-formats-form em strong { + color: #000; + font-weight: normal; +} + +#download-formats-form-submit { + margin-left: 0.5em; + +} + +#download-formats-form #download-formats-form-submit-li { + margin-top: 0.75em; +} + +#download-formats-form-cancel { + color: #900; } /* ============================ */ diff --git a/wolnelektury/static/js/catalogue.js b/wolnelektury/static/js/catalogue.js index fa5357554..576f6c02a 100644 --- a/wolnelektury/static/js/catalogue.js +++ b/wolnelektury/static/js/catalogue.js @@ -246,5 +246,43 @@ function changeBannerText() { $('#user-info').show(); changeBannerText(); $('#onepercent-banner').slideDown('slow'); + + var formatsDownloaded = false; + $('#download-shelf').click(function() { + $('#download-shelf-menu').slideDown('fast'); + + if (!formatsDownloaded) { + // Pobierz dane o formatach + formatsDownloaded = true; + $.ajax({ + url: $('#download-formats-form').attr('data-formats-feed'), + type: 'GET', + dataType: 'json', + complete: function() { + $('#download-formats-form-submit').attr('disabled', null); + $('#download-formats-form-submit-li img').remove(); + $('#updating-formats').fadeOut('fast', function() { + $('#formats-updated').fadeIn('fast'); + }); + }, + success: function(data) { + $('#download-formats-form li').each(function() { + var item = $(this); + if (!!item.attr('data-format') && !data[item.attr('data-format')]) { + item.fadeOut('fast', function() { + item.remove(); + }); + } + }); + } + }); + } + return false; + }); + + $('#download-formats-form-cancel').click(function() { + $('#download-shelf-menu').slideUp('fast'); + return false; + }); }); })(jQuery) \ No newline at end of file diff --git a/wolnelektury/templates/catalogue/tagged_object_list.html b/wolnelektury/templates/catalogue/tagged_object_list.html index 497fbb1fe..f89d498d1 100644 --- a/wolnelektury/templates/catalogue/tagged_object_list.html +++ b/wolnelektury/templates/catalogue/tagged_object_list.html @@ -29,6 +29,18 @@ Pobierz wszystkie książki z tej półki + {% endif %} {% if last_tag.gazeta_link %}

-- 2.20.1