11import random
22import datetime
33
4- from django .shortcuts import render_to_response
4+ from django .shortcuts import render
55from django .template import RequestContext
66
77from onisite .plugins .featured_content import config
88from core import models
99
1010def featured (request ):
11- number = config .NUMBER
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 :
18- page_len = len (models .Page .objects .all ())
19- select_pages = random .sample (xrange (1 , page_len ), number )
20- pages = map (_get_page_info_by_id , select_pages )
21- elif pages is None :
22- # grab randomly from the curated selection
23- page_info = config .PAGES
24- page_len = len (page_info )
25- if page_len <= number :
26- pages = map (_get_page_by_info , page_info )
27- else :
28- rand_nums = random .sample (range (1 , page_len ), number )
29- random_pages = []
30- for num in rand_nums :
31- random_pages .append (page_info [num ])
32- pages = map (_get_page_by_info , random_pages )
33-
34- return render_to_response ('featured.html' ,
35- dictionary = locals (),
36- context_instance = RequestContext (request ))
11+ # Seed the RNG with today's date so we always feature the same page(s) for
12+ # an entire day
13+ random .seed (datetime .date .today ().strftime ("%Y%m%d" ))
14+
15+ # Get and randomize pages
16+ pages = []
17+ all_pages , this_day_title = _get_pages ()
18+ if len (all_pages ) > 0 :
19+ rand_nums = random .sample (xrange (len (all_pages )), config .NUMBER )
20+ for num in rand_nums :
21+ pages .append (all_pages [num ])
3722
23+ # Clear the seed so anything else using random numbers isn't affected
24+ random .seed (None )
25+
26+ return render (request , 'featured.html' , locals ())
3827
3928# helper methods
4029
41- def _get_page_info_by_id (page_id ):
42- page_obj = models .Page .objects .get (id = page_id )
30+ def _get_pages ():
31+ """Delegate page-fetching based on the configuration; if THISDAY is
32+ requested and gets valid pages, the second argument returned is True to
33+ signal to the template which title to use"""
34+
35+ # We use random pages if explicitly requested *or* THISDAY is requested but
36+ # no pages are found
37+ isrand = config .RANDOM or config .THISDAY
38+
39+ if config .THISDAY :
40+ pages = _pages_this_day ()
41+ if len (pages ) > 0 :
42+ return pages , True
43+
44+ if isrand :
45+ return _random_pages (config .NUMBER ), False
46+
47+ return map (_get_page_by_info , config .PAGES ), False
48+
49+ def _pages_this_day ():
50+ """Find any pages within the min/max years and today's month/year"""
51+ pages = []
52+
53+ # Filtering variables
54+ dt_range_start = datetime .date (config .MINYEAR , 1 , 1 )
55+ dt_range_end = datetime .date (config .MAXYEAR , 12 , 31 )
56+ now = datetime .date .today ()
57+
58+ # Grab each issue that matches, and create a page_info structure for the
59+ # first page of that issue. For sanity's sake, we only pull 100 max.
60+ issues = models .Issue .objects
61+ issues = issues .filter (date_issued__range = (dt_range_start , dt_range_end ))
62+ issues = issues .filter (date_issued__month = now .month )
63+ issues = issues .filter (date_issued__day = now .day )
64+ for issue in issues [:100 ]:
65+ first_page = issue .first_page
66+ if first_page and first_page .jp2_filename :
67+ pages .append ({
68+ 'date' : issue .date_issued ,
69+ 'edition' : issue .edition ,
70+ 'lccn' : issue .title .lccn ,
71+ 'name' : issue .title .name ,
72+ 'page_obj' : first_page ,
73+ 'sequence' : first_page .sequence ,
74+ })
75+
76+ return pages
77+
78+ def _random_pages (limit ):
79+ page_len = models .Page .objects .count ()
80+ indices = random .sample (xrange (page_len ), limit )
81+ pages = []
82+ for index in indices :
83+ pages .append (models .Page .objects .all ()[index ])
84+
85+ return map (_get_page_by_object , pages )
86+
87+ def _get_page_by_object (page_obj ):
4388 issue_obj = page_obj .issue
4489 page = {
4590 'date' : issue_obj .date_issued ,
@@ -62,39 +107,3 @@ def _get_page_by_info(page_info):
62107 page_info ['name' ] = 'Unknown Title'
63108 page_info ['page_obj' ] = None
64109 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