def post_to_twitter(user, status): """Helper function to post a user's status to her authorized Twitter account.""" posting_status = '' try: tw_prof = user.get_profile().twitter_profile if tw_prof and tw_prof.access_token: _log.debug("Posting %s's status - %s", user.username.title(), status) tw = TwitterAPI() posting_status = tw.post_status(tw_prof.access_token, status) tw.close() s = json.loads(posting_status) if s.get('id', False): _log.debug("Posted %s's status - %s", user.username.title(), s.get("text", "NO TEXT")) return s else: _log.error("Could not post %s's status:\n%s\n", user.username.title(), posting_status) return False except Exception, e: _log.exception(e) if posting_status: _log.debug("Posting status error:\n%s", posting_status) if tw_prof and 'invalid oauth request' in posting_status.lower( ) or 'failed to validate oauth' in posting_status.lower(): # Remove unusable access token tw_prof.access_token = u'' tw_prof.save() return False
def post_to_twitter(user, status): """Helper function to post a user's status to her authorized Twitter account.""" posting_status = '' try: tw_prof = user.get_profile().twitter_profile if tw_prof and tw_prof.access_token: _log.debug("Posting %s's status - %s", user.username.title(), status) tw = TwitterAPI() posting_status = tw.post_status(tw_prof.access_token, status) tw.close() s = json.loads(posting_status) if s.get('id', False): _log.debug("Posted %s's status - %s", user.username.title(), s.get("text", "NO TEXT")) return s else: _log.error("Could not post %s's status:\n%s\n", user.username.title(), posting_status) return False except Exception, e: _log.exception(e) if posting_status: _log.debug("Posting status error:\n%s", posting_status) if tw_prof and 'invalid oauth request' in posting_status.lower() or 'failed to validate oauth' in posting_status.lower(): # Remove unusable access token tw_prof.access_token = u'' tw_prof.save() return False
def save(self, commit=True): comment = super(CommentForm, self).save(commit=False) comment.user = self.user comment.content_type = self.content_type comment.object_pk = self.object_pk comment.is_public = self.is_public comment.submit_date = datetime.now() comment.site = self.site comment.save() try: if self.cleaned_data.get("do_tweet", False) and getattr(settings, 'ENABLE_COMMENT_TWEET', True): tw_prof = self.user.get_profile().twitter_profile if tw_prof and tw_prof.access_token: tw = TwitterAPI() tweet = tw.post_status(tw_prof.access_token, comment.comment) tw.close() self.tweet_posted(comment, tweet) except Exception, e: _log.exception(e)
def save(self, commit=True): comment = super(CommentForm, self).save(commit=False) comment.user = self.user comment.content_type = self.content_type comment.object_pk = self.object_pk comment.is_public = self.is_public comment.submit_date = datetime.now() comment.site = self.site comment.save() try: if self.cleaned_data.get("do_tweet", False) and getattr( settings, 'ENABLE_COMMENT_TWEET', True): tw_prof = self.user.get_profile().twitter_profile if tw_prof and tw_prof.access_token: tw = TwitterAPI() tweet = tw.post_status(tw_prof.access_token, comment.comment) tw.close() self.tweet_posted(comment, tweet) except Exception, e: _log.exception(e)