mirror of
https://github.com/postgres/pgweb.git
synced 2025-08-12 23:05:12 +00:00
Import newer version of django-selectable
Sync up to the same version we have on the commitfest app, which will also be required for eventual django 1.11 support.
This commit is contained in:
@ -2,13 +2,14 @@ from __future__ import unicode_literals
|
||||
|
||||
import random
|
||||
import string
|
||||
from collections import defaultdict
|
||||
from xml.dom.minidom import parseString
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from ..base import ModelLookup
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from . import Thing
|
||||
from ..base import ModelLookup
|
||||
|
||||
|
||||
def as_xml(html):
|
||||
@ -28,22 +29,8 @@ def parsed_inputs(html):
|
||||
return inputs
|
||||
|
||||
|
||||
class PatchSettingsMixin(object):
|
||||
def setUp(self):
|
||||
super(PatchSettingsMixin, self).setUp()
|
||||
self.is_limit_set = hasattr(settings, 'SELECTABLE_MAX_LIMIT')
|
||||
if self.is_limit_set:
|
||||
self.original_limit = settings.SELECTABLE_MAX_LIMIT
|
||||
settings.SELECTABLE_MAX_LIMIT = 25
|
||||
|
||||
def tearDown(self):
|
||||
super(PatchSettingsMixin, self).tearDown()
|
||||
if self.is_limit_set:
|
||||
settings.SELECTABLE_MAX_LIMIT = self.original_limit
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF='selectable.tests.urls')
|
||||
class BaseSelectableTestCase(TestCase):
|
||||
urls = 'selectable.tests.urls'
|
||||
|
||||
def get_random_string(self, length=10):
|
||||
return ''.join(random.choice(string.ascii_letters) for x in range(length))
|
||||
@ -60,4 +47,51 @@ class BaseSelectableTestCase(TestCase):
|
||||
|
||||
class SimpleModelLookup(ModelLookup):
|
||||
model = Thing
|
||||
search_fields = ('name__icontains', )
|
||||
search_fields = ('name__icontains', )
|
||||
|
||||
|
||||
def parsed_widget_attributes(widget):
|
||||
"""
|
||||
Get a dictionary-like object containing all HTML attributes
|
||||
of the rendered widget.
|
||||
|
||||
Lookups on this object raise ValueError if there is more than one attribute
|
||||
of the given name in the HTML, and they have different values.
|
||||
"""
|
||||
# For the tests that use this, it generally doesn't matter what the value
|
||||
# is, so we supply anything.
|
||||
rendered = widget.render('a_name', 'a_value')
|
||||
return AttrMap(rendered)
|
||||
|
||||
|
||||
class AttrMap(object):
|
||||
def __init__(self, html):
|
||||
dom = as_xml(html)
|
||||
self._attrs = defaultdict(set)
|
||||
self._build_attr_map(dom)
|
||||
|
||||
def _build_attr_map(self, dom):
|
||||
for node in _walk_nodes(dom):
|
||||
if node.attributes is not None:
|
||||
for k, v in node.attributes.items():
|
||||
self._attrs[k].add(v)
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in self._attrs and len(self._attrs[key]) > 0
|
||||
|
||||
def __getitem__(self, key):
|
||||
if key not in self:
|
||||
raise KeyError(key)
|
||||
vals = self._attrs[key]
|
||||
if len(vals) > 1:
|
||||
raise ValueError("More than one value for attribute {0}: {1}".
|
||||
format(key, ", ".join(vals)))
|
||||
else:
|
||||
return list(vals)[0]
|
||||
|
||||
|
||||
def _walk_nodes(dom):
|
||||
yield dom
|
||||
for child in dom.childNodes:
|
||||
for item in _walk_nodes(child):
|
||||
yield item
|
||||
|
Reference in New Issue
Block a user