Skip to content

Commit f33d77b

Browse files
authored
Merge pull request #4 from open-oni/add_this_day
Add this day
2 parents faa96de + 123515d commit f33d77b

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

views.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
import datetime
23

34
from django.shortcuts import render_to_response
45
from django.template import RequestContext
@@ -8,11 +9,16 @@
89

910
def featured(request):
1011
number = config.NUMBER
11-
if config.RANDOM:
12+
pages = None
13+
if config.THISDAY:
14+
pages = _this_day()
15+
if not pages is None:
16+
this_day_title = True
17+
if pages is None and config.RANDOM:
1218
page_len = len(models.Page.objects.all())
1319
select_pages = random.sample(xrange(1, page_len), number)
1420
pages = map(_get_page_info_by_id, select_pages)
15-
else:
21+
elif pages is None:
1622
# grab randomly from the curated selection
1723
page_info = config.PAGES
1824
page_len = len(page_info)
@@ -56,3 +62,39 @@ def _get_page_by_info(page_info):
5662
page_info['name'] = 'Unknown Title'
5763
page_info['page_obj'] = None
5864
return page_info
65+
66+
def _this_day():
67+
pages = []
68+
today = datetime.date.today()
69+
rand_years = random.sample(xrange(config.MINYEAR,config.MAXYEAR), config.MAXYEAR-config.MINYEAR)
70+
rand_years.insert(0, today.year-100)
71+
for rand_year in rand_years:
72+
page = _get_page_by_date(today.replace(year = rand_year))
73+
if not page is None:
74+
pages.append(page)
75+
return pages
76+
return None
77+
78+
def _get_page_by_date(date):
79+
issues = list(models.Issue.objects.filter(date_issued=date)[:10])
80+
issue_count = len(issues)
81+
if issue_count < 1:
82+
return None
83+
if issue_count < 2:
84+
rand_indices = [0]
85+
else:
86+
rand_indices = random.sample(xrange(issue_count), issue_count)
87+
for rand_index in rand_indices:
88+
issue = issues[rand_index]
89+
first_page = issue.first_page
90+
if first_page and first_page.jp2_filename:
91+
page = {
92+
'date': issue.date_issued,
93+
'edition': issue.edition,
94+
'lccn': issue.title.lccn,
95+
'name': issue.title.name,
96+
'page_obj': first_page,
97+
'sequence': first_page.sequence,
98+
}
99+
return page
100+
return None

0 commit comments

Comments
 (0)