/blogmaker/comments/models.py

http://blogmaker.googlecode.com/ · Python · 146 lines · 102 code · 25 blank · 19 comment · 4 complexity · 6249a8205f371ed0a3e9ba8d1fb020f6 MD5 · raw file

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