-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathissue_calendar.py
More file actions
122 lines (102 loc) · 3.9 KB
/
Copy pathissue_calendar.py
File metadata and controls
122 lines (102 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import calendar
from django import forms
from django import urls
from django.db import connection
from django.db.models import Count
from core import models
class Day(object):
def __init__(self, week, daydata):
self.week = week
self.day_of_month = daydata[0]
self.day_of_week = daydata[1]
self.month = self.week.month.num
self.year = self.week.month.cal.year
self.dtstr = "%04d%02d%02d" % (self.year, self.month, self.day_of_month)
self.issue_count = 0
if self.dtstr in self.week.month.cal.date_counts:
self.issue_count = self.week.month.cal.date_counts[self.dtstr]
if self.issue_count is None:
self.issue_count = 0
def link(self):
lccn = "all"
if self.week.month.cal.title is not None:
lccn = self.week.month.cal.title.lccn
return urls.reverse('calendar_issues_for_date', kwargs=dict(
lccn = lccn,
year = "%04d" % self.year,
month = "%02d" % self.month,
day = "%02d" % self.day_of_month,
))
class Week(object):
def __init__(self, month, weekdata):
self.month = month
self.weekdata = weekdata
self._setup_days()
def _setup_days(self):
self.days = []
for daydata in self.weekdata:
self.days.append(Day(self, daydata))
class Month(object):
def __init__(self, cal, num):
self.cal = cal
self.num = num
self._setup_weeks()
def name(self):
return calendar.month_name[self.num]
def _setup_weeks(self):
self.weeks = []
raw_weeks = self.cal.monthdays2calendar(self.cal.year, self.num)
for weekdata in raw_weeks:
self.weeks.append(Week(self, weekdata))
class IssueCalendar(calendar.Calendar):
def __init__(self, title, year):
calendar.Calendar.__init__(self, 6)
self.title = title
self.year = year
self._get_issue_years()
self._normalize_selected_year()
self._get_issue_dates_for_selected_year()
self._setup_months()
def _get_issue_years(self):
"""Set up an array of years and issue counts for each year"""
cursor = connection.cursor()
select = "SELECT year(date_issued) AS issue_year, COUNT(id) FROM core_issue"
where = ""
params = []
if self.title is not None:
where = "WHERE title_id = %s"
params = [self.title.lccn]
grouporder = "GROUP BY issue_year ORDER BY issue_year"
cursor.execute("%s %s %s" % (select, where, grouporder), params)
self.yeardata = cursor.fetchall()
def _normalize_selected_year(self):
"""Determine the selected year in case one was not already selected"""
if len(self.yeardata) > 0:
if self.year is None:
self.year = self.yeardata[0][0]
else:
self.year = int(self.year)
else:
self.year = 1900
def _get_issue_dates_for_selected_year(self):
"""Get a count of issues for each day of the selected year"""
self.date_counts = {}
qs = models.Issue.objects.filter(date_issued__year = self.year)
if self.title is not None:
qs = qs.filter(title_id = self.title.lccn)
for issue in qs.all():
dtstr = "%04d%02d%02d" % (issue.date_issued.year, issue.date_issued.month, issue.date_issued.day)
if dtstr not in self.date_counts:
self.date_counts[dtstr] = 0
self.date_counts[dtstr] += 1
def _setup_months(self):
self.months = []
for m in range(12):
self.months.append(Month(self, m+1))
def year_form(self):
class SelectYearForm(forms.Form):
year = forms.fields.ChoiceField(
choices=((yd[0], yd[0]) for yd in self.yeardata),
initial=self.year
)
return SelectYearForm()