From 64af15fe792f42d3a7dc3e857a45577d16806255 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Sat, 16 Feb 2019 22:09:45 +0100 Subject: [PATCH] Passing for Django 1.10. --- requirements/requirements.txt | 6 +-- src/catalogue/models/book.py | 5 -- src/catalogue/models/tag.py | 82 +++++++++++++++---------------- src/catalogue/tests/test_visit.py | 14 +++--- src/catalogue/urls.py | 2 +- src/newtagging/models.py | 52 ++++---------------- src/newtagging/views.py | 39 --------------- src/pdcounter/models.py | 4 +- src/picture/models.py | 2 +- 9 files changed, 63 insertions(+), 143 deletions(-) delete mode 100644 src/newtagging/views.py diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 85e69d1bc..f9374c3b9 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -1,7 +1,7 @@ -i https://py.mdrn.pl/simple/ # django -Django>=1.9,<1.10 +Django>=1.10,<1.11 fnpdjango==0.3 django-pipeline==1.6.10 jsmin @@ -56,8 +56,8 @@ pyoai==2.4.4 ## egenix-mx-base # Doesn't play nice with mx in dist-packages. sunburnt -#django-getpaid==1.7.4 --e git+https://github.com/django-getpaid/django-getpaid.git@v1.7.4#egg=django-getpaid +django-getpaid==1.8.0 +deprecated httplib2 Texml diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 068bae899..923a60485 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -207,11 +207,6 @@ class Book(models.Model): def get_absolute_url(self): return 'book_detail', [self.slug] - @staticmethod - @permalink - def create_url(slug): - return 'book_detail', [slug] - def gallery_path(self): return gallery_path(self.slug) diff --git a/src/catalogue/models/tag.py b/src/catalogue/models/tag.py index c6b6f2663..31da2564b 100644 --- a/src/catalogue/models/tag.py +++ b/src/catalogue/models/tag.py @@ -14,7 +14,7 @@ from django.db.models.query import Prefetch from django.dispatch import Signal from django.utils.translation import ugettext_lazy as _ -from newtagging.models import TagBase +from newtagging.models import TagManager, TaggedItemManager from ssify import flush_ssi_includes @@ -37,6 +37,8 @@ class TagRelation(models.Model): object_id = models.PositiveIntegerField(_('object id'), db_index=True) content_object = GenericForeignKey('content_type', 'object_id') + objects = TaggedItemManager() + class Meta: db_table = 'catalogue_tag_relation' unique_together = (('tag', 'content_type', 'object_id'),) @@ -48,7 +50,7 @@ class TagRelation(models.Model): return u' [%s]' % self.tag -class Tag(TagBase): +class Tag(models.Model): """A tag attachable to books and fragments (and possibly anything). Used to represent searchable metadata (authors, epochs, genres, kinds), @@ -74,6 +76,7 @@ class Tag(TagBase): after_change = Signal(providing_args=['instance', 'languages']) intermediary_table_model = TagRelation + objects = TagManager() class UrlDeprecationWarning(DeprecationWarning): def __init__(self, tags=None): @@ -178,13 +181,6 @@ class Tag(TagBase): def get_absolute_gallery_url(self): return 'tagged_object_list_gallery', [self.url_chunk] - @classmethod - @permalink - def create_url(cls, category, slug): - return ('catalogue.views.tagged_object_list', [ - '/'.join((cls.categories_dict[category], slug)) - ]) - def has_description(self): return len(self.description) > 0 has_description.short_description = _('description') @@ -192,41 +188,38 @@ class Tag(TagBase): @staticmethod def get_tag_list(tag_str): - if isinstance(tag_str, basestring): - if not tag_str: - return [] - tags = [] - ambiguous_slugs = [] - category = None - deprecated = False - tags_splitted = tag_str.split('/') - for name in tags_splitted: - if category: - tags.append(Tag.objects.get(slug=name, category=category)) - category = None - elif name in Tag.categories_rev: - category = Tag.categories_rev[name] - else: - try: - tags.append(Tag.objects.get(slug=name)) - deprecated = True - except Tag.MultipleObjectsReturned: - ambiguous_slugs.append(name) - + if not tag_str: + return [] + tags = [] + ambiguous_slugs = [] + category = None + deprecated = False + tags_splitted = tag_str.split('/') + for name in tags_splitted: if category: - # something strange left off - raise Tag.DoesNotExist() - if ambiguous_slugs: - # some tags should be qualified - e = Tag.MultipleObjectsReturned() - e.tags = tags - e.ambiguous_slugs = ambiguous_slugs - raise e - if deprecated: - raise Tag.UrlDeprecationWarning(tags=tags) - return tags - else: - return TagBase.get_tag_list(tag_str) + tags.append(Tag.objects.get(slug=name, category=category)) + category = None + elif name in Tag.categories_rev: + category = Tag.categories_rev[name] + else: + try: + tags.append(Tag.objects.get(slug=name)) + deprecated = True + except Tag.MultipleObjectsReturned: + ambiguous_slugs.append(name) + + if category: + # something strange left off + raise Tag.DoesNotExist() + if ambiguous_slugs: + # some tags should be qualified + e = Tag.MultipleObjectsReturned() + e.tags = tags + e.ambiguous_slugs = ambiguous_slugs + raise e + if deprecated: + raise Tag.UrlDeprecationWarning(tags=tags) + return tags @property def url_chunk(self): @@ -275,6 +268,9 @@ class Tag(TagBase): return meta_tags +TagRelation.tag_model = Tag + + def prefetch_relations(objects, category, only_name=True): queryset = TagRelation.objects.filter(tag__category=category).select_related('tag') if only_name: diff --git a/src/catalogue/tests/test_visit.py b/src/catalogue/tests/test_visit.py index e6a83edd0..c170c00b4 100644 --- a/src/catalogue/tests/test_visit.py +++ b/src/catalogue/tests/test_visit.py @@ -13,14 +13,14 @@ class VisitTest(WLTestCase): def setUp(self): WLTestCase.setUp(self) author = PersonStub(("Jane",), "Doe") - book_info = BookInfoStub(author=author, genre="Genre", + book_info = BookInfoStub(author=author, genre="Sielanka", epoch='Epoch', kind="Kind", **info_args(u"A book")) self.book = models.Book.from_text_and_meta(ContentFile(''' - Theme + Sielanka Test @@ -40,11 +40,13 @@ class VisitTest(WLTestCase): 'nowe/', 'lektura/a-book/', 'lektura/a-book.html', - 'lektura/a-book/motyw/theme/', - 'motyw/theme/', + 'lektura/a-book/motyw/sielanka/', + 'motyw/sielanka/', + 'sielanka/', 'autor/jane-doe/', + 'daisy/', # 'autor/jane-doe/gatunek/genre/', - # 'autor/jane-doe/gatunek/genre/motyw/theme/', + # 'autor/jane-doe/gatunek/genre/motyw/sielanka/', 'b/%d/mini.pl.html' % self.book.pk, 'b/%d/mini_nolink.pl.html' % self.book.pk, 'b/%d/short.pl.html' % self.book.pk, @@ -56,7 +58,7 @@ class VisitTest(WLTestCase): 'lektury/nonexistent/', # Nonexistent Collection. 'lektura/nonexistent/', # Nonexistent Book. 'lektura/nonexistent.html', # Nonexistent Book's HTML. - 'lektura/nonexistent/motyw/theme/', # Nonexistent Book's theme. + 'lektura/nonexistent/motyw/sielanka/', # Nonexistent Book's theme. 'lektura/a-book/motyw/nonexistent/', # Nonexistent theme in a Book. 'autor/nonexistent/', # Nonexistent author. 'motyw/nonexistent/', # Nonexistent theme. diff --git a/src/catalogue/urls.py b/src/catalogue/urls.py index 8b37e0774..458f336f1 100644 --- a/src/catalogue/urls.py +++ b/src/catalogue/urls.py @@ -18,7 +18,7 @@ urlpatterns = [ # pictures - currently pictures are coupled with catalogue, hence the url is here url(r'^obraz/$', picture.views.picture_list_thumb, name='picture_list_thumb'), url(r'^obraz/(?P%s).html$' % SLUG, picture.views.picture_viewer, name='picture_viewer'), - url(r'^obraz/(?P%s)/$' % SLUG, picture.views.picture_detail), + url(r'^obraz/(?P%s)/$' % SLUG, picture.views.picture_detail, name='picture_detail'), url(r'^p/(?P\d+)/mini\.(?P.+)\.html', picture.views.picture_mini, name='picture_mini'), url(r'^p/(?P\d+)/short\.(?P.+)\.html', picture.views.picture_short, name='picture_short'), diff --git a/src/newtagging/models.py b/src/newtagging/models.py index b90ea8e83..800cd0028 100644 --- a/src/newtagging/models.py +++ b/src/newtagging/models.py @@ -31,11 +31,14 @@ def get_queryset_and_model(queryset_or_model): # Managers # ############ class TagManager(models.Manager): - def __init__(self, intermediary_table_model): + def __init__(self): super(TagManager, self).__init__() - self.intermediary_table_model = intermediary_table_model models.signals.pre_delete.connect(self.target_deleted) + @property + def intermediary_table_model(self): + return self.model.intermediary_table_model + def target_deleted(self, instance, **kwargs): """ clear tag relations before deleting an object """ try: @@ -52,7 +55,7 @@ class TagManager(models.Manager): content_type = ContentType.objects.get_for_model(obj) current_tags = list(self.filter(items__content_type__pk=content_type.pk, items__object_id=obj.pk)) - updated_tags = self.model.get_tag_list(tags) + updated_tags = tags # Remove tags which no longer apply tags_for_removal = [tag for tag in current_tags if tag not in updated_tags] @@ -156,9 +159,9 @@ class TagManager(models.Manager): class TaggedItemManager(models.Manager): - def __init__(self, tag_model): - super(TaggedItemManager, self).__init__() - self.tag_model = tag_model + @property + def tag_model(self): + return self.model.tag_model def get_by_model(self, queryset_or_model, tags): """ @@ -166,7 +169,6 @@ class TaggedItemManager(models.Manager): model associated with a given tag or list of tags. """ queryset, model = get_queryset_and_model(queryset_or_model) - tags = self.tag_model.get_tag_list(tags) if not tags: # No existing tags were given return queryset.none() @@ -183,7 +185,6 @@ class TaggedItemManager(models.Manager): model associated with *any* of the given list of tags. """ queryset, model = get_queryset_and_model(queryset_or_model) - tags = self.tag_model.get_tag_list(tags) if not tags: return queryset.none() # TODO: presumes reverse generic relation @@ -200,38 +201,3 @@ class TaggedItemManager(models.Manager): # Do we know it's 'tags'? return queryset.filter(tag_relations__tag__in=obj.tags).annotate( count=models.Count('pk')).order_by('-count').exclude(pk=obj.pk) - - -########## -# Models # -########## - -class TagMeta(ModelBase): - """Metaclass for tag models (models inheriting from TagBase).""" - def __new__(mcs, name, bases, attrs): - model = super(TagMeta, mcs).__new__(mcs, name, bases, attrs) - if not model._meta.abstract: - # Register custom managers for concrete models - TagManager(model.intermediary_table_model).contribute_to_class(model, 'objects') - TaggedItemManager(model).contribute_to_class(model.intermediary_table_model, 'objects') - return model - - -class TagBase(models.Model): - """Abstract class to be inherited by model classes.""" - __metaclass__ = TagMeta - - class Meta: - abstract = True - - @staticmethod - def get_tag_list(tag_list): - """ - Utility function for accepting tag input in a flexible manner. - - You should probably override this method in your subclass. - """ - if isinstance(tag_list, TagBase): - return [tag_list] - else: - return tag_list diff --git a/src/newtagging/views.py b/src/newtagging/views.py deleted file mode 100644 index 12e528a3b..000000000 --- a/src/newtagging/views.py +++ /dev/null @@ -1,39 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Tagging related views. -""" -from django.http import Http404 -from django.utils.translation import ugettext as _ -from django.views.generic import ListView - - -def tagged_object_list(request, queryset_or_model, tag_model, tags, related_tags=False, - related_tag_counts=True, **kwargs): - """ - A thin wrapper around - ``django.views.generic.list_detail.object_list`` which creates a - ``QuerySet`` containing instances of the given queryset or model - tagged with the given tag. - - In addition to the context variables set up by ``object_list``, a - ``tag`` context variable will contain the ``Tag`` instance for the - tag. - - If ``related_tags`` is ``True``, a ``related_tags`` context variable - will contain tags related to the given tag for the given model. - Additionally, if ``related_tag_counts`` is ``True``, each related - tag will have a ``count`` attribute indicating the number of items - which have it in addition to the given tag. - """ - - tag_instances = tag_model.get_tag_list(tags) - if tag_instances is None: - raise Http404(_('No tags found matching "%s".') % tags) - queryset = tag_model.intermediary_table_model.objects.get_by_model(queryset_or_model, tag_instances) - if 'extra_context' not in kwargs: - kwargs['extra_context'] = {} - kwargs['extra_context']['tags'] = tag_instances - if related_tags: - kwargs['extra_context']['related_tags'] = \ - tag_model.objects.related_for_model(tag_instances, queryset_or_model, counts=related_tag_counts) - return ListView.as_view(queryset=queryset)(request, **kwargs) diff --git a/src/pdcounter/models.py b/src/pdcounter/models.py index 69c70c1c0..24866fee5 100644 --- a/src/pdcounter/models.py +++ b/src/pdcounter/models.py @@ -36,7 +36,7 @@ class Author(models.Model): @permalink def get_absolute_url(self): - return 'catalogue.views.tagged_object_list', [self.url_chunk] + return 'tagged_object_list', [self.url_chunk] def has_description(self): return len(self.description) > 0 @@ -76,7 +76,7 @@ class BookStub(models.Model): @permalink def get_absolute_url(self): - return 'catalogue.views.book_detail', [self.slug] + return 'book_detail', [self.slug] def in_pd(self): return self.pd is not None and self.pd <= datetime.now().year diff --git a/src/picture/models.py b/src/picture/models.py index e20325702..a3c098c79 100644 --- a/src/picture/models.py +++ b/src/picture/models.py @@ -144,7 +144,7 @@ class Picture(models.Model): @permalink def get_absolute_url(self): - return 'picture.views.picture_detail', [self.slug] + return 'picture_detail', [self.slug] def get_initial(self): try: -- 2.20.1