1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

merge develop

This commit is contained in:
John Anderson
2020-02-18 00:39:30 -05:00
80 changed files with 354 additions and 1138 deletions

View File

@ -69,3 +69,16 @@ FILTER_LOOKUP_HELP_TEXT_MAP = dict(
gte='greater than or equal',
n='negated'
)
# Keys for PostgreSQL advisory locks. These are arbitrary bigints used by
# the advisory_lock contextmanager. When a lock is acquired,
# one of these keys will be used to identify said lock.
#
# When adding a new key, pick something arbitrary and unique so
# that it is easily searchable in query logs.
ADVISORY_LOCK_KEYS = {
'available-prefixes': 100100,
'available-ips': 100200,
}

View File

@ -309,12 +309,17 @@ class APISelect(SelectWithDisabled):
def add_additional_query_param(self, name, value):
"""
Add details for an additional query param in the form of a data-* attribute.
Add details for an additional query param in the form of a data-* JSON-encoded list attribute.
:param name: The name of the query param
:param value: The value of the query param
"""
self.attrs['data-additional-query-param-{}'.format(name)] = value
key = 'data-additional-query-param-{}'.format(name)
values = json.loads(self.attrs.get(key, '[]'))
values.append(value)
self.attrs[key] = json.dumps(values)
def add_conditional_query_param(self, condition, value):
"""

View File

@ -10,7 +10,7 @@ INTERFACE_NAME_REGEX = r'(^(?P<type>[^\d\.:]+)?)' \
r'(.(?P<vc>\d+)$)?'
def naturalize(value, max_length=None, integer_places=8):
def naturalize(value, max_length, integer_places=8):
"""
Take an alphanumeric string and prepend all integers to `integer_places` places to ensure the strings
are ordered naturally. For example:
@ -39,10 +39,10 @@ def naturalize(value, max_length=None, integer_places=8):
output.append(segment)
ret = ''.join(output)
return ret[:max_length] if max_length else ret
return ret[:max_length]
def naturalize_interface(value, max_length=None):
def naturalize_interface(value, max_length):
"""
Similar in nature to naturalize(), but takes into account a particular naming format adapted from the old
InterfaceManager.
@ -77,4 +77,4 @@ def naturalize_interface(value, max_length=None):
output.append('000000')
ret = ''.join(output)
return ret[:max_length] if max_length else ret
return ret[:max_length]

View File

@ -1,9 +1,10 @@
import datetime
import json
import re
import yaml
import yaml
from django import template
from django.urls import NoReverseMatch, reverse
from django.utils.html import strip_tags
from django.utils.safestring import mark_safe
from markdown import markdown
@ -11,7 +12,6 @@ from markdown import markdown
from utilities.choices import unpack_grouped_choices
from utilities.utils import foreground_color
register = template.Library()
@ -101,6 +101,21 @@ def model_name_plural(obj):
return obj._meta.verbose_name_plural
@register.filter()
def url_name(model, action):
"""
Return the URL name for the given model and action, or None if invalid.
"""
url_name = '{}:{}_{}'.format(model._meta.app_label, model._meta.model_name, action)
try:
# Validate and return the URL name. We don't return the actual URL yet because many of the templates
# are written to pass a name to {% url %}.
reverse(url_name)
return url_name
except NoReverseMatch:
return None
@register.filter()
def contains(value, arg):
"""

View File

@ -21,7 +21,10 @@ class NaturalizationTestCase(TestCase):
)
for origin, naturalized in data:
self.assertEqual(naturalize(origin), naturalized)
self.assertEqual(naturalize(origin, max_length=50), naturalized)
def test_naturalize_max_length(self):
self.assertEqual(naturalize('abc123def456', max_length=10), 'abc0000012')
def test_naturalize_interface(self):
@ -40,4 +43,7 @@ class NaturalizationTestCase(TestCase):
)
for origin, naturalized in data:
self.assertEqual(naturalize_interface(origin), naturalized)
self.assertEqual(naturalize_interface(origin, max_length=50), naturalized)
def test_naturalize_interface_max_length(self):
self.assertEqual(naturalize_interface('Gi1/2/3', max_length=20), '0001000299999999Gi00')

View File

@ -71,7 +71,8 @@ class ObjectListView(View):
filterset = None
filterset_form = None
table = None
template_name = None
template_name = 'utilities/obj_list.html'
action_buttons = ('add', 'import', 'export')
def queryset_to_yaml(self):
"""
@ -156,9 +157,11 @@ class ObjectListView(View):
# Provide a hook to tweak the queryset based on the request immediately prior to rendering the object list
self.queryset = self.alter_queryset(request)
# Compile user model permissions for access from within the template
perm_base_name = '{}.{{}}_{}'.format(model._meta.app_label, model._meta.model_name)
permissions = {p: request.user.has_perm(perm_base_name.format(p)) for p in ['add', 'change', 'delete']}
# Compile a dictionary indicating which permissions are available to the current user for this model
permissions = {}
for action in ('add', 'change', 'delete', 'view'):
perm_name = '{}.{}_{}'.format(model._meta.app_label, action, model._meta.model_name)
permissions[action] = request.user.has_perm(perm_name)
# Construct the table based on the user's permissions
table = self.table(self.queryset)
@ -176,6 +179,7 @@ class ObjectListView(View):
'content_type': content_type,
'table': table,
'permissions': permissions,
'action_buttons': self.action_buttons,
'filter_form': self.filterset_form(request.GET, label_suffix='') if self.filterset_form else None,
}
context.update(self.extra_context())
@ -630,7 +634,7 @@ class BulkEditView(GetReturnURLMixin, View):
post_data['pk'] = [obj.pk for obj in self.filterset(request.GET, model.objects.only('pk')).qs]
if '_apply' in request.POST:
form = self.form(model, request.POST, initial=request.GET)
form = self.form(model, request.POST)
if form.is_valid():
custom_fields = form.custom_fields if hasattr(form, 'custom_fields') else []
@ -714,10 +718,6 @@ class BulkEditView(GetReturnURLMixin, View):
else:
# Pass the PK list as initial data to avoid binding the form
initial_data = querydict_to_dict(post_data)
# Append any normal initial data (passed as GET parameters)
initial_data.update(request.GET)
form = self.form(model, initial=initial_data)
# Retrieve objects being edited