1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import logging
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound
from deform import (
Form,
Button,
ValidationFailure,
)
import colander
PARTNER_FIELDS = [
('nama', 'string', 'Nama', 'required'),
('tempat_lahir', 'string', 'Tempat Lahir', 'required'),
('tgl_lahir', 'date', 'Tanggal Lahir', 'required'),
('nik','string', 'NIK', ''),
]
EMPLOYEE_FIELDS = PARTNER_FIELDS + [
('nip', 'string', 'NIP', 'required'),
]
TITLES = dict(
customer='Pelanggan',
employee='Karyawan')
FIELDS = dict(
customer=PARTNER_FIELDS,
employee=EMPLOYEE_FIELDS)
TYPES = {'string': colander.String(), 'date': colander.Date()}
MISSING = {'required': colander.required, '': colander.drop}
def schema_add(schema, field):
name, typ, title, required = field
colander_type = TYPES[typ]
missing = MISSING[required]
node = colander.SchemaNode(
colander_type, name=name, title=title, missing=missing)
schema.add(node)
def schema_add_fields(schema, fields):
for field in fields:
schema_add(schema, field)
def form_response(resp, form):
resp['form'] = form.render()
return resp
def insert(data):
logging.info('data: {}'.format(data))
@view_config(
route_name='partner-add', renderer='../templates/partner/add.pt')
def view_agent_add(request):
schema = colander.Schema()
category = request.matchdict['category']
fields = FIELDS[category]
schema_add_fields(schema, fields)
btn_save = Button('save', 'Simpan')
btn_cancel = Button('cancel', 'Batalkan')
buttons = (btn_save, btn_cancel)
form = Form(schema, buttons=buttons)
title = TITLES[category]
resp = dict(title=title)
if not request.POST:
return form_response(resp, form)
items = request.POST.items()
try:
c = form.validate(items)
except ValidationFailure:
return form_response(resp, form)
data = dict(c.items())
insert(data)
url = request.route_url('partner-add', category=category)
return HTTPFound(location=url)