I have a custom form from SQLFORM.grid. After clicking the submit button onvalidation or oncreate funciton are not triggered - although some fields are validated, but that is even before the my_processing_form function there is no redirect after submit and the record is not inserted/updated in the database. I used the same principle with some other functions and views and there it works fine, but I have no idea why at this one the on validate is not triggered.
def shifts():
user = auth.user_id
org = db(db.auth_user.id == user).select(db.auth_user.organization)[0]["organization"]
db.workers.w_user.default = user
db.workers.w_organization.default = org
#####Naredimo še grid za šifrant turnusov
db.shifts.sh_organization.default = org
query_shifts = ((db.shifts.sh_organization == org) & (db.shifts.sh_status == 1))
query_inactive = db((db.shifts.sh_organization == org) & (db.shifts.sh_status == 100)).select().as_list()
# print query_inactive
###V kolikor uporabnik naredi turnus ki je že ampak je arhiviran/deaktiviran ga je potrebno na to opozorit
##Določimo da ne more bit za istga userja dvakrat isti turnus
# db.shifts.sh_code.requires=IS_NOT_IN_DB(db(db.shifts.sh_organization == org),'shifts.sh_code')
# najprej popravmo definicijo tabele
fields_shifts = (
db.shifts.sh_code,
db.shifts.sh_name,
db.shifts.sh_color,
db.shifts.sh_category,
db.shifts.sh_start1,
db.shifts.sh_end1,
db.shifts.sh_length1,
db.shifts.sh_start2,
db.shifts.sh_end2,
db.shifts.sh_length2,
db.shifts.sh_duration1,
# db.shifts.sh_start3,
# db.shifts.sh_end3,
# db.shifts.sh_start4,
# db.shifts.sh_end4,
# db.shifts.sh_start5,
# db.shifts.sh_end5,
db.shifts.sh_note)
db.shifts.sh_organization.readable = False
db.shifts.sh_organization.writable = False
db.shifts.sh_organization.editable = False
# db.shifts.sh_code.editable = db.shifts.sh_code.writable = False
# db.shifts.sh_duration.writable = False
db.shifts.sh_duration1.readable = db.shifts.sh_duration1.writable = False
db.shifts.sh_start3.readable = db.shifts.sh_start3.writable = False
db.shifts.sh_end3.readable = db.shifts.sh_end3.writable = False
db.shifts.sh_length3.readable = db.shifts.sh_length3.writable = False
db.shifts.sh_start4.readable = db.shifts.sh_start4.writable = False
db.shifts.sh_length4.readable = db.shifts.sh_length4.writable = False
db.shifts.sh_end4.readable = db.shifts.sh_end4.writable = False
db.shifts.sh_start5.readable = db.shifts.sh_start5.writable = False
db.shifts.sh_length5.readable = db.shifts.sh_length5.writable = False
db.shifts.sh_end5.readable = db.shifts.sh_end5.writable = False
db.shifts.sh_code.widget = SQLFORM.widgets.string.widget
db.shifts.sh_name.widget = SQLFORM.widgets.string.widget
db.shifts.sh_note.widget = SQLFORM.widgets.string.widget
db.shifts.partner_id.widget = SQLFORM.widgets.integer.widget
#db.shifts.sh_category.widget = SQLFORM.widgets.integer.widget
#db.shifts.sh_start1 = SQLFORM.widgets.time.widget
#db.shifts.sh_start1 = SQLFORM.widgets.timewidget.widget
db.shifts.sh_code.requires = [IS_NOT_IN_DB(db((db.shifts.sh_organization == org)&(db.shifts.sh_status != 250)), 'shifts.sh_code',
error_message=T('Shift with this code already exist. Follow the link in upper right corner'))]
db.shifts.sh_name.requires = [IS_NOT_EMPTY(error_message=T('Missing shift name'))]
db.shifts.sh_color.requires = [IS_NOT_EMPTY(error_message=T('Missing shift color'))]
db.shifts.sh_start1.requires = [IS_NOT_EMPTY(error_message=T('Missing start time of the shift'))]
db.shifts.sh_end1.requires = [IS_NOT_EMPTY(error_message=T('Missing end time of the shift'))]
# db.shifts.sh_category.requires = [IS_NOT_EMPTY(error_message=T("""Missing shift category.1 – Morning, 11- Afternoon, 21 – Night
# 101 – All day,121 – All night,201 – Day split"""))]
db.shifts.partner_id.requires = IS_EMPTY_OR(IS_NOT_IN_DB(db((db.shifts.sh_organization == org)
& (db.shifts.sh_status != 250)
& (db.shifts.partner_id == request.vars.partner_id)
& (db.shifts.partner_id >= 0)), db.shifts.partner_id,
error_message=T('There is a shift with the same partned ID.')))
links = [lambda row: A('', _class='glyphicon glyphicon glyphicon-remove-sign',
callback=URL('settings', 'deactivate',
vars=dict(table='shifts', field='sh_status', value=row.id)))]
#Tole je funcija ki preverja če je ok zta deljen delovni čas
"""Turnus je deljen lahko samo v enem dnevu.
Zato ni dovoljen zapis
0600-1400&1800-1300
"""
def my_form_processing_new_shifts(form):
shift_name = form.vars.sh_name
shifts = db((db.shifts.sh_organization == org) & (db.shifts.sh_name == shift_name) & (db.shifts.sh_status != 250)).select()
partner_id = form.vars.partner_id
if shifts:
form.errors.sh_name = T('Shift with this nick name already exists!')
if not partner_id or partner_id == '':
form.vars.partner_id = -1
redirect(URL('settings', 'shifts'))
def my_form_processing_edit_shifts(form):
partner_id = form.vars.partner_id
sh_category = form.vars.sh_category
# if not partner_id or partner_id == '':
# form.vars.partner_id = -1
# print
# print form.vars
# print
start1 = form.vars.sh_start1
end1 = form.vars.sh_end1
#start2 že pride kot datetime - Do not ask me why
start2 = form.vars.sh_start2
end2 = form.vars.sh_end2
# print "start1 ",start1
# print "end1 ",end1
# print "start2 ",start2
# print "end2 ", end2
if start1:
start1 = start1.split(":")
start1 = start1[0] + ":" + start1[1]
start1 = datetime.datetime.strptime(start1,'%H:%M').time()
if end1:
end1 = end1.split(":")
end1 = end1[0] + ":" + end1[1]
end1 = datetime.datetime.strptime(end1,'%H:%M').time()
#ker mi vrača čas end1 00:00 kot string ostalo pa kot time
#vsah en člas more bit
if start1 and end1 is None:
form.errors.sh_end1 = T('Please insert end time')
#če je neki vnešen v start2 pol more bit tut v end2
if start1 and end1 and start2 and end2 is None and end2 != datetime.datetime.strptime('00:00', '%H:%M').time():
form.errors.sh_end2 = T('Please insert end time')
if start1 and end1 and start2 == datetime.datetime.strptime('00:00', '%H:%M').time() and end2 is None:
form.errors.sh_end2 = T('Please insert end time')
if start1 and end1 and start2 is None and end2:
form.errors.sh_start2 = T('Please insert start time')
if start1 and end1 and start2 is None and end2 == datetime.datetime.strptime('00:00', '%H:%M').time():
form.errors.sh_start2 = T('Please insert start time')
if start1 and end1 and start2 == datetime.datetime.strptime('00:00', '%H:%M').time() and end2 == datetime.datetime.strptime('00:00', '%H:%M').time():
form.errors.sh_start2 = T('Second part of the shift cannot last 24hours')
form.errors.sh_end2 = T('Second part of the shift cannot last 24hours')
#Če so vsi časi morjo bit vsak večji od drucga, razen zadn je lahko 0:00
if (start1 and end1 and start2 and end2) or \
start1 and end1 and start2 and end2 == datetime.datetime.strptime('00:00', '%H:%M').time() or \
(start1 == datetime.datetime.strptime('00:00', '%H:%M').time() and end1 and start2 and end2 == datetime.datetime.strptime('00:00', '%H:%M').time()) or\
(start1 == datetime.datetime.strptime('00:00', '%H:%M').time() and end1 and start2 and end2):
if end1 <= start1:
form.errors.sh_end1 = T('End time 1 cannot be before Start time 1')
if end1 >= start2:
form.errors.sh_start2 = T('Start time 2 cannot be before End time 1')
if end1 >= start2:
form.errors.sh_start2 = T('Start time 2 cannot be before End time 1')
if end2 <= start2 and end2 != datetime.datetime.strptime('00:00', '%H:%M').time():
form.errors.sh_end2 = T('End time must be later than Start time')
"""Če user izbere kategorijo NA- 0, mu jo mi izračunamo glede na to kdaj se je time_start1
SET sh_category = 0 --> tole spremeniti
--- če se začne od 00-11:59 je 10
--- če se začne od 12-18:59 je 20
--- če se začne od 19-23:59 je 30
"""
if int(sh_category) == 0:
if datetime.datetime.strptime('00:00', '%H:%M').time() <= start1 <= datetime.datetime.strptime('11:59', '%H:%M').time():
form.vars.sh_category = 10
elif datetime.datetime.strptime('12:00', '%H:%M').time() <= start1 <= datetime.datetime.strptime('18:59', '%H:%M').time():
form.vars.sh_category = 20
elif datetime.datetime.strptime('19:00', '%H:%M').time() <= start1 <= datetime.datetime.strptime('23:59', '%H:%M').time():
form.vars.sh_category = 30
# redirect(URL('settings', 'shifts'))
grid_shifts = SQLFORM.grid(query=query_shifts,
fields=fields_shifts,orderby=db.shifts.sh_code,
searchable=False, create=True,
deletable=False, editable=True, paginate=100, buttons_placement='right',
user_signature=True,
showbuttontext=False,
formname='shiftTable',
onvalidation=my_form_processing_edit_shifts,
oncreate=my_form_processing_new_shifts,
formargs=dict(message_onsuccess=T('New record inserted'),
message_onfailure=T('Form has errors')),
links=links,
# oncreate=myfunction,
ui=dict(widget='',
header='',
content='',
default='',
cornerall='',
cornertop='',
cornerbottom='',
button='button btn btn-default',
buttontext='buttontext button',
buttonadd='icon plus icon-plus glyphicon glyphicon-plus',
buttonback='icon leftarrow icon-arrow-left glyphicon glyphicon-arrow-left',
buttonexport='icon downarrow icon-download glyphicon glyphicon-download',
buttondelete='icon trash icon-trash glyphicon glyphicon-trash',
buttonedit='icon pen icon-pencil glyphicon glyphicon-pencil',
buttontable='icon rightarrow icon-arrow-right glyphicon glyphicon-arrow-right',
buttonview='icon magnifier icon-zoom-in glyphicon glyphicon-eye-open',
buttonvidov='icon glyphicon glyphicon-euro'
),
exportclasses=dict(csv=False, csv_with_hidden_cols=False,
html=False, tsv_with_hidden_cols=False,json=False, tsv= False, xml = False))
# for input in grid_shifts.elements('input', _class='string'):
# input['_class'] = 'testniKlass'
if request.args and request.args[0] in ['edit', 'new']:
# dolocamo sirino polj
form = grid_shifts.create_form or grid_shifts.update_form
#grid_shifts.element('input[name=sh_color]')['_style'] = 'width:30%'
"PReverjanje polj za čase"
# form = grid_shifts.element('.web2py_form')
# form = grid_shifts.create_form or grid_shifts.update_form
if form.errors["sh_code"] == "Shift with this code already exist. Follow the link in upper right corner":
codeField = form.vars.sh_code
if codeField or codeField != '':
redId = db((db.shifts.sh_code == codeField) & (db.shifts.sh_organization == org) & (db.shifts.sh_status != 250)).select().first().id
# shiftId = db(db.shifts.sh_code == codeField).select(db.shifts.id).as_list()
# redId = shiftId[0]["id"]
url_ = URL('shifts/edit/shifts', args=redId)
'static', 'js/web2py-bootstrap3.js'
session.flash = A(
T("Click here if you want to activate shift with that code or if you want to be redirected to the shift."),
_href=url_)
return dict(form = form, query_inactive = query_inactive)
# if form.accepts(request.vars, session,onvalidation=my_form_processing):
# session.flash = T('Shift inserted')
# redirect(URL())
return dict(grid_shifts=grid_shifts, query_inactive=query_inactive)