コード例 #1
0
    def post(self, request, format=None):
        """Creates a new task"""
        required_fields = [
            'question_id', 'answer_id', 'annotation_url', 'task_type', 'phrase'
        ]
        if not all(param in request.data for param in required_fields):
            errorMsg = {'Error': "Input Error", 'Message': "Missing fields"}
            return Response(errorMsg, status=status.HTTP_400_BAD_REQUEST)

        taskType = int(request.data['task_type'])

        if not 0 <= taskType <= 2:
            errorMsg = {
                'Error': "Input Error",
                'Message': "Task Type not in range (0-2)"
            }
            return Response(errorMsg, status=status.HTTP_400_BAD_REQUEST)

        # create a new annotation with data
        newAnnotation = Annotation()
        newAnnotation.question_id = request.data['question_id']
        newAnnotation.answer_id = request.data['answer_id']
        newAnnotation.phrase = request.data['phrase']
        newAnnotation.save()

        # append and shorten url
        appended_url = request.data['annotation_url'] + "/" \
                                                      + str(newAnnotation.id) \
                                                      + "?taskType=" \
                                                      + str(taskType)

        post_url_with_key = settings.POST_URLSHORTENER_GOOGLE_URL +\
                            "?key=" + settings.GOOGLE_URL_SHORTENER_KEY
        post_header = {'Content-Type': 'application/json'}
        google_response = requests.post(post_url_with_key,
                                        data=json.dumps(
                                            {'longUrl': appended_url}),
                                        headers=post_header).json()
        if 'id' not in google_response:
            errorMsg = {
                'Error': "Google URL Shortener Error",
                'Google Response': google_response.pop('error')
            }
            newAnnotation.delete()
            return Response(errorMsg, status=status.HTTP_400_BAD_REQUEST)

        shortened_url = google_response['id']
        message = self.create_message(str(request.data['phrase'][:6] + ".."),
                                      taskType, shortened_url)

        # post twitter message
        auth = OAuth1(settings.TWITTER_CONSUMER_KEY,
                      settings.TWITTER_CONSUMER_SECRET,
                      settings.TWITTER_ACCESS_TOKEN,
                      settings.TWITTER_ACCESS_TOKEN_SECRET)
        twitter_response = requests.post(settings.POST_STATUS_TWITTER_URL,
                                         data={'status': message},
                                         auth=auth)
        tweet_info = twitter_response.json()
        if 'id' not in tweet_info:
            errorMsg = {
                'Error': "Twitter Error",
                'Twitter Response': tweet_info.pop('errors')
            }
            newAnnotation.delete()
            return Response(errorMsg, status=status.HTTP_400_BAD_REQUEST)

        # create a new task
        task = Task()
        task.tweet_id = tweet_info['id']
        task.annotation_id = newAnnotation.id
        task.task_type = taskType
        task.created_on = task.checked_on = timezone.now()
        task.save()

        return Response(TaskSerializer(task).data,
                        status=status.HTTP_201_CREATED)