This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
views.py
324 lines (259 loc) · 10.3 KB
/
views.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import datetime
import itertools
import json
import time
from django.conf import settings
from django.contrib.syndication.views import Feed
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.utils.feedgenerator import Atom1Feed
import commonware.log
import jingo
from product_details import product_details
from product_details.version_compare import Version
from tower import ugettext as _, ugettext_lazy as _lazy
import input
from input import (PRODUCTS, PRODUCT_IDS, FIREFOX, LATEST_VERSION,
OPINION_PRAISE, OPINION_ISSUE, OPINION_IDEA, OPINION_TYPES)
from input.decorators import cache_page
from input.urlresolvers import reverse
from search.client import Client, RatingsClient, SearchError
from search.forms import ReporterSearchForm, PROD_CHOICES, VERSION_CHOICES
log = commonware.log.getLogger('i.search')
unixtime = lambda s: int(time.mktime(time.strptime(s, '%Y-%m-%d')))
def get_plotbands():
"""Get's plotbands from product details."""
plotbands = []
color = itertools.cycle(('rgba(255, 255, 255, 0)',
'rgba(0, 0, 255, 0.07)'))
to = int(time.time())
versions = product_details.firefox_history_development_releases
for version in FIREFOX.beta_versions:
frm = unixtime(versions[version])
plotband = dict(color=color.next(),
to=to,
label=dict(text=version))
plotband['from'] = frm
plotbands.append(plotband)
to = frm
return plotbands
def _get_results(request, meta=[], client=None):
form = ReporterSearchForm(request.GET)
if form.is_valid():
data = form.cleaned_data
query = data.get('q', '')
product = data.get('product') or FIREFOX.short
version = data.get('version')
search_opts = _get_results_opts(request, data, product, meta)
c = client or Client()
opinions = c.query(query, **search_opts)
metas = c.meta
else:
opinions = []
product = request.default_prod
query = ''
version = Version(LATEST_VERSION()[product]).simplified
metas = {}
product = PRODUCTS.get(product, FIREFOX)
return (opinions, form, product, version, metas)
def _get_results_opts(request, data, product, meta=[]):
"""Prepare the search options for the Sphinx query"""
search_opts = data
search_opts['product'] = PRODUCTS[product].id
search_opts['meta'] = meta
search_opts['offset'] = ((data.get('page', 1) - 1) *
settings.SEARCH_PERPAGE)
sentiment = data.get('sentiment', '')
if sentiment == 'happy':
search_opts['type'] = OPINION_PRAISE.id
elif sentiment == 'sad':
search_opts['type'] = OPINION_ISSUE.id
elif sentiment == 'ideas':
search_opts['type'] = OPINION_IDEA.id
return search_opts
def get_sentiment(data=[]):
r = dict(happy=0, sad=0, ideas=0, sentiment='happy')
for el in data:
if el['type'] == OPINION_PRAISE.id:
r['happy'] = el['count']
elif el['type'] == OPINION_ISSUE.id:
r['sad'] = el['count']
elif el['type'] == OPINION_IDEA.id:
r['ideas'] = el['count']
r['total'] = r['sad'] + r['happy'] + r['ideas']
if r['sad'] > r['happy']:
r['sentiment'] = 'sad'
return r
class SearchFeed(Feed):
# TODO(davedash): Gracefully degrade for unavailable search.
feed_type = Atom1Feed
author_name = _lazy('Firefox Input')
subtitle = _lazy("Search Results in Firefox Beta Feedback.")
def get_object(self, request):
data = dict(opinions=_get_results(request)[0], request=request)
return data
def link(self, obj):
"""Global feed link. Also used as GUID."""
return u'%s?%s' % (reverse('search'),
obj['request'].META['QUERY_STRING'])
def title(self, obj):
"""Global feed title."""
request = obj['request']
query = request.GET.get('q')
# L10n: This is the title to the Search ATOM feed.
return (_(u"Firefox Input: '{query}'").format(query=query) if query
else _('Firefox Input'))
def items(self, obj):
"""List of comments."""
return obj['opinions'][:settings.SEARCH_PERPAGE]
def item_categories(self, item):
"""Categorize comments. Style: "product:firefox" etc."""
categories = {
'product': PRODUCT_IDS.get(item.product).short,
'version': item.version,
'platform': item.platform,
'locale': item.locale,
'sentiment': OPINION_TYPES[item.type].short
}
return (':'.join(i) for i in categories.items())
def item_description(self, item):
"""A comment's main text."""
return item.description
def item_link(self, item):
"""Permalink per item. Also used as GUID."""
return item.get_url_path()
def item_pubdate(self, item):
"""Publishing date of a comment."""
return item.created
def item_title(self, item):
"""A comment's title."""
return unicode(item)
# TODO(davedash): use a bound form for defaults in views and get rid of this.
def get_defaults(form):
"""
Keep form data as default options for further searches, but remove page
from defaults so that every parameter change returns to page 1.
"""
return dict((k, v) for k, v in form.data.items()
if k != 'page' and k in form.fields)
def get_period(form):
"""Determine date period chosen."""
days = 0
if not getattr(form, 'cleaned_data', None):
return None, days
d = form.cleaned_data
start = d.get('date_start')
end = d.get('date_end') or datetime.date.today()
if not (start and end):
return 'infin', days
_ago = lambda x: datetime.date.today() - datetime.timedelta(days=x)
days = (end - start).days
if (end == datetime.date.today() and start):
return {_ago(1): '1d',
_ago(7): '7d',
_ago(30): '30d'}.get(start, 'custom'), days
return 'custom', days
@cache_page(use_get=True)
def index(request):
try:
meta = ('type', 'locale', 'platform', 'day_sentiment', 'manufacturer',
'device')
(results, form, product, version, metas) = _get_results(
request, meta=meta)
except SearchError, e:
return jingo.render(request, 'search/unavailable.html',
{'search_error': e}, status=500)
page = form.data.get('page', 1)
data = dict(
form=form,
product=product,
products=PROD_CHOICES,
version=dict(form.fields['version'].choices).get(version or '--'),
versions=VERSION_CHOICES['beta'][product],
)
data['period'], days = get_period(form)
if results:
pager = Paginator(results, settings.SEARCH_PERPAGE)
data['opinion_count'] = pager.count
# If page request (e.g., 9999) is out of range, deliver last page of
# results.
try:
data['page'] = pager.page(page)
except (EmptyPage, InvalidPage):
data['page'] = pager.page(pager.num_pages)
data['opinions'] = data['page'].object_list
data['sent'] = get_sentiment(metas.get('type', {}))
data['demo'] = dict(locale=metas.get('locale'),
platform=metas.get('platform'),
manufacturer=metas.get('manufacturer'),
device=metas.get('device'))
if days >= 7 or data['period'] == 'infin':
daily = metas.get('day_sentiment', {})
chart_data = dict(series=[
dict(name=_('Praise'), data=daily['praise']),
dict(name=_('Issues'), data=daily['issue']),
dict(name=_('Idea'), data=daily['idea']),
],
plotBands=get_plotbands()
) if daily else None
data['chart_data_json'] = json.dumps(chart_data)
else:
data.update({
'opinion_count': 0,
'opinions': None,
'sent': get_sentiment(),
'demo': {},
})
data['defaults'] = get_defaults(form)
template = 'search/%ssearch.html' % (
'mobile/' if request.mobile_site else '')
return jingo.render(request, template, data)
@cache_page(use_get=True)
def release(request):
"""Front page and search view for the release channel."""
c = RatingsClient()
metas = ['platform', 'locale']
for dimension in input.RATING_TYPES.keys():
metas.append(dimension)
metas.append('day__avg__%s' % dimension)
try:
(_, form, product, version, metas) = _get_results(request, metas,
client=c)
except SearchError, e:
return jingo.render(request, 'search/unavailable.html',
{'search_error': e}, status=500)
rating_values = dict((k, unicode(v)) for k, v in input.RATING_CHOICES)
series = []
categories = []
if 'ratings' in metas:
for rating_value in rating_values:
data = [v[rating_value] for k, v in metas['ratings'].items() if v]
series.append(dict(name=rating_values[rating_value], data=data))
ratings_chart = dict(series=series)
categories = []
charts = []
if 'ratings_avg' in metas:
for r in input.RATING_USAGE:
categories.append(unicode(r.pretty))
charts.append(dict(rating=r, json=json.dumps(dict(series=[dict(
data=metas['ratings_avg'][r.id])]))))
data = dict(
product=product,
products=PROD_CHOICES,
version=dict(form.fields['version'].choices).get(version or '--'),
versions=VERSION_CHOICES['release'][product],
platforms=metas.get('platform'),
locales=metas.get('locale'),
chart_json=json.dumps(ratings_chart),
chart_categories=json.dumps(categories),
avg_charts=charts,
defaults=get_defaults(form),
period=get_period(form)[0],
search_form=form,
total=c.total_found,
utc=True, # The date partitions are by UTC.
)
# TODO do not mess with mobile site detection anymore once the release
# dashboard is mobilified. Bug 632225.
request.mobile_site = False
template = 'search/release.html'
return jingo.render(request, template, data)