def get_period_from_request(request): try: period = ReportingPeriod.get( ReportingPeriod.id == request.GET.get('period', 0)) except NoResultFound: period = ReportingPeriod.get_current_period() return period
def create(self): form = Form(ReportingPeriodForm().bind(), buttons=('Save', ), css_class='form-horizontal') if self.request.method == "POST": post = self.request.POST.items() try: payload = form.validate(post) except ValidationFailure: pass else: if payload['form_xpath'] not in ALLOWED_XPATHS and \ valid_year(payload['form_xpath']) is None: self.request.session.flash( _(u"Only numeric values 1-5 and calendar " "years (YYYY) allowed For Form Xpath"), 'error') elif DBSession.query(ReportingPeriod).filter_by( form_xpath=payload['form_xpath']).first(): self.request.session.flash(_(u"Form Xpath Exists"), 'error') else: period = ReportingPeriod(title=payload['title'], form_xpath=payload['form_xpath'], start_date=payload['start_date'], end_date=payload['end_date']) DBSession.add(period) DBSession.flush() self.request.session.flash(_(u"Reporting period created"), 'success') return HTTPFound( self.request.route_url('periods', traverse=('list'))) # render form return {'form': form, 'period': ReportingPeriod.get_current_period()}