<commit_before>from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
import django_filters
from rest_framework import viewsets

from obcy.models import Joke
from obcy.extras import prepare_view
from api.serializers import ObcyJokeSerializer
from api.models import Device


class ObcyJokeFilter(django_filters.FilterSet):
    before = django_filters.DateTimeFilter(name='added', lookup_type='lt')
    after = django_filters.DateTimeFilter(name='added', lookup_type='gt')
    min_votes = django_filters.NumberFilter(lookup_type='gte')

    class Meta:
        model = Joke
        fields = ['after', 'before', 'min_votes']


class AllViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = ObcyJokeSerializer
    filter_class = ObcyJokeFilter
    lookup_field = 'key'
    paginate_by_param = 'limit'

    def get_queryset(self):
        return prepare_view.all_sites(self.request, pages=False)['jokes']

    def get_object(self, queryset=None):
        return prepare_view.one_joke(self.request, self.kwargs['key'])['joke']


class RandomJokes(viewsets.ReadOnlyModelViewSet):
    serializer_class = ObcyJokeSerializer
    lookup_field = 'key'
    paginate_by_param = 'limit'

    def get_queryset(self):
        return prepare_view.random(self.request, pages=False)['jokes']


@csrf_exempt
@require_POST
def register_device(request):
    registration_id = request.POST.get('registration_id')
    android_id = request.POST.get('android_id')
    try:
        device = Device.objects.get(android_id=android_id)
        device.registration_id = registration_id
        device.active = True
        device.save()
    except Device.DoesNotExist:
        Device.objects.create(registration_id=registration_id, android_id=android_id)
    return HttpResponse(status=201)


@csrf_exempt
@require_POST
def deactivate_device(request):
    registration_id = request.POST.get('registration_id')
    try:
        device = Device.objects.get(registration_id=registration_id)
        device.active = False
        device.save()
    except Device.DoesNotExist:
        pass
    return HttpResponse(status=200)<commit_msg>Return HTTP 200 code when registering device<commit_after>from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
import django_filters
from rest_framework import viewsets

from obcy.models import Joke
from obcy.extras import prepare_view
from api.serializers import ObcyJokeSerializer
from api.models import Device


class ObcyJokeFilter(django_filters.FilterSet):
    before = django_filters.DateTimeFilter(name='added', lookup_type='lt')
    after = django_filters.DateTimeFilter(name='added', lookup_type='gt')
    min_votes = django_filters.NumberFilter(lookup_type='gte')

    class Meta:
        model = Joke
        fields = ['after', 'before', 'min_votes']


class AllViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = ObcyJokeSerializer
    filter_class = ObcyJokeFilter
    lookup_field = 'key'
    paginate_by_param = 'limit'

    def get_queryset(self):
        return prepare_view.all_sites(self.request, pages=False)['jokes']

    def get_object(self, queryset=None):
        return prepare_view.one_joke(self.request, self.kwargs['key'])['joke']


class RandomJokes(viewsets.ReadOnlyModelViewSet):
    serializer_class = ObcyJokeSerializer
    lookup_field = 'key'
    paginate_by_param = 'limit'

    def get_queryset(self):
        return prepare_view.random(self.request, pages=False)['jokes']


@csrf_exempt
@require_POST
def register_device(request):
    registration_id = request.POST.get('registration_id')
    android_id = request.POST.get('android_id')
    try:
        device = Device.objects.get(android_id=android_id)
        device.registration_id = registration_id
        device.active = True
        device.save()
    except Device.DoesNotExist:
        Device.objects.create(registration_id=registration_id, android_id=android_id)
    return HttpResponse(status=200)


@csrf_exempt
@require_POST
def deactivate_device(request):
    registration_id = request.POST.get('registration_id')
    try:
        device = Device.objects.get(registration_id=registration_id)
        device.active = False
        device.save()
    except Device.DoesNotExist:
        pass
    return HttpResponse(status=200)
{'fraction_non_alphanumeric': 0.09125818838942851, 'fraction_numerical': 0.00338829907386492, 'mean_word_length': 4.406593406593407, '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': '4062981', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1321, 'n_words': 255}