models.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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(_('Advertiser'), blank=True, null=True, max_length=140)
  15. ADVERT_FIELD=models.ImageField(_('Advertisment'), blank=True, null=True, upload_to='historical/ads/')
  16. class PublishModel(TimeStampedModel):
  17. published=models.BooleanField(_('Published'), default=False)
  18. published_on=models.DateTimeField(_('Published on'), default=datetime.now())
  19. objects = PublishedManager()
  20. class Meta:
  21. abstract=True
  22. class Group(PublishModel):
  23. '''Group model.
  24. Holds historical objects for organizational purposes.'''
  25. title=models.CharField(_('Title'), max_length=140)
  26. slug=models.SlugField(_('Slug'), unique=True)
  27. graphic=models.ImageField(_('Graphic'), upload_to='historical/group/', blank=True, null=True)
  28. description=models.TextField(_('Description'), blank=True, null=True)
  29. sites=models.ManyToManyField(Site)
  30. template_name = models.CharField(_('template name'), max_length=200, blank=True, help_text=_("Example: 'historical/group/special_history_section.html'. If this isn't provided, the system will use 'historical/group/default.html'."))
  31. created_by=models.ForeignKey(User, editable=False)
  32. class Meta:
  33. ordering=('title',)
  34. verbose_name = _('Group')
  35. verbose_name_plural = _('Groups')
  36. def __unicode__(self):
  37. return u'%s' % (self.title)
  38. @models.permalink
  39. def get_absolute_url(self):
  40. return ('hi-group-detail', None, {'slug': self.slug})
  41. class HistoricArticle(PublishModel):
  42. '''Historic Article model.
  43. An historic article, belongs to a group and holds a story and an advertiser.'''
  44. story=models.ForeignKey(Story)
  45. group=models.ForeignKey(Group)
  46. edition=models.ForeignKey(WebEdition, blank=True, null=True)
  47. description=models.TextField(_('Description'), blank=True, null=True)
  48. #advertisment=models.ImageField(_('Ad'), blank=True, null=True, upload_to='historical/ads/')
  49. advert=ADVERT_FIELD
  50. advertiser=ADVERTISER_FIELD
  51. view_count = models.PositiveIntegerField(default=0, editable=False)
  52. created_by=models.ForeignKey(User, editable=False)
  53. class Meta:
  54. ordering=('published_on',)
  55. verbose_name = _('Historic article')
  56. verbose_name_plural = _('Historic articles')
  57. def __unicode__(self):
  58. return u'%s' % self.story.web_hed
  59. @models.permalink
  60. def get_absolute_url(self, site=''):
  61. return ('hi-article-detail', (), {
  62. 'group_slug': self.group.slug,
  63. 'year': self.story.published_on.year,
  64. 'month': self.story.published_on.strftime('%b').lower(),
  65. 'day': self.story.published_on.day,
  66. 'slug': self.story.slug})