website/conservancy/news/models.py

118 lines
3.9 KiB
Python

from datetime import datetime, timedelta
from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models
from .. import bsoup
from ..events.models import Event
from ..staff.models import Person
class PressRelease(models.Model, bsoup.SoupModelMixin):
"""News release model"""
headline = models.CharField(max_length=300)
subhead = models.CharField(max_length=300, blank=True)
slug = models.SlugField(unique_for_date="pub_date",
help_text=("automatically built from headline"))
summary = models.TextField(help_text="First paragraph (raw HTML)")
body = models.TextField(help_text="Remainder of post (raw HTML)",
blank=True)
pub_date = models.DateTimeField("date [to be] published")
sites = models.ManyToManyField(Site)
date_last_modified = models.DateTimeField(auto_now=True)
class Meta:
ordering = ("-pub_date",)
get_latest_by = "pub_date"
SOUP_ATTRS = ['summary', 'body']
def __str__(self):
return self.headline
def get_absolute_url(self):
return "/news/{}/{}/".format(
self.pub_date.strftime("%Y/%b/%d").lower(),
self.slug,
)
def is_recent(self):
return self.pub_date > (datetime.now() - timedelta(days=5))
# question: does datetime.now() do a syscall each time is it called?
def is_in_past_month(self):
# This function is deprecated. Use the date_within template
# filter instead (example in conservancy/templates/frontpage.html)
return self.pub_date > (datetime.now() - timedelta(days=30))
def save(self):
if settings.DEBUG or True:
super().save()
return
blog_name = 'Software Freedom Conservancy News'
blog_url = 'https://www.sfconservancy.org/news/'
post_url = ('https://www.sfconservancy.org'
+ self.get_absolute_url())
import xmlrpc.client
# Ping Technorati
j = xmlrpc.client.Server('http://rpc.technorati.com/rpc/ping')
j.weblogUpdates.ping(blog_name, blog_url)
# Ping Google Blog Search
j = xmlrpc.client.Server('http://blogsearch.google.com/ping/RPC2')
j.weblogUpdates.ping(blog_name, blog_url, post_url)
# Call any superclass's method
super().save()
class ExternalArticleTag(models.Model):
"""A way to tag external articles"""
label = models.CharField(max_length=100)
date_created = models.DateField(auto_now_add=True)
def __str__(self):
return self.label
class PublicExternalArticleManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(visible=True)
class ExternalArticle(models.Model):
"""A system for displaying Conservancy news mentions on the site.
(Currently unused)
"""
title = models.CharField(max_length=400)
info = models.CharField(help_text="subscribers only? audio? pdf warning?",
blank=True, max_length=300)
publication = models.CharField("source of article", max_length=300)
# verify_exists removed https://docs.djangoproject.com/en/1.7/releases/1.4/
url = models.URLField(blank=True)
date = models.DateField()
visible = models.BooleanField(help_text="Whether to display on website", default=True)
tags = models.ManyToManyField(ExternalArticleTag, blank=True)
people = models.ManyToManyField(Person, blank=True)
event = models.ForeignKey(Event, null=True, blank=True, on_delete=models.CASCADE)
press_release = models.ForeignKey(PressRelease, null=True, blank=True, on_delete=models.CASCADE)
date_created = models.DateField(auto_now_add=True)
class Meta:
ordering = ("-date_created",)
get_latest_by = "date_created"
def __str__(self):
return "{} ({})".format(self.title, self.publication)
objects = models.Manager()
public = PublicExternalArticleManager()