Skip to content

Commit

Permalink
Merge branch 'master' into workflows/replace-flake8-with-ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Sep 20, 2024
2 parents 1a9e87c + 46c253f commit 0d9a790
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 56 deletions.
1 change: 1 addition & 0 deletions .github/workflows/towncrier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [pull_request]

jobs:
towncrier:
if: ${{ github.actor != 'dependabot[bot]' }}
runs-on: ubuntu-latest
name: Towncrier check
steps:
Expand Down
1 change: 1 addition & 0 deletions changelog.d/2989.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix IPAM API crash bug that caused unnecessary error reports sent as e-mail to site admins
6 changes: 3 additions & 3 deletions python/nav/web/ipam/prefix_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,10 @@ def make_tree(prefixes, family=None, root_ip=None, show_all=None, sort_by="ip"):
init = []

if root_ip is not None and root_ip:
scope = Prefix.objects.get(net_address=root_ip)
if scope is not None:
try:
scope = Prefix.objects.get(net_address=root_ip)
node = PrefixNode(scope)
else:
except Prefix.DoesNotExist:
node = FauxNode(root_ip, "scope", "scope")
init.append(node)

Expand Down
48 changes: 34 additions & 14 deletions python/nav/web/seeddb/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from django.utils.safestring import mark_safe

from crispy_forms.helper import FormHelper
from crispy_forms_foundation.layout import Layout, Field, Fieldset, Row, Column
from crispy_forms_foundation.layout import Layout, Fieldset, Row, Column

from nav.django.forms import HStoreField
from nav.web.crispyforms import LabelSubmit
Expand Down Expand Up @@ -123,6 +123,31 @@ def cut_branch(field, klass, pk):
return [c for c in field.choices if c[0] not in descendant_ids]


# non-crispy helpers


def set_filter_form_attributes(
legend,
submit_value='Filter',
form_action='',
form_method='get',
form_class='custom',
):
class Obj:
pass

obj = Obj()
obj.legend = legend
obj.submit_value = submit_value
obj.action = form_action
obj.method = form_method
obj.form_class = form_class
return obj


# crispy helpers


def get_formhelper():
"""Get the default formhelper for seeddb forms"""
helper = FormHelper()
Expand Down Expand Up @@ -157,17 +182,17 @@ def get_submit_button(value='Filter'):
return LabelSubmit('submit', value, css_class='postfix')


# forms


class RoomFilterForm(forms.Form):
"""Form for filtering rooms"""

location = forms.ModelChoiceField(
Location.objects.order_by('id').all(), required=False
Location.objects.order_by('id').all(), required=False, label_suffix=''
)

def __init__(self, *args, **kwargs):
super(RoomFilterForm, self).__init__(*args, **kwargs)
self.helper = get_formhelper()
self.helper.layout = get_single_layout('Filter rooms', 'location')
location.widget.attrs.update({"class": "select"})
no_crispy = set_filter_form_attributes('Filter rooms')


class RoomForm(forms.ModelForm):
Expand Down Expand Up @@ -347,6 +372,8 @@ class DeviceGroupForm(forms.ModelForm):
netboxes = forms.ModelMultipleChoiceField(
queryset=Netbox.objects.all(), required=False
)
netboxes.widget.attrs.update({"class": "select2"})
no_crispy = True

def __init__(self, *args, **kwargs):
# If the form is based on an existing model instance, populate the
Expand All @@ -355,13 +382,6 @@ def __init__(self, *args, **kwargs):
initial = kwargs.setdefault('initial', {})
initial['netboxes'] = [n.pk for n in kwargs['instance'].netboxes.all()]
forms.ModelForm.__init__(self, *args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
'id',
'description',
Field('netboxes', css_class='select2'),
)

class Meta(object):
model = NetboxGroup
Expand Down
1 change: 0 additions & 1 deletion python/nav/web/seeddb/page/netboxgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def netboxgroup_edit(request, netboxgroup_id=None):
DeviceGroupForm,
netboxgroup_id,
'seeddb-netboxgroup-edit',
template='seeddb/edit_device_group.html',
extra_context=extra_context,
)

Expand Down
39 changes: 10 additions & 29 deletions python/nav/web/seeddb/page/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
from django import forms
from django.urls import reverse_lazy

from crispy_forms.helper import FormHelper
from crispy_forms_foundation.layout import Layout, Row, Column, Fieldset
from nav.web.crispyforms import LabelSubmit

from nav.models.manage import Vlan, NetType, Organization, Usage

from nav.web.seeddb import SeeddbInfo
from nav.web.seeddb.forms import set_filter_form_attributes
from nav.web.seeddb.utils.list import render_list
from nav.web.seeddb.utils.edit import render_edit

Expand All @@ -43,34 +40,18 @@ class VlanInfo(SeeddbInfo):

class VlanFilterForm(forms.Form):
net_type = forms.ModelChoiceField(
NetType.objects.order_by('id').all(), required=False
NetType.objects.order_by('id').all(), required=False, label_suffix=""
)
organization = forms.ModelChoiceField(
Organization.objects.order_by('id').all(), required=False
Organization.objects.order_by('id').all(), required=False, label_suffix=""
)
usage = forms.ModelChoiceField(Usage.objects.order_by('id').all(), required=False)

def __init__(self, *args, **kwargs):
super(VlanFilterForm, self).__init__(*args, **kwargs)
col_class = 'medium-3'
self.helper = FormHelper()
self.helper.form_action = ''
self.helper.form_method = 'GET'
self.helper.form_class = 'custom'
self.helper.layout = Layout(
Fieldset(
'Filter vlans',
Row(
Column('net_type', css_class=col_class),
Column('organization', css_class=col_class),
Column('usage', css_class=col_class),
Column(
LabelSubmit('submit', 'Filter', css_class='postfix'),
css_class=col_class,
),
),
)
)
usage = forms.ModelChoiceField(
Usage.objects.order_by('id').all(), required=False, label_suffix=""
)
net_type.widget.attrs.update({"class": "select"})
organization.widget.attrs.update({"class": "select"})
usage.widget.attrs.update({"class": "select"})
no_crispy = set_filter_form_attributes('Filter vlans')


class VlanForm(forms.ModelForm):
Expand Down
24 changes: 24 additions & 0 deletions python/nav/web/templates/seeddb/_filter_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<form class="{{attrs.form_class }}" method="{{ attrs.method }}"{% if attrs.action %} action="{{ attrs.action }}"{% endif %}>
<fieldset>
<legend>{{ attrs.legend }}</legend>
<div class="row">
<div class="columns medium-8">
{% for field in filter_form %}
<div id="div_id_{{ field.name }}" class="ctrlHolder{% if field.errors %} error {% endif %}">
{{ field.label_tag }}
{{ field }}
{% for error in field.errors %}
<small id="error_{{ forloop.counter}}_id_{{ field.name }}" class="error">
{{ error }}
</small>
{% endfor %}
</div>
{% endfor %}
</div>
<div class="columns medium-4">
<label>&nbsp;</label>
<input type="submit" name="submit" value="{{ attrs.submit_value }}" class="submit button postfix" id="submit-id-submit">
</div>
</div>
</fieldset>
</form>
24 changes: 24 additions & 0 deletions python/nav/web/templates/seeddb/_filter_form_columns.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<form class="{{attrs.form_class }}" method="{{ attrs.method }}"{% if attrs.action %} action="{{ attrs.action }}"{% endif %}>
<fieldset>
<legend>{{ attrs.legend }}</legend>
<div class="row">
{% for field in filter_form %}
<div class="columns medium-3">
<div id="div_id_{{ field.name }}" class="ctrlHolder{% if field.errors %} error {% endif %}">
{{ field.label_tag }}
{{ field }}
{% for error in field.errors %}
<small id="error_{{ forloop.counter}}_id_{{ field.name }}" class="error">
{{ error }}
</small>
{% endfor %}
</div>
</div>
{% endfor %}
<div class="columns medium-3">
<label>&nbsp;</label>
<input type="submit" name="submit" value="{{ attrs.submit_value }}" class="submit button postfix" id="submit-id-submit">
</div>
</div>
</fieldset>
</form>
18 changes: 18 additions & 0 deletions python/nav/web/templates/seeddb/_form_fields.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% for field in form %}
<div id="div_id_{{ field.name }}"
class="ctrlHolder{% if field.errors %} error{% endif %}"
>
<label for="id_{{ field.name }}"
class="{% if field.field.required %}requiredField{% endif %}"
>
{{ field.label.title }}
{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</label>
{{ field }}
{% for error in field.errors %}
<small id="error_{{ forloop.counter }}_id_{{ field.name }}" class="error">
{{ error }}
</small>
{% endfor %}
</div>
{% endfor %}
12 changes: 9 additions & 3 deletions python/nav/web/templates/seeddb/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ <h4>{{ title }}
<form class="seeddb-edit" action="" method="post">
<fieldset>
<legend>Attributes</legend>
{% block crispyfields %}
{{ form|crispy }}
{% endblock %}
{% if form.no_crispy %}
{% block formfields %}
{% include "seeddb/_form_fields.html" %}
{% endblock %}
{% else %}
{% block crispyfields %}
{{ form|crispy }}
{% endblock %}
{% endif %}
</fieldset>
<input type="submit" name="submit" value="Save {{ verbose_name }}" class="submit button small left" id="submit-id-submit">
</form>
Expand Down
5 changes: 0 additions & 5 deletions python/nav/web/templates/seeddb/edit_device_group.html

This file was deleted.

10 changes: 9 additions & 1 deletion python/nav/web/templates/seeddb/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@

{% if filter_form %}
{% if filter_form.base_fields|length >= 3 %}
{% crispy filter_form %}
{% if filter_form.no_crispy %}
{% include "seeddb/_filter_form_columns.html" with attrs=filter_form.no_crispy %}
{% else %}
{% crispy filter_form %}
{% endif %}
{% else %}
<div class="row">
<div class="medium-6 column">
{% if filter_form.no_crispy %}
{% include "seeddb/_filter_form.html" with attrs=filter_form.no_crispy %}
{% else %}
{% crispy filter_form %}
{% endif %}
</div>
</div>
{% endif %}
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/web/ipam/api_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class TestPrefixViewSet:
def test_when_prefix_address_is_unknown_it_should_not_crash(self, client):
response = client.get(
"/ipam/api/",
follow=True,
data={
"net_type": "all",
"within": "192.168.42.0/24",
"show_all": "True",
},
)
assert response.status_code == 200

0 comments on commit 0d9a790

Please sign in to comment.