Skip to content

Commit

Permalink
Use standard Exception classes (#83)
Browse files Browse the repository at this point in the history
* Use standard Exception classes
* Remove unused code
  • Loading branch information
dyve committed May 3, 2021
1 parent 7df8c27 commit aa44048
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 50 deletions.
5 changes: 2 additions & 3 deletions src/django_bootstrap5/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.utils.translation import gettext as _

from .css import merge_css_classes
from .exceptions import BootstrapError
from .html import render_tag
from .size import DEFAULT_SIZE, SIZE_MD, get_size_class

Expand Down Expand Up @@ -46,7 +45,7 @@ def render_button(

if button_type:
if button_type not in ("submit", "reset", "button", "link"):
raise BootstrapError(
raise ValueError(
(
'Parameter "button_type" should be "submit", "reset", "button", "link" or empty '
f'("{button_type}" given).'
Expand All @@ -57,7 +56,7 @@ def render_button(

if href:
if button_type and button_type != "link":
raise BootstrapError(f'Button of type "{button_type}" is not allowed a "href" parameter.')
raise ValueError(f'Button of type "{button_type}" is not allowed a "href" parameter.')
tag = "a"
attrs["href"] = href
attrs.setdefault("role", "button")
Expand Down
6 changes: 0 additions & 6 deletions src/django_bootstrap5/exceptions.py

This file was deleted.

30 changes: 4 additions & 26 deletions src/django_bootstrap5/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@

from .core import get_bootstrap_setting
from .css import merge_css_classes
from .exceptions import BootstrapError
from .forms import WRAPPER_CLASS, WRAPPER_TAG, render_field, render_form, render_label
from .size import DEFAULT_SIZE, SIZE_MD, SIZE_XS, get_size_class, parse_size
from .size import DEFAULT_SIZE, SIZE_MD, get_size_class, parse_size
from .text import text_value
from .utils import render_template_file
from .widgets import ReadOnlyPasswordHashWidget, is_widget_with_placeholder
Expand Down Expand Up @@ -68,13 +67,6 @@ def is_inline(self):
"""Return whether to render widgets with inline layout."""
return self.layout == "inline"

def parse_size(self, size):
"""Return size if it is valid, default size if size is empty, or throws exception."""
size = parse_size(size, default=DEFAULT_SIZE)
if size == SIZE_XS:
raise BootstrapError('Size "xs" is not valid for form controls.')
return size

def get_size_class(self, prefix):
"""Return size class for given prefix."""
return get_size_class(self.size, prefix=prefix) if self.size in ["sm", "lg"] else ""
Expand Down Expand Up @@ -103,10 +95,6 @@ def get_kwargs(self):
}
return context

def get_context_data(self):
"""Return context data for rendering."""
return self.get_kwargs()

def render(self):
"""Render to string."""
return ""
Expand All @@ -117,15 +105,10 @@ class FormsetRenderer(BaseRenderer):

def __init__(self, formset, *args, **kwargs):
if not isinstance(formset, BaseFormSet):
raise BootstrapError('Parameter "formset" should contain a valid Django Formset.')
raise TypeError('Parameter "formset" should contain a valid Django Formset.')
self.formset = formset
super().__init__(*args, **kwargs)

def get_context_data(self):
context = super().get_context_data()
context["formset"] = self.formset
return context

def render_management_form(self):
"""Return HTML for management form."""
return text_value(self.formset.management_form)
Expand Down Expand Up @@ -162,15 +145,10 @@ class FormRenderer(BaseRenderer):

def __init__(self, form, *args, **kwargs):
if not isinstance(form, BaseForm):
raise BootstrapError('Parameter "form" should contain a valid Django Form.')
raise TypeError('Parameter "form" should contain a valid Django Form.')
self.form = form
super().__init__(*args, **kwargs)

def get_context_data(self):
context = super().get_context_data()
context["form"] = self.form
return context

def render_fields(self):
rendered_fields = mark_safe("")
kwargs = self.get_kwargs()
Expand Down Expand Up @@ -213,7 +191,7 @@ class FieldRenderer(BaseRenderer):

def __init__(self, field, *args, **kwargs):
if not isinstance(field, BoundField):
raise BootstrapError('Parameter "field" should contain a valid Django BoundField.')
raise TypeError('Parameter "field" should contain a valid Django BoundField.')
self.field = field
super().__init__(*args, **kwargs)

Expand Down
7 changes: 3 additions & 4 deletions src/django_bootstrap5/size.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django_bootstrap5.css import merge_css_classes
from django_bootstrap5.exceptions import BootstrapError
from django_bootstrap5.text import text_value
from .css import merge_css_classes
from .text import text_value

SIZE_XS = "xs"
SIZE_SM = "sm"
Expand All @@ -15,7 +14,7 @@ def parse_size(value, default=None):
size = text_value(value or default)
if size not in SIZES:
valid_sizes = ", ".join(SIZES)
raise BootstrapError(f'Invalid value "{size}" for parameter "size" (valid values are {valid_sizes}).')
raise ValueError(f'Invalid value "{size}" for parameter "size" (valid values are {valid_sizes}).')
return size


Expand Down
3 changes: 1 addition & 2 deletions tests/test_bootstrap_button.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django_bootstrap5.exceptions import BootstrapError
from tests.base import BootstrapTestCase


Expand Down Expand Up @@ -65,5 +64,5 @@ def test_button_type_link(self):
link_button,
)

with self.assertRaises(BootstrapError):
with self.assertRaises(ValueError):
self.render("{% bootstrap_button 'button' button_type='button' href='#' %}")
4 changes: 1 addition & 3 deletions tests/test_bootstrap_field.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django import forms

from django_bootstrap5.exceptions import BootstrapError

from .base import BootstrapTestCase, html_39x27


Expand All @@ -20,7 +18,7 @@ class SubjectTestForm(forms.Form):

class FieldTestCase(BootstrapTestCase):
def test_illegal_field(self):
with self.assertRaises(BootstrapError):
with self.assertRaises(TypeError):
self.render("{% bootstrap_field field %}", {"field": "illegal"})

def test_show_help(self):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_bootstrap_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django import forms
from django.forms import formset_factory

from django_bootstrap5.exceptions import BootstrapError
from tests.base import BootstrapTestCase


Expand All @@ -25,7 +24,7 @@ def clean(self):

class BootstrapFormTestCase(BootstrapTestCase):
def test_illegal_form(self):
with self.assertRaises(BootstrapError):
with self.assertRaises(TypeError):
self.render("{% bootstrap_form form %}", {"form": "illegal"})

def test_exclude(self):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_bootstrap_formset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django import forms

from django_bootstrap5.exceptions import BootstrapError
from tests.base import BootstrapTestCase


Expand Down Expand Up @@ -51,5 +50,5 @@ def test_formset_post(self):
)

def test_illegal_formset(self):
with self.assertRaises(BootstrapError):
with self.assertRaises(TypeError):
self.render("{% bootstrap_formset formset %}", {"formset": "illegal"})
3 changes: 1 addition & 2 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.utils.safestring import mark_safe

from django_bootstrap5.components import render_alert, render_button
from django_bootstrap5.exceptions import BootstrapError


class AlertsTestCase(TestCase):
Expand Down Expand Up @@ -60,7 +59,7 @@ def test_button_with_illegal_type(self):
self.assertEqual(
render_button("button", button_type="illegal"), '<button class="btn btn-primary">button</button>'
)
except BootstrapError as e:
except ValueError as e:
self.assertEqual(
str(e),
'Parameter "button_type" should be "submit", "reset", "button", "link" or empty ("illegal" given).',
Expand Down

0 comments on commit aa44048

Please sign in to comment.