def post_validate(self, form, validation_stopped): errors = [] data = {} if not self.raw_data: raise ValueError(_("No data")) lines = self.raw_data[0].file.read().decode('cp1252').split('\r\n') for line_number, line in enumerate(lines): if not line.strip(): continue try: parts = [part.strip() for part in line.split(';')] parts = [part for part in parts if part] bfs_number = int(parts[1]) dates = [parse(d, dayfirst=True).date() for d in parts[4:]] except (IndexError, TypeError, ValueError): errors.append(line_number) else: data[bfs_number] = {'dates': dates} if errors: raise ValueError(_( "Some rows contain invalid values: ${errors}.", mapping={'errors': ', '.join((str(e) for e in errors))} )) self.data = data
def breadcrumbs(self): return [ Link(_("Homepage"), self.homepage_url), Link(_("Users"), self.users_url), Link(self.model.title, self.request.link(self.model)), Link(_("Edit"), '#') ]
class ScanJobsForm(Form): sort_by = HiddenField() sort_order = HiddenField() from_date = DateField(label=_("Start date"), fieldset=_("Filter")) to_date = DateField(label=_("End date"), fieldset=_("Filter")) type = MultiCheckboxField(label=_("Type"), fieldset=_("Filter"), choices=[('normal', _("Regular shipment")), ('express', _("Express shipment"))]) term = StringField(label=_("Term"), fieldset=_("Filter")) def on_request(self): if hasattr(self, 'csrf_token'): self.delete_field('csrf_token') def select_all(self, name): field = getattr(self, name) if not field.data: field.data = list(next(zip(*field.choices))) def apply_model(self, model): self.from_date.data = model.from_date self.to_date.data = model.to_date self.type.data = model.type self.term.data = model.term self.sort_by.data = model.sort_by self.sort_order.data = model.sort_order # default unselected checkboxes to all choices self.select_all('type')
def breadcrumbs(self): return [ Link(_("Homepage"), self.homepage_url), Link(_("Municipalities"), self.municipalities_url), Link(self.model.name, self.request.link(self.model)), Link(self.title, '#') ]
def breadcrumbs(self): return [ Link(_("Homepage"), self.homepage_url), Link(_("Notifications"), self.notifications_url), Link(self.model.title, self.request.link(self.model)), Link(_("Edit"), '#') ]
def editbar_links(self): result = [] if self.request.has_permission(self.model, AddModelUnrestricted): result.append( Link( text=_("Add"), url=self.request.link( self.model, name='add-unrestricted' ), attrs={'class': 'add-icon'} ) ) elif self.request.has_permission(self.model, AddModel): result.append( Link( text=_("Add"), url=self.request.link( self.model, name='add' ), attrs={'class': 'add-icon'} ) ) return result
class MunicipalityForm(Form): name = StringField(label=_("Name"), validators=[InputRequired()]) bfs_number = IntegerField(label=_("BFS number"), validators=[InputRequired()]) address_supplement = StringField(label=_("Address supplement"), ) gpn_number = IntegerField(label=_("GPN number"), validators=[Optional()]) payment_type = RadioField(label=_("Payment type"), validators=[InputRequired()]) def on_request(self): query = self.request.session.query(PaymentType.name) self.payment_type.choices = [(r.name, r.name.capitalize()) for r in query] def update_model(self, model): model.name = self.name.data model.bfs_number = self.bfs_number.data model.address_supplement = self.address_supplement.data model.gpn_number = self.gpn_number.data model.payment_type = self.payment_type.data def apply_model(self, model): self.name.data = model.name self.bfs_number.data = model.bfs_number self.address_supplement.data = model.address_supplement self.gpn_number.data = model.gpn_number self.payment_type.data = model.payment_type
def breadcrumbs(self): return [ Link(_("Homepage"), self.homepage_url), Link(_("Scan jobs"), self.scan_jobs_url), Link(self.model.delivery_number, self.request.link(self.model)), Link(self.title, '#') ]
def breadcrumbs(self): return [ Link(_("Homepage"), self.homepage_url), Link(_("Report"), self.report_url), Link(self.title, "#"), Link(self.subtitle, self.request.link(self.model)), ]
def update_labels(self): year = date.today().year self.dispatch_tax_forms_older.label.text = _( "Tax forms until ${year}", mapping={'year': year - 2}) self.dispatch_tax_forms_last_year.label.text = _( "Tax forms ${year}", mapping={'year': year - 1}) self.dispatch_tax_forms_current_year.label.text = _( "Tax forms ${year}", mapping={'year': year})
def view_scan_jobs_unrestricted(self, request, form): if not form.errors: form.apply_model(self) return { 'layout': ScanJobsLayout(self, request), 'form': form, 'button_text': _("Apply filter"), 'reset_text': _("Reset filter"), 'permission': ViewModel }
def handle_notfound(self, request): """ Displays a nice HTTP 404 error. """ @request.after def set_status_code(response): response.status_code = self.code return { 'layout': DefaultLayout(self, request), 'title': _("Page not Found"), 'message': _("The page you are looking for could not be found."), }
class NotificationForm(Form): title = StringField(label=_("Title"), validators=[InputRequired()]) text = TextAreaField(label=_("Text"), validators=[InputRequired()]) def update_model(self, model): model.title = self.title.data model.text = self.text.data def apply_model(self, model): self.title.data = model.title self.text.data = model.text
def on_request(self): # Shipment types if self.request.has_role('editor'): self.type.choices = [('normal', _("Regular shipment")), ('express', _("Express shipment"))] # Dispatch dates self.dispatch_date_normal.choices = [ (r, f"{r:%d.%m.%Y}") for r in self.dispatch_dates(date.today()) ] # Labels self.update_labels()
def editbar_links(self): result = [] if self.request.has_permission(self.model, EditModel): result.append( Link(text=_("Import data"), url=self.request.link(self.model, 'import-data'), attrs={'class': 'upload-icon'})) if self.request.has_permission(self.model, AddModel): result.append( Link(text=_("Add"), url=self.request.link(self.model, name='add'), attrs={'class': 'add-icon'})) return result
def view_scan_jobs(self, request, form): if request.has_permission(self, ViewModelUnrestricted): return redirect(request.link(self, 'unrestricted')) if not form.errors: form.apply_model(self) return { 'layout': ScanJobsLayout(self, request), 'form': form, 'button_text': _("Apply filter"), 'reset_text': _("Reset filter"), 'permission': ViewModel }
def handle_forbidden(self, request): """ Displays a nice HTTP 403 error. """ @request.after def set_status_code(response): response.status_code = self.code return { 'layout': DefaultLayout(self, request), 'title': _("Access Denied"), 'message': _("You are trying to open a page for which you are not authorized.") }
def editbar_links(self): result = [] result.append( Link(text=_("Print delivery note"), url=self.request.link(self.model, 'delivery-note'), attrs={'class': 'print-icon'})) if self.request.has_permission(self.model, EditModelUnrestricted): result.append( Link(text=_("Edit"), url=self.request.link(self.model, 'edit-unrestricted'), attrs={'class': 'edit-icon'})) elif self.request.has_permission(self.model, EditModel): result.append( Link(text=_("Edit"), url=self.request.link(self.model, 'edit'), attrs={'class': 'edit-icon'})) if self.request.has_permission(self.model, DeleteModel): result.append( Link(text=_("Delete"), url=self.csrf_protected_url(self.request.link( self.model)), attrs={'class': 'delete-icon'}, traits=(Confirm( _("Do you really want to delete this " "scan job?"), _("This cannot be undone."), _("Delete"), _("Cancel")), Intercooler(request_method='DELETE', redirect_after=self.scan_jobs_url)))) return result
def view_select_report(self, request, form): layout = ReportLayout(self, request) if form.submitted(request): return redirect(request.link(form.get_model())) return {'layout': layout, 'form': form, 'button_text': _("Show")}
class ImportMunicipalityDataForm(Form): file = MunicipalityDataUploadField(label=_("File"), validators=[DataRequired()]) def update_model(self, model): model.import_data(self.file.data)
def import_municipality_data(self, request, form): """ Import municipality data. """ layout = ImportMunicipalityDataLayout(self, request) if form.submitted(request): form.update_model(self) request.message(_("Municipality data imported."), 'success') return redirect(layout.success_url) return { 'layout': layout, 'form': form, 'button_text': _("Import"), 'cancel': layout.cancel_url }
def delete_user(self, request): """ Delete a user. """ request.assert_valid_csrf_token() self.logout_all_sessions(request) UserCollection(request.session).delete(self.username) request.message(_("User deleted."), 'success')
def add_user_unrestricted(self, request, form): """ Create a new user. """ layout = AddUserLayout(self, request) if form.submitted(request): user = User() form.update_model(user) request.session.add(user) request.message(_("User added."), 'success') return redirect(layout.success_url) return { 'layout': layout, 'form': form, 'button_text': _("Save"), 'cancel': layout.cancel_url }
def add_notification(self, request, form): """ Create a new notification. """ layout = AddNotificationLayout(self, request) if form.submitted(request): notification = Notification.create(request) form.update_model(notification) request.message(_("Notification added."), 'success') return redirect(layout.success_url) return { 'layout': layout, 'form': form, 'button_text': _("Save"), 'cancel': layout.cancel_url }
def editbar_links(self): result = [] if self.request.has_permission(self.model, EditModel): result.append( Link(text=_("Edit"), url=self.request.link(self.model, 'edit'), attrs={'class': 'edit-icon'})) result.append( Link(text=_("Delete pick-up dates"), url=self.request.link(self.model, 'delete-dates'), attrs={'class': 'delete-icon'})) if self.request.has_permission(self.model, DeleteModel): result.append( Link(text=_("Delete"), url=self.csrf_protected_url(self.request.link( self.model)), attrs={'class': 'delete-icon'}, traits=(Confirm( _("Do you really want to delete this " "municipality?"), _("This cannot be undone."), _("Delete"), _("Cancel")), Intercooler( request_method='DELETE', redirect_after=self.municipalities_url)))) return result
def add_municipality(self, request, form): """ Create a new municipality. """ layout = AddMunicipalityLayout(self, request) if form.submitted(request): municipality = Municipality() form.update_model(municipality) request.session.add(municipality) request.message(_("Municipality added."), 'success') return redirect(layout.success_url) return { 'layout': layout, 'form': form, 'button_text': _("Save"), 'cancel': layout.cancel_url }
class UnrestrictedScanJobsForm(ScanJobsForm): municipality_id = ChosenSelectMultipleField(label=_("Municipality"), fieldset=_("Filter")) def on_request(self): super().on_request() query = self.request.session.query( Municipality.id.label('id'), Municipality.name.label('name'), Municipality.meta['bfs_number'].label('bfs_number')) query = query.order_by(unaccent(Municipality.name)) self.municipality_id.choices = [ (r.id.hex, f"{r.name} ({r.bfs_number})") for r in query ] def apply_model(self, model): super().apply_model(model) self.municipality_id.data = model.municipality_id
def editbar_links(self): result = [] model = PaymentTypeCollection(self.request.session) if self.request.has_permission(model, EditModel): result.append( Link(text=_("Manage payment types"), url=self.request.link(model), attrs={'class': 'payment-icon'})) return result
def editbar_links(self): result = [] result.append( Link(text=_("Print"), url='#', attrs={ 'class': 'print-icon', 'onclick': 'window.print();return false;' })) return result
def edit_user_manual(self, request, form): """ Edit the user manual """ layout = EditUserManualLayout(self, request) if form.submitted(request): form.update_model(self) request.message(_("User manual modified."), 'success') return redirect(layout.success_url) if not form.errors: form.apply_model(self) return { 'layout': layout, 'form': form, 'button_text': _("Save"), 'cancel': layout.cancel_url, }