def action_create(self): if not self.handler.new_enabled: return self.handler.raise405() data = self.handler.get_request_data() try: self.handler.data_manager.save_instance(data) return self.redirect(message='Object added successfully.') except AssertionError as e: instance = ObjectDict() instance.update(**data) return self.page_edit(instance, exception=e)
def page_edit(self, instance, exception=None, alert=None): if instance and not self.handler.edit_enabled: return self.handler.raise405() errors = None if exception: alert = 'Data sent contains some issues.' errors = ObjectDict() if hasattr(exception, 'to_dict'): errors.update(**exception.to_dict()) value_for = lambda field: getattr(instance, field, '') if getattr( instance, field, '') else '' has_error = lambda field: errors and field in list(errors.keys()) error_for = lambda field: errors[ field] if errors and field in errors else '' return self.render(self.handler.edit_template, obj=instance, errors=errors, alert=alert, value_for=value_for, has_error=has_error, error_for=error_for)