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)
+
urlpatterns = patterns('catalogue.views',
url(r'^$', 'main_page', name='main_page'),
+ url(r'^polki/(?P<shelf>[a-zA-Z0-9-]+)/formaty/$', 'shelf_book_formats', name='shelf_book_formats'),
url(r'^polki/(?P<shelf>[a-zA-Z0-9-]+)/(?P<book>[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<slug>[a-zA-Z0-9-]+)/usun/$', 'delete_shelf', name='delete_shelf'),
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(),
+ },
)
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):
""""
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')
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
-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;
}
/* ============================ */
$('#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
<a id="download-shelf" href="{% url download_shelf last_tag.slug %}">
Pobierz wszystkie książki z tej półki
</a>
+ <div id="download-shelf-menu" style="display:none;">
+ <form action="{% url download_shelf last_tag.slug %}" method="get" accept-charset="utf-8" id="download-formats-form" data-formats-feed="{% url shelf_book_formats last_tag.slug %}">
+ <p>Wybierz formaty książek, które chcesz pobrać:</p>
+ <li data-format="pdf"><label for="id_formats_2"><input type="checkbox" name="formats" value="pdf" id="id_formats_2" /> PDF</label> <em><strong>do czytania</strong> i drukowania przy pomocy <a href="http://get.adobe.com/reader/">Adobe Reader</a></em></li>
+ <li data-format="odt"><label for="id_formats_3"><input type="checkbox" name="formats" value="odt" id="id_formats_3" /> ODT</label> <em><strong>do czytania</strong> i edytowania przy pomocy <a href="http://pl.openoffice.org/">OpenOffice.org</a></em></li>
+ <li data-format="txt"><label for="id_formats_4"><input type="checkbox" name="formats" value="txt" id="id_formats_4" /> TXT</label> <em><strong>do czytania</strong> na małych ekranach, np. na komórce</em></li>
+ <li data-format="mp3"><label for="id_formats_0"><input type="checkbox" name="formats" value="mp3" id="id_formats_0" /> MP3</label> <em><strong>do słuchania</strong> w ulubionym odtwarzaczu MP3</em></li>
+ <li data-format="ogg"><label for="id_formats_1"><input type="checkbox" name="formats" value="ogg" id="id_formats_1" /> OGG Vorbis</label> <em><strong>do słuchania</strong> — otwarty format <a href="http://www.vorbis.com/">Fundacji Xiph.Org</a></em></li>
+ <li id="download-formats-form-submit-li"><label><input type="submit" name="submit" value="Pobierz" id="download-formats-form-submit" disabled="disabled" /> <img src="/static/img/indicator.gif" /></label> <span id="updating-formats">Uaktualniam listę formatów książek na półce.</span><span id="formats-updated" style="display:none;">lub <a href="#" id="download-formats-form-cancel">anuluj</a></span></li>
+ <div class="clearboth"></div>
+ </form>
+ </div>
{% endif %}
{% if last_tag.gazeta_link %}
<p><a href="{{ last_tag.gazeta_link }}">