<commit_before>__author__ = 'Claude'
import json
from django.utils import timezone
from django.shortcuts import HttpResponse
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from Web.models import VM
from Web.models import DeleteApplication
from Web.communication import communicate


@login_required
def apply_delete_vm(request):
    user = request.user
    vm_uuid = request.POST.get('uuid', '')
    try:
        vm = VM.objects.get(uuid=vm_uuid)
        if vm.user == user:
            application = DeleteApplication(applicant=user, vm=vm, host=vm.host, state='pending')
            application.save()
            return HttpResponse('1')
        else:
            return HttpResponse('0')
    except ObjectDoesNotExist:
        return HttpResponse('0')


def delete_vm(application_id):
    try:
        application = DeleteApplication.objects.get(id=application_id)
        host = application.vm.host
        request_dict = dict(request_id=application_id, request_type='delete', request_userid=application.applicant.id,
                            vm_name=application.vm.name, vm_uuid=application.vm.uuid)
        response = communicate(request_dict, host.ip, host.vm_manager_port)
        if response and response['request_result'] == 'success':
            application.state = 'success'
            application.host = host
            ports = json.loads(host.ports_info)
            ports["free"].append(application.vm.info.ssh_port)
            ports["used"].remove(application.vm.info.ssh_port)
            host.ports_info = json.dumps(ports)
            host.save()
        elif not response:<commit_msg>bug fix<commit_after>__author__ = 'Claude'
import json
from django.utils import timezone
from django.shortcuts import HttpResponse
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from Web.models import VM
from Web.models import DeleteApplication
from Web.communication import communicate


@login_required
def apply_delete_vm(request):
    user = request.user
    vm_uuid = request.POST.get('uuid', '')
    try:
        vm = VM.objects.get(uuid=vm_uuid)
        if vm.user == user:
            application = DeleteApplication(applicant=user, vm=vm, host=vm.host, state='pending')
            application.save()
            return HttpResponse('1')
        else:
            return HttpResponse('0')
    except ObjectDoesNotExist:
        return HttpResponse('0')


def delete_vm(application_id):
    try:
        application = DeleteApplication.objects.get(id=application_id)
        host = application.vm.host
        request_dict = dict(request_id=application_id, request_type='delete', request_userid=application.applicant.id,
                            vm_name=application.vm.name, vm_uuid=application.vm.uuid)
        response = communicate(request_dict, host.ip, host.vm_manager_port)
        if response and response['request_result'] == 'success':
            application.state = 'success'
            application.host = host
            ports = json.loads(host.ports_info)
            nat_rules = json.loads(application.vm.nat_rules)
            for rule in nat_rules:
                ports["free"].append(rule["host_port"])
                ports["used"].remove(rule["host_port"])
            ports["free"].append(application.vm.info.ssh_port)
            ports["used"].remove(application.vm.info.ssh_port)
            host.ports_info = json.dumps(ports)
            host.save()
        elif not response:
{'fraction_non_alphanumeric': 0.09926884139482564, 'fraction_numerical': 0.001687289088863892, 'mean_word_length': 2.900219298245614, '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': '2448291', 'n_tokens_mistral': 975, 'n_tokens_neox': 959, 'n_words': 200}