<commit_before>import datetime
from typing import List

from django.contrib.auth.decorators import login_required
from django.http import JsonResponse

from clients.models import Card
from doctor_call.models import DoctorCall
from laboratory.utils import current_time
from utils.data_verification import data_parse


@login_required
def create(request):
    data = data_parse(
        request.body,
        {
            'card_pk': 'card',
            'comment': 'str_strip',
            'date': str,
            'district': int,
            'fact_address': 'str_strip',
            'researches': list,
            'phone': 'str_strip',
            'doc': int,
            'purpose': int,
            'hospital': int,
        },
    )

    card: Card = data[0]
    comment: str = data[1]
    date: str = data[2]
    district: int = data[3]
    fact_address: str = data[4]
    researches: List[int] = data[5]
    phone: str = data[6]
    doc: int = data[7]
    purpose: int = data[8]
    hospital: int = data[9]

    card_updates = []
    if district != (card.district_id or -1):
        card.district_id = district if district > -1 else None
        card_updates.append('district_id')

    #     if fact_address != card.fact_address:
    #         card.fact_address = fact_address
    #         card_updates.append('fact_address')

    if phone != card.phone:
        card.phone = phone
        card_updates.append('phone')

    if card_updates:
        card.save(update_fields=card_updates)

    for research_pk in researches:
        DoctorCall.doctor_call_save(
            {
                'card': card,
                'research': research_pk,
                'address': fact_address,
                'district': district,
                'date': date,
                'comment': comment,
                'phone': phone,
                'doc': doc,
                'purpose': purpose,
                'hospital': hospital,
                'external': False,
                'num_book': -1,
            },
            request.user.doctorprofile,
        )

    return JsonResponse({"ok": True})


@login_required
def actual_rows(request):
    data = data_parse(request.body, {'card_pk': int})
    card_pk: int = data[0]

    date_from = datetime.datetime.combine(current_time(), datetime.time.min)

    rows = list(
        DoctorCall.objects.filter(client_id=card_pk, exec_at__gte=date_from)
        .order_by('exec_at', 'pk')
        .values(
            'pk',
            'exec_at',
            'research__title',
            'comment',
            'cancel',
            'district__title',
            'address',
            'phone',
            'cancel',
            'doc_assigned__fio',
            'doc_assigned__podrazdeleniye__title',
            'purpose',
            'hospital__title',
            'hospital__short_title',
        )
    )

    return JsonResponse(rows, safe=False)


@login_required
def cancel_row(request):
    data = data_parse(request.body, {'pk': int})
    pk_row: int = data[0]
    row = DoctorCall.objects.get(pk=pk_row)
    row.cancel = not row.cancel
    row.save()

    return JsonResponse(True, safe=False)
<commit_msg>doc_call log t cancel<commit_after>import datetime
import json
from typing import List

from django.contrib.auth.decorators import login_required
from django.http import JsonResponse

from slog.models import Log
from clients.models import Card
from doctor_call.models import DoctorCall
from laboratory.utils import current_time
from utils.data_verification import data_parse
import simplejson as json_simple


@login_required
def create(request):
    data = data_parse(
        request.body,
        {
            'card_pk': 'card',
            'comment': 'str_strip',
            'date': str,
            'district': int,
            'fact_address': 'str_strip',
            'researches': list,
            'phone': 'str_strip',
            'doc': int,
            'purpose': int,
            'hospital': int,
        },
    )

    card: Card = data[0]
    comment: str = data[1]
    date: str = data[2]
    district: int = data[3]
    fact_address: str = data[4]
    researches: List[int] = data[5]
    phone: str = data[6]
    doc: int = data[7]
    purpose: int = data[8]
    hospital: int = data[9]

    card_updates = []
    if district != (card.district_id or -1):
        card.district_id = district if district > -1 else None
        card_updates.append('district_id')

    #     if fact_address != card.fact_address:
    #         card.fact_address = fact_address
    #         card_updates.append('fact_address')

    if phone != card.phone:
        card.phone = phone
        card_updates.append('phone')

    if card_updates:
        card.save(update_fields=card_updates)

    for research_pk in researches:
        DoctorCall.doctor_call_save(
            {
                'card': card,
                'research': research_pk,
                'address': fact_address,
                'district': district,
                'date': date,
                'comment': comment,
                'phone': phone,
                'doc': doc,
                'purpose': purpose,
                'hospital': hospital,
                'external': False,
                'num_book': -1,
            },
            request.user.doctorprofile,
        )

    return JsonResponse({"ok": True})


@login_required
def actual_rows(request):
    data = data_parse(request.body, {'card_pk': int})
    card_pk: int = data[0]

    date_from = datetime.datetime.combine(current_time(), datetime.time.min)

    rows = list(
        DoctorCall.objects.filter(client_id=card_pk, exec_at__gte=date_from)
        .order_by('exec_at', 'pk')
        .values(
            'pk',
            'exec_at',
            'research__title',
            'comment',
            'cancel',
            'district__title',
            'address',
            'phone',
            'cancel',
            'doc_assigned__fio',
            'doc_assigned__podrazdeleniye__title',
            'purpose',
            'hospital__title',
            'hospital__short_title',
        )
    )

    return JsonResponse(rows, safe=False)


@login_required
def cancel_row(request):
    data = data_parse(request.body, {'pk': int})
    pk_row: int = data[0]
    row = DoctorCall.objects.get(pk=pk_row)
    row.cancel = not row.cancel
    row.save()

    Log(
        key=data[0],
        type=80004,
        body=json_simple.dumps({"card_pk": row.client.pk, "status": row.cancel}),
        user=request.user.doctorprofile,
    ).save()

    return JsonResponse(True, safe=False)

{'fraction_non_alphanumeric': 0.11331917475728155, 'fraction_numerical': 0.0054611650485436895, 'mean_word_length': 1.9485688729874777, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '5866229', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1931, 'n_words': 430}