From d1d6ea8ed2889f871b6d89d4fae6021f286f921a Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 15 Feb 2019 15:07:05 +0100 Subject: [PATCH] Preparing for Django 1.10 --- requirements/requirements.txt | 14 +++--- src/ajaxable/utils.py | 5 +-- src/api/piston/admin.py | 6 ++- src/api/tests/tests.py | 2 +- src/catalogue/views.py | 65 ++++++++++++++++------------ src/infopages/views.py | 6 +-- src/lesmianator/views.py | 34 +++++++-------- src/libraries/views.py | 49 ++++++++++----------- src/pdcounter/views.py | 11 +++-- src/picture/views.py | 13 +++--- src/polls/views.py | 7 ++- src/reporting/views.py | 7 ++- src/search/views.py | 27 ++++++------ src/wolnelektury/settings/contrib.py | 4 ++ src/wolnelektury/utils.py | 3 +- 15 files changed, 129 insertions(+), 124 deletions(-) diff --git a/requirements/requirements.txt b/requirements/requirements.txt index fd98553b0..85e69d1bc 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -2,16 +2,16 @@ # django Django>=1.9,<1.10 -fnpdjango>=0.2.8,<0.3 -django-pipeline>=1.6,<1.7 +fnpdjango==0.3 +django-pipeline==1.6.10 jsmin -fnp-django-pagination +fnp-django-pagination==2.2.2 django-maintenancemode==0.10 jsonfield==2.0.2 django-picklefield>=1.0,<1.1 -django-modeltranslation==0.11 +django-modeltranslation==0.12 django-allauth>=0.32,<0.33 -django-extensions +django-extensions==1.7.8 djangorestframework<3.7 djangorestframework-xml oauthlib>=3.0.1,<3.1 @@ -20,7 +20,7 @@ oauthlib>=3.0.1,<3.1 pyyaml polib -django-babel==0.5.0 +django-babel==0.6.0 pytz @@ -72,7 +72,7 @@ requests paypalrestsdk django-haystack<2.8.0 -django-migdal>=0.8.3 +django-migdal==0.8.5 python-slugify diff --git a/src/ajaxable/utils.py b/src/ajaxable/utils.py index 82de84771..89dd0e2d9 100755 --- a/src/ajaxable/utils.py +++ b/src/ajaxable/utils.py @@ -5,8 +5,7 @@ from functools import wraps from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden -from django.shortcuts import render_to_response -from django.template import RequestContext +from django.shortcuts import render from django.utils.encoding import force_unicode from django.utils.functional import Promise from django.utils.http import urlquote_plus @@ -153,7 +152,7 @@ class AjaxableFormView(object): "view_kwargs": kwargs, } context.update(self.extra_context(request, obj)) - return render_to_response(template, context, context_instance=RequestContext(request)) + return render(request, template, context) def validate_object(self, obj, request): return None diff --git a/src/api/piston/admin.py b/src/api/piston/admin.py index 792f19629..c931db060 100644 --- a/src/api/piston/admin.py +++ b/src/api/piston/admin.py @@ -2,6 +2,10 @@ from django.contrib import admin from . import models +class TokenAdmin(admin.ModelAdmin): + raw_id_fields = ['user'] + + admin.site.register(models.Nonce) admin.site.register(models.Consumer) -admin.site.register(models.Token) +admin.site.register(models.Token, TokenAdmin) diff --git a/src/api/tests/tests.py b/src/api/tests/tests.py index 3b4d4c59e..8dd29a5b6 100644 --- a/src/api/tests/tests.py +++ b/src/api/tests/tests.py @@ -230,7 +230,7 @@ class BooksTests(ApiTest): class BlogTests(ApiTest): def test_get(self): - self.assertEqual(self.load_json('/api/blog/'), []) + self.assertEqual(self.load_json('/api/blog'), []) class PreviewTests(ApiTest): diff --git a/src/catalogue/views.py b/src/catalogue/views.py index 2e43bd081..1f2db19c6 100644 --- a/src/catalogue/views.py +++ b/src/catalogue/views.py @@ -7,9 +7,8 @@ import random from django.conf import settings from django.http.response import HttpResponseForbidden -from django.template import RequestContext from django.template.loader import render_to_string -from django.shortcuts import render_to_response, get_object_or_404, render, redirect +from django.shortcuts import get_object_or_404, render, redirect from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect from django.core.urlresolvers import reverse from django.db.models import Q, QuerySet @@ -52,14 +51,14 @@ def book_list(request, filters=None, template_name='catalogue/book_list.html', for tag in books_by_author: if books_by_author[tag]: books_nav.setdefault(tag.sort_key[0], []).append(tag) - return render_to_response(template_name, { + return render(request, template_name, { 'rendered_nav': render_to_string(nav_template_name, {'books_nav': books_nav}), 'rendered_book_list': render_to_string(list_template_name, { 'books_by_author': books_by_author, 'orphans': orphans, 'books_by_parent': books_by_parent, }) - }, context_instance=RequestContext(request)) + }) def daisy_list(request): @@ -80,9 +79,9 @@ def differentiate_tags(request, tags, ambiguous_slugs): 'url_args': '/'.join((beginning, tag.url_chunk, unparsed)).strip('/'), 'tags': [tag] }) - return render_to_response( - 'catalogue/differentiate_tags.html', {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]}, - context_instance=RequestContext(request)) + return render( + request, + 'catalogue/differentiate_tags.html', {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]}) def object_list(request, objects, fragments=None, related_tags=None, tags=None, list_type='books', extra=None): @@ -133,9 +132,10 @@ def object_list(request, objects, fragments=None, related_tags=None, tags=None, } if extra: result.update(extra) - return render_to_response( + return render( + request, 'catalogue/tagged_object_list.html', result, - context_instance=RequestContext(request)) + ) def literature(request): @@ -258,12 +258,15 @@ def book_fragments(request, slug, theme_slug): fragments = Fragment.tagged.with_all([theme]).filter( Q(book=book) | Q(book__ancestor=book)) - return render_to_response('catalogue/book_fragments.html', { - 'book': book, - 'theme': theme, - 'fragments': fragments, - 'active_menu_item': 'books', - }, context_instance=RequestContext(request)) + return render( + request, + 'catalogue/book_fragments.html', + { + 'book': book, + 'theme': theme, + 'fragments': fragments, + 'active_menu_item': 'books', + }) def book_detail(request, slug): @@ -272,11 +275,14 @@ def book_detail(request, slug): except Book.DoesNotExist: return pdcounter_views.book_stub_detail(request, slug) - return render_to_response('catalogue/book_detail.html', { - 'book': book, - 'book_children': book.children.all().order_by('parent_number', 'sort_key'), - 'active_menu_item': 'books', - }, context_instance=RequestContext(request)) + return render( + request, + 'catalogue/book_detail.html', + { + 'book': book, + 'book_children': book.children.all().order_by('parent_number', 'sort_key'), + 'active_menu_item': 'books', + }) # używane w publicznym interfejsie @@ -287,12 +293,15 @@ def player(request, slug): audiobooks, projects = book.get_audiobooks() - return render_to_response('catalogue/player.html', { - 'book': book, - 'audiobook': '', - 'audiobooks': audiobooks, - 'projects': projects, - }, context_instance=RequestContext(request)) + return render( + request, + 'catalogue/player.html', + { + 'book': book, + 'audiobook': '', + 'audiobooks': audiobooks, + 'projects': projects, + }) def book_text(request, slug): @@ -303,7 +312,7 @@ def book_text(request, slug): if not book.has_html_file(): raise Http404 - return render_to_response('catalogue/book_text.html', {'book': book}, context_instance=RequestContext(request)) + return render(request, 'catalogue/book_text.html', {'book': book}) # ========= @@ -338,7 +347,7 @@ def book_info(request, book_id, lang='pl'): book = get_object_or_404(Book, id=book_id) # set language by hand translation.activate(lang) - return render_to_response('catalogue/book_info.html', {'book': book}, context_instance=RequestContext(request)) + return render(request, 'catalogue/book_info.html', {'book': book}) def tag_info(request, tag_id): diff --git a/src/infopages/views.py b/src/infopages/views.py index 9f21ed7ba..fe2db8432 100644 --- a/src/infopages/views.py +++ b/src/infopages/views.py @@ -2,7 +2,7 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from django.shortcuts import render_to_response, get_object_or_404 +from django.shortcuts import render, get_object_or_404 from django.template import RequestContext, Template, TemplateSyntaxError from infopages.models import InfoPage @@ -21,8 +21,8 @@ def infopage(request, slug): except TemplateSyntaxError: right_column = '' - return render_to_response('infopages/infopage.html', { + return render(request, 'infopages/infopage.html', { 'page': page, 'left_column': left_column, 'right_column': right_column, - }, context_instance=RequestContext(request)) + }) diff --git a/src/lesmianator/views.py b/src/lesmianator/views.py index 35aa0785e..f814bbc99 100644 --- a/src/lesmianator/views.py +++ b/src/lesmianator/views.py @@ -2,8 +2,7 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from django.shortcuts import render_to_response, get_object_or_404 -from django.template import RequestContext +from django.shortcuts import render, get_object_or_404 from django.views.decorators import cache from catalogue.utils import get_random_hash @@ -15,11 +14,10 @@ def main_page(request): last = Poem.objects.all().order_by('-created_at')[:10] shelves = Tag.objects.filter(user__username='lesmianator') - return render_to_response( + return render( + request, 'lesmianator/lesmianator.html', - {"last": last, "shelves": shelves}, - context_instance=RequestContext(request)) - + {"last": last, "shelves": shelves}) @cache.never_cache def new_poem(request): @@ -28,10 +26,10 @@ def new_poem(request): p = Poem(slug=get_random_hash(text), text=text, created_by=user) p.save() - return render_to_response( + return render( + request, 'lesmianator/poem.html', - {"poem": p}, - context_instance=RequestContext(request)) + {"poem": p}) @cache.never_cache @@ -43,10 +41,10 @@ def poem_from_book(request, slug): p.created_from = [book.id] p.save() - return render_to_response( + return render( + request, 'lesmianator/poem.html', - {"poem": p, "books": [book], "book": book}, - context_instance=RequestContext(request)) + {"poem": p, "books": [book], "book": book}) @cache.never_cache @@ -61,10 +59,10 @@ def poem_from_set(request, shelf): book = books[0] if len(books) == 1 else None - return render_to_response( + return render( + request, 'lesmianator/poem.html', - {"poem": p, "shelf": tag, "books": books, "book": book}, - context_instance=RequestContext(request)) + {"poem": p, "shelf": tag, "books": books, "book": book}) def get_poem(request, poem): @@ -76,7 +74,7 @@ def get_poem(request, poem): else: books = book = None - return render_to_response( + return render( + request, 'lesmianator/poem.html', - {"poem": p, "books": books, "book": book}, - context_instance=RequestContext(request)) + {"poem": p, "books": books, "book": book}) diff --git a/src/libraries/views.py b/src/libraries/views.py index ab4b4b76f..98b36340b 100644 --- a/src/libraries/views.py +++ b/src/libraries/views.py @@ -1,26 +1,23 @@ -# -*- coding: utf-8 -*- -# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. -# -from django.shortcuts import render_to_response, get_object_or_404 -from django.template import RequestContext - -from libraries.models import Catalog, Library - - -def main_view(request): - context = RequestContext(request) - context['catalogs'] = Catalog.objects.all() - return render_to_response('libraries/main_view.html', context_instance=context) - - -def catalog_view(request, slug): - context = RequestContext(request) - context['catalog'] = get_object_or_404(Catalog.objects.filter(slug=slug).select_related()) - return render_to_response('libraries/catalog_view.html', context_instance=context) - - -def library_view(request, catalog_slug, slug): - context = RequestContext(request) - context['library'] = get_object_or_404(Library.objects.filter(slug=slug).filter(catalog__slug=catalog_slug)) - return render_to_response('libraries/library_view.html', context_instance=context) +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from django.shortcuts import render, get_object_or_404 +from libraries.models import Catalog, Library + + +def main_view(request): + return render(request, 'libraries/main_view.html', { + "catalogs": Catalog.objects.all(), + }) + + +def catalog_view(request, slug): + return render(request, 'libraries/catalog_view.html', { + "catalog": get_object_or_404(Catalog.objects.filter(slug=slug).select_related()), + }) + +def library_view(request, catalog_slug, slug): + return render(request, 'libraries/library_view.html', { + "library": get_object_or_404(Library.objects.filter(slug=slug).filter(catalog__slug=catalog_slug)), + }) diff --git a/src/pdcounter/views.py b/src/pdcounter/views.py index b8a685eb8..3be24c0a6 100644 --- a/src/pdcounter/views.py +++ b/src/pdcounter/views.py @@ -3,8 +3,7 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from datetime import datetime -from django.template import RequestContext -from django.shortcuts import render_to_response, get_object_or_404 +from django.shortcuts import render, get_object_or_404 from django.views.decorators import cache from suggest.forms import PublishingSuggestForm from . import models @@ -20,11 +19,11 @@ def book_stub_detail(request, slug): form = PublishingSuggestForm(initial={"books": u"%s — %s, \n" % (book.author, book.title)}) - return render_to_response('pdcounter/book_stub_detail.html', { + return render(request, 'pdcounter/book_stub_detail.html', { 'book': book, 'pd_counter': pd_counter, 'form': form, - }, context_instance=RequestContext(request)) + }) @cache.never_cache @@ -37,8 +36,8 @@ def author_detail(request, slug): form = PublishingSuggestForm(initial={"books": author.name + ", \n"}) - return render_to_response('pdcounter/author_detail.html', { + return render(request, 'pdcounter/author_detail.html', { 'author': author, 'pd_counter': pd_counter, 'form': form, - }, context_instance=RequestContext(request)) + }) diff --git a/src/picture/views.py b/src/picture/views.py index 6f02007e0..a43c09555 100644 --- a/src/picture/views.py +++ b/src/picture/views.py @@ -4,8 +4,7 @@ # from django.conf import settings from django.contrib.auth.decorators import permission_required -from django.shortcuts import render_to_response, get_object_or_404, render -from django.template import RequestContext +from django.shortcuts import render, get_object_or_404, render from picture.models import Picture, PictureArea from catalogue.utils import split_tags from ssify import ssi_included @@ -20,7 +19,7 @@ def picture_list_thumb(request, filter=None, get_filter=None, template_name='pic pictures = pictures.filter(filter) if get_filter: pictures = pictures.filter(get_filter()) - return render_to_response(template_name, {'book_list': list(pictures)}, context_instance=RequestContext(request)) + return render(request, template_name, {'book_list': list(pictures)}) def picture_detail(request, slug): @@ -28,12 +27,12 @@ def picture_detail(request, slug): theme_things = split_tags(picture.related_themes()) - return render_to_response("picture/picture_detail.html", { + return render(request, "picture/picture_detail.html", { 'picture': picture, 'themes': theme_things.get('theme', []), 'things': theme_things.get('thing', []), 'active_menu_item': 'gallery', - }, context_instance=RequestContext(request)) + }) def picture_viewer(request, slug): @@ -43,10 +42,10 @@ def picture_viewer(request, slug): have_sponsors = Sponsor.objects.filter(name=sponsor) if have_sponsors.exists(): sponsors.append(have_sponsors[0]) - return render_to_response("picture/picture_viewer.html", { + return render(request, "picture/picture_viewer.html", { 'picture': picture, 'sponsors': sponsors, - }, context_instance=RequestContext(request)) + }) @ajax(method='get') diff --git a/src/polls/views.py b/src/polls/views.py index 0d9590d85..ffafd2a98 100644 --- a/src/polls/views.py +++ b/src/polls/views.py @@ -3,8 +3,7 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.core.urlresolvers import reverse -from django.shortcuts import get_object_or_404, redirect, render_to_response -from django.template import RequestContext +from django.shortcuts import get_object_or_404, redirect, render from django.views.decorators import cache from django.views.decorators.http import require_http_methods @@ -30,7 +29,7 @@ def poll(request, slug): poll_item.vote(request.session) return redirect(redirect_to) elif request.method == 'GET': - context = RequestContext(request) + context = {} context['poll'] = poll context['voted_already'] = poll.voted(request.session) - return render_to_response('polls/poll.html', context) + return render(request, 'polls/poll.html', context) diff --git a/src/reporting/views.py b/src/reporting/views.py index 963440cd8..cc1bf7ee5 100644 --- a/src/reporting/views.py +++ b/src/reporting/views.py @@ -7,8 +7,7 @@ from datetime import date from django.conf import settings from django.contrib.admin.views.decorators import staff_member_required from django.db.models import Count -from django.shortcuts import render_to_response -from django.template import RequestContext +from django.shortcuts import render from catalogue.models import Book, BookMedia from reporting.utils import render_to_pdf, render_to_csv, generated_file_view @@ -30,10 +29,10 @@ def stats_page(request): (b.extra_info.get('license'), b.extra_info.get('license_description')) for b in Book.objects.all().iterator() if b.extra_info.get('license')) - return render_to_response('reporting/main.html', { + return render(request, 'reporting/main.html', { 'media_types': media_types, 'licenses': licenses, - }, context_instance=RequestContext(request)) + }) @generated_file_view('reports/katalog.pdf', 'application/pdf', diff --git a/src/search/views.py b/src/search/views.py index 39284c70c..3bbe720b0 100644 --- a/src/search/views.py +++ b/src/search/views.py @@ -4,8 +4,7 @@ # from django.conf import settings from django.http.response import HttpResponseRedirect -from django.shortcuts import render_to_response -from django.template import RequestContext +from django.shortcuts import render from django.views.decorators import cache from django.http import HttpResponse, JsonResponse @@ -108,12 +107,13 @@ def hint(request): def main(request): query = request.GET.get('q', '') if len(query) < 2: - return render_to_response( - 'catalogue/search_too_short.html', {'prefix': query}, - context_instance=RequestContext(request)) + return render( + request, 'catalogue/search_too_short.html', + {'prefix': query}) elif len(query) > 256: - return render_to_response( - 'catalogue/search_too_long.html', {'prefix': query}, context_instance=RequestContext(request)) + return render( + request, 'catalogue/search_too_long.html', + {'prefix': query}) query = prepare_query(query) pd_authors = search_pd_authors(query) @@ -123,27 +123,26 @@ def main(request): if not (books or pictures or pd_authors): form = PublishingSuggestForm(initial={"books": query + ", "}) - return render_to_response( + return render( + request, 'catalogue/search_no_hits.html', { 'form': form, 'did_you_mean': suggestion - }, - context_instance=RequestContext(request)) + }) if not (books or pictures) and len(pd_authors) == 1: return HttpResponseRedirect(pd_authors[0].get_absolute_url()) - return render_to_response( + return render( + request, 'catalogue/search_multiple_hits.html', { 'pd_authors': pd_authors, 'books': books, 'pictures': pictures, 'did_you_mean': suggestion - }, - context_instance=RequestContext(request)) - + }) def search_books(query): search = Search() diff --git a/src/wolnelektury/settings/contrib.py b/src/wolnelektury/settings/contrib.py index f3e7b6ba6..2fed2f3ee 100644 --- a/src/wolnelektury/settings/contrib.py +++ b/src/wolnelektury/settings/contrib.py @@ -44,6 +44,10 @@ MIGDAL_TYPES = ( EntryType('event', _('events'), commentable=False), ) +MIGDAL_OPTIONAL_LANGUAGES = ( + ('en', u'English'), +) + REST_FRAMEWORK = { "DEFAULT_RENDERER_CLASSES": ( 'rest_framework.renderers.JSONRenderer', diff --git a/src/wolnelektury/utils.py b/src/wolnelektury/utils.py index 2657a5542..142d33fe3 100644 --- a/src/wolnelektury/utils.py +++ b/src/wolnelektury/utils.py @@ -15,7 +15,6 @@ from inspect import getargspec import re from django.core.mail import send_mail from django.http import HttpResponse -from django.template import RequestContext from django.template.loader import render_to_string from django.utils import timezone from django.conf import settings @@ -101,7 +100,7 @@ def ajax(login_required=False, method=None, template=None, permission_required=N try: res = fun(request, **kwargs) if res and template: - res = {'html': render_to_string(template, res, RequestContext(request))} + res = {'html': render_to_string(template, res, request=request)} except AjaxError as e: res = {'result': e.args[0]} if 'result' not in res: -- 2.20.1