PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/blogmaker/comments/models.py

http://blogmaker.googlecode.com/
Python | 146 lines | 109 code | 20 blank | 17 comment | 0 complexity | 6249a8205f371ed0a3e9ba8d1fb020f6 MD5 | raw file
  1''' Comments model.
  2Copyright (c) 2006-2007, PreFab Software Inc.
  3
  4Copyright (c) 2005, the Lawrence Journal-World
  5All rights reserved.
  6'''
  7
  8
  9from django.db import models
 10from django.contrib.contenttypes.models import ContentType
 11from django.contrib.sites.models import Site
 12from django.utils.translation import gettext_lazy as _
 13
 14from blogmaker.util import strip_domain
 15
 16class CommentManager(models.Manager):
 17    def comments_for_object(self, site_id, app_label, model, obj_id, comment_type='comment'):
 18        ''' Get a QuerySet of all public comments for the specified object. '''
 19        kwargs = {
 20            'site__id__exact': site_id,
 21            'content_type__app_label__exact': app_label,
 22            'content_type__model__exact': model,
 23            'object_id__exact': obj_id,
 24            'comment_type__exact': comment_type,
 25            'is_public__exact': True,
 26        }
 27        return self.filter(**kwargs)
 28        
 29        
 30class Comment(models.Model):
 31    ''' Data model for both comments and trackbacks '''
 32    objects = CommentManager()
 33    
 34    content_type = models.ForeignKey(ContentType)
 35    object_id = models.IntegerField(_('object ID'))
 36    comment = models.TextField(_('comment'), maxlength=3000)
 37    submit_date = models.DateTimeField(_('date/time submitted'), auto_now_add=True)
 38    is_public = models.BooleanField(_('is public'))
 39    ip_address = models.IPAddressField(_('ip address'))
 40
 41    site = models.ForeignKey(Site)
 42    
 43    typeChoices = (
 44        ('comment', 'Comment'),
 45        ('trackback', 'Trackback'),
 46    )
 47    comment_type = models.CharField(maxlength=10, blank=False, choices=typeChoices, default='comment')
 48    
 49    statusChoices = (
 50        ('new', 'new'),
 51        ('tbd', 'TBD'),
 52        ('pub', 'published'),
 53        ('co_tbd', 'company TBD'),
 54        ('co_sent', 'company SENT'),
 55        ('boring', 'no, boring'),
 56        ('self', 'no, self-promotion'),
 57        ('scr_spam', 'spam per script'),
 58        ('spam', 'spam'),
 59    )
 60    status = models.CharField(maxlength=10, blank=False, choices=statusChoices, default='new')
 61    notes = models.TextField(_('notes'), blank=True)
 62    
 63    # These fields are used only for comments
 64    person_name = models.CharField(_("commenter"), maxlength=50, blank=True)
 65    person_email = models.EmailField(_('e-mail address'), blank=True)
 66    person_www = models.URLField("person's URL", verify_exists=False, blank=True)
 67    
 68    # These fields are used only for trackbacks
 69    trackback_name = models.CharField(_("blog name"), maxlength=255, blank=True)
 70    trackback_title = models.CharField(_('trackback name'), maxlength=255, blank=True)
 71    trackback_www = models.URLField('entry URL', verify_exists=False, blank=True)
 72    
 73    
 74    class Meta:
 75        ordering = ('-submit_date',)
 76        
 77    class Admin:
 78        fields = (
 79            (None, {'fields': (('content_type', 'object_id', 'site'), 'comment')}),
 80            ('Moderation/Notes', {'fields': (('is_public', 'status'), 'notes')}),
 81            ('Comment from', {'fields': ('person_name', 'person_email', 'person_www')}),
 82            ('Trackback from', {'fields': ('trackback_name', 'trackback_title', 'trackback_www')}),
 83            ('Meta', {'fields': ('submit_date', 'ip_address', 'comment_type')}),
 84        )
 85        list_display = ('person_name', 'person_www_stripped', 'trackback_title', 'linkToAdminItem', 'linkToPublicItem', 'submit_date', 'content_type', 'is_public', 'status')
 86        list_display_links = ('person_name', 'trackback_title')
 87        list_filter = ('submit_date', 'comment_type', 'is_public', 'status')
 88        date_hierarchy = 'submit_date'
 89        search_fields = ('comment', 'person_name', 'trackback_title', 'trackback_name', 'person_email')
 90
 91    def __repr__(self):
 92        return "%s: %s..." % (self.person_name, self.comment[:100])
 93
 94    def get_absolute_url(self):
 95        return self.get_content_object().get_absolute_url() + "#c" + str(self.id)
 96        
 97    def person_www_stripped(self):
 98        ''' For the admin list view '''
 99        return strip_domain(self.person_www)
100        
101    person_www_stripped.short_description = 'person URL'
102
103    def linkToAdminItem(self):
104        ''' A link to the corresponding content object's admin interface
105        '''
106        content = self.get_content_object()
107        if not content:
108            return ' '
109        
110        contentUrl = "/admin/%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, self.object_id)
111        return '''<a href=%s>&raquo;</a>''' %contentUrl
112        
113    # Configure linkToAdminItem for the Admin app
114    linkToAdminItem.short_description = 'item'
115    linkToAdminItem.allow_tags = True
116    
117    
118    def linkToPublicItem(self):
119        ''' A link to the content object's public page
120        '''
121        content = self.get_content_object()
122        if not content:
123            return '&nbsp;'
124        
125        try:
126            return '''<a href="%s">%s</a>''' %(self.get_content_object().get_absolute_url(), content)
127        except:
128            return content
129        
130    # Configure linkToAdminItem for the Admin app
131    linkToPublicItem.short_description = 'item public page'
132    linkToPublicItem.allow_tags = True
133    
134    
135    def get_content_object(self):
136        """
137        Returns the object that this comment is a comment on. Returns None if
138        the object no longer exists.
139        """
140        from django.core.exceptions import ObjectDoesNotExist
141        try:
142            return self.content_type.get_object_for_this_type(pk=self.object_id)
143        except ObjectDoesNotExist:
144            return None
145
146    get_content_object.short_description = _('Content object')