Skip to content

Commit

Permalink
initial django 1.9 compatilbity. fix willhardy#48. blank spaces clean…
Browse files Browse the repository at this point in the history
…u. pep8 remove unused imports
  • Loading branch information
llazzaro committed Jan 3, 2016
1 parent 3089686 commit cfce638
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
26 changes: 13 additions & 13 deletions rollyourown/seo/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
"""


Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -295,30 +295,30 @@ 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)
return ModelMetadataBase

@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'):
Expand All @@ -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:
Expand Down
34 changes: 16 additions & 18 deletions rollyourown/seo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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:
Expand All @@ -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.
"""
Expand All @@ -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():
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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():
Expand 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,
Expand Down
16 changes: 8 additions & 8 deletions rollyourown/seo/options.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -55,28 +56,29 @@ 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):
""" Builds a subclass model for the given 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)
new_md_meta['verbose_name_plural'] = '%s (%s)' % (self.verbose_name_plural, md_type)
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
Expand All @@ -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

0 comments on commit cfce638

Please sign in to comment.