fix: use wuttaweb logic for util.get_form_data()
This commit is contained in:
parent
5ec899cf08
commit
3b92bb3a9e
6
docs/api/util.rst
Normal file
6
docs/api/util.rst
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
``tailbone.util``
|
||||
=================
|
||||
|
||||
.. automodule:: tailbone.util
|
||||
:members:
|
|
@ -52,6 +52,7 @@ Package API:
|
|||
api/grids.core
|
||||
api/progress
|
||||
api/subscribers
|
||||
api/util
|
||||
api/views/batch
|
||||
api/views/batch.vendorcatalog
|
||||
api/views/core
|
||||
|
|
|
@ -35,7 +35,7 @@ from sqlalchemy import orm
|
|||
from sqlalchemy.ext.associationproxy import AssociationProxy, ASSOCIATION_PROXY
|
||||
from wuttjamaican.util import UNSPECIFIED
|
||||
|
||||
from rattail.util import prettify, pretty_boolean
|
||||
from rattail.util import pretty_boolean
|
||||
from rattail.db.util import get_fieldnames
|
||||
|
||||
import colander
|
||||
|
@ -47,8 +47,10 @@ from pyramid_deform import SessionFileUploadTempStore
|
|||
from pyramid.renderers import render
|
||||
from webhelpers2.html import tags, HTML
|
||||
|
||||
from wuttaweb.util import get_form_data
|
||||
|
||||
from tailbone.db import Session
|
||||
from tailbone.util import raw_datetime, get_form_data, render_markdown
|
||||
from tailbone.util import raw_datetime, render_markdown
|
||||
from tailbone.forms import types
|
||||
from tailbone.forms.widgets import (ReadonlyWidget, PlainDateWidget,
|
||||
JQueryDateWidget, JQueryTimeWidget,
|
||||
|
@ -570,7 +572,9 @@ class Form(object):
|
|||
self.schema[key].title = label
|
||||
|
||||
def get_label(self, key):
|
||||
return self.labels.get(key, prettify(key))
|
||||
config = self.request.rattail_config
|
||||
app = config.get_app()
|
||||
return self.labels.get(key, app.make_title(key))
|
||||
|
||||
def set_readonly(self, key, readonly=True):
|
||||
if readonly:
|
||||
|
|
|
@ -39,6 +39,8 @@ from pyramid.renderers import get_renderer
|
|||
from pyramid.interfaces import IRoutesMapper
|
||||
from webhelpers2.html import HTML, tags
|
||||
|
||||
from wuttaweb.util import get_form_data as wutta_get_form_data
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -75,17 +77,13 @@ def csrf_token(request, name='_csrf'):
|
|||
|
||||
def get_form_data(request):
|
||||
"""
|
||||
Returns the effective form data for the given request. Mostly
|
||||
this is a convenience, to return either POST or JSON depending on
|
||||
the type of request.
|
||||
DEPECATED - use :func:`wuttaweb:wuttaweb.util.get_form_data()`
|
||||
instead.
|
||||
"""
|
||||
# nb. we prefer JSON only if no POST is present
|
||||
# TODO: this seems to work for our use case at least, but perhaps
|
||||
# there is a better way? see also
|
||||
# https://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.is_xhr
|
||||
if (request.is_xhr or request.content_type == 'application/json') and not request.POST:
|
||||
return request.json_body
|
||||
return request.POST
|
||||
warnings.warn("tailbone.util.get_form_data() is deprecated; "
|
||||
"please use wuttaweb.util.get_form_data() instead",
|
||||
DeprecationWarning, stacklevel=2)
|
||||
return wutta_get_form_data(request)
|
||||
|
||||
|
||||
def get_global_search_options(request):
|
||||
|
|
|
@ -25,22 +25,22 @@ Views for 'receiving' (purchasing) batches
|
|||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import decimal
|
||||
import logging
|
||||
from collections import OrderedDict
|
||||
|
||||
import humanize
|
||||
# import humanize
|
||||
|
||||
from rattail import pod
|
||||
from rattail.util import prettify, simple_error
|
||||
from rattail.util import simple_error
|
||||
|
||||
import colander
|
||||
from deform import widget as dfwidget
|
||||
from webhelpers2.html import tags, HTML
|
||||
|
||||
from wuttaweb.util import get_form_data
|
||||
|
||||
from tailbone import forms, grids
|
||||
from tailbone.util import get_form_data
|
||||
from tailbone.views.purchasing import PurchasingBatchView
|
||||
|
||||
|
||||
|
|
39
tests/test_util.py
Normal file
39
tests/test_util.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
from pyramid import testing
|
||||
|
||||
from rattail.config import RattailConfig
|
||||
|
||||
from tailbone import util
|
||||
|
||||
|
||||
class TestGetFormData(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = RattailConfig()
|
||||
|
||||
def make_request(self, **kwargs):
|
||||
kwargs.setdefault('wutta_config', self.config)
|
||||
kwargs.setdefault('rattail_config', self.config)
|
||||
kwargs.setdefault('is_xhr', None)
|
||||
kwargs.setdefault('content_type', None)
|
||||
kwargs.setdefault('POST', {'foo1': 'bar'})
|
||||
kwargs.setdefault('json_body', {'foo2': 'baz'})
|
||||
return testing.DummyRequest(**kwargs)
|
||||
|
||||
def test_default(self):
|
||||
request = self.make_request()
|
||||
data = util.get_form_data(request)
|
||||
self.assertEqual(data, {'foo1': 'bar'})
|
||||
|
||||
def test_is_xhr(self):
|
||||
request = self.make_request(POST=None, is_xhr=True)
|
||||
data = util.get_form_data(request)
|
||||
self.assertEqual(data, {'foo2': 'baz'})
|
||||
|
||||
def test_content_type(self):
|
||||
request = self.make_request(POST=None, content_type='application/json')
|
||||
data = util.get_form_data(request)
|
||||
self.assertEqual(data, {'foo2': 'baz'})
|
Loading…
Reference in a new issue