@@ -42,25 +42,35 @@ def get_doctypes(queryargs, pluralize=False):
4242
4343def make_title (queryargs ):
4444 title = 'New '
45- title += get_doctypes (queryargs )
46- title += ' Revisions '
45+ title += get_doctypes (queryargs ). lower ()
46+ title += ' revisions '
4747 # radio choices
4848 by = queryargs .get ('by' )
4949 if by == "author" :
5050 title += ' with author "%s"' % queryargs ['author' ].title ()
5151 elif by == "group" :
52- title += ' for %s' % queryargs ['group' ].capitalize ()
52+ group = queryargs ['group' ]
53+ if group :
54+ title += ' for %s' % group .capitalize ()
5355 elif by == "area" :
54- title += ' in %s Area' % queryargs ['area' ].upper ()
56+ area = queryargs ['area' ]
57+ if area :
58+ title += ' in %s Area' % area .upper ()
5559 elif by == "ad" :
56- title += ' with AD %s' % Person .objects .get (id = queryargs ['ad' ])
60+ ad_id = queryargs ['ad' ]
61+ if ad_id :
62+ title += ' with AD %s' % Person .objects .get (id = ad_id )
5763 elif by == "state" :
58- title += ' in state %s::%s' % (queryargs ['state' ], queryargs ['substate' ])
64+ state = queryargs ['state' ]
65+ if state :
66+ title += ' in state %s::%s' % (state , queryargs ['substate' ])
5967 elif by == "stream" :
60- title += ' in stream %s' % queryargs ['stream' ]
68+ stream = queryargs ['stream' ]
69+ if stream :
70+ title += ' in stream %s' % stream
6171 name = queryargs .get ('name' )
6272 if name :
63- title += ' matching "%s"' % name
73+ title += ' with name matching "%s"' % name
6474 return title
6575
6676def chart_newrevisiondocevent (request ):
@@ -76,12 +86,79 @@ def dt(s):
7686 ys , ms , ds = s .split ('-' )
7787 return datetime .date (int (ys ), int (ms ), int (ds ))
7888
89+ def model_to_timeline (model , ** kwargs ):
90+ """Takes a Django model and a set of queryset filter arguments, and
91+ returns a dictionary with highchart settings and data, suitable as
92+ a JsonResponse() argument. The model must have a time field."""
93+ #debug.pprint('model._meta.fields')
94+ assert 'time' in model ._meta .get_all_field_names ()
95+
96+ objects = ( model .objects .filter (** kwargs )
97+ .order_by ('date' )
98+ .extra (select = {'date' : 'date(doc_docevent.time)' })
99+ .values ('date' )
100+ .annotate (count = Count ('id' )))
101+ if objects .exists ():
102+ # debug.lap('got event query')
103+ obj_list = list (objects )
104+ # debug.lap('got event list')
105+ # This is needed for sqlite, when we're running tests:
106+ if type (obj_list [0 ]['date' ]) != datetime .date :
107+ # debug.say('converting string dates to datetime.date')
108+ obj_list = [ {'date' : dt (e ['date' ]), 'count' : e ['count' ]} for e in obj_list ]
109+ points = [ ((e ['date' ].toordinal ()- epochday )* 1000 * 60 * 60 * 24 , e ['count' ]) for e in obj_list ]
110+ # debug.lap('got event points')
111+ counts = dict (points )
112+ # debug.lap('got points dictionary')
113+ day_ms = 1000 * 60 * 60 * 24
114+ days = range (points [0 ][0 ], points [- 1 ][0 ]+ day_ms , day_ms )
115+ # debug.lap('got days array')
116+ data = [ (d , counts [d ] if d in counts else 0 ) for d in days ]
117+ # debug.lap('merged points into days')
118+ else :
119+ data = []
120+
121+ info = {
122+ "chart" : {
123+ "type" : 'column'
124+ },
125+ "rangeSelector" : {
126+ "selected" : 4 ,
127+ "allButtonsEnabled" : True ,
128+ },
129+ "title" : {
130+ "text" : "%s items over time" % model ._meta .model_name
131+ },
132+ "credits" : {
133+ "enabled" : False ,
134+ },
135+ "series" : [{
136+ "name" : "Items" ,
137+ "type" : "column" ,
138+ "data" : data ,
139+ "dataGrouping" : {
140+ "units" : [[
141+ 'week' , # unit name
142+ [1 ,], # allowed multiples
143+ ], [
144+ 'month' ,
145+ [1 , 4 ,],
146+ ]]
147+ },
148+ "turboThreshold" : 1 , # Only check format of first data point. All others are the same
149+ "pointInterval" : 24 * 60 * 60 * 1000 ,
150+ "pointPadding" : 0.05 ,
151+ }]
152+ }
153+ return info
154+
155+
156+
79157@cache_page (60 * 15 )
80158def chart_data_newrevisiondocevent (request ):
81159 # debug.mark()
82160 queryargs = request .GET
83161 if queryargs :
84-
85162 # debug.lap('got queryargs')
86163 key = get_search_cache_key (queryargs )
87164 # debug.lap('got cache key')
@@ -98,73 +175,28 @@ def chart_data_newrevisiondocevent(request):
98175 if results .exists ():
99176 cache .set (key , results )
100177 # debug.lap('cached search result')
101-
102178 if results .exists ():
103- events = ( DocEvent .objects .filter (doc__in = results , type = 'new_revision' )
104- .order_by ('date' )
105- .extra (select = {'date' : 'date(doc_docevent.time)' })
106- .values ('date' )
107- .annotate (count = Count ('id' )))
108- if events .exists ():
109- # debug.lap('got event query')
110- event_list = list (events )
111- # debug.lap('got event list')
112- # This is needed for sqlite, when we're running tests:
113- if type (event_list [0 ]['date' ]) != datetime .date :
114- # debug.say('converting string dates to datetime.date')
115- event_list = [ {'date' : dt (e ['date' ]), 'count' : e ['count' ]} for e in event_list ]
116- points = [ ((e ['date' ].toordinal ()- epochday )* 1000 * 60 * 60 * 24 , e ['count' ]) for e in event_list ]
117- # debug.lap('got event points')
118- counts = dict (points )
119- # debug.lap('got points dictionary')
120- day_ms = 1000 * 60 * 60 * 24
121- days = range (points [0 ][0 ], points [- 1 ][0 ]+ day_ms , day_ms )
122- # debug.lap('got days array')
123- data = [ (d , counts [d ] if d in counts else 0 ) for d in days ]
124- # debug.lap('merged points into days')
125- else :
126- data = []
179+ info = model_to_timeline (DocEvent , doc__in = results , type = 'new_revision' )
180+ info ['title' ]['text' ] = make_title (queryargs )
181+ info ['series' ][0 ]['name' ] = "Submitted %s" % get_doctypes (queryargs , pluralize = True ).lower (),
127182 else :
128- data = []
129- info = {
130-
131- "chart" : {
132- "type" : 'column'
133- },
134-
135- "rangeSelector" : {
136- "selected" : 4 ,
137- "allButtonsEnabled" : True ,
138- },
139- "title" : {
140- "text" : make_title (queryargs )
141- },
142- "credits" : {
143- "enabled" : False ,
144- },
145- "series" : [{
146- "name" : "Submitted %s" % get_doctypes (queryargs , pluralize = True ),
147- "type" : "column" ,
148- "data" : data ,
149- "dataGrouping" : {
150- "units" : [[
151- 'week' , # unit name
152- [1 ,], # allowed multiples
153- ], [
154- 'month' ,
155- [1 , 4 ,],
156- ]]
157- },
158- "turboThreshold" : 1 , # Only check format of first data point. All others are the same
159- "pointInterval" : 24 * 60 * 60 * 1000 ,
160- "pointPadding" : 0.05 ,
161-
162- }]
163-
164- }
183+ info = {}
165184 # debug.clock('set up info dict')
166185 else :
167186 info = {}
168187 return JsonResponse (info )
169188
170189
190+ @cache_page (60 * 15 )
191+ def chart_data_person_drafts (request , id ):
192+ # debug.mark()
193+ person = Person .objects .filter (id = id ).first ()
194+ if not person :
195+ info = {}
196+ else :
197+ info = model_to_timeline (DocEvent , doc__authors__person = person , type = 'new_revision' )
198+ info ['title' ]['text' ] = "New draft revisions over time for %s" % person .name
199+ info ['series' ][0 ]['name' ] = "Submitted drafts"
200+ return JsonResponse (info )
201+
202+
0 commit comments