Beispiel #1
0
    def post(request):
        """ Cоздание модели после ввода данных в таблице """

        user_profile = get_userprofile(request)
        model = create_model(user_profile.id,
                             demo_model=False,
                             request=request)

        if model is not False:
            create_files(model)
            return redirect('models_id', id=model.id)

        else:
            number_of_criterion = request.POST["number_of_criterion"]
            number_of_alternatives = request.POST["number_of_alternatives"]
            number_of_criterion_for_select = list(
                range(1,
                      int(number_of_criterion) + 1))
            number_of_alternatives_for_select = list(
                range(1,
                      int(number_of_alternatives) + 1))
            return render(
                request, "model/input_data.html", {
                    'number_of_criterion_for_select':
                    number_of_criterion_for_select,
                    'number_of_alternatives_for_select':
                    number_of_alternatives_for_select,
                    'error': "Ошибка при заполнении. Повторите попытку ввода"
                })
def demo_create(request):
    # Создаем демо модель на базе метода ШНУР
    if request.method == 'GET':
        model = create_model(demo_model=True)
        create_files(model)
        message = make_question(model)

        return JsonResponse(message, status=200)
Beispiel #3
0
 def post(self, request):
     uploaded_file = request.FILES['document']
     fs = FileSystemStorage()
     r_int = str(randint(0, 100))
     path_csv = f'{request.user.username}/{r_int}{uploaded_file.name}'
     fs.save(path_csv, uploaded_file)
     user_profile = get_userprofile(request)
     model = create_model(user_profile.id,
                          demo_model=False,
                          path_csv=path_csv)
     if model is False:
         error = 'Возникла ошибка при загрузке файла. Проверьте файл'
         return render(request, "upload_model.html", {'error': error})
     create_files(model)
     return redirect('models_id', id=model.id)
Beispiel #4
0
def create_model(user_id,
                 demo_model: bool = False,
                 path_csv=None,
                 request=None,
                 number_of_alternatives=None,
                 demo_vkr=False) -> object:
    """ Создание объекта модели (Поиска лучшей альтернативы для задачи выбора) """

    try:
        result = True  # Результат создания и заполнения модели

        if demo_model:
            model = Model.objects.create(is_demo=True,
                                         name='Демонстрационная',
                                         id_user=User.objects.get(id=user_id))
            if demo_vkr:
                """Выбор 1комнатной квартиры"""
                result = _filling_demo_model_vkr(model)
            else:
                result = _filling_demo_model(
                    model, number_of_alternatives
                )  # Заполняем модель исходными данными
        else:

            model = Model.objects.create(is_demo=False,
                                         name='Пользовательская',
                                         id_user=User.objects.get(id=user_id))
            if path_csv:
                result = _filling_model_from_file(
                    model,
                    path_csv=path_csv)  # Заполняем модель исходными данными

            elif request:
                """Данные модели из интерфейса система"""
                result = _filling_custom_model(model, request)

        if result is False:
            model.delete()
            return False

        _create_dir(str(model.id))
        create_files(model)
        Model.objects.filter(id=model.id).update(is_done=True)
        return model

    except Exception as e:
        pass
def auto_create(request):
    if request.method == 'GET':
        options_obj_list = []
        model = create_model(demo_model=True)

        with open("api/files/demo.csv", encoding='utf-8') as r_file:
            file_reader = csv.reader(r_file, delimiter=",")
            count = False

            criterion_number = 1
            option_number = 1
            for row in file_reader:
                if count is False:

                    for i in range(2, len(row)):
                        option = Option.objects.create(name=row[i], id_model=model, number=option_number)
                        options_obj_list.append(option)
                        option_number += 1

                    count = True

                else:

                    if row[1] == 'min':
                        direction = False
                    else:
                        direction = True

                    max = float(row[2])
                    for i in range(3, len(row)):
                        if max < float(row[i]):
                            max = float(row[i])

                    criterion = Criterion.objects.create(name=row[0], id_model=model, direction=direction, max=max,
                                                         number=criterion_number)

                    criterion_number += 1

                    for i in range(2, len(row)):
                        value = float(row[i])
                        Value.objects.create(value=value, id_option=options_obj_list[i-2], id_criterion=criterion)

        n = len(options_obj_list)
        k = 1
        for i in range(n):
            for j in range(k, n):
                if i != j:
                    s = PairsOfOptions.objects.create(id_option_1=options_obj_list[i], id_option_2=options_obj_list[j],
                                                      id_model=model)

            k += 1
        create_files(model)
        message = make_question(model)
        i = 1
        while i == 1:
            message = question(message, auto=True)
            flag = message['flag_find_winner']
            if flag == 1:
                break

        response = {'model_id': model.id}
        return JsonResponse(response, status=200)