def clean(self):
# remove leading @
if self.name.startswith('@'):
self.name = self.name[1:]
twitter = TwitterAPI(settings.TWITTER_CONSUMER_KEY,
settings.TWITTER_CONSUMER_SECRET,
auth_type='oAuth2')
try:
user = twitter.request('users/show', {'screen_name': self.name}).json()
except TwitterConnectionError:
logger.warning("Could not connect to Twitter.")
raise ValidationError("Could not connect to Twitter to retrieve user_id.")
if 'id' in user:
self.account_id = user['id']
else:
logger.warning("Could not find user with provided name.")
raise ValidationError("Could not find user with provided name.")
if TwitterAccount.objects.filter(account_id=self.account_id):
logger.warning("TwitterAccount with account_id is already in database.")
raise ValidationError("Twitter account with this name already exists.")
评论列表
文章目录