simplified post handling
This commit is contained in:
parent
3444f83c91
commit
5de3f74a88
|
@ -1,5 +1,3 @@
|
||||||
from itertools import chain
|
|
||||||
|
|
||||||
from pyramid_sqlalchemy import BaseObject
|
from pyramid_sqlalchemy import BaseObject
|
||||||
from wtforms import validators
|
from wtforms import validators
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import logging
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from wtforms_sqlalchemy.orm import model_form
|
from wtforms_sqlalchemy.orm import model_form
|
||||||
|
@ -26,7 +25,6 @@ class BaseController:
|
||||||
def configure(self):
|
def configure(self):
|
||||||
self.load_from_id()
|
self.load_from_id()
|
||||||
|
|
||||||
|
|
||||||
def load_from_id(self):
|
def load_from_id(self):
|
||||||
if not self.request.POST['id']:
|
if not self.request.POST['id']:
|
||||||
return
|
return
|
||||||
|
@ -37,28 +35,12 @@ class BaseController:
|
||||||
if not self.model:
|
if not self.model:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
# no user submission to process
|
if self.request.method == 'POST':
|
||||||
if self.request.method != 'POST':
|
form = self.model_form(self.request.POST, obj=self.record)
|
||||||
return self.model_form(obj=self.record)
|
if self.model.validate(form):
|
||||||
|
form.populate_obj(self.record)
|
||||||
# process submission
|
return form
|
||||||
form = self.model_form(self.request.POST, obj=self.record)
|
return self.model_form(obj=self.record)
|
||||||
if self.model.validate(form):
|
|
||||||
form.populate_obj(self.record)
|
|
||||||
error = self.save_changes()
|
|
||||||
if error:
|
|
||||||
form.errors['process'] = error
|
|
||||||
return form
|
|
||||||
|
|
||||||
def save_changes(self):
|
|
||||||
try:
|
|
||||||
with db.transaction():
|
|
||||||
for (key, val) in self.request.POST.items():
|
|
||||||
if hasattr(self.record, key):
|
|
||||||
setattr(self.record, key, val)
|
|
||||||
except Exception as e:
|
|
||||||
return e
|
|
||||||
return None
|
|
||||||
|
|
||||||
def output(self, **kwargs) -> dict:
|
def output(self, **kwargs) -> dict:
|
||||||
return dict(
|
return dict(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user