Refactor feedback feature to use colander/deform

This commit is contained in:
Lance Edgar 2018-02-11 16:05:56 -06:00
parent 4191e50456
commit 1c27efc8f1

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar # Copyright © 2010-2018 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -34,26 +34,27 @@ from rattail.mail import send_email
from rattail.util import OrderedDict from rattail.util import OrderedDict
from rattail.files import resource_path from rattail.files import resource_path
import formencode as fe import colander
from pyramid import httpexceptions from pyramid import httpexceptions
from pyramid.response import Response from pyramid.response import Response
from pyramid_simpleform import Form
import tailbone import tailbone
from tailbone import forms from tailbone import forms2 as forms
from tailbone.db import Session from tailbone.db import Session
from tailbone.views import View from tailbone.views import View
class Feedback(fe.Schema): class Feedback(colander.Schema):
""" """
Form schema for user feedback. Form schema for user feedback.
""" """
allow_extra_fields = True referrer = colander.SchemaNode(colander.String())
referrer = fe.validators.NotEmpty()
user = forms.validators.ValidUser() user = colander.SchemaNode(forms.types.UserType())
user_name = fe.validators.NotEmpty()
message = fe.validators.NotEmpty() user_name = colander.SchemaNode(colander.String())
message = colander.SchemaNode(colander.String())
class CommonView(View): class CommonView(View):
@ -117,9 +118,9 @@ class CommonView(View):
""" """
Generic view to present/handle the user feedback form. Generic view to present/handle the user feedback form.
""" """
form = Form(self.request, schema=Feedback) form = forms.Form(schema=Feedback(), request=self.request)
if form.validate(): if form.validate(newstyle=True):
data = dict(form.data) data = dict(form.validated)
if data['user']: if data['user']:
data['user_url'] = self.request.route_url('users.view', uuid=data['user'].uuid) data['user_url'] = self.request.route_url('users.view', uuid=data['user'].uuid)
send_email(self.rattail_config, 'user_feedback', data=data) send_email(self.rattail_config, 'user_feedback', data=data)