models.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import unicode_literals
  2. from future.builtins import str
  3. from django.db import models
  4. from django.core.urlresolvers import reverse
  5. from django.utils.translation import ugettext_lazy as _
  6. from mezzanine.conf import settings
  7. from mezzanine.core.fields import FileField
  8. from mezzanine.core.models import Displayable, Ownable, RichText, Slugged
  9. from mezzanine.generic.fields import CommentsField, RatingField
  10. from mezzanine.utils.models import AdminThumbMixin, upload_to
  11. class BlogPost(Displayable, Ownable, RichText, AdminThumbMixin):
  12. """
  13. A blog post.
  14. """
  15. categories = models.ManyToManyField("BlogCategory",
  16. verbose_name=_("Categories"),
  17. blank=True, related_name="blogposts")
  18. allow_comments = models.BooleanField(verbose_name=_("Allow comments"),
  19. default=True)
  20. comments = CommentsField(verbose_name=_("Comments"))
  21. rating = RatingField(verbose_name=_("Rating"))
  22. featured_image = FileField(verbose_name=_("Featured Image"),
  23. upload_to=upload_to("blog.BlogPost.featured_image", "blog"),
  24. format="Image", max_length=255, null=True, blank=True)
  25. related_posts = models.ManyToManyField("self",
  26. verbose_name=_("Related posts"), blank=True)
  27. admin_thumb_field = "featured_image"
  28. class Meta:
  29. verbose_name = _("Blog post")
  30. verbose_name_plural = _("Blog posts")
  31. ordering = ("-publish_date",)
  32. def get_absolute_url(self):
  33. """
  34. URLs for blog posts can either be just their slug, or prefixed
  35. with a portion of the post's publish date, controlled by the
  36. setting ``BLOG_URLS_DATE_FORMAT``, which can contain the value
  37. ``year``, ``month``, or ``day``. Each of these maps to the name
  38. of the corresponding urlpattern, and if defined, we loop through
  39. each of these and build up the kwargs for the correct urlpattern.
  40. The order which we loop through them is important, since the
  41. order goes from least granular (just year) to most granular
  42. (year/month/day).
  43. """
  44. url_name = "blog_post_detail"
  45. kwargs = {"slug": self.slug}
  46. date_parts = ("year", "month", "day")
  47. if settings.BLOG_URLS_DATE_FORMAT in date_parts:
  48. url_name = "blog_post_detail_%s" % settings.BLOG_URLS_DATE_FORMAT
  49. for date_part in date_parts:
  50. date_value = str(getattr(self.publish_date, date_part))
  51. if len(date_value) == 1:
  52. date_value = "0%s" % date_value
  53. kwargs[date_part] = date_value
  54. if date_part == settings.BLOG_URLS_DATE_FORMAT:
  55. break
  56. return reverse(url_name, kwargs=kwargs)
  57. class BlogCategory(Slugged):
  58. """
  59. A category for grouping blog posts into a series.
  60. """
  61. class Meta:
  62. verbose_name = _("Blog Category")
  63. verbose_name_plural = _("Blog Categories")
  64. ordering = ("title",)
  65. @models.permalink
  66. def get_absolute_url(self):
  67. return ("blog_post_list_category", (), {"category": self.slug})