]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/models/base.py
Migrate to Django 1.6
[osqa.git] / forum / models / base.py
index 2cfe7db7c89410c5667bd864dd5c7ecc14943832..9c328c597a27712632d7067eaff029381512338c 100644 (file)
@@ -8,6 +8,7 @@ from urllib import quote_plus, urlencode
 from django.db import models, IntegrityError, connection, transaction
 from django.utils.http import urlquote  as django_urlquote
 from django.utils.html import strip_tags
+from django.conf import settings as django_settings
 from django.core.urlresolvers import reverse
 from django.contrib.contenttypes import generic
 from django.contrib.contenttypes.models import ContentType
@@ -82,7 +83,8 @@ class CachedQuerySet(models.query.QuerySet):
                 obj.cache()
             else:
                 obj = self.obj_from_datadict(obj)
-                obj.reset_original_state()
+
+            obj.reset_original_state()
 
             return obj
 
@@ -170,19 +172,25 @@ class CachedQuerySet(models.query.QuerySet):
         if to_return:
             for row in to_return:
                 if hasattr(row, 'leaf'):
-                    yield row.leaf
-                else:
-                    yield row
+                    row = row.leaf
 
-    def _get_query_hash(self):
-        return md5(unicode(self.query).encode("utf-8")).hexdigest()
+                row.reset_original_state()
+                yield row
 
+    def _get_query_hash(self):
+        try:
+            return md5(unicode(self.query).encode("utf-8")).hexdigest()
+        except:
+            try:
+                return md5(self.query).hexdigest()
+            except:
+                return md5(str(self.query)).hexdigest()
 
 
 class CachedManager(models.Manager):
     use_for_related_fields = True
 
-    def get_query_set(self):
+    def get_queryset(self):
         return CachedQuerySet(self.model)
 
     def get_or_create(self, *args, **kwargs):
@@ -218,7 +226,7 @@ class DenormalizedField(object):
         cls.add_to_class("reset_%s_cache" % name, reset_cache)
 
 
-class BaseMetaClass(models.Model.__metaclass__):
+class BaseMetaClass(type(models.Model)):
     to_denormalize = []
 
     def __new__(cls, *args, **kwargs):
@@ -323,7 +331,12 @@ class BaseModel(models.Model):
             pk = [v for (k,v) in querydict.items() if k in ('pk', 'pk__exact', 'id', 'id__exact'
                             ) or k.endswith('_ptr__pk') or k.endswith('_ptr__id')][0]
 
-            return cls._generate_cache_key(pk)
+            cache_key = cls._generate_cache_key(pk)
+
+            if len(cache_key) > django_settings.CACHE_MAX_KEY_LENGTH:
+                cache_key = cache_key[:django_settings.CACHE_MAX_KEY_LENGTH]
+
+            return cache_key
         except:
             return None