<commit_before>from django.http import HttpResponse
from django.shortcuts import get_object_or_404  # , render
from django.utils import timezone
from django.views import generic

from .models import Channel, Record, Field

import re

field_pattern = re.compile(r"(field[0-9]+)")
HTTP_WRITE_KEY = "X_Write_API_Key"


class IndexView(generic.ListView):
    template_name = 'weather_station_app/index.html'
    context_object_name = 'latest_channels_edited'

    def get_queryset(self):
        return Channel.objects.all()


class ChannelView(generic.ListView):
    template_name = 'weather_station_app/channel.html'
    context_object_name = 'latest_records_added'

    n_elements_display = 10

    def get_queryset(self):
        return Record.objects.order_by('-insertion_time')[:self.n_elements_display]


def upload(request, channel_id):
    """View that analyses and performs some checks on an HTTP request 
    submitted to a specific channel.

    Example of POST request:

        POST /12345678/upload HTTP/1.1
        Host: localhost
        Connection: close
        User-Agent: __whatever__
        X-Write-API-Key: <KEY>
        Content-Type: application/x-www-form-urlencoded
        Content-Length: 10

        field2=42


    Reference for the HTTP status codes
    https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    """
    
    if request.method == "POST":
        write_API_key = request.META.get("HTTP_{}".format(HTTP_WRITE_KEY.upper()))

        if write_API_key:
            channel = get_object_or_404(Channel, pk=channel_id)

            if str(channel.write_key) == write_API_key:
                # Collect the fields value from the body of the http POST 
                # message into a dictionary.
                fields = {k: v for (k, v) in request.POST.items()
                               if field_pattern.match(k)}

                if len(fields) > Record.MAX_NUMBER_FIELDS:
                    return HttpResponse("Max number of fields exceeded.", status=406)

                elif any(fields):
                    r = Record.objects.create(channel=channel,
                                              insertion_time=timezone.now() )

                    # Create new Field objects to link to the newly created record.
                    for i, k in enumerate(fields):
                        val = fields[k]
                        # Store in the db only the fields with a value.
                        if not val:
                            continue

                        # Shift the field counter by 1, since enumerate starts from 0.
                        i += 1
                        Field.objects.create(record=r, field_no=i, value=val)

                        return HttpResponse(status=200)

                else:
                    return HttpResponse("Send at least one field inside your "
                                        "message.", status=406)
            else:
                return HttpResponse("Incorrect API key associated with the "
                             "channel you have chosen.", status=400)
        else:
            return HttpResponse("Missing writing API key.", status=400)
    else:
        return HttpResponse("Only POST requests allowed.", status=400)<commit_msg>Avoid higher indentation level by putting branch with 'return' statements first<commit_after>from django.http import HttpResponse
from django.shortcuts import get_object_or_404  # , render
from django.utils import timezone
from django.views import generic

from .models import Channel, Record, Field

import re

field_pattern = re.compile(r"(field[0-9]+)")
HTTP_WRITE_KEY = "X_Write_API_Key"


class IndexView(generic.ListView):
    template_name = 'weather_station_app/index.html'
    context_object_name = 'latest_channels_edited'

    def get_queryset(self):
        return Channel.objects.all()


class ChannelView(generic.ListView):
    template_name = 'weather_station_app/channel.html'
    context_object_name = 'latest_records_added'

    n_elements_display = 10

    def get_queryset(self):
        return Record.objects.order_by('-insertion_time')[:self.n_elements_display]


def upload(request, channel_id):
    """View that analyses and performs some checks on an HTTP request
    submitted to a specific channel.

    Example of POST request:

        POST /12345678/upload HTTP/1.1
        Host: localhost
        Connection: close
        User-Agent: __whatever__
        X-Write-API-Key: <KEY>
        Content-Type: application/x-www-form-urlencoded
        Content-Length: 10

        field2=42


    Reference for the HTTP status codes
    https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    """

    if request.method != "POST":
        return HttpResponse("Only POST requests allowed.", status=400)

    write_API_key = request.META.get("HTTP_{}".format(HTTP_WRITE_KEY.upper()))

    if not write_API_key:
        return HttpResponse("Missing writing API key.", status=400)

    channel = get_object_or_404(Channel, pk=channel_id)

    if str(channel.write_key) != write_API_key:
        return HttpResponse("Incorrect API key associated with the channel you "
                            "have chosen.", status=400)

    # Collect the fields value from the body of the http POST
    # message into a dictionary.
    fields = {k: v for (k, v) in request.POST.items()
              if field_pattern.match(k)}

    if len(fields) > Record.MAX_NUMBER_FIELDS:
        return HttpResponse("Max number of fields exceeded.", status=406)

    elif any(fields):
        r = Record.objects.create(channel=channel,
                                  insertion_time=timezone.now())

        # Create new Field objects to link to the newly created record.
        for i, k in enumerate(fields):
            val = fields[k]
            # Store in the db only the fields with a value.
            if not val:
                continue

            # Shift the field counter by 1, since enumerate starts from 0.
            i += 1
            Field.objects.create(record=r, field_no=i, value=val)

            return HttpResponse(status=200)

    else:
        return HttpResponse("Send at least one field inside your message.",
                            status=406)
{'fraction_non_alphanumeric': 0.0825188271110399, 'fraction_numerical': 0.01826630347700689, 'mean_word_length': 2.201025641025641, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2107813', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1634, 'n_words': 483}