From cfce638090837ac3db57dec1e79d85c316b26b08 Mon Sep 17 00:00:00 2001 From: Leonardo Lazzaro Date: Sun, 3 Jan 2016 13:20:29 -0500 Subject: [PATCH] initial django 1.9 compatilbity. fix #48. blank spaces cleanu. pep8 remove unused imports --- rollyourown/seo/backends.py | 26 +++++++++++++------------- rollyourown/seo/base.py | 34 ++++++++++++++++------------------ rollyourown/seo/options.py | 16 ++++++++-------- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/rollyourown/seo/backends.py b/rollyourown/seo/backends.py index 1b3acb5..7abf2ca 100644 --- a/rollyourown/seo/backends.py +++ b/rollyourown/seo/backends.py @@ -8,12 +8,12 @@ from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic from django.template import Template, Context -from django.utils.datastructures import SortedDict +from collections import OrderedDict as SortedDict from rollyourown.seo.utils import resolve_to_name, NotSet, Literal RESERVED_FIELD_NAMES = ('_metadata', '_path', '_content_type', '_object_id', - '_content_object', '_view', '_site', 'objects', + '_content_object', '_view', '_site', 'objects', '_resolve_value', '_set_context', 'id', 'pk' ) backend_registry = SortedDict() @@ -104,7 +104,7 @@ def for_site_and_language(self, site=None, language=None): # This means that: # - all fields that share uniqueness (backend fields, _site, _language) need to be defined in the same model # - as backends should have full control over the model, therefore every backend needs to define the compulsory fields themselves (eg _site and _language). -# There is no way to add future compulsory fields to all backends without editing each backend individually. +# There is no way to add future compulsory fields to all backends without editing each backend individually. # This is probably going to have to be a limitataion we need to live with. class MetadataBackend(object): @@ -148,7 +148,7 @@ def for_site_and_language(self, site=None, language=None): @staticmethod def validate(options): - """ Validates the application of this backend to a given metadata + """ Validates the application of this backend to a given metadata """ @@ -209,7 +209,7 @@ def _process_context(self, context): def _populate_from_kwargs(self): return {'view_name': self._view} - + def _resolve_value(self, name): value = super(ViewMetadataBase, self)._resolve_value(name) try: @@ -219,7 +219,7 @@ def _resolve_value(self, name): def __unicode__(self): return self._view - + class Meta: abstract = True unique_together = self.get_unique_together(options) @@ -246,7 +246,7 @@ class ModelInstanceMetadataBase(MetadataBaseModel): if options.use_i18n: _language = models.CharField(_("language"), max_length=5, null=True, blank=True, db_index=True, choices=settings.LANGUAGES) objects = self.get_manager(options)() - + def __unicode__(self): return self._path @@ -295,22 +295,22 @@ def __unicode__(self): return unicode(self._content_type) def _process_context(self, context): - """ Use the given model instance as context for rendering - any substitutions. + """ Use the given model instance as context for rendering + any substitutions. """ if 'model_instance' in context: self.__instance = context['model_instance'] def _populate_from_kwargs(self): return {'content_type': self._content_type} - + def _resolve_value(self, name): value = super(ModelMetadataBase, self)._resolve_value(name) try: return _resolve(value, self.__instance._content_object) except AttributeError: return value - + class Meta: abstract = True unique_together = self.get_unique_together(options) @@ -318,7 +318,7 @@ class Meta: @staticmethod def validate(options): - """ Validates the application of this backend to a given metadata + """ Validates the application of this backend to a given metadata """ try: if options.backends.index('modelinstance') > options.backends.index('model'): @@ -329,7 +329,7 @@ def validate(options): def _resolve(value, model_instance=None, context=None): - """ Resolves any template references in the given value. + """ Resolves any template references in the given value. """ if isinstance(value, basestring) and "{" in value: diff --git a/rollyourown/seo/base.py b/rollyourown/seo/base.py index dea9f49..1d2b4a2 100644 --- a/rollyourown/seo/base.py +++ b/rollyourown/seo/base.py @@ -8,18 +8,16 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -from django.utils.datastructures import SortedDict +from collections import OrderedDict as SortedDict from django.utils.functional import curry -from django.contrib.sites.models import Site from django.contrib.contenttypes.models import ContentType -from django.conf import settings from django.utils.safestring import mark_safe from django.core.cache import cache from django.utils.encoding import iri_to_uri from rollyourown.seo.utils import NotSet, Literal from rollyourown.seo.options import Options -from rollyourown.seo.fields import MetadataField, Tag, MetaTag, KeywordTag, Raw +from rollyourown.seo.fields import MetadataField from rollyourown.seo.backends import backend_registry, RESERVED_FIELD_NAMES @@ -35,9 +33,9 @@ def __init__(self, metadata, instances, path, site=None, language=None): self.__metadata = metadata if metadata._meta.use_cache: if metadata._meta.use_sites and site: - hexpath = hashlib.md5(iri_to_uri(site.domain+path)).hexdigest() + hexpath = hashlib.md5(iri_to_uri(site.domain+path)).hexdigest() else: - hexpath = hashlib.md5(iri_to_uri(path)).hexdigest() + hexpath = hashlib.md5(iri_to_uri(path)).hexdigest() if metadata._meta.use_i18n: self.__cache_prefix = 'rollyourown.seo.%s.%s.%s' % (self.__metadata.__class__.__name__, hexpath, language) else: @@ -48,7 +46,7 @@ def __init__(self, metadata, instances, path, site=None, language=None): self.__instances_cache = [] def __instances(self): - """ Cache instances, allowing generators to be used and reused. + """ Cache instances, allowing generators to be used and reused. This fills a cache as the generator gets emptied, eventually reading exclusively from the cache. """ @@ -59,7 +57,7 @@ def __instances(self): yield instance def _resolve_value(self, name): - """ Returns an appropriate value for the given name. + """ Returns an appropriate value for the given name. This simply asks each of the instances for a value. """ for instance in self.__instances(): @@ -166,9 +164,9 @@ def __new__(cls, name, bases, attrs): options = Options(Meta, help_text) # Collect and sort our elements - elements = [(key, attrs.pop(key)) for key, obj in attrs.items() + elements = [(key, attrs.pop(key)) for key, obj in attrs.items() if isinstance(obj, MetadataField)] - elements.sort(lambda x, y: cmp(x[1].creation_counter, + elements.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) elements = SortedDict(elements) @@ -221,7 +219,7 @@ def _get_formatted_data(cls, path, context=None, site=None, language=None): # TODO: Move this function out of the way (subclasses will want to define their own attributes) def _get_instances(cls, path, context=None, site=None, language=None): - """ A sequence of instances to discover metadata. + """ A sequence of instances to discover metadata. Each instance from each backend is looked up when possible/necessary. This is a generator to eliminate unnecessary queries. """ @@ -279,20 +277,20 @@ def get_linked_metadata(obj, name=None, context=None, site=None, language=None): model_md = ModelMetadata.objects.get(_content_type=content_type) except ModelMetadata.DoesNotExist: model_md = ModelMetadata(_content_type=content_type) - instances.append(model_md) + instances.append(model_md) return FormattedMetadata(Metadata, instances, '', site, language) def create_metadata_instance(metadata_class, instance): # If this instance is marked as handled, don't do anything - # This typically means that the django admin will add metadata + # This typically means that the django admin will add metadata # using eg an inline. if getattr(instance, '_MetadataFormset__seo_metadata_handled', False): return metadata = None content_type = ContentType.objects.get_for_model(instance) - + # If this object does not define a path, don't worry about automatic update try: path = instance.get_absolute_url() @@ -314,7 +312,7 @@ def create_metadata_instance(metadata_class, instance): else: # This is our instance! metadata = md - + # If the path-based search didn't work, look for (or create) an existing # instance linked to this object. if not metadata: @@ -324,7 +322,7 @@ def create_metadata_instance(metadata_class, instance): def populate_metadata(model, MetadataClass): - """ For a given model and metadata class, ensure there is metadata for every instance. + """ For a given model and metadata class, ensure there is metadata for every instance. """ content_type = ContentType.objects.get_for_model(model) for instance in model.objects.all(): @@ -333,8 +331,8 @@ def populate_metadata(model, MetadataClass): def _update_callback(model_class, sender, instance, created, **kwargs): """ Callback to be attached to a post_save signal, updating the relevant - metadata, or just creating an entry. - + metadata, or just creating an entry. + NB: It is theoretically possible that this code will lead to two instances with the same generic foreign key. If you have non-overlapping URLs, diff --git a/rollyourown/seo/options.py b/rollyourown/seo/options.py index 1c1d8ca..67ce333 100644 --- a/rollyourown/seo/options.py +++ b/rollyourown/seo/options.py @@ -1,9 +1,10 @@ #!/usr/bin/env python # -*- coding: UTF-8 -*- -from django.db.models.options import get_verbose_name +from django.utils.text import camel_case_to_spaces as get_verbose_name from django.db import models -from django.utils.datastructures import SortedDict +from collections import OrderedDict as SortedDict + class Options(object): def __init__(self, meta, help_text=None): @@ -55,12 +56,13 @@ def _register_elements(self, elements): # 0. Abstract base model with common fields base_meta = type('Meta', (), self.original_meta) + class BaseMeta(base_meta): abstract = True app_label = 'seo' fields['Meta'] = BaseMeta # Do we need this? - fields['__module__'] = __name__ #attrs['__module__'] + fields['__module__'] = __name__ # attrs['__module__'] self.MetadataBaseModel = type('%sBase' % self.name, (models.Model,), fields) def _add_backend(self, backend): @@ -68,7 +70,7 @@ def _add_backend(self, backend): md_type = backend.verbose_name base = backend().get_model(self) # TODO: Rename this field - new_md_attrs = {'_metadata': self.metadata, '__module__': __name__ } + new_md_attrs = {'_metadata': self.metadata, '__module__': __name__} new_md_meta = {} new_md_meta['verbose_name'] = '%s (%s)' % (self.verbose_name, md_type) @@ -76,7 +78,7 @@ def _add_backend(self, backend): new_md_meta['unique_together'] = base._meta.unique_together new_md_attrs['Meta'] = type("Meta", (), new_md_meta) new_md_attrs['_metadata_type'] = backend.name - model = type("%s%s"%(self.name,"".join(md_type.split())), (base, self.MetadataBaseModel), new_md_attrs.copy()) + model = type("%s%s" % (self.name, "".join(md_type.split())), (base, self.MetadataBaseModel), new_md_attrs.copy()) self.models[backend.name] = model # This is a little dangerous, but because we set __module__ to __name__, the model needs tobe accessible here globals()[model.__name__] = model @@ -94,7 +96,5 @@ def _set_seo_models(self, value): app = models.get_app(model_name) if app: seo_models.extend(models.get_models(app)) - - self.seo_models = seo_models - + self.seo_models = seo_models