/
views.py
293 lines (237 loc) · 9.61 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
import calendar
import json
import logging
from datetime import datetime, timedelta
from email.utils import parsedate, formatdate
from django.conf import settings
from django.http import (HttpResponse, HttpResponseBadRequest,
HttpResponseNotFound, HttpResponseServerError)
from django.shortcuts import render
from django.utils.datastructures import SortedDict
from django.views.decorators.http import require_POST, require_GET
import bleach
import tweepy
from session_csrf import anonymous_csrf
from statsd import statsd
from tower import ugettext as _, ugettext_lazy as _lazy
from kitsune import twitter
from kitsune.customercare.models import Tweet, Reply
from kitsune.customercare.replies import get_common_replies
from kitsune.sumo.redis_utils import redis_client, RedisError
log = logging.getLogger('k.customercare')
MAX_TWEETS = 20
FILTERS = SortedDict([('recent', _lazy('Most Recent')),
('unanswered', _lazy('Unanswered')),
('answered', _lazy('Answered')),
('all', _lazy('All'))])
def _tweet_for_template(tweet, https=False):
"""Return the dict needed for tweets.html to render a tweet + replies."""
data = json.loads(tweet.raw_json)
parsed_date = parsedate(data['created_at'])
date = datetime(*parsed_date[0:6])
# Recursively fetch replies.
if settings.CC_SHOW_REPLIES:
# If ever slow, optimize to do fewer queries.
replies = _get_tweets(limit=0, reply_to=tweet, https=https)
else:
replies = None
if 'from_user' in data: #For tweets collected using v1 API
user_data = data
from_user = data['from_user']
else:
user_data = data['user']
from_user = user_data['screen_name']
if https:
img = bleach.clean(user_data['profile_image_url_https'])
else:
img = bleach.clean(user_data['profile_image_url'])
return {'profile_img': img,
'user': from_user,
'text': bleach.clean(data['text']),
'id': tweet.pk,
'date': date,
'reply_count': len(replies) if replies else 0,
'replies': replies,
'reply_to': tweet.reply_to and tweet.reply_to.pk,
'hidden': tweet.hidden}
def _get_tweets(locale=settings.LANGUAGE_CODE, limit=MAX_TWEETS, max_id=None,
reply_to=None, filter=None, https=False):
"""
Fetch a list of tweets.
Args:
limit: the maximum number of tweets returned
max_id: Return tweets with tweet_id less than this.
reply_to: Return only tweets that are replies to the given Tweet. If
None, return only top-level (non-reply) tweets.
filter: One of the keys from FILTERS
"""
locale = settings.LOCALES[locale].iso639_1
created_limit = datetime.now() - timedelta(days=settings.CC_TWEETS_DAYS)
# Uncached so we can immediately see our reply if we switch to the Answered
# filter:
q = Tweet.uncached.filter(
locale=locale,
reply_to=reply_to,
created__gt=created_limit)
if max_id:
q = q.filter(pk__lt=max_id)
if filter != 'all':
q = q.filter(hidden=False)
if filter == 'unanswered':
q = q.filter(replies__pk__isnull=True)
elif filter == 'answered':
q = q.filter(replies__pk__isnull=False)
if limit:
q = q[:limit]
return [_tweet_for_template(tweet, https) for tweet in q]
def _count_answered_tweets(since=None):
q = Reply.uncached.values('reply_to_tweet_id').distinct()
if since:
q = q.filter(created__gte=since)
return q.count()
@require_GET
def more_tweets(request):
"""AJAX view returning a list of tweets."""
max_id = request.GET.get('max_id')
raw_filter = request.GET.get('filter')
filter = raw_filter if raw_filter in FILTERS else 'recent'
return render(request, 'customercare/tweets.html', {
'tweets': _get_tweets(
locale=request.LANGUAGE_CODE,
max_id=max_id,
filter=filter,
https=request.is_secure())})
@require_GET
@anonymous_csrf # Need this so the anon csrf gets set for forms rendered.
@twitter.auth_wanted
def landing(request):
"""Customer Care Landing page."""
# Get a redis client
redis = None
try:
redis = redis_client(name='default')
except RedisError as e:
statsd.incr('redis.errror')
log.error('Redis error: %s' % e)
contributor_stats = redis and redis.get(settings.CC_TOP_CONTRIB_CACHE_KEY)
if contributor_stats:
contributor_stats = json.loads(contributor_stats)
statsd.incr('customercare.stats.contributors.hit')
else:
statsd.incr('customercare.stats.contributors.miss')
try:
twitter_user = (request.twitter.api.auth.get_username() if
request.twitter.authed else None)
except tweepy.TweepError:
# Bad oauth token. Create a new session so user re-auths.
twitter_user = None
request.twitter = twitter.Session()
yesterday = datetime.now() - timedelta(days=1)
recent_replied_count = _count_answered_tweets(since=yesterday)
return render(request, 'customercare/landing.html', {
'contributor_stats': contributor_stats,
'canned_responses': get_common_replies(request.LANGUAGE_CODE),
'tweets': _get_tweets(locale=request.LANGUAGE_CODE,
https=request.is_secure()),
'authed': request.twitter.authed,
'twitter_user': twitter_user,
'filters': FILTERS,
'goal': settings.CC_REPLIES_GOAL,
'recent_replied_count': recent_replied_count})
@require_POST
@anonymous_csrf
@twitter.auth_required
def twitter_post(request):
"""Post a tweet, and return a rendering of it (and any replies)."""
try:
reply_to_id = int(request.POST.get('reply_to', ''))
except ValueError:
# L10n: the tweet needs to be a reply to another tweet.
return HttpResponseBadRequest(_('Reply-to is empty'))
content = request.POST.get('content', '')
if len(content) == 0:
# L10n: the tweet has no content.
return HttpResponseBadRequest(_('Message is empty'))
if len(content) > 140:
return HttpResponseBadRequest(_('Message is too long'))
try:
username = request.twitter.api.auth.get_username()
if username in settings.CC_BANNED_USERS:
return render(request, 'customercare/tweets.html',
{'tweets': []})
result = request.twitter.api.update_status(content, reply_to_id)
except tweepy.TweepError, e:
# L10n: {message} is an error coming from our twitter api library
return HttpResponseBadRequest(
_('An error occured: {message}').format(message=e))
# Store reply in database.
# If tweepy's status models actually implemented a dictionary, it would
# be too boring.
status = dict(result.__dict__)
author = dict(result.author.__dict__)
# Raw JSON blob data
# Note: The JSON for the tweet posted is different than what we get from
# the search API. This makes it similar with the fields that we use.
raw_tweet_data = {
'id': status['id'],
'text': status['text'],
'created_at': formatdate(calendar.timegm(
status['created_at'].timetuple())),
'iso_language_code': author['lang'],
'from_user_id': author['id'],
'from_user': author['screen_name'],
'profile_image_url': author['profile_image_url'],
'profile_image_url_https': author['profile_image_url_https'],
}
# The tweet with id `reply_to_id` will not be missing from the DB unless
# the purge cron job has run since the user loaded the form and we are
# replying to a deleted tweet. TODO: Catch integrity error and log or
# something.
tweet = Tweet.objects.create(pk=status['id'],
raw_json=json.dumps(raw_tweet_data),
locale=author['lang'],
created=status['created_at'],
reply_to_id=reply_to_id)
# Record in our Reply table.
Reply.objects.create(
user=request.user if request.user.is_authenticated() else None,
twitter_username=author['screen_name'],
tweet_id=status['id'],
raw_json=json.dumps(raw_tweet_data),
locale=author['lang'],
created=status['created_at'],
reply_to_tweet_id=reply_to_id
)
# We could optimize by not encoding and then decoding JSON.
return render(request, 'customercare/tweets.html', {
'tweets': [_tweet_for_template(tweet, request.is_secure())]})
@require_POST
@anonymous_csrf
def hide_tweet(request):
"""
Hide the tweet with a given ID. Only hides tweets that are not replies
and do not have replies.
Returns proper HTTP status codes.
"""
# If feature disabled, bail.
if not settings.CC_ALLOW_REMOVE:
return HttpResponse(status=418) # I'm a teapot.
try:
id = int(request.POST.get('id'))
except (ValueError, TypeError):
return HttpResponseBadRequest(_('Invalid ID.'))
try:
tweet = Tweet.objects.get(pk=id)
except Tweet.DoesNotExist:
return HttpResponseNotFound(_('Invalid ID.'))
if (tweet.reply_to is not None or
Tweet.objects.filter(reply_to=tweet).exists()):
return HttpResponseBadRequest(_('Tweets that are replies or have '
'replies must not be hidden.'))
try:
tweet.hidden = True
tweet.save(force_update=True)
except Exception, e:
return HttpResponseServerError(
_('An error occured: {message}').format(message=e))
return HttpResponse('ok')