Server IP : 85.214.239.14 / Your IP : 18.224.38.176 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /srv/modoboa/env/lib64/python3.5/site-packages/rest_framework/utils/ |
Upload File : |
""" Helpers for dealing with HTML input. """ import re from django.utils.datastructures import MultiValueDict def is_html_input(dictionary): # MultiDict type datastructures are used to represent HTML form input, # which may have more than one value for each key. return hasattr(dictionary, 'getlist') def parse_html_list(dictionary, prefix='', default=None): """ Used to support list values in HTML forms. Supports lists of primitives and/or dictionaries. * List of primitives. { '[0]': 'abc', '[1]': 'def', '[2]': 'hij' } --> [ 'abc', 'def', 'hij' ] * List of dictionaries. { '[0]foo': 'abc', '[0]bar': 'def', '[1]foo': 'hij', '[1]bar': 'klm', } --> [ {'foo': 'abc', 'bar': 'def'}, {'foo': 'hij', 'bar': 'klm'} ] :returns a list of objects, or the value specified in ``default`` if the list is empty """ ret = {} regex = re.compile(r'^%s\[([0-9]+)\](.*)$' % re.escape(prefix)) for field, value in dictionary.items(): match = regex.match(field) if not match: continue index, key = match.groups() index = int(index) if not key: ret[index] = value elif isinstance(ret.get(index), dict): ret[index][key] = value else: ret[index] = MultiValueDict({key: [value]}) # return the items of the ``ret`` dict, sorted by key, or ``default`` if the dict is empty return [ret[item] for item in sorted(ret)] if ret else default def parse_html_dict(dictionary, prefix=''): """ Used to support dictionary values in HTML forms. { 'profile.username': 'example', 'profile.email': 'example@example.com', } --> { 'profile': { 'username': 'example', 'email': 'example@example.com' } } """ ret = MultiValueDict() regex = re.compile(r'^%s\.(.+)$' % re.escape(prefix)) for field in dictionary: match = regex.match(field) if not match: continue key = match.groups()[0] value = dictionary.getlist(field) ret.setlist(key, value) return ret