Ticket #16524: comments-add-queryset-methods.diff
File comments-add-queryset-methods.diff, 3.6 KB (added by , 13 years ago) |
---|
-
managers.py
1 1 from django.db import models 2 from django.conf import settings 2 3 from django.contrib.contenttypes.models import ContentType 3 4 from django.utils.encoding import force_unicode 4 5 5 class CommentManager(models.Manager): 6 6 class CommentQuerySet(models.query.QuerySet): 7 7 def in_moderation(self): 8 8 """ 9 9 QuerySet for all comments currently in the moderation queue. 10 10 """ 11 return self.get_query_set().filter(is_public=False, is_removed=False) 12 11 return self.filter(is_public=False, is_removed=False) 12 13 def not_in_moderation(self): 14 """ 15 QuerySet for all comments currently not in the moderation queue, i.e., 16 all the comments that are publicly viewable. 17 """ 18 # The is_public and is_removed fields are implementation details of the 19 # built-in comment model's spam filtering system, so they might not 20 # be present on a custom comment model subclass. If they exist, we 21 # should filter on them. 22 field_names = [f.name for f in self.model._meta.fields] 23 if 'is_public' in field_names: 24 qs = self.filter(is_public=True) 25 if getattr(settings, 'COMMENTS_HIDE_REMOVED', True) and 'is_removed' in field_names: 26 qs = qs.filter(is_removed=False) 27 return qs 28 13 29 def for_model(self, model): 14 30 """ 15 31 QuerySet for all comments for a particular model (either an instance or 16 32 a class). 17 33 """ 18 34 ct = ContentType.objects.get_for_model(model) 19 qs = self. get_query_set().filter(content_type=ct)35 qs = self.filter(content_type=ct) 20 36 if isinstance(model, models.Model): 21 37 qs = qs.filter(object_pk=force_unicode(model._get_pk_val())) 22 38 return qs 39 40 class CommentManager(models.Manager): 41 42 def get_query_set(self): 43 return CommentQuerySet(self.model) 44 45 def in_moderation(self): 46 """ 47 QuerySet for all comments currently in the moderation queue. 48 """ 49 return self.get_query_set().in_moderation() 50 51 def not_in_moderation(self): 52 """ 53 QuerySet for all comments currently not in the moderation queue, i.e., 54 all the comments that are publicly viewable. 55 """ 56 return self.get_query_set().not_in_moderation() 57 58 def for_model(self, model): 59 """ 60 QuerySet for all comments for a particular model (either an instance or 61 a class). 62 """ 63 return self.get_query_set().for_model(model) 64 -
templatetags/comments.py
79 79 site__pk = settings.SITE_ID, 80 80 ) 81 81 82 # The is_public and is_removed fields are implementation details of the 83 # built-in comment model's spam filtering system, so they might not 84 # be present on a custom comment model subclass. If they exist, we 85 # should filter on them. 86 field_names = [f.name for f in self.comment_model._meta.fields] 87 if 'is_public' in field_names: 88 qs = qs.filter(is_public=True) 89 if getattr(settings, 'COMMENTS_HIDE_REMOVED', True) and 'is_removed' in field_names: 90 qs = qs.filter(is_removed=False) 82 return qs.not_in_moderation() 91 83 92 return qs93 94 84 def get_target_ctype_pk(self, context): 95 85 if self.object_expr: 96 86 try: