models.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from datetime import datetime
  2. from django.db import models
  3. from django.utils.translation import ugettext_lazy as _
  4. from django.contrib.sites.models import Site
  5. from django.contrib.auth.models import User
  6. from django_extensions.db.models import TimeStampedModel
  7. from historical.managers import PublishedManager
  8. from newsroom.models import Story, WebEdition
  9. try:
  10. from adzone.models import Advertiser, BannerAd
  11. ADVERTISER_FIELD = models.ForeignKey(Advertiser, blank=True, null=True)
  12. ADVERT_FIELD = models.ForeignKey(BannerAd, blank=True, null=True)
  13. except:
  14. ADVERTISER_FIELD = models.CharField(
  15. _("Advertiser"), blank=True, null=True, max_length=140
  16. )
  17. ADVERT_FIELD = models.ImageField(
  18. _("Advertisment"), blank=True, null=True, upload_to="historical/ads/"
  19. )
  20. class PublishModel(TimeStampedModel):
  21. published = models.BooleanField(_("Published"), default=False)
  22. published_on = models.DateTimeField(_("Published on"), default=datetime.now())
  23. objects = PublishedManager()
  24. class Meta:
  25. abstract = True
  26. class Group(PublishModel):
  27. """Group model.
  28. Holds historical objects for organizational purposes."""
  29. title = models.CharField(_("Title"), max_length=140)
  30. slug = models.SlugField(_("Slug"), unique=True)
  31. graphic = models.ImageField(
  32. _("Graphic"), upload_to="historical/group/", blank=True, null=True
  33. )
  34. description = models.TextField(_("Description"), blank=True, null=True)
  35. sites = models.ManyToManyField(Site)
  36. template_name = models.CharField(
  37. _("template name"),
  38. max_length=200,
  39. blank=True,
  40. help_text=_(
  41. "Example: 'historical/group/special_history_section.html'. If this isn't provided, the system will use 'historical/group/default.html'."
  42. ),
  43. )
  44. created_by = models.ForeignKey(User, editable=False)
  45. class Meta:
  46. ordering = ("title",)
  47. verbose_name = _("Group")
  48. verbose_name_plural = _("Groups")
  49. def __unicode__(self):
  50. return u"%s" % (self.title)
  51. @models.permalink
  52. def get_absolute_url(self):
  53. return ("hi-group-detail", None, {"slug": self.slug})
  54. class HistoricArticle(PublishModel):
  55. """Historic Article model.
  56. An historic article, belongs to a group and holds a story and an advertiser."""
  57. story = models.ForeignKey(Story)
  58. group = models.ForeignKey(Group)
  59. edition = models.ForeignKey(WebEdition, blank=True, null=True)
  60. description = models.TextField(_("Description"), blank=True, null=True)
  61. # advertisment=models.ImageField(_('Ad'), blank=True, null=True, upload_to='historical/ads/')
  62. advert = ADVERT_FIELD
  63. advertiser = ADVERTISER_FIELD
  64. view_count = models.PositiveIntegerField(default=0, editable=False)
  65. created_by = models.ForeignKey(User, editable=False)
  66. class Meta:
  67. ordering = ("published_on",)
  68. verbose_name = _("Historic article")
  69. verbose_name_plural = _("Historic articles")
  70. def __unicode__(self):
  71. return u"%s" % self.story.web_hed
  72. @models.permalink
  73. def get_absolute_url(self, site=""):
  74. return (
  75. "hi-article-detail",
  76. (),
  77. {
  78. "group_slug": self.group.slug,
  79. "year": self.story.published_on.year,
  80. "month": self.story.published_on.strftime("%b").lower(),
  81. "day": self.story.published_on.day,
  82. "slug": self.story.slug,
  83. },
  84. )