|
1 | 1 | import random |
| 2 | +import datetime |
2 | 3 |
|
3 | 4 | from django.shortcuts import render_to_response |
4 | 5 | from django.template import RequestContext |
|
8 | 9 |
|
9 | 10 | def featured(request): |
10 | 11 | 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: |
12 | 18 | page_len = len(models.Page.objects.all()) |
13 | 19 | select_pages = random.sample(xrange(1, page_len), number) |
14 | 20 | pages = map(_get_page_info_by_id, select_pages) |
15 | | - else: |
| 21 | + elif pages is None: |
16 | 22 | # grab randomly from the curated selection |
17 | 23 | page_info = config.PAGES |
18 | 24 | page_len = len(page_info) |
@@ -56,3 +62,39 @@ def _get_page_by_info(page_info): |
56 | 62 | page_info['name'] = 'Unknown Title' |
57 | 63 | page_info['page_obj'] = None |
58 | 64 | 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