<commit_before>import json

from django.http import HttpResponse, HttpResponseBadRequest
from django.views.generic.edit import BaseFormView

from distributor.packages import handlers
from distributor.packages.forms import PackageUploadForm
from distributor.packages.settings import CHANNEL_SETTINGS


class PackageUploadView(BaseFormView):
    form_class = PackageUploadForm

    def get(self, request, *args, **kwargs):
        return HttpResponseBadRequest('Internal service url, please see documentation on how to communicate.')

    def select_handler(self, raw_channel):
        if raw_channel in CHANNEL_SETTINGS:
            channel = CHANNEL_SETTINGS[raw_channel]
            try:
                handler = getattr(handlers, channel)
            except:
                raise Exception("Unable to load handler for this channel.")
            else:
                return handler
        raise Exception("Unknown channel, please check the value or the configuration in the settings file.")

    def form_valid(self, form):
        try:
            handler = self.select_handler(form.cleaned_data.get('channel'))
        except Exception as e:
            return self.response(False, {'Selector': str(e)})
        else:
            handler.handle_upload(form.cleaned_data.get('package'), self.request.FILES['package'])
            return self.response(True)

    def form_invalid(self, form):
        return self.response(False, form.errors)

    def response(self, success, errors={}):
        data = {
            'status': 'error'
        }
        if success:
            data['status'] = 'ok'
        else:
            data['errors'] = errors
        return HttpResponse(json.dumps(data))
<commit_msg>add a minimal view to provide version information as json<commit_after>import json

from django.http import HttpResponse, HttpResponseBadRequest
from django.views.generic.edit import View, BaseFormView

from distributor.packages import handlers
from distributor.packages.forms import PackageUploadForm
from distributor.packages.settings import CHANNEL_SETTINGS


class PackageVersionView(View):
    def get(self, request, *args, **kwargs):
        version = '0.0.0'
        response = {
            'version': version
        }
        return HttpResponse(json.dumps(response))


class PackageUploadView(BaseFormView):
    form_class = PackageUploadForm

    def get(self, request, *args, **kwargs):
        return HttpResponseBadRequest('Internal service url, please see documentation on how to communicate.')

    def select_handler(self, raw_channel):
        if raw_channel in CHANNEL_SETTINGS:
            channel = CHANNEL_SETTINGS[raw_channel]
            try:
                handler = getattr(handlers, channel)
            except:
                raise Exception("Unable to load handler for this channel.")
            else:
                return handler
        raise Exception("Unknown channel, please check the value or the configuration in the settings file.")

    def form_valid(self, form):
        try:
            handler = self.select_handler(form.cleaned_data.get('channel'))
        except Exception as e:
            return self.response(False, {'Selector': str(e)})
        else:
            handler.handle_upload(form.cleaned_data.get('package'), self.request.FILES['package'])
            return self.response(True)

    def form_invalid(self, form):
        return self.response(False, form.errors)

    def response(self, success, errors={}):
        data = {
            'status': 'error'
        }
        if success:
            data['status'] = 'ok'
        else:
            data['errors'] = errors
        return HttpResponse(json.dumps(data))

{'fraction_non_alphanumeric': 0.08859387699810349, 'fraction_numerical': 0.000812787862367922, 'mean_word_length': 2.641025641025641, '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': '3686313', 'n_tokens_mistral': 949, 'n_tokens_neox': 920, 'n_words': 270}