from datetime import datetime, timedelta from taggit.managers import TaggableManager from taggit.models import Tag from django.db import models from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from django.contrib.sites.managers import CurrentSiteManager from django.contrib.auth.models import User, Group from django.conf import settings from newsroom.managers import * from django.utils.safestring import mark_safe from django.contrib.markup.templatetags import markup from django_inlines import inlines from darkroom.inlines import PhotoInline, GraphicInline inlines.registry.register('photo', PhotoInline) inlines.registry.register('graphic', GraphicInline) from darkroom.models import Gallery, Photo, Graphic, Movie, Slideshow from adzone.models import BannerAd, AdCategory from directory.models import Town, Place, Person from documents.models import Document, PDFDocument from remember.models import Announcement, InMemoriam, Service, Remembrance #from historical.models import HistoricArticle from django_extensions.db.models import TimeStampedModel from django_extensions.db.fields import AutoSlugField # Get relative media path try: NEWSROOM_DIR = getattr(settings, 'NEWSROOM_DIR', 'newsroom/') except: NEWSROOM_DIR = 'newsroom/' MARKUP_HTML = 'h' MARKUP_MARKDOWN = 'm' MARKUP_REST = 'r' MARKUP_TEXTILE = 't' MARKUP_OPTIONS = getattr(settings, 'ARTICLE_MARKUP_OPTIONS', ( (MARKUP_MARKDOWN, _('Markdown')), (MARKUP_HTML, _('HTML/Plain Text')), (MARKUP_REST, _('ReStructured Text')), (MARKUP_TEXTILE, _('Textile')) )) MARKUP_DEFAULT = getattr(settings, 'COPY_MARKUP_DEFAULT', MARKUP_MARKDOWN) AUTO_TAG=getattr(settings, 'NEWSROOM_AUTO_TAG', True) class Paper(TimeStampedModel): name=models.CharField(_('name'), max_length=100) towns=models.ManyToManyField(Town) slug=AutoSlugField(_('slug'), populate_from='name', editable=True) description=models.TextField(_('description'), blank=True, null=True) class Meta: verbose_name = _('paper') verbose_name_plural = _('papers') ordering = ('name',) def __unicode__(self): return self.name class Link(TimeStampedModel): url = models.URLField('URL') title = models.CharField(_('title'), max_length=255) description=models.CharField(_('description'), max_length=200, blank=True, null=True) published=models.BooleanField(_('published'), default=False) published_on=models.DateTimeField(_('published on')) class Meta: verbose_name=_('link') verbose_name_plural=_('links') ordering=('created',) get_latest_by='published_on' def __unicode__(self): return self.title @models.permalink def get_absolute_url(self): return ('link_detail', (), {'slug': self.slug}) class AuthorType(models.Model): title=models.CharField(_('name'), max_length=100) slug=AutoSlugField(_('slug'), populate_from='title', editable=True) description=models.TextField(_('description'), blank=True, null=True) class Meta: ordering=('title',) def __unicode__(self): return self.title @models.permalink def get_absolute_url(self): return ('author_type_detail', (), {'slug': self.slug}) class Author(TimeStampedModel): '''Author model.''' name=models.CharField(_('name'), max_length=100) slug=AutoSlugField(_('slug'), populate_from='name', editable=True) fixed_initials=models.CharField(_('initials'), blank=True, null=True, max_length=5) type=models.ForeignKey(AuthorType) title=models.CharField(_('Title'), blank=True, null=True, max_length=100) organization=models.ForeignKey(Place, blank=True, null=True) about=models.TextField(_('about'), blank=True, null=True) user=models.ForeignKey(User, blank=True, null=True) def initials(self): if self.fixed_initials: return self.fixed_initials else: initials='' for i in self.name.split(" "): initials += i[0] return initials def __unicode__(self): return self.name @models.permalink def get_absolute_url(self): return ('nr-author-detail', (), {'type_slug': self.type.slug, 'slug': self.slug}) class Meta: verbose_name=_('author') verbose_name_plural=_('authors') ordering=('name',) get_latest_by='created' class AttachedLink(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.IntegerField(db_index=True) content_object = generic.GenericForeignKey() link=models.ForeignKey(Link) def __unicode__(self): return self.link.title class AttachedGraphic(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.IntegerField(db_index=True) content_object = generic.GenericForeignKey() graphic = models.ForeignKey(Graphic) def __unicode__(self): return self.graphic.title class AttachedMovie(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.IntegerField(db_index=True) content_object = generic.GenericForeignKey() movie=models.ForeignKey(Movie) def __unicode__(self): return self.movie.title class AttachedSlideshow(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.IntegerField(db_index=True) content_object = generic.GenericForeignKey() slideshow=models.ForeignKey(Slideshow) def __unicode__(self): return self.slideshow.title class AttachedDocument(TimeStampedModel): content_type = models.ForeignKey(ContentType) object_id = models.IntegerField(db_index=True) content_object = generic.GenericForeignKey() document=models.ForeignKey(Document) class Meta: ordering = ['-created'] get_latest_by = 'created' def __unicode__(self): return self.document.title def get_absolute_url(self): return self.document.get_absolute_url() def doc_dir(self): return os.path.dirname(self.get_file_filename()) def remove_dirs(self): if os.path.isdir(self.doc_dir()): if os.listdir(self.doc_dir()) == []: os.removedirs(self.doc_dir()) def delete(self): super(Document, self).delete() self.remove_dirs() class AttachedGallery(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.IntegerField(db_index=True) content_object = generic.GenericForeignKey() gallery = models.ForeignKey(Gallery) def __unicode__(self): return self.gallery.title class Dateline(TimeStampedModel): '''Dateline model. A dateline specifies where a story's action takes place. The reason for the separate model is so that the string displayed can be custom, while the towns the dateline refers to are predetermined in the database. Thus a date line of: One Town and Anotehr Town or Our Community, can have specific locations related to them, while being able to be displayed per editorial spec. ''' title=models.CharField(_('title'), max_length=100) slug=AutoSlugField(_('slug'), populate_from='title', editable=True) towns=models.ManyToManyField(Town) class Meta: ordering=('title',) def __unicode__(self): return self.title @models.permalink def get_absolute_url(self): return ('dateline_detail', (), {'slug':self.slug}) MARKUP_HELP = _("""Select the type of markup you are using in this article. """) class StandingHed(TimeStampedModel): '''Standing Hed model. In lieu of a proper column or blog application, this is a hack to get some stories to always be associated with one author or column. ''' title=models.CharField(_('Title'), max_length=100) slug=AutoSlugField(_('Slug'), populate_from='title', editable=True) def __unicode__(self): return self.title class Copy(TimeStampedModel): '''Copy model. An abstract model for anything that can be copy. By default, we have two choices for heds. If a different web_hed isn't specified, we use the one from print. ''' print_hed=models.CharField(_('print headline'), max_length=255) web_hed=models.CharField(_('web headline'), max_length=255) standing_hed=models.ForeignKey(StandingHed, blank=True, null=True) slug=AutoSlugField(_('slug'), populate_from='web_hed', editable=True) kicker=models.CharField(_('kicker'), max_length=255, blank=True, null=True) subhed=models.CharField(_('sub-headline'), max_length=255, blank=True, null=True) body=models.TextField(_('body')) summary=models.TextField(_('summary'), blank=True) markup = models.CharField(max_length=1, choices=MARKUP_OPTIONS, default=MARKUP_DEFAULT, help_text=MARKUP_HELP) published=models.BooleanField(_('publish'), default=False) published_on=models.DateTimeField(_('publish date'), default=datetime.now()) paper_editions = models.ManyToManyField('PaperEdition', blank=True, null=True) sites=models.ManyToManyField(Site) view_count = models.PositiveIntegerField(default=0, editable=False) created_by=models.ForeignKey(User, editable=False) auto_tag=models.BooleanField(_('auto-tag'), default=True) tags = TaggableManager(blank=True) class Meta: abstract=True ordering=('-published_on', 'print_hed') get_latest_by='published_on' @property def hed(self): if self.web_hed: return self.web_hed else: return self.print_hed @property def paragraphs(self): """ Return the paragraphs as a list """ import re if self.markup=='m': import markdown html=markdown.markdown(self.body) elif self.markup=='t': import textile html=textile.textile(self.body) elif self.markup=='r': html='Cant figure out how to implement RST yet...' else: html= self.body return re.findall("(.+?

)", html, re.I | re.S) def do_auto_tag(self): """ Performs the auto-tagging work if necessary. Returns True if an additional save is required, False otherwise. """ found = False if self.auto_tag: import re # don't clobber any existing tags! existing_ids = [t.id for t in self.tags.all()] unused = Tag.objects.all() unused = unused.exclude(id__in=existing_ids) for tag in unused: regex = re.compile(r'\b%s\b' % tag.name, re.I) if regex.search(self.body) or regex.search(self.web_hed) or regex.search(self.print_hed)\ or regex.search(self.subhed) or regex.search(self.kicker): self.tags.add(tag) found = True return found def do_scrub_rich_text(self): scrubbed=False if self.markup == 'h': import re from django.template.defaultfilters import removetags self.body = re.sub('\n', ' ', re.sub('style=".*?"', '', re.sub('align=".*?"', '', removetags(self.body, "font br")))) scrubbed=True return scrubbed def do_set_summary(self): summarized=False if (len(self.paragraphs[0].split(' ')) > 100) or (len(self.paragraphs) == 1): self.summary = self.paragraphs[0] summarized=True elif len(self.paragraphs)==0: summarized=False else: self.summary = self.paragraphs[0] + self.paragraphs[1] summarized=True return summarized def save(self, *args, **kwargs): self.do_set_summary() super(TimeStampedModel, self).save(*args, **kwargs) self.do_auto_tag() self.do_scrub_rich_text() super(TimeStampedModel, self).save(*args, **kwargs) class Story(Copy): '''Story model. A story is one of our primary types of content. ''' STATUS_CHOICES=( (0, 'None'), (1, 'Primary'), (2, 'Secondary'), ) dateline=models.ForeignKey(Dateline) authors=models.ManyToManyField(Author, blank=True, null=True) lead_photo=models.ForeignKey(Photo, blank=True, null=True) cropped_photo=models.ImageField(_('cropped photo'), blank=True, null=True,upload_to='newsroom/cropped_photos/') status=models.IntegerField(_('status'), choices=STATUS_CHOICES, default=0) towns=models.ManyToManyField(Town) weight=models.IntegerField(_('Weight'), null=True, blank=True, default=0, help_text='Stories weighted higher appear before those with lower weights.') press_release=models.BooleanField(_('press release'), default=False) source=models.ForeignKey(Place, blank=True, null=True, help_text="Source of original copy if is a press release.") attached_documents=generic.GenericRelation(AttachedDocument) attached_gallery=generic.GenericRelation(AttachedGallery) attached_graphic=generic.GenericRelation(AttachedGraphic) attached_movie=generic.GenericRelation(AttachedMovie) attached_slideshow=generic.GenericRelation(AttachedSlideshow) # Custom story manager (see manager.py) objects = StoryManager() on_site = CurrentSiteManager('sites') class Meta: verbose_name_plural=('stories') ordering=('-published_on', 'web_hed', 'print_hed') get_latest_by='published_on' def prod_slug(self): authors='' for a in self.authors.all(): authors = a.initials() if authors: return u'%s-%s' % (authors, self.slug) else: return u'PR-%s' % (self.slug) def __unicode__(self): return self.hed def editions(self): list = "" for e in self.edition_set.all(): list += e.paper.name +" " + e.published_on.strftime("%m-%d-%y") + "; " return list @property def author(self): #link='%s' string='%s' #authors=[link % (a.get_absolute_url(), a, a.name) for a in self.authors.all()] authors = [string % (a.name) for a in self.authors.all()] if len(authors) > 1: author_string="%s and %s" % (", ".join(authors[:-1]), authors[-1]) if len(authors) == 1: author_string=authors[0] else: author_string = '' return mark_safe(author_string) @models.permalink def get_absolute_url(self, site=''): return ('nr-story-detail', (), { 'year': self.published_on.year, 'month': self.published_on.strftime('%b').lower(), 'day': self.published_on.day, 'slug': self.slug}) def get_admin_url(self): return u'admin/newsroom/story/%s/' % (str(self.id)) def get_relative_url(self): return u'news/%s/%s/%s/%s' % (self.published_on.year, self.published_on.strftime('%b').lower(), self.published_on.day, self.slug) class EditorialType(models.Model): '''Editorial Type model. A type of editorial... ''' name= models.CharField(_('name'), max_length=100) slug=AutoSlugField(_('slug'), populate_from='name', editable=True) description=models.TextField(_('description'), blank=True, null=True) def __unicode__(self): return self.name @models.permalink def get_absolute_url(self): return ('editorial_type_detail', (), {'slug':self.slug}) class Editorial(Copy): '''Editorial model. Manages editorial content: letter to the editor, editor's notes, corrections. ''' dateline=models.ForeignKey(Dateline, blank=True, null=True) type = models.ForeignKey(EditorialType) author=models.ForeignKey(Author, blank=True, null=True) citizen_authors=models.ManyToManyField(Person, blank=True, null=True) towns=models.ManyToManyField(Town) # Custom story manager (see manager.py) objects = EditorialManager() on_site = CurrentSiteManager('sites') class Meta: verbose_name_plural=('editorials') ordering=('-published_on', 'web_hed', 'print_hed') get_latest_by='published_on' def __unicode__(self): return self.web_hed def editions(self): list = "" for e in self.edition_set.all(): list += e.paper.name +" " + e.published_on.strftime("%m-%d-%y") + "; " return list @models.permalink def get_absolute_url(self, site=''): return ('nr-editorial-detail', (), { 'year': self.published_on.year, 'month': self.published_on.strftime('%b').lower(), 'day': self.published_on.day, 'slug': self.slug}) def get_relative_url(self): return u'%s/%s/%s/%s' % (self.published_on.year, self.published_on.strftime('%b').lower(), self.published_on.day, self.slug) class PaperEdition(TimeStampedModel): paper=models.ForeignKey(Paper) published_on=models.DateField(_('publish on')) published=models.BooleanField(_('published'), default=False) site=models.ManyToManyField(Site) class Meta: verbose_name=_('Paper edition') verbose_name_plural=_('Paper editions') ordering=('-published_on', 'paper') get_latest_by='published_on' def __unicode__(self): return u'%s published %s' % (self.paper, self.published_on) class WebEdition(TimeStampedModel): paper = models.ForeignKey(Paper) published_on = models.DateField(_('publish on')) published = models.BooleanField(_('published'), default=False) site = models.ForeignKey(Site) featured_photo = models.ForeignKey(Photo, null=True, blank=True, related_name="featured_photo") featured_story = models.ForeignKey(Story, related_name="featured_story", null=True, blank=True) stories = models.ManyToManyField( Story, blank=True, null=True, help_text="Include the featured story in this list of all stories in this edition.", limit_choices_to={'published_on__gte': datetime.today()-timedelta(days = 45)}, ) photos = models.ManyToManyField( Photo, blank=True, null=True, related_name="photos", limit_choices_to={'published_on__gte': datetime.today()-timedelta(days = 45)}, ) galleries = models.ManyToManyField( Gallery, blank=True, null=True, limit_choices_to={'published_on__gte': datetime.today()-timedelta(days = 45)}, ) movies = models.ManyToManyField(Movie, blank=True, null=True) slideshows = models.ManyToManyField(Slideshow, blank=True, null=True) editorials = models.ManyToManyField(Editorial, blank=True, null=True) pdfdocuments = models.ManyToManyField(PDFDocument, blank=True, null=True) remembrances = models.ManyToManyField( Remembrance, blank=True, null=True, limit_choices_to={'published_on__gte': datetime.today()-timedelta(days = 45)}, ) death_announcements = models.ManyToManyField( Announcement, blank=True, null=True, limit_choices_to={'published_on__gte': datetime.today()-timedelta(days = 45)}, ) memorial_servicesl = models.ManyToManyField( Service, blank=True, null=True, limit_choices_to={'published_on__gte': datetime.today()-timedelta(days = 45)}, ) inmemoriams = models.ManyToManyField( InMemoriam, blank=True, null=True, limit_choices_to={'published_on__gte': datetime.today()-timedelta(days = 45)}, ) objects=EditionManager() on_site = CurrentSiteManager('site') # per-paper managers patriot=PatriotEditionManager() advantages=AdvantagesEditionManager() packet=PacketEditionManager() compass=CompassEditionManager() class Meta: verbose_name=_('Web edition') verbose_name_plural=_('Web editions') ordering=('-published_on', 'paper') get_latest_by='published_on' def __unicode__(self): return u'Web edition of %s published on %s' % (self.paper, self.published_on) ''' def save(self): if not self.id: super(Edition, self).save(*args, **kwargs) if self.published==True: self.stories.all().update(published=True) self.galleries.all().update(published=True) self.slideshows.all().update(published=True) self.movies.all().update(published=True) self.editorials.all().update(published=True) for p in self.photos.all(): p.published=True p.save() for s in self.stories.all(): if s.lead_photo: s.lead_photo.published=True s.lead_photo.save() s.photos.all().update(published=True) for g in s.galleries.all(): for p in g.photos.all(): p.published=True p.save() for g in self.galleries.all(): for p in g.photos.all(): p.published=True p.save() for a in self.death_announcements.all(): a.published=True a.save() for s in self.memorial_services.all(): s.published=True s.save() for i in self.inmemoriams.all(): i.published=True i.save() for r in self.remembrances.all(): r.published=True r.save() else: self.stories.all().update(published=False) self.galleries.all().update(published=False) self.photos.all().update(published=False) self.slideshows.all().update(published=False) self.movies.all().update(published=False) self.editorials.all().update(published=False) for s in self.stories.all(): if s.lead_photo: s.lead_photo.published=False s.lead_photo.save() for g in self.galleries.all(): for p in g.photos.all(): p.published=False p.save() for a in self.death_announcements.all(): a.published=False a.save() for s in self.memorial_services.all(): s.published=False s.save() for i in self.inmemoriams.all(): i.published=False i.save() for r in self.remembrances.all(): r.published=False r.save() super(WebEdition, self).save(*args, **kwargs) ''' class ArchiveItem(TimeStampedModel): '''Archive item model. A base class inherited by whatever content class we want to stuff in the archive. Items are associated with a particular paper, can expire and belong to "groups" The groups allow for very basic access control, so we could sell access to certain archive items. ''' archived_on=models.DateTimeField(_('Archived on'), default=datetime.now()) expires_on=models.DateTimeField(_('Expires on'), blank=True, null=True) groups=models.ManyToManyField(Group, blank=True, null=True) paper=models.ForeignKey(Paper) class ArchiveSection(ArchiveItem): '''Archive section model. This model is used to collect editioral content into a produced section The group can then be displayed on as an archive on a particular subject. It actually duplicates a lot of what's in a story because section objects don't exist in and of their own like stories or documents. ''' GROUP_BY_CHOICES = ( ('type', _('Type')), ('dline', _('Dateline')), ('both', _('Both')), ) title=models.CharField(_('title'), max_length=200) slug=AutoSlugField(_('slug'), populate_from='title', editable=True) description=models.TextField(_('body')) published=models.BooleanField(_('published'), default=False) published_on=models.DateTimeField(_('published on')) thumbnail=models.ImageField(_('Thumbnail'), blank=True, null=True, upload_to="newsroom/archives/thumbnails/") lead_image=models.ImageField(_('Lead image'), blank=True, null=True, upload_to="newsroom/archives/lead_images/") dateline=models.ForeignKey(Dateline, blank=True, null=True) stories=models.ManyToManyField(Story, blank=True, null=True) editorials=models.ManyToManyField(Editorial, blank=True, null=True) pdf_documents=models.ManyToManyField(PDFDocument, blank=True, null=True) photos=models.ManyToManyField(Photo, blank=True, null=True) galleries=models.ManyToManyField(Gallery, blank=True, null=True) movies=models.ManyToManyField(Movie, blank=True, null=True) slideshows=models.ManyToManyField(Slideshow, blank=True, null=True) group_by=models.CharField(_('Group by'), choices=GROUP_BY_CHOICES, default='type', max_length=10) class Meta: verbose_name=_('archive section') verbose_name_plural=('archive sections') ordering=['-modified'] def __unicode__(self): return u'%s' % self.title @models.permalink def get_absolute_url(self): return ('nr-archive-section-detail', (), {'year':self.published_on.year, 'slug':self.slug}) def get_relative_url(self): return u'%s/%s/' % (self.published_on.year, self.slug) class ArchiveStory(ArchiveItem): '''Archive story model. A wrapper class for putting stories into our "archive" section. ''' story=models.ForeignKey(Story) class Meta: verbose_name=_('Archived story') verbose_name_plural=_('Archived stories') def __unicode__(self): return u'%s' % self.story.hed() @models.permalink def get_absolute_url(self): return self.story.get_absolute_url class ArchiveMovie(ArchiveItem): '''Archive movie model. A wrapper class for putting movies into our "archive" section. ''' movie=models.ForeignKey(Movie) class Meta: verbose_name=_('Archived movie') verbose_name_plural=_('Archived movies') def __unicode__(self): return u'%s' % self.movie.title() @models.permalink def get_absolute_url(self): return self.movie.get_absolute_url class ArchiveSlideshow(ArchiveItem): '''Archive slideshow model. A wrapper class for putting slideshows into our "archive" section. ''' slideshow=models.ForeignKey(Slideshow) class Meta: verbose_name=_('Archived slideshow') verbose_name_plural=_('Archived slideshows') def __unicode__(self): return u'%s' % self.slideshow.title() @models.permalink def get_absolute_url(self): return self.slideshow.get_absolute_url class ArchiveDocument(ArchiveItem): '''Archive document model. We need a way to stick documents in the archive... ''' document=models.ForeignKey(Document) class Meta: verbose_name=_('Archived document') verbose_name_plural=_('Archived documents') def __unicode__(self): return u'%s' % self.document.title @models.permalink def get_absolute_url(self): return self.document.get_absolute_url ''' class PageType(models.Model): name = models.CharField(_('name'), max_length=150) slug=AutoSlugField(_('slug'), populate_from='name', editable=True) description = models.TextField(_('description'), blank=True, null=True) class Meta: verbose_name=_('page type') verbose_name_plural=('page types') def __unicode__(self): return u'%s' % (self.name) class Page(models.Model): number=models.IntegerField(_('number'), max_length=3) edition=models.ForeignKey(Edition) copy_inches=models.IntegerField(_('inches of copy'), max_length=3, blank=True, null=True, help_text='If left blank, will be calcuated by included copy.' ) ad_inches=models.IntegerField(_('inches of ads'), max_length=3, blank=True, null=True, help_text='If left blank, will be calcuated by included ads.' ) ad_group=models.ForeignKey(AdCategory, null=True, blank=True, help_text='Primary ad group for page.') ad_notes=models.TextField(_('ad notes'), null=True, blank=True) ad_spec_pos=models.TextField(_('ad special position'), null=True, blank=True) ads = models.ManyToManyField(BannerAd, blank=True, null=True) copy = models.ManyToManyField(Story, blank=True, null=True ) photos = models.ManyToManyField(Photo, blank=True, null=True ) type = models.ForeignKey(PageType, blank=True, null=True ) class Meta: verbose_name=_('page') verbose_name_plural=('pages') unique_together = (("number", "edition"),) def __unicode__(self): return u'Page %s of %s' % (self.number, self.edition) '''