<commit_before># coding: utf-8

import datetime
from itertools import groupby

from django.conf import settings
from django.db import transaction
from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import View

from item.forms import ItemForm, ItemDeleteForm, ItemMetaAutoForm
from item.models import Item


class YearMonthItemApi(View):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.date = None

    def get_queryset(self, date: datetime.date):
        return Item.objects.filter(date__year=date.year, date__month=date.month)

    def dispatch(self, request, *args, year: str, month: str, **kwargs):
        self.date = datetime.date(int(year), int(month), 1)

        return super().dispatch(request, *args, year, month, **kwargs)

    def get(self, *args, **kwargs):
        qs = self.get_queryset(self.date)

        date_item_list = []

        for date, items in groupby(qs, lambda x: x.date):  # type: datetime.date, List[Item]
            item_list = []
            date_item_list.append({
                'date': date.strftime('%Y-%m-%d'),
                'items': item_list
            })

            for item in items:
                item_list.append({
                    'price': '{0:.2f}'.format(item.price),
                    'tags': list(item.tags.names()),
                    'id': item.id
                })

        return JsonResponse(date_item_list, safe=False)


class ItemApi(View):
    def post(self, request):
        form = ItemForm(request.POST)

        if form.is_valid():
            item = form.save()
            return JsonResponse({
                'date': item.date,
                'item': {
                    'price': '{0:.2f}'.format(item.price),
                    'tags': list(item.tags.names()),
                    'id': item.id
                }
            }, safe=False)
        else:
            raise NotImplementedError(form.errors)

    def delete(self, request):
        form = ItemDeleteForm(request.DELETE)
        if form.is_valid():
            with transaction.atomic():
                form.cleaned_data['items'].delete()
                return JsonResponse({})
        else:
            raise NotImplementedError(form.errors)

    def patch(self, request, item_id: int):
        item = Item.objects.get(pk=item_id)
        form = ItemForm(request.PATCH, instance=item)

        if form.is_valid():
            form.save()
            return JsonResponse({})
        else:
            raise NotImplementedError(form.errors)


class ItemList(View):
    def get(self, request, year, month):
        date = datetime.date(int(year), int(month), 1)

        return render(request, 'item/list.html', {
            'date': date,
            'form': ItemForm()
        })


class ItemTagsAuto(View):
    def get(self, request):
        form = ItemMetaAutoForm(request.GET)
        if form.is_valid():
            return JsonResponse(form.cleaned_data['term'], safe=False)
        else:
            raise NotImplementedError(form.errors)


class Index(View):
    def get(self, request):
        context = {
            'root': settings.FORCE_SCRIPT_NAME
        }

        return render(request, 'index.html')<commit_msg>FIX: forgot to pass the template context to index.html<commit_after># coding: utf-8

import datetime
from itertools import groupby

from django.conf import settings
from django.db import transaction
from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import View

from item.forms import ItemForm, ItemDeleteForm, ItemMetaAutoForm
from item.models import Item


class YearMonthItemApi(View):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.date = None

    def get_queryset(self, date: datetime.date):
        return Item.objects.filter(date__year=date.year, date__month=date.month)

    def dispatch(self, request, *args, year: str, month: str, **kwargs):
        self.date = datetime.date(int(year), int(month), 1)

        return super().dispatch(request, *args, year, month, **kwargs)

    def get(self, *args, **kwargs):
        qs = self.get_queryset(self.date)

        date_item_list = []

        for date, items in groupby(qs, lambda x: x.date):  # type: datetime.date, List[Item]
            item_list = []
            date_item_list.append({
                'date': date.strftime('%Y-%m-%d'),
                'items': item_list
            })

            for item in items:
                item_list.append({
                    'price': '{0:.2f}'.format(item.price),
                    'tags': list(item.tags.names()),
                    'id': item.id
                })

        return JsonResponse(date_item_list, safe=False)


class ItemApi(View):
    def post(self, request):
        form = ItemForm(request.POST)

        if form.is_valid():
            item = form.save()
            return JsonResponse({
                'date': item.date,
                'item': {
                    'price': '{0:.2f}'.format(item.price),
                    'tags': list(item.tags.names()),
                    'id': item.id
                }
            }, safe=False)
        else:
            raise NotImplementedError(form.errors)

    def delete(self, request):
        form = ItemDeleteForm(request.DELETE)
        if form.is_valid():
            with transaction.atomic():
                form.cleaned_data['items'].delete()
                return JsonResponse({})
        else:
            raise NotImplementedError(form.errors)

    def patch(self, request, item_id: int):
        item = Item.objects.get(pk=item_id)
        form = ItemForm(request.PATCH, instance=item)

        if form.is_valid():
            form.save()
            return JsonResponse({})
        else:
            raise NotImplementedError(form.errors)


class ItemList(View):
    def get(self, request, year, month):
        date = datetime.date(int(year), int(month), 1)

        return render(request, 'item/list.html', {
            'date': date,
            'form': ItemForm()
        })


class ItemTagsAuto(View):
    def get(self, request):
        form = ItemMetaAutoForm(request.GET)
        if form.is_valid():
            return JsonResponse(form.cleaned_data['term'], safe=False)
        else:
            raise NotImplementedError(form.errors)


class Index(View):
    def get(self, request):
        context = {
            'root': settings.FORCE_SCRIPT_NAME
        }

        return render(request, 'index.html', context)
{'fraction_non_alphanumeric': 0.12140042437102153, 'fraction_numerical': 0.002121855107608366, 'mean_word_length': 2.0424158598432456, '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': '2793145', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1833, 'n_words': 440}