Skip to content

Instantly share code, notes, and snippets.

@ewr
Created November 2, 2011 03:26
Show Gist options
  • Save ewr/1332769 to your computer and use it in GitHub Desktop.
Save ewr/1332769 to your computer and use it in GitHub Desktop.
Interleave multiple content types by timestamp
# -- find content by bylines -- #
# HACK -- This is hardcoded to news stories, blog entries and show segments for now
finder = ContentByline.objects.raw("""
select
contentbase_contentbyline.id,
contentbase_contentbyline.object_id,
contentbase_contentbyline.content_type_id,
COALESCE(news_story.published_at,shows_segment.created_at,blogs_entry.published_at) as pub_date
from
contentbase_contentbyline
left join
news_story
on
news_story.id = contentbase_contentbyline.object_id
and contentbase_contentbyline.content_type_id = 15
left join
shows_segment
on
shows_segment.id = contentbase_contentbyline.object_id
and contentbase_contentbyline.content_type_id = 24
left join
blogs_entry
on
blogs_entry.id = contentbase_contentbyline.object_id
and contentbase_contentbyline.content_type_id = 44
where
contentbase_contentbyline.user_id = %s
and contentbase_contentbyline.role in (0,1)
having
pub_date is not null
order by
pub_date desc
""",[bio.id])
#bylines = [c.content for c in ContentByline.objects.filter(user=bio)[0:25]]
#bylines.sort(reverse=True,key=lambda c: datetime.datetime(*c.public_datetime().timetuple()[:-4]))
#stories = Story.objects.filter(Q(status=Story.STATUS_LIVE),
# Q(primary_reporter__exact=bio.id) | Q(secondary_reporter__exact=bio.id))\
# .order_by('-published_at')
paginator = Paginator(list(finder), 15)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
bylines = paginator.page(page)
except (EmptyPage, InvalidPage):
bylines = paginator.page(paginator.num_pages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment