Skip to content

Commit

Permalink
Further iterative changes to CRUD system.
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Aug 23, 2016
1 parent 1963192 commit 7356f52
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
13 changes: 12 additions & 1 deletion websauna/system/crud/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from websauna.compat import typing
from websauna.compat.typing import Iterable
from websauna.compat.typing import List
from websauna.system.core import messages
from websauna.system.form import interstitial
from websauna.system.form.fieldmapper import EditMode
Expand Down Expand Up @@ -405,7 +406,7 @@ def get_title(self):
return "Add new {}".format(self.get_crud().singular_name)

def get_form(self) -> object:
return self.create_form(EditMode.add, buttons=("add", "cancel",))
return self.create_form(EditMode.add, buttons=self.get_buttons())

def get_crud(self) -> CRUD:
"""Get CRUD manager object for this view."""
Expand All @@ -423,6 +424,9 @@ def initialize_object(self, form, appstruct: dict, obj: object):
"""Record values from the form on a freshly created object."""
form.schema.objectify(appstruct, obj)

def bind_schema(self, schema):
return schema.bind(request=self.request, context=self.context)

def add_object(self, obj):
"""Add objects to transaction lifecycle and flush newly created object to persist storage to give them id."""
dbsession = self.context.get_dbsession()
Expand All @@ -449,6 +453,13 @@ def build_object(self, form, appstruct: dict) -> object:
self.add_object(obj)
return obj

def get_buttons(self)-> List[deform.form.Button]:
buttons = (
deform.form.Button("add"),
deform.form.Button("cancel"),
)
return buttons

@view_config(context=CRUD, name="add", renderer="crud/add.html", permission='add')
def add(self):
"""View for showing an individual object."""
Expand Down
21 changes: 5 additions & 16 deletions websauna/system/form/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,11 @@ class EnumValue(colander.String):
class AssetClass(enum.Enum):
'''What's preferred display format for this asset.'''
#: 0,00
fiat = "fiat"
#: 100.000,000,000
cryptocurrency = "cryptocurrency"
#: 10.000
token = "token"
#: 10.000
tokenized_shares = "tokenized_shares"
#: up to 18 decimals
ether = "ether"
fiat = "fiat"
cryptocurrency = "cryptocurrency"
token = "token"
tokenized_shares = "tokenized_shares"
ether = "ether"
class Schema(CSRFSchema):
Expand All @@ -79,15 +70,13 @@ def __init__(self, enum_class: type):
def deserialize(self, node: colander.SchemaNode, cstruct: str):
"""Parse incoming form values to Python objects if needed.
"""
print("Deserialize", cstruct)
if cstruct:
return self.enum_class(cstruct)
else:
return None

def serialize(self, node: colander.SchemaNode, _enum: enum.Enum) -> str:
"""Convert Enum object to str for widget processing."""
print("Serialize ", _enum)
if _enum:
assert isinstance(_enum, self.enum_class), "Expected {}, got {}".format(self.enum_class, _enum)
return _enum.value
Expand Down
6 changes: 5 additions & 1 deletion websauna/system/form/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ def get_match_column(self, node: colander.SchemaNode, model:type) -> Column:


class ForeignKeyValue(ModelSchemaType, colander.String):
"""Hold a reference to one SQLAlchemy object for Colander schema serialization."""
"""Hold a reference to one SQLAlchemy object for Colander schema serialization.
Example:
"""

def serialize(self, node, appstruct):

Expand Down

0 comments on commit 7356f52

Please sign in to comment.