]> git.openstreetmap.org Git - osqa.git/blob - forum/models/base.py
ALteration of the schema to a single content model. As a bonus there is a complete...
[osqa.git] / forum / models / base.py
1 import datetime
2 import hashlib
3 from urllib import quote_plus, urlencode
4 from django.db import models, IntegrityError, connection, transaction
5 from django.utils.http import urlquote  as django_urlquote
6 from django.utils.html import strip_tags
7 from django.core.urlresolvers import reverse
8 from django.contrib.contenttypes import generic
9 from django.contrib.contenttypes.models import ContentType
10 from django.core.cache import cache
11 from django.template.defaultfilters import slugify
12 from django.db.models.signals import post_delete, post_save, pre_save, pre_delete
13 from django.utils.translation import ugettext as _
14 from django.utils.safestring import mark_safe
15 from django.contrib.sitemaps import ping_google
16 import django.dispatch
17 from django.conf import settings
18 from forum import const
19 import logging
20
21 from forum.const import *
22
23 class CachedManager(models.Manager):
24     use_for_related_fields = True
25
26     def get(self, *args, **kwargs):
27         try:
28             pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact') or k.endswith('_ptr__pk')][0]
29         except:
30             pk = None
31
32         if pk is not None:
33             key = self.model.cache_key(pk)
34             obj = cache.get(key)
35
36             if obj is None:
37                 obj = super(CachedManager, self).get(*args, **kwargs)
38                 cache.set(key, obj, 60 * 60)
39
40             return obj
41         
42         return super(CachedManager, self).get(*args, **kwargs)
43
44     def get_or_create(self, *args, **kwargs):
45         try:
46             return self.get(*args, **kwargs)
47         except:
48             return super(CachedManager, self).get_or_create(*args, **kwargs)
49
50
51 class BaseModel(models.Model):
52     objects = CachedManager()
53
54     class Meta:
55         abstract = True
56         app_label = 'forum'
57
58     def __init__(self, *args, **kwargs):
59         super(BaseModel, self).__init__(*args, **kwargs)
60         self._original_state = dict([(k, v) for k,v in self.__dict__.items() if not k in kwargs])
61
62     @classmethod
63     def cache_key(cls, pk):
64         return '%s.%s:%s' % (settings.APP_URL, cls.__name__, pk)
65
66     def get_dirty_fields(self):
67         missing = object()
68         return dict([(k, self._original_state.get(k, None)) for k,v in self.__dict__.items()
69                  if self._original_state.get(k, missing) == missing or self._original_state[k] != v])
70
71     def save(self, *args, **kwargs):
72         super(BaseModel, self).save(*args, **kwargs)
73         self._original_state = dict(self.__dict__)
74         cache.set(self.cache_key(self.pk), self, 86400)
75
76     def delete(self):
77         cache.delete(self.cache_key(self.pk))
78         super(BaseModel, self).delete()
79
80
81 class ActiveObjectManager(models.Manager):
82     def get_query_set(self):
83         return super(ActiveObjectManager, self).get_query_set().filter(canceled=False)
84
85 class UndeletedObjectManager(models.Manager):
86     def get_query_set(self):
87         return super(UndeletedObjectManager, self).get_query_set().filter(deleted=False)
88
89 class GenericContent(BaseModel):
90     """
91         Base class for Vote, Comment and FlaggedItem
92     """
93     content_type   = models.ForeignKey(ContentType)
94     object_id      = models.PositiveIntegerField()
95     content_object = generic.GenericForeignKey('content_type', 'object_id')
96
97     class Meta:
98         abstract = True
99         app_label = 'forum'
100
101 class MetaContent(BaseModel):
102     node = models.ForeignKey('Node', null=True, related_name='%(class)ss')
103
104     def __init__(self, *args, **kwargs):
105         if 'content_object' in kwargs:
106             kwargs['node'] = kwargs['content_object']
107             del kwargs['content_object']
108
109         super (MetaContent, self).__init__(*args, **kwargs)
110     
111     @property
112     def content_object(self):
113         return self.node.leaf
114
115     class Meta:
116         abstract = True
117         app_label = 'forum'
118
119 from user import User
120
121 class UserContent(models.Model):
122     user = models.ForeignKey(User, related_name='%(class)ss')
123
124     class Meta:
125         abstract = True
126         app_label = 'forum'
127
128
129 marked_deleted = django.dispatch.Signal(providing_args=["instance", "deleted_by"])
130
131 class DeletableContent(models.Model):
132     deleted     = models.BooleanField(default=False)
133     deleted_at  = models.DateTimeField(null=True, blank=True)
134     deleted_by  = models.ForeignKey(User, null=True, blank=True, related_name='deleted_%(class)ss')
135
136     active = UndeletedObjectManager()
137
138     class Meta:
139         abstract = True
140         app_label = 'forum'
141
142     def mark_deleted(self, user):
143         if not self.deleted:
144             self.deleted = True
145             self.deleted_at = datetime.datetime.now()
146             self.deleted_by = user
147             self.save()
148             marked_deleted.send(sender=self.__class__, instance=self, deleted_by=user)
149             return True
150         else:
151             return False
152
153     def unmark_deleted(self):
154         if self.deleted:
155             self.deleted = False
156             self.save()
157             return True
158         else:
159             return False
160
161
162 from meta import Comment, Vote, FlaggedItem
163 from node import Node, NodeRevision
164
165 class QandA(Node):
166     wiki                 = models.BooleanField(default=False)
167     wikified_at          = models.DateTimeField(null=True, blank=True)
168
169     class Meta:
170         abstract = True
171         app_label = 'forum'
172
173     def wikify(self):
174         if not self.wiki:
175             self.wiki = True
176             self.wikified_at = datetime.datetime.now()
177             self.save()
178
179     def save(self, *args, **kwargs):
180         self.__dict__['score'] = self.__dict__['vote_up_count'] - self.__dict__['vote_down_count']
181         super(QandA,self).save(*args, **kwargs)
182
183         try:
184             ping_google()
185         except Exception:
186             logging.debug('problem pinging google did you register you sitemap with google?')
187
188
189