Skip to content

Commit

Permalink
Merge pull request #4 from open-oni/add_this_day
Browse files Browse the repository at this point in the history
Add this day
  • Loading branch information
jechols authored Jul 17, 2017
2 parents faa96de + 123515d commit f33d77b
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import datetime

from django.shortcuts import render_to_response
from django.template import RequestContext
Expand All @@ -8,11 +9,16 @@

def featured(request):
number = config.NUMBER
if config.RANDOM:
pages = None
if config.THISDAY:
pages = _this_day()
if not pages is None:
this_day_title = True
if pages is None and config.RANDOM:
page_len = len(models.Page.objects.all())
select_pages = random.sample(xrange(1, page_len), number)
pages = map(_get_page_info_by_id, select_pages)
else:
elif pages is None:
# grab randomly from the curated selection
page_info = config.PAGES
page_len = len(page_info)
Expand Down Expand Up @@ -56,3 +62,39 @@ def _get_page_by_info(page_info):
page_info['name'] = 'Unknown Title'
page_info['page_obj'] = None
return page_info

def _this_day():
pages = []
today = datetime.date.today()
rand_years = random.sample(xrange(config.MINYEAR,config.MAXYEAR), config.MAXYEAR-config.MINYEAR)
rand_years.insert(0, today.year-100)
for rand_year in rand_years:
page = _get_page_by_date(today.replace(year = rand_year))
if not page is None:
pages.append(page)
return pages
return None

def _get_page_by_date(date):
issues = list(models.Issue.objects.filter(date_issued=date)[:10])
issue_count = len(issues)
if issue_count < 1:
return None
if issue_count < 2:
rand_indices = [0]
else:
rand_indices = random.sample(xrange(issue_count), issue_count)
for rand_index in rand_indices:
issue = issues[rand_index]
first_page = issue.first_page
if first_page and first_page.jp2_filename:
page = {
'date': issue.date_issued,
'edition': issue.edition,
'lccn': issue.title.lccn,
'name': issue.title.name,
'page_obj': first_page,
'sequence': first_page.sequence,
}
return page
return None

0 comments on commit f33d77b

Please sign in to comment.