2024-01-31 22:39:54 -08:00
|
|
|
import logging
|
|
|
|
from collections import defaultdict
|
2024-01-28 22:14:50 -08:00
|
|
|
|
2024-01-31 22:39:54 -08:00
|
|
|
from wtforms_sqlalchemy.orm import model_form
|
2024-01-28 22:14:50 -08:00
|
|
|
|
2024-01-31 22:39:54 -08:00
|
|
|
from ttfrog.db.manager import db
|
2024-01-28 22:14:50 -08:00
|
|
|
|
|
|
|
|
2024-01-31 22:39:54 -08:00
|
|
|
class BaseController:
|
|
|
|
model = None
|
|
|
|
|
|
|
|
def __init__(self, request):
|
|
|
|
self.request = request
|
|
|
|
self.record = None
|
|
|
|
self.attrs = defaultdict(str)
|
|
|
|
self.configure()
|
|
|
|
if self.model:
|
|
|
|
self.model_form = model_form(self.model, db_session=db.session)
|
|
|
|
|
|
|
|
# load this from dotenv or something
|
|
|
|
self.config = {
|
|
|
|
'static_url': '/static',
|
|
|
|
'project_name': 'TTFROG'
|
|
|
|
}
|
|
|
|
|
|
|
|
def configure(self):
|
|
|
|
self.load_from_id()
|
|
|
|
|
|
|
|
|
|
|
|
def load_from_id(self):
|
|
|
|
if not self.request.POST['id']:
|
|
|
|
return
|
|
|
|
self.record = db.query(self.model).get(self.request.POST['id'])
|
|
|
|
|
|
|
|
def form(self) -> str:
|
|
|
|
# no model? no form.
|
|
|
|
if not self.model:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
# no user submission to process
|
|
|
|
if self.request.method != 'POST':
|
|
|
|
return self.model_form(obj=self.record)
|
|
|
|
|
|
|
|
# process submission
|
|
|
|
form = self.model_form(self.request.POST, 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
|
2024-01-28 22:14:50 -08:00
|
|
|
|
|
|
|
def output(self, **kwargs) -> dict:
|
|
|
|
return dict(
|
2024-01-31 22:39:54 -08:00
|
|
|
config=self.config,
|
|
|
|
request=self.request,
|
|
|
|
record=self.record,
|
|
|
|
form=self.form(),
|
|
|
|
**self.attrs,
|
2024-01-28 22:14:50 -08:00
|
|
|
**kwargs,
|
|
|
|
)
|
2024-01-31 22:39:54 -08:00
|
|
|
|
|
|
|
def response(self):
|
|
|
|
return self.output()
|