PEP 8 cleaning

Conflicts:

	polymorphic/__init__.py
	polymorphic/polymorphic_model.py
fix_request_path_info
German M. Bravo 2011-11-26 00:22:18 -06:00 committed by Christopher Glass
parent c9922d8987
commit f987818fe4
9 changed files with 147 additions and 102 deletions

View File

@ -6,7 +6,6 @@ Copyright:
This code and affiliated files are (C) by Bert Constantin and individual contributors.
Please see LICENSE and AUTHORS for more information.
"""
from polymorphic_model import PolymorphicModel
from manager import PolymorphicManager
from query import PolymorphicQuerySet
@ -17,6 +16,7 @@ from showfields import ShowFields, ShowFieldTypes, ShowFieldsAndTypes # import o
VERSION = (1, 0, 0, 'beta')
def get_version():
version = '%s.%s' % VERSION[0:2]
if VERSION[2]:
@ -24,3 +24,34 @@ def get_version():
if VERSION[3]:
version += ' %s' % VERSION[3]
return version
# Proxied models need to have it's own ContentType
from django.contrib.contenttypes.models import ContentTypeManager
from django.utils.encoding import smart_unicode
def get_for_proxied_model(self, model):
"""
Returns the ContentType object for a given model, creating the
ContentType if necessary. Lookups are cached so that subsequent lookups
for the same model don't hit the database.
"""
opts = model._meta
key = (opts.app_label, opts.object_name.lower())
try:
ct = self.__class__._cache[self.db][key]
except KeyError:
# Load or create the ContentType entry. The smart_unicode() is
# needed around opts.verbose_name_raw because name_raw might be a
# django.utils.functional.__proxy__ object.
ct, created = self.get_or_create(
app_label=opts.app_label,
model=opts.object_name.lower(),
defaults={'name': smart_unicode(opts.verbose_name_raw)},
)
self._add_to_cache(self.db, ct)
return ct
ContentTypeManager.get_for_proxied_model = get_for_proxied_model

View File

@ -20,36 +20,43 @@ except:
raise TypeError('first argument must be callable')
dict.__init__(self, *a, **kw)
self.default_factory = default_factory
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return self.__missing__(key)
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
self[key] = value = self.default_factory()
return value
def __reduce__(self):
if self.default_factory is None:
args = tuple()
else:
args = self.default_factory,
return type(self), args, None, None, self.items()
def copy(self):
return self.__copy__()
def __copy__(self):
return type(self)(self.default_factory, self)
def __deepcopy__(self, memo):
import copy
return type(self)(self.default_factory,
copy.deepcopy(self.items()))
return type(self)(self.default_factory, copy.deepcopy(self.items()))
def __repr__(self):
return 'defaultdict(%s, %s)' % (self.default_factory, dict.__repr__(self))
if getattr(str, 'partition', None):
def compat_partition(s,sep): return s.partition(sep)
def compat_partition(s, sep):
return s.partition(sep)
else:
""" from:
http://mail.python.org/pipermail/python-dev/2005-September/055962.html
@ -82,4 +89,3 @@ else:
assert ''.join(result) == s
assert result[1] == '' or result[1] is sep
return result

View File

@ -37,4 +37,3 @@ class PolymorphicManager(models.Manager):
def __unicode__(self):
return u'%s (PolymorphicManager) using %s' % (self.__class__.__name__, self.queryset_class.__name__)

View File

@ -150,7 +150,7 @@ class PolymorphicModel(models.Model):
for name, model in subclasses_and_superclasses_accessors.iteritems():
orig_accessor = getattr(self.__class__, name, None)
if type(orig_accessor) in [SingleRelatedObjectDescriptor, ReverseSingleRelatedObjectDescriptor]:
#print >>sys.stderr, '---------- replacing',name, orig_accessor
#print >>sys.stderr, '---------- replacing', name, orig_accessor, '->', model
setattr(self.__class__, name, property(create_accessor_function_for_model(model, name)))
def _get_inheritance_relation_fields_and_models(self):

View File

@ -47,6 +47,7 @@ def translate_polymorphic_filter_definitions_in_kwargs(queryset_model, kwargs):
return additional_args
def translate_polymorphic_Q_object(queryset_model, potential_q_object):
def tree_node_correct_field_specs(my_model, node):
" process all children of this Q node "
@ -68,6 +69,7 @@ def translate_polymorphic_Q_object(queryset_model, potential_q_object):
return potential_q_object
def translate_polymorphic_filter_definitions_in_args(queryset_model, args):
"""
Translate the non-keyword argument list for PolymorphicQuerySet.filter()
@ -229,4 +231,3 @@ def _create_model_filter_Q(modellist, not_instance_of=False):
if not_instance_of:
q_ored = ~q_ored
return q_ored

View File

@ -2,6 +2,7 @@
from django.db import models
class ShowFieldBase(object):
""" base class for the ShowField... model mixins, does the work """
@ -107,7 +108,9 @@ class ShowFieldBase(object):
indent = len(self.__class__.__name__) + 5
indentstr = ''.rjust(indent)
out=u''; xpos=0; possible_line_break_pos = None
out = u''
xpos = 0
possible_line_break_pos = None
for i in xrange(len(parts)):
new_section, p, separator = parts[i]
@ -123,12 +126,15 @@ class ShowFieldBase(object):
out += '\n' + indentstr + rest
xpos = indent + len(rest)
out += p; xpos += len(p)
out += p
xpos += len(p)
if not final:
if not next_new_section:
out += separator; xpos += len(separator)
out += ' '; xpos += 1
out += separator
xpos += len(separator)
out += ' '
xpos += 1
if not new_section:
possible_line_break_pos = len(out)
@ -140,10 +146,12 @@ class ShowFieldType(ShowFieldBase):
""" model mixin that shows the object's class and it's field types """
polymorphic_showfield_type = True
class ShowFieldContent(ShowFieldBase):
""" model mixin that shows the object's class, it's fields and field contents """
polymorphic_showfield_content = True
class ShowFieldTypeAndContent(ShowFieldBase):
""" model mixin, like ShowFieldContent, but also show field types """
polymorphic_showfield_type = True

View File

@ -4,15 +4,15 @@
import uuid
from django.forms.util import ValidationError
from django import forms
from django.db import models
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext_lazy
class UUIDVersionError(Exception):
pass
class UUIDField(models.CharField):
"""Encode and stores a Python uuid.UUID in a manner that is appropriate
for the given datatabase that we are using.