<commit_before>"""Views to control requests for first and last tweet ids, posting of tweet data, and new tweet requests."""
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from .models import Tweet
from .serializers import TweetSerializer


class JSONResponse(HttpResponse):
    """An HttpResponse that renders content to JSON."""

    def __init__(self, data, **kwargs):
        """An init method."""
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)


@csrf_exempt
@api_view(['GET', 'POST'])
def tweet_loader(request):
    """Get first and last tweet id from database, or add tweets."""
    if request.method == 'GET':
        try:
            tweets = Tweet.objects.all().order_by('tweet_id')
            data = {'oldest': tweets.first().tweet_id, 'newest': tweets.last().tweet_id}
        except AttributeError:
            data = {'oldest': 0, 'newest': 0}

        # json = JSONRenderer.render(data)
        return JSONResponse(data)

    if request.method == 'POST':
        new_tweet_list = request.data
        for tweet in new_tweet_list:
            new_tweet = TweetSerializer(tweet)
            new_tweet.is_valid()
            if new_tweet.is_valid():
                new_tweet.create()<commit_msg>changed post serializer input from positional paramater to keyword.<commit_after>"""Views to control requests for first and last tweet ids, posting of tweet data, and new tweet requests."""
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from .models import Tweet
from .serializers import TweetSerializer


class JSONResponse(HttpResponse):
    """An HttpResponse that renders content to JSON."""

    def __init__(self, data, **kwargs):
        """An init method."""
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)


@csrf_exempt
@api_view(['GET', 'POST'])
def tweet_loader(request):
    """Get first and last tweet id from database, or add tweets."""
    if request.method == 'GET':
        try:
            tweets = Tweet.objects.all().order_by('tweet_id')
            data = {'oldest': tweets.first().tweet_id, 'newest': tweets.last().tweet_id}
        except AttributeError:
            data = {'oldest': 0, 'newest': 0}

        # json = JSONRenderer.render(data)
        return JSONResponse(data)

    if request.method == 'POST':
        new_tweet_list = request.data
        for tweet in new_tweet_list:
            new_tweet = TweetSerializer(data=tweet)
            new_tweet.is_valid()
            if new_tweet.is_valid():
                new_tweet.create()
{'fraction_non_alphanumeric': 0.10620031796502384, 'fraction_numerical': 0.0012718600953895071, 'mean_word_length': 3.2861035422343323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4122988', 'n_tokens_mistral': 904, 'n_tokens_neox': 868, 'n_words': 259}