Exemple #1
0
 def post(self, request):
     request.data["created_by"] = request.user.id
     serialize = TweetSerializer(data=request.data)
     if serialize.is_valid():
         serialize.save()
         return Response(serialize.data, status=status.HTTP_201_CREATED)
     return Response(serialize.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
 def put(self, request, id):
     tweet = self.get_object(id)
     serialize = TweetSerializer(tweet, data=request.data)
     if serialize.is_valid():
         serialize.save()
         return Response(serialize.data)
     return Response(serialize.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #3
0
def get_tweets(query):
    r = api_request().get('https://api.twitter.com/1.1/search/tweets.json' +
                          query,
                          headers={'Authorization': 'Bearer ' + token()})

    for status in r.json().get('statuses'):
        serializer = TweetSerializer(data=parse(status))
        if serializer.is_valid():
            serializer.save()

    next_query = r.json().get('search_metadata').get('next_results')
    if next_query:
        get_tweets.delay(next_query)
Exemple #4
0
def tweet_list(request, format=None):
    """
    List all tweets, or create a new tweet.
    """
    if request.method == 'GET':
        tweets = Tweet.objects.all()
        serializer = TweetSerializer(tweets, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = TweetSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)