/blogmaker/comments/models.py
http://blogmaker.googlecode.com/ · Python · 146 lines · 102 code · 25 blank · 19 comment · 4 complexity · 6249a8205f371ed0a3e9ba8d1fb020f6 MD5 · raw file
- ''' Comments model.
- Copyright (c) 2006-2007, PreFab Software Inc.
- Copyright (c) 2005, the Lawrence Journal-World
- All rights reserved.
- '''
- from django.db import models
- from django.contrib.contenttypes.models import ContentType
- from django.contrib.sites.models import Site
- from django.utils.translation import gettext_lazy as _
- from blogmaker.util import strip_domain
- class CommentManager(models.Manager):
- def comments_for_object(self, site_id, app_label, model, obj_id, comment_type='comment'):
- ''' Get a QuerySet of all public comments for the specified object. '''
- kwargs = {
- 'site__id__exact': site_id,
- 'content_type__app_label__exact': app_label,
- 'content_type__model__exact': model,
- 'object_id__exact': obj_id,
- 'comment_type__exact': comment_type,
- 'is_public__exact': True,
- }
- return self.filter(**kwargs)
-
-
- class Comment(models.Model):
- ''' Data model for both comments and trackbacks '''
- objects = CommentManager()
-
- content_type = models.ForeignKey(ContentType)
- object_id = models.IntegerField(_('object ID'))
- comment = models.TextField(_('comment'), maxlength=3000)
- submit_date = models.DateTimeField(_('date/time submitted'), auto_now_add=True)
- is_public = models.BooleanField(_('is public'))
- ip_address = models.IPAddressField(_('ip address'))
- site = models.ForeignKey(Site)
-
- typeChoices = (
- ('comment', 'Comment'),
- ('trackback', 'Trackback'),
- )
- comment_type = models.CharField(maxlength=10, blank=False, choices=typeChoices, default='comment')
-
- statusChoices = (
- ('new', 'new'),
- ('tbd', 'TBD'),
- ('pub', 'published'),
- ('co_tbd', 'company TBD'),
- ('co_sent', 'company SENT'),
- ('boring', 'no, boring'),
- ('self', 'no, self-promotion'),
- ('scr_spam', 'spam per script'),
- ('spam', 'spam'),
- )
- status = models.CharField(maxlength=10, blank=False, choices=statusChoices, default='new')
- notes = models.TextField(_('notes'), blank=True)
-
- # These fields are used only for comments
- person_name = models.CharField(_("commenter"), maxlength=50, blank=True)
- person_email = models.EmailField(_('e-mail address'), blank=True)
- person_www = models.URLField("person's URL", verify_exists=False, blank=True)
-
- # These fields are used only for trackbacks
- trackback_name = models.CharField(_("blog name"), maxlength=255, blank=True)
- trackback_title = models.CharField(_('trackback name'), maxlength=255, blank=True)
- trackback_www = models.URLField('entry URL', verify_exists=False, blank=True)
-
-
- class Meta:
- ordering = ('-submit_date',)
-
- class Admin:
- fields = (
- (None, {'fields': (('content_type', 'object_id', 'site'), 'comment')}),
- ('Moderation/Notes', {'fields': (('is_public', 'status'), 'notes')}),
- ('Comment from', {'fields': ('person_name', 'person_email', 'person_www')}),
- ('Trackback from', {'fields': ('trackback_name', 'trackback_title', 'trackback_www')}),
- ('Meta', {'fields': ('submit_date', 'ip_address', 'comment_type')}),
- )
- list_display = ('person_name', 'person_www_stripped', 'trackback_title', 'linkToAdminItem', 'linkToPublicItem', 'submit_date', 'content_type', 'is_public', 'status')
- list_display_links = ('person_name', 'trackback_title')
- list_filter = ('submit_date', 'comment_type', 'is_public', 'status')
- date_hierarchy = 'submit_date'
- search_fields = ('comment', 'person_name', 'trackback_title', 'trackback_name', 'person_email')
- def __repr__(self):
- return "%s: %s..." % (self.person_name, self.comment[:100])
- def get_absolute_url(self):
- return self.get_content_object().get_absolute_url() + "#c" + str(self.id)
-
- def person_www_stripped(self):
- ''' For the admin list view '''
- return strip_domain(self.person_www)
-
- person_www_stripped.short_description = 'person URL'
- def linkToAdminItem(self):
- ''' A link to the corresponding content object's admin interface
- '''
- content = self.get_content_object()
- if not content:
- return ' '
-
- contentUrl = "/admin/%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, self.object_id)
- return '''<a href=%s>»</a>''' %contentUrl
-
- # Configure linkToAdminItem for the Admin app
- linkToAdminItem.short_description = 'item'
- linkToAdminItem.allow_tags = True
-
-
- def linkToPublicItem(self):
- ''' A link to the content object's public page
- '''
- content = self.get_content_object()
- if not content:
- return ' '
-
- try:
- return '''<a href="%s">%s</a>''' %(self.get_content_object().get_absolute_url(), content)
- except:
- return content
-
- # Configure linkToAdminItem for the Admin app
- linkToPublicItem.short_description = 'item public page'
- linkToPublicItem.allow_tags = True
-
-
- def get_content_object(self):
- """
- Returns the object that this comment is a comment on. Returns None if
- the object no longer exists.
- """
- from django.core.exceptions import ObjectDoesNotExist
- try:
- return self.content_type.get_object_for_this_type(pk=self.object_id)
- except ObjectDoesNotExist:
- return None
- get_content_object.short_description = _('Content object')