Skip to content

Commit

Permalink
Store replies to customer care in the database. Bug 612931.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Wenzel committed Dec 7, 2010
1 parent d5b28d1 commit f9e4846
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion apps/customercare/cron.py
Expand Up @@ -83,7 +83,8 @@ def collect_tweets():

# When all is done, truncate list of tweets to (approx.) maximum number.
try:
keep_tweet = Tweet.objects.all()[settings.CC_MAX_TWEETS]
keep_tweet = Tweet.objects.filter(
reply_to=None)[settings.CC_MAX_TWEETS]
except IndexError:
pass
else:
Expand Down
2 changes: 2 additions & 0 deletions apps/customercare/models.py
Expand Up @@ -12,6 +12,8 @@ class Tweet(ModelBase):
raw_json = models.TextField()
locale = models.CharField(max_length=20, db_index=True)
created = models.DateTimeField(default=datetime.now, db_index=True)
reply_to = models.BigIntegerField(blank=True, null=True, default=None,
db_index=True)

class Meta:
get_latest_by = 'created'
Expand Down
35 changes: 32 additions & 3 deletions apps/customercare/views.py
@@ -1,7 +1,8 @@
from datetime import datetime
from email.Utils import parsedate
from email.utils import parsedate, formatdate
import json
import logging
import time

from django.conf import settings
from django.core.cache import cache
Expand Down Expand Up @@ -32,7 +33,7 @@ def _get_tweets(limit=MAX_TWEETS, max_id=None):
max_id will only return tweets with the status ids less than the given id.
"""
tweets = []
q = Tweet.objects.filter(locale='en')
q = Tweet.objects.filter(locale='en', reply_to=None)
if max_id:
q = q.filter(tweet_id__lt=max_id)
if limit:
Expand Down Expand Up @@ -128,8 +129,36 @@ def twitter_post(request):
return HttpResponseBadRequest('Message is too long')

try:
request.twitter.api.update_status(content, reply_to)
result = request.twitter.api.update_status(content, reply_to)
except tweepy.TweepError, e:
return HttpResponseBadRequest('An error occured: %s' % 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
raw_tweet_data = {
'id': status['id'],
'text': status['text'],
'created_at': formatdate(time.mktime(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'],
}
# Tweet metadata
tweet_model_data = {
'tweet_id': status['id'],
'raw_json': json.dumps(raw_tweet_data),
'locale': author['lang'],
'created': status['created_at'],
'reply_to': reply_to,
}
tweet = Tweet(**tweet_model_data)
tweet.save()

return HttpResponse()
2 changes: 2 additions & 0 deletions migrations/62-customercare-replies.sql
@@ -0,0 +1,2 @@
ALTER TABLE `customercare_tweet` ADD `reply_to` BIGINT NULL DEFAULT NULL ;
ALTER TABLE `customercare_tweet` ADD INDEX ( `reply_to` ) ;

0 comments on commit f9e4846

Please sign in to comment.