def testValidationRequiredSplitDate(self): s1 = getattr(self, 's1') sdq1 = getattr(s1, 'sdq1') sdq1.setRequired(True) now = DateTime() now_value = str(now.year()) + '/' + str(now.month()) + '/' + str(now.day()) + ' ' + str(now.hour()) + ':' + str(now.minute()) + ':00 GMT' self.layer['request'].form['sdq1_ampm'] = '' self.layer['request'].form['sdq1_day'] = str(now.day()) self.layer['request'].form['sdq1_hour'] = str(now.hour()) self.layer['request'].form['sdq1_minute'] = str(now.minute()) self.layer['request'].form['sdq1_month'] = str(now.month()) self.layer['request'].form['sdq1_year'] = str(now.year()) dummy_controller_state = ControllerState( id='survey_view', context=s1, button='submit', status='success', errors={}, next_action=None,) controller = self.portal.portal_form_controller controller_state = controller.validate(dummy_controller_state, self.layer['request'], ['validate_survey',]) assert controller_state.getErrors() == {}, "Validation error raised: %s" % controller_state.getErrors() userid = s1.getSurveyId() assert userid == "test_user_1_", "Not default test user" questions = s1.getQuestions() for question in questions: if question.portal_type == 'Survey Date Question': assert question.getAnswerFor(userid) == now_value, "Answer not saved correctly: %s" % question.getAnswerFor(userid)
def render(self): context = aq_inner(self.context) annotations = IAnnotations(context) event_title = context.Title() event_start = context.startDate event_day = DateTime.day(event_start) event_month = DateTime.month(event_start) event_year = DateTime.year(event_start) event_hour = DateTime.Time(event_start) event_link = context.absolute_url() mailhost = getToolByName(context, 'MailHost') urltool = getToolByName(context, 'portal_url') portal = urltool.getPortalObject() email_charset = portal.getProperty('email_charset') to_address = '*****@*****.**' from_name = portal.getProperty('email_from_name') from_address = portal.getProperty('email_from_address') titulo_web = portal.getProperty('title') mtool = self.context.portal_membership userid = mtool.getAuthenticatedMember().id source = "%s <%s>" % (from_name, from_address) subject = "[Nou esdeveniment] %s" % (titulo_web) message = MESSAGE_TEMPLATE % dict(titolGW=titulo_web, titleEvent=event_title, dayEvent=event_day, monthEvent=event_month, yearEvent=event_year, hourEvent=event_hour, linkEvent=event_link, from_address=from_address, from_name=from_name, user_name=userid) mailhost.secureSend( message, to_address, source, subject=subject, subtype='plain', charset=email_charset, debug=False, ) if 'eventsent' not in annotations: annotations['eventsent'] = True # confirm = _(u"Mail sent.") confirm = _( u"Gràcies per la vostra col·laboració. Les dades de l\'activitat s\'han enviat correctament i seran publicades com més aviat millor." ) IStatusMessage(self.request).addStatusMessage(confirm, type='info') self.request.response.redirect(self.context.absolute_url())
def testValidationRequiredSplitDate(self): s1 = getattr(self, 's1') sdq1 = getattr(s1, 'sdq1') sdq1.setRequired(True) now = DateTime() now_value = str(now.year()) + \ '/' + str(now.month()) + \ '/' + str(now.day()) + \ ' ' + str(now.hour()) + \ ':' + str(now.minute()) + \ ':00 GMT' self.layer['request'].form['sdq1_ampm'] = '' self.layer['request'].form['sdq1_day'] = str(now.day()) self.layer['request'].form['sdq1_hour'] = str(now.hour()) self.layer['request'].form['sdq1_minute'] = str(now.minute()) self.layer['request'].form['sdq1_month'] = str(now.month()) self.layer['request'].form['sdq1_year'] = str(now.year()) dummy_controller_state = ControllerState( id='survey_view', context=s1, button='submit', status='success', errors={}, next_action=None, ) controller = self.portal.portal_form_controller controller_state = controller.validate(dummy_controller_state, self.layer['request'], [ 'validate_survey', ]) assert controller_state.getErrors() == {}, \ "Validation error raised: %s" % controller_state.getErrors() userid = s1.getSurveyId() assert userid == "test_user_1_", "Not default test user" questions = s1.getQuestions() for question in questions: if question.portal_type == 'Survey Date Question': assert question.getAnswerFor(userid) == now_value, \ "Answer not saved correctly: %s" \ % question.getAnswerFor(userid)
def getDaysClass(self, day, month, year, event=None): """ Determine the CSS class to use for the given day """ current = DateTime() if (current.year() == year and current.month() == month and current.day() == int(day)): if event: return 'todayevent' else: return 'todaynoevent' if event: return 'event' else: return ''
def render(self): context = aq_inner(self.context) annotations = IAnnotations(context) event_title = context.Title() event_start = context.startDate event_day = DateTime.day(event_start) event_month = DateTime.month(event_start) event_year = DateTime.year(event_start) event_hour = DateTime.Time(event_start) event_link = context.absolute_url() mailhost = getToolByName(context, 'MailHost') urltool = getToolByName(context, 'portal_url') portal = urltool.getPortalObject() email_charset = portal.getProperty('email_charset') to_address = '*****@*****.**' from_name = portal.getProperty('email_from_name') from_address = portal.getProperty('email_from') titulo_web = portal.getProperty('title') mtool = self.context.portal_membership userid = mtool.getAuthenticatedMember().id source = "%s <%s>" % (from_name, from_address) subject = "[Nou esdeveniment] %s" % (titulo_web) message = MESSAGE_TEMPLATE % dict(titolGW=titulo_web, titleEvent=event_title, dayEvent=event_day, monthEvent=event_month, yearEvent=event_year, hourEvent=event_hour, linkEvent=event_link, from_address=from_address, from_name=from_name, user_name=userid) mailhost.secureSend(message, to_address, source, subject=subject, subtype='plain', charset=email_charset, debug=False, ) if 'eventsent' not in annotations: annotations['eventsent'] = True # confirm = _(u"Mail sent.") confirm = _(u"Gràcies per la vostra col·laboració. Les dades de l\'activitat s\'han enviat correctament i seran publicades com més aviat millor.") IStatusMessage(self.request).addStatusMessage(confirm, type='info') self.request.response.redirect(self.context.absolute_url())
def getDay(self, data): day = str(DateTime.day(data)) return day
def result(self, date=None, use_ampm=False, starting_year=None, ending_year=None, future_years=None, minute_step=5): """Returns a dict with date information. """ ptool = getToolByName(self.context, 'portal_properties') site_props = getattr(ptool, 'site_properties', None) # Get the date format from the locale dates = self.request.locale.dates timepattern = dates.getFormatter('time').getPattern() if 'a' in timepattern: use_ampm = True month_names = dates.getFormatter('date').calendar.months # 'id' is what shows up. December for month 12. # 'value' is the value for the form. # 'selected' is whether or not it is selected. default = 0 years = [] days = [] months = [] hours = [] minutes = [] ampm = [] now = DateTime() if isinstance(date, basestring): date = date.strip() if not date: date = None else: # Please see datecomponents.txt for an explanation of # the next few lines. Also see #11423 dateParts = date.split(" ") dateParts[0] = dateParts[0].replace("-", "/") date = ' '.join(dateParts) if date is None: date = now default = 1 elif not isinstance(date, DateTime): try: date = DateTime(date) except (TypeError, DateTimeError): date = now default = 1 # Anything above PLONE_CEILING should be PLONE_CEILING if date.greaterThan(PLONE_CEILING): date = PLONE_CEILING # Represent the date in the local timezone try: local_zone = date.localZone(localtime(date.timeTime())) except ValueError: # Dates before 1970 use a negative timeTime() value, which on # on some platforms are not handled well and lead to a ValueError. # In those cases, calculate the local timezone (which is DST based) # from the same date in the *current year* instead. This is better # than failing altogether! timeZoneDate = DateTime(localtime().tm_year, *date.parts()[1:]) local_zone = date.localZone(localtime(timeZoneDate.timeTime())) date = date.toZone(local_zone) # Get portal year range min_year = starting_year if starting_year is None and site_props is not None: min_year = site_props.getProperty('calendar_starting_year', 1999) else: min_year = int(starting_year or 1999) if ending_year is None: if future_years is None and site_props is not None: future_years = site_props.getProperty('calendar_future_years_available', 5) else: future_years = int(future_years or 5) max_year = int(future_years) + now.year() else: max_year = int(ending_year) # keeps the existing date if it's out of range if not default: if min_year > date.year(): min_year = date.year() if max_year < date.year(): max_year = date.year() year = date.year() if default: years.append({'id': '--', 'value': '0000', 'selected': 1}) else: years.append({'id': '--', 'value': '0000', 'selected': None}) for x in range(min_year, max_year+1): d = {'id': x, 'value': x, 'selected': None} if x == year and not default: d['selected'] = 1 years.append(d) month = date.month() if default: months.append({'id': '--', 'value': '00', 'selected': 1, 'title': '--'}) else: months.append({'id': '--', 'value': '00', 'selected': None, 'title': '--'}) for x in range(1, 13): d = {'id': ENGLISH_MONTH_NAMES[x], 'value': '%02d' % x, 'selected': None} if x == month and not default: d['selected'] = 1 d['title'] = month_names[x][0] months.append(d) day = date.day() if default: days.append({'id': '--', 'value': '00', 'selected': 1}) else: days.append({'id': '--', 'value': '00', 'selected': None}) for x in range(1, 32): d = {'id': x, 'value': '%02d' % x, 'selected': None} if x == day and not default: d['selected'] = 1 days.append(d) if use_ampm: hours_range = [12] + range(1, 12) hour_default = '12' hour = int(date.h_12()) else: hours_range = range(0, 24) hour_default = '00' hour = int(date.h_24()) if default: hours.append({'id': '--', 'value': hour_default, 'selected': 1}) else: hours.append({'id': '--', 'value': hour_default, 'selected': None}) for x in hours_range: d = {'id': '%02d' % x, 'value': '%02d' % x, 'selected': None} if x == hour and not default: d['selected'] = 1 hours.append(d) if default: minutes.append({'id': '--', 'value': '00', 'selected': 1}) else: minutes.append({'id': '--', 'value': '00', 'selected': None}) minute = date.minute() if minute_step is None: minute_step = 5 if minute + minute_step >= 60: # edge case. see doctest for explanation minute = 60 - minute_step for x in range(0, 60, minute_step): d = {'id': '%02d' % x, 'value': '%02d' % x, 'selected': None} if (x == minute or minute < x < minute + minute_step) and not default: d['selected'] = 1 minutes.append(d) if use_ampm: p = date.strftime('%p') if default: ampm.append({'id': '--', 'value': 'AM', 'selected': 1}) else: ampm.append({'id': '--', 'value': 'AM', 'selected': None}) for x in ('AM', 'PM'): d = {'id': x, 'value': x, 'selected': None} if x == p and not default: d['selected'] = 1 ampm.append(d) return {'years': years, 'months': months, 'days': days, 'hours': hours, 'minutes': minutes, 'ampm': ampm}
def result(self, date=None, use_ampm=False, starting_year=None, ending_year=None, future_years=None, minute_step=5): """Returns a dict with date information. """ ptool = getToolByName(self.context, 'portal_properties') site_props = getattr(ptool, 'site_properties', None) # Get the date format from the locale dates = self.request.locale.dates timepattern = dates.getFormatter('time').getPattern() if 'a' in timepattern: use_ampm = True month_names = dates.getFormatter('date').calendar.months # 'id' is what shows up. December for month 12. # 'value' is the value for the form. # 'selected' is whether or not it is selected. default = 0 years = [] days = [] months = [] hours = [] minutes = [] ampm = [] now = DateTime() if isinstance(date, basestring): date = date.strip() if not date: date = None else: # Please see datecomponents.txt for an explanation of # the next few lines. Also see #11423 dateParts = date.split(" ") dateParts[0] = dateParts[0].replace("-", "/") date = ' '.join(dateParts) if date is None: date = now default = 1 elif not isinstance(date, DateTime): try: date = DateTime(date) except (TypeError, DateTimeError): date = now default = 1 # Anything above PLONE_CEILING should be PLONE_CEILING if date.greaterThan(PLONE_CEILING): date = PLONE_CEILING # Represent the date in the local timezone try: local_zone = date.localZone(localtime(date.timeTime())) except ValueError: # Dates before 1970 use a negative timeTime() value, which on # on some platforms are not handled well and lead to a ValueError. # In those cases, calculate the local timezone (which is DST based) # from the same date in the *current year* instead. This is better # than failing altogether! timeZoneDate = DateTime(localtime().tm_year, *date.parts()[1:]) local_zone = date.localZone(localtime(timeZoneDate.timeTime())) date = date.toZone(local_zone) # Get portal year range min_year = starting_year if starting_year is None and site_props is not None: min_year = site_props.getProperty('calendar_starting_year', 1999) else: min_year = int(starting_year or 1999) if ending_year is None: if future_years is None and site_props is not None: future_years = site_props.getProperty( 'calendar_future_years_available', 5) else: future_years = int(future_years or 5) max_year = int(future_years) + now.year() else: max_year = int(ending_year) # keeps the existing date if it's out of range if not default: if min_year > date.year(): min_year = date.year() if max_year < date.year(): max_year = date.year() year = date.year() if default: years.append({'id': '--', 'value': '0000', 'selected': 1}) else: years.append({'id': '--', 'value': '0000', 'selected': None}) for x in range(min_year, max_year + 1): d = {'id': x, 'value': x, 'selected': None} if x == year and not default: d['selected'] = 1 years.append(d) month = date.month() if default: months.append({ 'id': '--', 'value': '00', 'selected': 1, 'title': '--' }) else: months.append({ 'id': '--', 'value': '00', 'selected': None, 'title': '--' }) for x in range(1, 13): d = { 'id': ENGLISH_MONTH_NAMES[x], 'value': '%02d' % x, 'selected': None } if x == month and not default: d['selected'] = 1 d['title'] = month_names[x][0] months.append(d) day = date.day() if default: days.append({'id': '--', 'value': '00', 'selected': 1}) else: days.append({'id': '--', 'value': '00', 'selected': None}) for x in range(1, 32): d = {'id': x, 'value': '%02d' % x, 'selected': None} if x == day and not default: d['selected'] = 1 days.append(d) if use_ampm: hours_range = [12] + range(1, 12) hour_default = '12' hour = int(date.h_12()) else: hours_range = range(0, 24) hour_default = '00' hour = int(date.h_24()) if default: hours.append({'id': '--', 'value': hour_default, 'selected': 1}) else: hours.append({'id': '--', 'value': hour_default, 'selected': None}) for x in hours_range: d = {'id': '%02d' % x, 'value': '%02d' % x, 'selected': None} if x == hour and not default: d['selected'] = 1 hours.append(d) if default: minutes.append({'id': '--', 'value': '00', 'selected': 1}) else: minutes.append({'id': '--', 'value': '00', 'selected': None}) minute = date.minute() if minute_step is None: minute_step = 5 if minute + minute_step >= 60: # edge case. see doctest for explanation minute = 60 - minute_step for x in range(0, 60, minute_step): d = {'id': '%02d' % x, 'value': '%02d' % x, 'selected': None} if (x == minute or minute < x < minute + minute_step) and not default: d['selected'] = 1 minutes.append(d) if use_ampm: p = date.strftime('%p') if default: ampm.append({'id': '--', 'value': 'AM', 'selected': 1}) else: ampm.append({'id': '--', 'value': 'AM', 'selected': None}) for x in ('AM', 'PM'): d = {'id': x, 'value': x, 'selected': None} if x == p and not default: d['selected'] = 1 ampm.append(d) return { 'years': years, 'months': months, 'days': days, 'hours': hours, 'minutes': minutes, 'ampm': ampm }