From: Marek Stępniowski Date: Sat, 30 Jan 2010 22:00:34 +0000 (+0100) Subject: Dodanie możliwości wyboru formatów przy ściąganiu półki. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/75349c5b71dfbc94bc28319c86794b56da36a08b?ds=sidebyside;hp=-c Dodanie możliwości wyboru formatów przy ściąganiu półki. --- 75349c5b71dfbc94bc28319c86794b56da36a08b 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 %}