[gen] Calendar field: one may now specify a method in param validator for validating the creation of an event.
This commit is contained in:
parent
7e7d0933f5
commit
e52fee658f
|
@ -879,7 +879,7 @@ class Field:
|
||||||
m_getRequestValue defined above, is valid according to this type
|
m_getRequestValue defined above, is valid according to this type
|
||||||
definition. If it is the case, None is returned. Else, a translated
|
definition. If it is the case, None is returned. Else, a translated
|
||||||
error message is returned.'''
|
error message is returned.'''
|
||||||
# If the value is required, check that a (complete) value is present.
|
# If the value is required, check that a (complete) value is present
|
||||||
if not self.isCompleteValue(obj, value):
|
if not self.isCompleteValue(obj, value):
|
||||||
if self.required and self.isClientVisible(obj):
|
if self.required and self.isClientVisible(obj):
|
||||||
# If the field is required, but not visible according to
|
# If the field is required, but not visible according to
|
||||||
|
@ -897,7 +897,7 @@ class Field:
|
||||||
if self.validator and (type(self.validator) in self.validatorTypes):
|
if self.validator and (type(self.validator) in self.validatorTypes):
|
||||||
obj = obj.appy()
|
obj = obj.appy()
|
||||||
if type(self.validator) != self.validatorTypes[-1]:
|
if type(self.validator) != self.validatorTypes[-1]:
|
||||||
# It is a custom function. Execute it.
|
# It is a custom function: execute it
|
||||||
try:
|
try:
|
||||||
validValue = self.validator(obj, value)
|
validValue = self.validator(obj, value)
|
||||||
if isinstance(validValue, basestring) and validValue:
|
if isinstance(validValue, basestring) and validValue:
|
||||||
|
|
|
@ -1165,19 +1165,15 @@ class Calendar(Field):
|
||||||
events.append(Event(eventType))
|
events.append(Event(eventType))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def createEvent(self, obj, date, timeslot='main', eventType=None,
|
def createEvent(self, obj, date, eventType, timeslot='main', eventSpan=None,
|
||||||
eventSpan=None, handleEventSpan=True):
|
handleEventSpan=True):
|
||||||
'''Create a new event in the calendar, at some p_date (day).
|
'''Create a new event in the calendar, at some p_date (day). If
|
||||||
If p_eventType is given, it is used; else, rq['eventType'] is used.
|
p_handleEventSpan is True, we will use p_eventSpan and also create
|
||||||
If p_handleEventSpan is True, we will use p_eventSpan (or
|
the same event for successive days.'''
|
||||||
rq["eventSpan"] if p_eventSpan is not given) and also
|
|
||||||
create the same event for successive days.'''
|
|
||||||
obj = obj.o # Ensure p_obj is not a wrapper
|
obj = obj.o # Ensure p_obj is not a wrapper
|
||||||
rq = obj.REQUEST
|
rq = obj.REQUEST
|
||||||
# Get values from parameters
|
# Get values from parameters
|
||||||
if not eventType: eventType = rq['eventType']
|
if not eventType: eventType = rq['eventType']
|
||||||
if handleEventSpan and not eventSpan:
|
|
||||||
eventSpan = rq.get('eventSpan', None)
|
|
||||||
# Split the p_date into separate parts
|
# Split the p_date into separate parts
|
||||||
year, month, day = date.year(), date.month(), date.day()
|
year, month, day = date.year(), date.month(), date.day()
|
||||||
# Check that the "preferences" dict exists or not
|
# Check that the "preferences" dict exists or not
|
||||||
|
@ -1216,11 +1212,11 @@ class Calendar(Field):
|
||||||
# Span the event on the successive days if required
|
# Span the event on the successive days if required
|
||||||
suffix = ''
|
suffix = ''
|
||||||
if handleEventSpan and eventSpan:
|
if handleEventSpan and eventSpan:
|
||||||
nbOfDays = min(int(eventSpan), self.maxEventLength)
|
for i in range(eventSpan):
|
||||||
for i in range(nbOfDays):
|
|
||||||
date = date + 1
|
date = date + 1
|
||||||
self.createEvent(obj, date, timeslot, handleEventSpan=False)
|
self.createEvent(obj, date, eventType, timeslot,
|
||||||
suffix = ', span+%d' % nbOfDays
|
handleEventSpan=False)
|
||||||
|
suffix = ', span+%d' % eventSpan
|
||||||
if handleEventSpan:
|
if handleEventSpan:
|
||||||
msg = 'added %s, slot %s%s' % (eventType, timeslot, suffix)
|
msg = 'added %s, slot %s%s' % (eventType, timeslot, suffix)
|
||||||
self.log(obj, msg, date)
|
self.log(obj, msg, date)
|
||||||
|
@ -1276,6 +1272,21 @@ class Calendar(Field):
|
||||||
break
|
break
|
||||||
i -= 1
|
i -= 1
|
||||||
|
|
||||||
|
def validate(self, obj, date, eventType, timeslot, span=0):
|
||||||
|
'''The validation process for a calendar is a bit different from the
|
||||||
|
standard one, that checks a "complete" request value. Here, we only
|
||||||
|
check the validity of some insertion of events within the
|
||||||
|
calendar.'''
|
||||||
|
if not self.validator: return
|
||||||
|
res = self.validator(obj, date, eventType, timeslot, span)
|
||||||
|
if isinstance(res, basestring):
|
||||||
|
# Validation failed, and we have the error message in "res"
|
||||||
|
return res
|
||||||
|
if not res:
|
||||||
|
# Validation failed, without specific message: return a standard one
|
||||||
|
return obj.translate('field_invalid')
|
||||||
|
return res
|
||||||
|
|
||||||
def process(self, obj):
|
def process(self, obj):
|
||||||
'''Processes an action coming from the calendar widget, ie, the creation
|
'''Processes an action coming from the calendar widget, ie, the creation
|
||||||
or deletion of a calendar event.'''
|
or deletion of a calendar event.'''
|
||||||
|
@ -1285,9 +1296,16 @@ class Calendar(Field):
|
||||||
obj.mayEdit(self.writePermission, raiseError=True)
|
obj.mayEdit(self.writePermission, raiseError=True)
|
||||||
# Get the date and timeslot for this action
|
# Get the date and timeslot for this action
|
||||||
date = DateTime(rq['day'])
|
date = DateTime(rq['day'])
|
||||||
|
eventType = rq.get('eventType')
|
||||||
timeslot = rq.get('timeslot', 'main')
|
timeslot = rq.get('timeslot', 'main')
|
||||||
|
eventSpan = rq.get('eventSpan') or 0
|
||||||
|
eventSpan = min(int(eventSpan), self.maxEventLength)
|
||||||
if action == 'createEvent':
|
if action == 'createEvent':
|
||||||
return self.createEvent(obj, date, timeslot)
|
# Trigger validation
|
||||||
|
valid = self.validate(obj.appy(), date, eventType, timeslot,
|
||||||
|
eventSpan)
|
||||||
|
if isinstance(valid, basestring): return valid
|
||||||
|
return self.createEvent(obj, date, eventType, timeslot, eventSpan)
|
||||||
elif action == 'deleteEvent':
|
elif action == 'deleteEvent':
|
||||||
return self.deleteEvent(obj, date, timeslot)
|
return self.deleteEvent(obj, date, timeslot)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue