def write_to_parameter_file(request):
    """
        Takes a PUT request.
        First opens the yaml file in read mode and writes it to the an 'old' file,
        which serves as a previous version for restoring.
        It then opens the yaml file in overwrite mode and writes the contents from the request to the file.
        Returns an OK status response upon successful writing.
    """
    if request.method == 'PUT':
        text = request.body.decode("utf-8")
        open_file = open(
            os.path.join(settings.BASE_DIR,
                         'resources/Pyradiomics_Params.yaml'), 'r')
        with open(
                os.path.join(settings.BASE_DIR, 'resources/backups/old.yaml'),
                'w+') as previous:
            previous.write(open_file.read())
        with open(
                os.path.join(settings.BASE_DIR,
                             'resources/Pyradiomics_Params.yaml'), 'w+') as f:
            f.write(text)
        global calculator
        calculator = calc.Calculator()
        return HttpResponse(status=status.HTTP_200_OK)
    else:
        return HttpResponseBadRequest()
def restore_old_config_file(request):
    """
        Takes a PUT request.
        First opens the old configuration file (or previous version) in read mode.
        It then opens the config.ini file in overwrite mode and writes the contents from the old file.
        Returns an OK status response upon successful writing.
    """
    if request.method == 'PUT':
        open_default = open(
            os.path.join(settings.BASE_DIR, 'resources/backups/old.ini'), 'r')
        with open(os.path.join(settings.BASE_DIR, 'resources/config.ini'),
                  'w+') as restored:
            restored.write(open_default.read())
            restored.seek(0)
        global calculator
        calculator = calc.Calculator()
        return HttpResponse(status=status.HTTP_200_OK)
    else:
        return HttpResponseBadRequest()
def restore_default_parameter_file(request):
    """
        Takes a PUT request.
        First opens the default parameter file in read mode.
        It then opens the yaml file in overwrite mode and writes the contents from the default file.
        Returns an OK status response upon successful writing.
    """
    if request.method == 'PUT':
        open_default = open(
            os.path.join(settings.BASE_DIR, 'resources/backups/default.yaml'),
            'r')
        with open(
                os.path.join(settings.BASE_DIR,
                             'resources/Pyradiomics_Params.yaml'),
                'w+') as restored:
            restored.write(open_default.read())
            restored.seek(0)
        global calculator
        calculator = calc.Calculator()
        return HttpResponse(status=status.HTTP_200_OK)
    else:
        return HttpResponseBadRequest()
# from django.shortcuts import render, HttpResponseRedirect
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
from django.views.decorators.csrf import csrf_exempt
from django.http.response import JsonResponse
from django.db import connection
# from rest_framework.parsers import JSONParser
from rest_framework import status
from django.conf import settings
from subprocess import Popen
import json
# import copy

# from pipelineapp.models import *
from pipelineapp.serializers import *

calculator: calc.Calculator = calc.Calculator()
calculator_copy: calc.Calculator = calculator

# Create your views here.


# REST METHODS
# Get data to view in front end table
# csrf_exempt allows the requests to be executed without a session token.
# When safe=False in the JSON responses, this allows for passing any data in the response, as long as they are objects
@csrf_exempt
def image_list(request):
    """
        Takes a GET request a returns a JSON response with the data from DICOMImages
    """
    if request.method == 'GET':