Skip to content

Commit

Permalink
Widget fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vas3k committed Jul 25, 2016
1 parent 01ae26d commit 1643181
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 22 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,10 @@ Policies are written in a very bad and unoptimized way, so they really need a hu

## Future Plans

* Buy more beer
* ~~Buy more beer~~
* Fix HTML/CSS bugs
* Support default values from database
* Make Widgets Great Again (+sexy appearance)
* Support default values from more databases
* Rewrite policies
* Security test for XSS and injections (be careful with that, check all models you create and send me pull requests)
* Think about better filtering/sorting interface
Expand Down
2 changes: 1 addition & 1 deletion base/create_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def post(self):
values = {}
for field in form:
field_obj = getattr(form, field.name, None)
if field_obj:
if field_obj is not None:
values[field.name] = field_obj.data

try:
Expand Down
2 changes: 1 addition & 1 deletion base/edit_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def post(self, id):
updates = {}
for field in form:
field_obj = getattr(form, field.name, None)
if field_obj:
if field_obj is not None:
updates[field.name] = field_obj.data

if "id" not in updates:
Expand Down
1 change: 1 addition & 0 deletions base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class BaseAdminModel:
create_view = BaseCreateView
details_view = BaseDetailsView
delete_view = BaseDeleteView
soft_delete_view = None

def __init__(self, app):
log.info("Init model: {}".format(self.__class__.__name__))
Expand Down
13 changes: 1 addition & 12 deletions base/view.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import copy
import logging

import wtforms
from flask import g, redirect, render_template
from flask.views import MethodView
from wtforms.validators import Optional, DataRequired

import settings
from common.acl import ACL
Expand Down Expand Up @@ -119,14 +117,5 @@ def form(self):
fields = {}
for column_name, column_options in self.fields_obj:
if column_options["widget"].field is not None:
field = copy.copy(column_options["widget"].field)
field.label = column_options["widget"].pretty_name
field.default = column_options["widget"].default

# because it's a UnboundField now, you can't simply set the validators
if "validators" not in field.kwargs:
field.kwargs["validators"] = []
field.kwargs["validators"] += [Optional() if column_options["meta"].nullable else DataRequired()]

fields[column_name] = field
fields[column_name] = column_options["widget"].field
return type("dummy_form", (wtforms.Form,), fields)
16 changes: 16 additions & 0 deletions base/widget.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import copy

import jinja2
import wtforms
from flask import render_template
from sqlalchemy.sql.elements import TextClause
from wtforms.fields.core import UnboundField
from wtforms.validators import Optional, InputRequired


class BaseWidget:
Expand All @@ -13,6 +17,18 @@ def __init__(self, name, meta, model):
self.name = name
self.meta = meta
self.model = model
self.field = copy.copy(self.field)
if self.field is not None:
self.field.label = self.pretty_name
self.field.default = self.default
if isinstance(self.field, wtforms.BooleanField) \
or (isinstance(self.field, UnboundField) and self.field.field_class == wtforms.BooleanField):
# because I don't found how to handle InputRequired for BooleanField :(
extra_validators = [Optional()]
else:
extra_validators = [Optional() if self.meta.nullable else InputRequired()]

self.field.kwargs["validators"] = list(set(self.field.kwargs.get("validators") or []) | set(extra_validators))

@property
def pretty_name(self):
Expand Down
2 changes: 1 addition & 1 deletion templates/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ul style="margin-top: 40px;">
{% for field, errors in form_errors.items() %}
{% for error in errors %}
<li>{{ field }}: {{ error }}</li>
<li>{{ field }}: {{ error }} ({{ request.form[field] }})</li>
{% endfor %}
{% endfor %}
</ul>
Expand Down
8 changes: 4 additions & 4 deletions widgets/datetime.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from datetime import datetime, date

from wtforms.fields.html5 import DateTimeLocalField
from wtforms.fields.html5 import DateTimeField

from base.widget import BaseWidget


class DatetimeWidget(BaseWidget):
field = DateTimeLocalField(format="%Y-%m-%dT%H:%M:%S")
field = DateTimeField(format="%Y-%m-%d %H:%M:%S")
field_kwargs = {"step": 1}

def render_list(self, item):
value = getattr(item, self.name, None)
try:
if isinstance(value, (date, datetime)):
return value.strftime("%d.%m.%Y&nbsp;%H:%M")
return value.strftime("%d.%m.%Y %H:%M")
elif isinstance(value, str):
if value.isnumeric():
value = datetime.utcfromtimestamp(int(value)).strftime("%d.%m.%Y&nbsp;%H:%M")
value = datetime.utcfromtimestamp(int(value)).strftime("%d.%m.%Y %H:%M")
else:
return value
else:
Expand Down
1 change: 0 additions & 1 deletion widgets/factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql import sqltypes
from wtforms.validators import Optional, DataRequired

from widgets.array import ArrayWidget
from widgets.binary import BinaryWidget
Expand Down

0 comments on commit 1643181

Please sign in to comment.