Skip to content

Instantly share code, notes, and snippets.

@dangayle
Last active August 29, 2015 14:06
Show Gist options
  • Save dangayle/6fa3e061f0eec307b530 to your computer and use it in GitHub Desktop.
Save dangayle/6fa3e061f0eec307b530 to your computer and use it in GitHub Desktop.
from django.db.models import Max
from blogs.models import Blog, BlogPost
def get_recent_posts():
"""Get latest blogpost for each blog."""
posts = []
blogs = Blog.objects.annotate(
most_recent=Max('blogpost__pubdate')
).order_by('-most_recent')[:Blog.objects.count()]
for blog in blogs:
try:
posts.append(blog.blogpost_set.latest('pubdate'))
except:
pass
return posts
class Blog(models.Model):
sites = models.ManyToManyField(Site, default=[settings.SITE_ID])
name = models.CharField(max_length=255, unique=True)
slug = models.SlugField(unique=True)
authors = models.ManyToManyField(User, related_name='blog_authors', limit_choices_to={'is_staff__exact': True},)
is_live = models.BooleanField(default=True)
description = models.TextField(blank=True)
teaser_photo = models.ImageField(upload_to='blog_images/', blank=True, null=True, help_text='A teaser photo for this blog.')
is_pick = models.BooleanField(default=False)
show_in_active_lists = models.BooleanField(default=True)
sidebar = models.TextField("Custom sidebar",blank=True,null=True)
template = models.CharField(max_length=255,blank=True,null=True)
categories = models.ManyToManyField(Category, blank=True, null=True, related_name='blog_categories')
tags = TagField(help_text='Topical keywords describing the blog, separated by commas', blank=True, null=True)
objects = models.Manager()
tagged_objects = ModelTaggedItemManager()
class Meta:
db_table = 'blogs_blog'
ordering = ('name',)
permissions = (('full_blog_access','Can edit/del ALL blogs (not just own)'),)
def __unicode__(self):
return '%s' % self.name
class BlogPost(models.Model):
objects = models.Manager()
live_objects = LiveBlogPostManager()
tagged_objects = ModelTaggedItemManager()
blog = models.ForeignKey(Blog, db_index=True)
pubdate = models.DateTimeField(default=datetime.now,db_index=True)
revision_datetime = models.DateTimeField("Last edited", auto_now=True)
is_live = models.BooleanField(default=True,db_index=True)
title = models.CharField(max_length=255, db_index=True)
slug = models.SlugField(unique_for_date='pubdate',max_length=255)
authors = models.ManyToManyField(User, related_name='blogpost_authors', blank=True, null=True, limit_choices_to=available_author_pk_option(),)
static_byline = models.CharField(blank=True, null=True, max_length=255, help_text="Used for non-staff contributions. Also used for legacy blog data from the old site.")
post = models.TextField()
extended_post = models.TextField(blank=True,null=True,help_text='The continuation of the post that shows up only on the post\'s detail page. ("After the jump")')
tags = TagField(help_text='Topical keywords describing the blog, separated by commas', blank=True, null=True)
comment_status = models.IntegerField(choices=COMMENTSTATUS_CHOICES, default=1)
num_comments = models.IntegerField(default=0,db_index=True)
internal_notes = models.TextField(blank=True, null=True)
photos = models.ManyToManyField(Photo, blank=True, null=True)
photo_sets = models.ManyToManyField(PhotoSet, blank=True, null=True)
audio = models.ManyToManyField(Audio, blank=True, null=True)
audio_sets = models.ManyToManyField(AudioSet, blank=True, null=True)
video = models.ManyToManyField(Video, blank=True, null=True)
video_sets = models.ManyToManyField(VideoSet, blank=True, null=True)
documents = models.ManyToManyField(Document, blank=True, null=True)
document_sets = models.ManyToManyField(DocumentSet, blank=True, null=True)
related_stories = models.ManyToManyField(Story, verbose_name="Related stories", blank=True, null=True)
topics = models.ManyToManyField(BlogPostTopic, blank=True, null=True)
class Meta:
db_table = 'blogs_blogpost'
ordering = ('-pubdate','title')
permissions = (('full_blogpost_access','Can edit/del ALL blogposts'),)
#unique_together = (('pubdate','slug','blog'),)
def __unicode__(self):
return '%s' % self.title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment