Exemplo n.º 1
0
 def post(self, request):  # обработка post запроса
     bound_form = ConverterForm(request.POST)
     if bound_form.is_valid():  # если ворма валидна
         some_number = bound_form.clean_number()  # очищеная строка(меньше 4000 и содержащее символы "IVXCDML")
         if some_number.isdigit():  # если в строке только цифры отправляем конвертировать из арабских с римские
             n = json.dumps(arabic_to_roman(some_number))
             n = re.sub('["]', '', n)
             return render(request, 'konverter.html', context={'n': n, 'form': bound_form})
         else:  # если в строке только буквы - конвертация из римких с арабские
             n = json.dumps(roman_to_arabic(some_number.upper()))
             n = re.sub('["]', '', n)
             return render(request, 'konverter.html', context={'n': n, 'form': bound_form})
     else:  # если не валидна
         return render(request, 'konverter.html', context={'form': bound_form})
Exemplo n.º 2
0
def converter_view(request):
    '''
        Defines the view for Converting an Integer to Roman numeral.
        :param request:
        :return: form with integer and/or roman numeral
    '''

    form = ConverterForm()
    if request.method == 'POST':
        form = ConverterForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            integer = cleaned_data.get('integer')
            request.integer = integer
            request.result = int2roman(integer)
            return render(request, 'converter.html', {'form': form})

    return render(request, 'converter.html', {'form': form})
Exemplo n.º 3
0
def converter_view(request):

    '''
        Defines the view for Converting an Integer to Roman numeral.
        :param request:
        :return: form with integer and/or roman numeral
    '''

    form = ConverterForm()
    if request.method == 'POST':
        form = ConverterForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            integer = cleaned_data.get('integer')
            request.integer = integer
            request.result = int2roman(integer)
            return render(request, 'converter.html', {'form': form})

    return render(request, 'converter.html', {'form': form})
Exemplo n.º 4
0
def test_invalid_form(self):
    data = {'integer': 4000,}
    form = ConverterForm(data=data)
    self.assertFalse(form.is_valid())
Exemplo n.º 5
0
def test_valid_form(self):
    data = {'integer': 23,}
    form = ConverterForm(data=data)
    self.assertTrue(form.is_valid())