import datetime
-import hashlib
+import re
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.safestring import mark_safe
from django.contrib.sitemaps import ping_google
import django.dispatch
-from django.conf import settings
-from forum import const
+from forum import settings
import logging
-from forum.const import *
-class CachedManager(models.Manager):
- use_for_related_fields = True
+class LazyQueryList(object):
+ def __init__(self, model, items):
+ self.model = model
+ self.items = items
+
+ def __getitem__(self, k):
+ return self.model.objects.get(id=self.items[k])
+
+ def __iter__(self):
+ for id in self.items:
+ yield self.model.objects.get(id=id)
+
+ def __len__(self):
+ return len(self.items)
+
+class CachedQuerySet(models.query.QuerySet):
+ def lazy(self):
+ if len(self.query.extra) == 0:
+ return LazyQueryList(self.model, list(self.values_list('id', flat=True)))
+ else:
+ return self
+
+ def obj_from_datadict(self, datadict):
+ obj = self.model()
+ obj.__dict__.update(datadict)
+ return obj
def get(self, *args, **kwargs):
- try:
- pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact') or k.endswith('_ptr__pk')][0]
- except:
- pk = None
+ key = self.model.infer_cache_key(kwargs)
- if pk is not None:
- key = self.model.cache_key(pk)
+ if key is not None:
obj = cache.get(key)
if obj is None:
- obj = super(CachedManager, self).get(*args, **kwargs)
- cache.set(key, obj, 60 * 60)
+ obj = super(CachedQuerySet, self).get(*args, **kwargs)
+ obj.cache()
+ else:
+ obj = self.obj_from_datadict(obj)
+ obj.reset_original_state()
return obj
-
- return super(CachedManager, self).get(*args, **kwargs)
+
+ return super(CachedQuerySet, self).get(*args, **kwargs)
+
+class CachedManager(models.Manager):
+ use_for_related_fields = True
+
+ def get_query_set(self):
+ return CachedQuerySet(self.model)
def get_or_create(self, *args, **kwargs):
try:
return super(CachedManager, self).get_or_create(*args, **kwargs)
-class BaseModel(models.Model):
- objects = CachedManager()
+class DenormalizedField(object):
+ def __init__(self, manager, *args, **kwargs):
+ self.manager = manager
+ self.filter = (args, kwargs)
- class Meta:
- abstract = True
- app_label = 'forum'
+ def setup_class(self, cls, name):
+ dict_name = '_%s_dencache_' % name
- def __init__(self, *args, **kwargs):
- super(BaseModel, self).__init__(*args, **kwargs)
- self._original_state = dict([(k, v) for k,v in self.__dict__.items() if not k in kwargs])
+ def getter(inst):
+ val = inst.__dict__.get(dict_name, None)
- @classmethod
- def cache_key(cls, pk):
- return '%s.%s:%s' % (settings.APP_URL, cls.__name__, pk)
+ if val is None:
+ val = getattr(inst, self.manager).filter(*self.filter[0], **self.filter[1]).count()
+ inst.__dict__[dict_name] = val
+ inst.cache()
- def get_dirty_fields(self):
- missing = object()
- return dict([(k, self._original_state.get(k, None)) for k,v in self.__dict__.items()
- if self._original_state.get(k, missing) == missing or self._original_state[k] != v])
+ return val
- def save(self, *args, **kwargs):
- super(BaseModel, self).save(*args, **kwargs)
- self._original_state = dict(self.__dict__)
- cache.set(self.cache_key(self.pk), self, 86400)
+ def reset_cache(inst):
+ inst.__dict__.pop(dict_name, None)
+ inst.uncache()
- def delete(self):
- cache.delete(self.cache_key(self.pk))
- super(BaseModel, self).delete()
+ cls.add_to_class(name, property(getter))
+ cls.add_to_class("reset_%s_cache" % name, reset_cache)
-class ActiveObjectManager(models.Manager):
- def get_query_set(self):
- return super(ActiveObjectManager, self).get_query_set().filter(canceled=False)
+class BaseMetaClass(models.Model.__metaclass__):
+ to_denormalize = []
-class UndeletedObjectManager(models.Manager):
- def get_query_set(self):
- return super(UndeletedObjectManager, self).get_query_set().filter(deleted=False)
+ def __new__(cls, *args, **kwargs):
+ new_cls = super(BaseMetaClass, cls).__new__(cls, *args, **kwargs)
-class MetaContent(BaseModel):
- """
- Base class for Vote, Comment and FlaggedItem
- """
- content_type = models.ForeignKey(ContentType)
- object_id = models.PositiveIntegerField()
- content_object = generic.GenericForeignKey('content_type', 'object_id')
+ BaseMetaClass.to_denormalize.extend(
+ [(new_cls, name, field) for name, field in new_cls.__dict__.items() if isinstance(field, DenormalizedField)]
+ )
- class Meta:
- abstract = True
- app_label = 'forum'
+ return new_cls
-from user import User
+ @classmethod
+ def setup_denormalizes(cls):
+ for new_cls, name, field in BaseMetaClass.to_denormalize:
+ field.setup_class(new_cls, name)
+
+
+class BaseModel(models.Model):
+ __metaclass__ = BaseMetaClass
-class UserContent(models.Model):
- user = models.ForeignKey(User, related_name='%(class)ss')
+ objects = CachedManager()
class Meta:
abstract = True
app_label = 'forum'
+ def __init__(self, *args, **kwargs):
+ super(BaseModel, self).__init__(*args, **kwargs)
+ self.reset_original_state(kwargs.keys())
-marked_deleted = django.dispatch.Signal(providing_args=["instance", "deleted_by"])
+ def reset_original_state(self, reset_fields=None):
+ self._original_state = self._as_dict()
+
+ if reset_fields:
+ self._original_state.update(dict([(f, None) for f in reset_fields]))
-class DeletableContent(models.Model):
- deleted = models.BooleanField(default=False)
- deleted_at = models.DateTimeField(null=True, blank=True)
- deleted_by = models.ForeignKey(User, null=True, blank=True, related_name='deleted_%(class)ss')
+ def get_dirty_fields(self):
+ return [f.name for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]]
- active = UndeletedObjectManager()
+ def _as_dict(self):
+ return dict([(name, getattr(self, name)) for name in
+ ([f.attname for f in self._meta.fields] + [k for k in self.__dict__.keys() if k.endswith('_dencache_')])
+ ])
- class Meta:
- abstract = True
- app_label = 'forum'
+ def _get_update_kwargs(self):
+ return dict([
+ (f.name, getattr(self, f.name)) for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]
+ ])
- def mark_deleted(self, user):
- if not self.deleted:
- self.deleted = True
- self.deleted_at = datetime.datetime.now()
- self.deleted_by = user
- self.save()
- marked_deleted.send(sender=self.__class__, instance=self, deleted_by=user)
- return True
- else:
- return False
+ def save(self, full_save=False, *args, **kwargs):
+ put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]
- def unmark_deleted(self):
- if self.deleted:
- self.deleted = False
- self.save()
- return True
+ if self.id and not full_save:
+ self.__class__.objects.filter(id=self.id).update(**self._get_update_kwargs())
else:
- return False
+ super(BaseModel, self).save()
+ if put_back:
+ try:
+ self.__dict__.update(
+ self.__class__.objects.filter(id=self.id).values(*put_back)[0]
+ )
+ except:
+ logging.error("Unable to read %s from %s" % (", ".join(put_back), self.__class__.__name__))
+ self.uncache()
-class ContentRevision(models.Model):
- """
- Base class for QuestionRevision and AnswerRevision
- """
- revision = models.PositiveIntegerField()
- author = models.ForeignKey(User, related_name='%(class)ss')
- revised_at = models.DateTimeField()
- summary = models.CharField(max_length=300, blank=True)
- text = models.TextField()
-
- class Meta:
- abstract = True
- app_label = 'forum'
+ self.reset_original_state()
+ self.cache()
+ @classmethod
+ def _generate_cache_key(cls, key, group=None):
+ if group is None:
+ group = cls.__name__
-class AnonymousContent(models.Model):
- """
- Base class for AnonymousQuestion and AnonymousAnswer
- """
- session_key = models.CharField(max_length=40) #session id for anonymous questions
- wiki = models.BooleanField(default=False)
- added_at = models.DateTimeField(default=datetime.datetime.now)
- ip_addr = models.IPAddressField(max_length=21) #allow high port numbers
- author = models.ForeignKey(User,null=True)
- text = models.TextField()
- summary = models.CharField(max_length=180)
+ return '%s:%s:%s' % (settings.APP_URL, group, key)
- class Meta:
- abstract = True
- app_label = 'forum'
+ def cache_key(self):
+ return self._generate_cache_key(self.id)
+ @classmethod
+ def infer_cache_key(cls, querydict):
+ try:
+ 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]
-from meta import Comment, Vote, FlaggedItem
-from user import activity_record
+ return cls._generate_cache_key(pk)
+ except:
+ return None
-class Content(BaseModel, DeletableContent):
- """
- Base class for Question and Answer
- """
- author = models.ForeignKey(User, related_name='%(class)ss')
- added_at = models.DateTimeField(default=datetime.datetime.now)
+ def cache(self):
+ cache.set(self.cache_key(), self._as_dict(), 60 * 60)
- wiki = models.BooleanField(default=False)
- wikified_at = models.DateTimeField(null=True, blank=True)
+ def uncache(self):
+ cache.delete(self.cache_key())
- #locked = models.BooleanField(default=False)
- #locked_by = models.ForeignKey(User, null=True, blank=True, related_name='locked_%(class)ss')
- #locked_at = models.DateTimeField(null=True, blank=True)
+ def delete(self):
+ self.uncache()
+ super(BaseModel, self).delete()
- score = models.IntegerField(default=0)
- vote_up_count = models.IntegerField(default=0)
- vote_down_count = models.IntegerField(default=0)
- comment_count = models.PositiveIntegerField(default=0)
- offensive_flag_count = models.SmallIntegerField(default=0)
+from user import User
+from node import Node, NodeRevision, NodeManager
+from action import Action
- last_edited_at = models.DateTimeField(null=True, blank=True)
- last_edited_by = models.ForeignKey(User, null=True, blank=True, related_name='last_edited_%(class)ss')
- html = models.TextField()
- comments = generic.GenericRelation(Comment)
- votes = generic.GenericRelation(Vote)
- flagged_items = generic.GenericRelation(FlaggedItem)
- class Meta:
- abstract = True
- app_label = 'forum'
- def save(self, *args, **kwargs):
- self.__dict__['score'] = self.__dict__['vote_up_count'] - self.__dict__['vote_down_count']
- super(Content,self).save(*args, **kwargs)
- try:
- ping_google()
- except Exception:
- logging.debug('problem pinging google did you register you sitemap with google?')
-
-
- def post_get_last_update_info(self):
- when = self.added_at
- who = self.author
- if self.last_edited_at and self.last_edited_at > when:
- when = self.last_edited_at
- who = self.last_edited_by
- comments = self.comments.all()
- if len(comments) > 0:
- for c in comments:
- if c.added_at > when:
- when = c.added_at
- who = c.user
- return when, who
\ No newline at end of file