def _handle_export_request(request, items, callback=None): """Helper function to handle the export request. This function provides the required logic to show the export configuration dialog and returning the exported items. It is called when exporting a single item or when exporting multiple items in a bundle.""" clazz = request.context.__model__ renderer = ExportDialogRenderer(request, clazz) form = renderer.form if (request.method == 'POST' and is_confirmed(request) and form.validate(request.params)): # Setup exporter ef = form.data.get('format') if ef == "json": exporter = JSONExporter(clazz) elif ef == "csv": exporter = CSVExporter(clazz) elif ef == "xlsx": exporter = XLSXExporter(clazz) export = exporter.perform(items) # Build response resp = request.response resp.content_type = str('application/%s' % ef) resp.content_disposition = 'attachment; filename=export.%s' % ef resp.body = export return resp else: # FIXME: Get the ActionItem here and provide this in the Dialog to get # the translation working (torsten) <2013-07-10 09:32> rvalue = {} rvalue['dialog'] = renderer.render(items) return rvalue
def _handle_evaluation_request(request, items, context=None): # context is always None if comming from Ringo. clazz = request.context.__model__ modul = request.context.__modul__ renderer = EvaluationDialogRenderer(request, clazz) form = renderer.form if (request.method == 'POST' and is_confirmed(request) and form.validate(request.params)): # 1. Load evaluation file factory = Extension.get_item_factory() evaluation = factory.load(form.data["evaluations"]) exportformat = form.data.get("exportformat", "ods") export_config = evaluation.config_as_json spreadsheet = ezodf.opendoc(load_file(evaluation.data)) # 2. Export data exporter = RecursiveRelationExporter(clazz, export_config) sheet_data = exporter.perform(items) relation_config = exporter.get_relation_config() # 3. Create sheets per relation sheets = spreadsheet.sheets for relation in relation_config: data = sheet_data[relation] if relation == "root": sheetname = modul.name else: sheetname = relation try: sheet = sheets[sheetname] except: sheets.append(ezodf.Table(sheetname)) sheet = sheets[sheetname] _fill_sheet(sheet, data, relation_config[relation]) # 4. Convert and recalcualte the spreadsheet # converter = get_converter() # if converter.is_available(): # spreadsheet_data = _convert_spreadsheet(spreadsheet, exportformat) # else: spreadsheet_data = spreadsheet.tobytes() # 5. Build response resp = request.response resp.content_type = str('application/%s' % exportformat) ctime = get_local_datetime(datetime.utcnow()) filename = "%s_%s.%s" % (ctime.strftime("%Y-%m-%d-%H%M"), sheetname.lower(), exportformat) resp.content_disposition = 'attachment; filename=%s' % filename resp.body = spreadsheet_data return resp else: # FIXME: Get the ActionItem here and provide this in the Dialog to get # the translation working (torsten) <2013-07-10 09:32> rvalue = {} rvalue['dialog'] = renderer.render(items) return rvalue
def _handle_anonymize_request(request, items): _ = request.translate clazz = request.context.__model__ modul = request.context.__modul__ renderer = ConfirmDialogRenderer(request, clazz, _("anonymize")) if (request.method == 'POST' and is_confirmed(request)): for item in items: item.anonymize() rvalue = {} rvalue['dialog'] = renderer.render(items) return rvalue
def _handle_delete_request(request, items, callback): clazz = request.context.__model__ _ = request.translate if request.method == 'POST' and is_confirmed(request): item_label = get_item_modul(request, clazz).get_label(plural=True) item_label_log = get_item_modul(request, clazz).get_label() mapping = {'item_type': item_label, 'num': len(items)} for item in items: if callback: item = callback(request, item) request.db.delete(item) # Invalidate cache invalidate_cache() try: request.db.flush() except (sa.exc.CircularDependencyError, sa.exc.IntegrityError) as e: mapping["error"] = e.message.decode("utf-8") title = _("Can not delete ${item_type} items.", mapping=mapping) body = _("There has been an integrity error which prevents " "the request to be fulfilled. There are still " "depended items on the item to be deleted. Please " "remove all depended relations to this item before " "deleting it and try again. Hint: ${error}", mapping=mapping) request.db.rollback() renderer = InfoDialogRenderer(request, title, body) rvalue = {} ok_url = request.session['history'].pop(2) rvalue['dialog'] = renderer.render(ok_url) return rvalue msg = _('Deleted ${num} ${item_type} successfully.', mapping=mapping) log_msg = u'User {user.login} deleted {item_label} {item.id}' \ .format(item_label=item_label, item=item, user=request.user) log.info(log_msg) request.session.flash(msg, 'success') # Handle redirect after success. return _handle_redirect(request) else: renderer = ConfirmDialogRenderer(request, clazz, 'delete') rvalue = {} rvalue['dialog'] = renderer.render(items) rvalue['clazz'] = clazz rvalue['item'] = items return rvalue
def print_(request): handle_history(request) handle_params(request) item = get_item_from_request(request) renderer = PrintDialogRenderer(request, item) form = renderer.form if (request.method == 'POST' and is_confirmed(request) and form.validate(request.params)): template = form.data.get('printtemplates')[0] out = _render_template(request, template, item) return _build_final_response(out, request, template) else: clazz = item.__class__ rvalue = {} rvalue['dialog'] = renderer.render() rvalue['clazz'] = clazz rvalue['item'] = item return rvalue
def import_(request, callback=None): handle_history(request) handle_params(request) clazz = request.context.__model__ _ = request.translate renderer = ImportDialogRenderer(request, clazz) imported_items = [] form = renderer.form if request.method == "POST" and is_confirmed(request) and form.validate(request.params): try: items = _import(request) except (ValueError, AttributeError) as e: err_title = _("Import failed") err_msg = ( _( "Bad news! The import could not be finished and " "returns with an error." "No data has been modified in this operation and no " "items has been imported or updated. " "The last message we heard from the importer was: %s" ) % e ) renderer = ErrorDialogRenderer(request, err_title, err_msg) rvalue = {} ok_url = request.session["history"].pop(2) rvalue["dialog"] = renderer.render(ok_url) rvalue["clazz"] = clazz return rvalue imported_items = _handle_save(request, items, callback) invalidate_cache() redirect = _handle_redirect(request) if redirect: return redirect rvalue = {} rvalue["dialog"] = renderer.render(imported_items) rvalue["clazz"] = clazz return rvalue