def upload_ziphandler(req): schemes = get_permitted_schemas() files = [] scheme_type = {} basenode = q(Node).get(req.params.get('id')) for file in basenode.files: if file.abspath.endswith(req.params.get('file')): z = zipfile.ZipFile(file.abspath) for f in z.namelist(): #strip unwanted garbage from string name = mybasename(f).decode('utf8', 'ignore').encode('utf8') random_str = ustr(random.random())[2:] if name.startswith("._"): # ignore Mac OS X junk continue if name.split('.')[0] == '': name = random_str + name files.append(name.replace(" ", "_")) _m = getMimeType(name) if random_str in name: newfilename = join_paths(config.get("paths.tempdir"), name.replace(" ", "_")) else: newfilename = join_paths( config.get("paths.tempdir"), random_str + name.replace(" ", "_")) with codecs.open(newfilename, "wb") as fi: fi.write(z.read(f)) fn = importFileToRealname(mybasename(name.replace(" ", "_")), newfilename) basenode.files.append(fn) if os.path.exists(newfilename): os.unlink(newfilename) if _m[1] not in scheme_type: scheme_type[_m[1]] = [] for scheme in schemes: if _m[1] in scheme.getDatatypes(): scheme_type[_m[1]].append(scheme) try: z.close() os.remove(file.abspath) except: pass basenode.files.remove(file) db.session.commit() return {'files': files, 'schemes': scheme_type}
def _finish_change(node, change_file, user, uploadfile, req): if change_file in ["yes", "no"]: # check that the correct filetype is uploaded # note: only the suffix of the filename is checked not the file content uploadfile_type = getMimeType(uploadfile.filename)[1] if uploadfile_type != node.type and uploadfile_type != node.get_upload_filetype(): req.setStatus(httpstatus.HTTP_NOT_ACCEPTABLE) return # sys files are always cleared to delete remaining thumbnails, presentation images etc. for f in node.files: if f.filetype in node.get_sys_filetypes(): node.files.remove(f) file = importFile(uploadfile.filename, uploadfile.tempname) # add new file file.filetype = node.get_upload_filetype() node.files.append(file) # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname) return attpath = "" for f in node.files: if f.mimetype == "inode/directory": attpath = f.base_name break if change_file == "attdir": # add attachmentdir if attpath == "": # add attachment directory attpath = req.params.get("inputname") if not os.path.exists(getImportDir() + "/" + attpath): os.mkdir(getImportDir() + "/" + attpath) node.files.append(File(getImportDir() + "/" + attpath, "attachment", "inode/directory")) importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file if change_file == "attfile": # add file as attachment if attpath == "": # no attachment directory existing file = importFileToRealname(uploadfile.filename, uploadfile.tempname) # add new file file.mimetype = "inode/file" file.filetype = "attachment" node.files.append(file) else: # import attachment file into existing attachment directory importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file
def upload_ziphandler(req): schemes = getSchemes(req) files = [] scheme_type = {} basenode = tree.getNode(req.params.get('id')) for file in basenode.getFiles(): if file.retrieveFile().endswith(req.params.get('file')): z = zipfile.ZipFile(file.retrieveFile()) for f in z.namelist(): #strip unwanted garbage from string name = mybasename(f).decode('utf8', 'ignore').encode('utf8') random_str = str(random.random())[2:] if name.startswith("._"): # ignore Mac OS X junk continue if name.split('.')[0] == '': name = random_str + name files.append(name.replace(" ", "_")) _m = getMimeType(name) if random_str in name: newfilename = join_paths(config.get("paths.tempdir"), name.replace(" ", "_")) else: newfilename = join_paths(config.get("paths.tempdir"), random_str + name.replace(" ", "_")) fi = open(newfilename, "wb") fi.write(z.read(f)) fi.close() fn = importFileToRealname(mybasename(name.replace(" ", "_")), newfilename) basenode.addFile(fn) if os.path.exists(newfilename): os.unlink(newfilename) if _m[1] not in scheme_type: scheme_type[_m[1]] = [] for scheme in schemes: if _m[1] in scheme.getDatatypes(): scheme_type[_m[1]].append(scheme) try: z.close() os.remove(file.retrieveFile()) except: pass basenode.removeFile(file) return {'files': files, 'schemes': scheme_type}
def upload_ziphandler(req): schemes = get_permitted_schemas() files = [] scheme_type = {} basenode = q(Node).get(req.params.get('id')) for file in basenode.files: if file.abspath.endswith(req.params.get('file')): z = zipfile.ZipFile(file.abspath) for f in z.namelist(): #strip unwanted garbage from string name = mybasename(f).decode('utf8', 'ignore').encode('utf8') random_str = ustr(random.random())[2:] if name.startswith("._"): # ignore Mac OS X junk continue if name.split('.')[0] == '': name = random_str + name files.append(name.replace(" ", "_")) _m = getMimeType(name) if random_str in name: newfilename = join_paths(config.get("paths.tempdir"), name.replace(" ", "_")) else: newfilename = join_paths(config.get("paths.tempdir"), random_str + name.replace(" ", "_")) with codecs.open(newfilename, "wb") as fi: fi.write(z.read(f)) fn = importFileToRealname(mybasename(name.replace(" ", "_")), newfilename) basenode.files.append(fn) if os.path.exists(newfilename): os.unlink(newfilename) if _m[1] not in scheme_type: scheme_type[_m[1]] = [] for scheme in schemes: if _m[1] in scheme.getDatatypes(): scheme_type[_m[1]].append(scheme) with suppress(Exception, warn=False): z.close() os.remove(file.abspath) basenode.files.remove(file) db.session.commit() return {'files': files, 'schemes': scheme_type}
def show_workflow_node(self, node, req, data=None): check_context() user = users.getUserFromRequest(req) current_workflow = getNodeWorkflow(node) current_workflow_step = getNodeWorkflowStep(node) FATAL_ERROR = False FATAL_ERROR_STR = "" if "gotrue" in req.params: if not PYPDF_MODULE_PRESENT: del req.params['gotrue'] return self.show_workflow_node(node, req) radio_apply_reset_accept = req.params.get( 'radio_apply_reset_accept', '') if radio_apply_reset_accept == 'reset': for f in node.files: f_name = f.base_name if f_name.startswith( 'addpic2pdf_%s_node_%s_' % (unicode(current_workflow_step.id), unicode( node.id))) and f.filetype.startswith('p_document'): logg.info( "workflow step addpic2pdf(%s): going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)", current_workflow_step.id, f_name, node.name, node.id, user.login_name, req.ip) node.files.remove(f) db.session.commit() try: os.remove(f.abspath) except: logg.exception( "exception in workflow setep addpic2pdf, removing file failed, ignoring" ) del req.params['gotrue'] return self.show_workflow_node(node, req) elif radio_apply_reset_accept == 'accept': p_document_files = [ f for f in node.files if f.filetype == 'p_document' and f.base_name.startswith( 'addpic2pdf_%s_node_%s_' % (unicode(current_workflow_step.id), unicode(node.id))) ] if len(p_document_files) > 0: p_document_file = p_document_files[0] document_file = [ f for f in node.files if f.filetype == 'document' ][0] o_document_file = File(document_file.path, 'o_document', document_file.mimetype) node.files.remove(document_file) node.files.append(o_document_file) o_document_name = o_document_file.base_name for f in node.files: if f.filetype in [ 'thumb', 'fileinfo', 'fulltext' ] or f.filetype.startswith('present'): if os.path.splitext( f.base_name)[0] == os.path.splitext( o_document_name)[0]: new_f = File(f.path, 'o_' + f.filetype, f.mimetype) node.files.remove(f) node.files.append(new_f) new_document_file = File(p_document_file.path, 'document', p_document_file.mimetype) node.files.remove(p_document_file) node.files.append(new_document_file) db.session.commit() node.event_files_changed() del req.params['gotrue'] return self.forwardAndShow(node, True, req) elif radio_apply_reset_accept == 'apply': drag_logo_fullname = req.params.get("input_drag_logo_fullname", None) if not drag_logo_fullname: req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_no_logo_selected")) del req.params['gotrue'] return self.show_workflow_node(node, req) drag_logo_filepath = [ f.abspath for f in current_workflow_step.files if f.base_name == drag_logo_fullname ][0] pos_cm = req.params.get("input_poffset_cm", "0, 0") x_cm, y_cm = [float(x.strip()) for x in pos_cm.split(",")] pdf_in_filepath = getPdfFilepathForProcessing( current_workflow_step, node) current_pageno = int( req.params.get("input_current_page", "0").strip()) radio_select_targetpages = req.params.get( "radio_select_targetpages", "").strip() input_select_targetpages = req.params.get( "input_select_targetpages", "").strip() printer_range = [] page_count = get_pdf_pagecount(pdf_in_filepath) _parser_error = False try: if radio_select_targetpages == "current_page": printer_range = [current_pageno] elif radio_select_targetpages == "all": printer_range = range(0, page_count) elif radio_select_targetpages == "pair": printer_range = [ x for x in range(0, page_count) if x % 2 ] if input_select_targetpages: printer_range = [ x for x in printer_range if x in parse_printer_range(input_select_targetpages, maximum=page_count + 1) ] elif radio_select_targetpages == "impair": printer_range = [ x for x in range(0, page_count) if not x % 2 ] if input_select_targetpages: printer_range = [ x for x in printer_range if x in parse_printer_range(input_select_targetpages, maximum=page_count + 1) ] elif radio_select_targetpages == "range_only" and input_select_targetpages: printer_range = parse_printer_range( input_select_targetpages, maximum=page_count + 1) except ValueError as e: _parser_error = True if _parser_error: req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_printer_range_error")) del req.params['gotrue'] return self.show_workflow_node(node, req) printer_range = map(int, list(printer_range)) if not printer_range: req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t( lang(req), "admin_wfstep_addpic2pdf_printer_range_selected_empty" )) del req.params['gotrue'] return self.show_workflow_node(node, req) x = x_cm * cm # cm = 28.346456692913385 y = y_cm * cm pic_dpi = get_pic_info(drag_logo_filepath).get('dpi', None) scale = 1.0 if pic_dpi: dpi_x, dpi_y = pic_dpi if dpi_x != dpi_y: req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_logo_dpix_dpiy")) dpi = int(dpi_x) if dpi == 72: scale = 1.0 else: scale = 1.0 * 72.0 / dpi else: dpi = 300 scale = 1.0 * 72.0 / dpi #dpi = 72 #scale = 1.0 tmppath = config.get("paths.datadir") + "tmp/" date_str = format_date().replace('T', '-').replace(' ', '').replace( ':', '-') filetempname = tmppath + \ "temp_addpic_pdf_wfs_%s_node_%s_%s_%s_.pdf" % ( unicode(current_workflow_step.id), unicode(node.id), date_str, unicode(random.random())) url = req.params.get('input_drag_logo_url', '') fn_out = filetempname build_logo_overlay_pdf(pdf_in_filepath, drag_logo_filepath, fn_out, x, y, scale=scale, mask='auto', pages=printer_range, follow_rotate=True, url=(" " * ADD_NBSP) + url) for f in node.files: f_name = f.base_name if f_name.startswith('addpic2pdf_%s_node_%s_' % ( unicode(current_workflow_step.id), unicode(node.id), )) and f.filetype.startswith('p_document'): logg.info( "workflow step addpic2pdf(%s): going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)", current_workflow_step.id, f_name, node.name, node.id, user.login_name, req.ip) node.files.remove(f) try: os.remove(f.abspath) except: pass break date_str = format_date().replace('T', '-').replace(' ', '').replace( ':', '-') nodeFile = importFileToRealname( "_has_been_processed_%s.pdf" % (date_str), filetempname, prefix='addpic2pdf_%s_node_%s_' % ( unicode(current_workflow_step.id), unicode(node.id), ), typeprefix="p_") node.files.append(nodeFile) db.session.commit() try: os.remove(filetempname) except: pass del req.params['gotrue'] return self.show_workflow_node(node, req) if "gofalse" in req.params: return self.forwardAndShow(node, False, req) # part of show_workflow_node not handled by "gotrue" and "gofalse" try: pdf_filepath = [ f.abspath for f in node.files if f.filetype.startswith('document') ][0] error_no_pdf = False except: error_no_pdf = t( lang(req), "admin_wfstep_addpic2pdf_no_pdf_document_for_this_node") if not PYPDF_MODULE_PRESENT or error_no_pdf: error = "" if not PYPDF_MODULE_PRESENT: error += t(lang(req), "admin_wfstep_addpic2pdf_no_pypdf") if error_no_pdf: error += error_no_pdf pdf_dimensions = { 'd_pageno2size': { 0: [595.275, 841.889] }, 'd_pageno2rotate': { 0: 0 } } # A4 keep_params = copyDictValues(req.params, {}, KEEP_PARAMS) context = { "key": req.params.get("key", req.session.get("key", "")), "error": error, "node": node, "files": node.files, "wfs": current_workflow_step, "wfs_files": [], "logo_info": {}, "logo_info_list": [], "getImageSize": lambda x: (0, 0), "pdf_page_count": 0, "pdf_dimensions": pdf_dimensions, "json_pdf_dimensions": json.dumps(pdf_dimensions), "keep_params": json.dumps(keep_params), "startpageno": 0, "FATAL_ERROR": 'true', "user": users.getUserFromRequest(req), "prefix": self.get("prefix"), "buttons": self.tableRowButtons(node) } return req.getTAL("workflow/addpic2pdf.html", context, macro="workflow_addpic2pdf") try: pdf_dimensions = get_pdf_dimensions(pdf_filepath) pdf_pagecount = get_pdf_pagecount(pdf_filepath) except Exception as e: logg.exception("exception in workflow step addpic2pdf(%s)", current_workflow_step.id) pdf_dimensions = { 'd_pages': 0, 'd_pageno2size': (0, 0), 'd_pageno2rotate': 0 } pdf_pagecount = 0 FATAL_ERROR = True FATAL_ERROR_STR += " - %s" % (unicode(e)) #wfs_files = [f for f in current_workflow_step.getFiles() if os.path.isfile(f.retrieveFile())] wfs_files0, wfs_files = getFilelist(current_workflow_step, 'logoupload') url_mapping = [ line.strip() for line in current_workflow_step.get("url_mapping").splitlines() if line.strip() and line.find("|") > 0 ] url_mapping = dict( map(lambda x: (x[0].strip(), x[1].strip()), [line.split("|", 1) for line in url_mapping])) logo_info = {} logo_info_list = [] for f in [ f for f in wfs_files if f.base_name.startswith('m_upload_logoupload') ]: f_path = f.abspath try: _size = list(get_pic_size(f_path)) _dpi = get_pic_dpi(f_path) except Exception as e: logg.exception("exception in workflow step addpic2pdf(%s)", current_workflow_step.id) FATAL_ERROR = True FATAL_ERROR_STR += (" - ERROR loading logo '%s'" % f_path) + unicode(e) continue logo_filename = f.base_name logo_url = "" for key in url_mapping: if logo_filename.find(key) >= 0: logo_url = url_mapping[key] break logo_info[logo_filename.encode('utf-8')] = { 'size': _size, 'dpi': _dpi, 'url': logo_url.encode('utf-8') } if _dpi == 'no-info': _dpi = 72.0 logo_info_list.append({ 'size': _size, 'dpi': _dpi, 'url': logo_url.encode('utf-8') }) if len(logo_info) == 0: logg.error( "workflow step addpic2pdf(%s): Error: no logo images found", current_workflow_step.id) FATAL_ERROR = True FATAL_ERROR_STR += " - Error: no logo images found" keep_params = copyDictValues(req.params, {}, KEEP_PARAMS) context = { "key": req.params.get("key", req.session.get("key", "")), "error": req.params.get('addpic2pdf_error', ''), "node": node, "files": node.files, "wfs": current_workflow_step, "wfs_files": wfs_files, "logo_info": logo_info, "logo_info_list": logo_info_list, "getImageSize": get_pic_size, "pdf_page_count": pdf_pagecount, "pdf_dimensions": pdf_dimensions, "json_pdf_dimensions": json.dumps(pdf_dimensions), "keep_params": json.dumps(keep_params), "startpageno": startpageno, "FATAL_ERROR": { False: 'false', True: 'true' }[bool(FATAL_ERROR)], "user": users.getUserFromRequest(req), "prefix": self.get("prefix"), "buttons": self.tableRowButtons(node) } if FATAL_ERROR: context["error"] += " - %s" % (FATAL_ERROR_STR) return req.getTAL("workflow/addpic2pdf.html", context, macro="workflow_addpic2pdf")
def show_workflow_node(self, node, req, data=None): check_context() user = users.getUserFromRequest(req) current_workflow = getNodeWorkflow(node) current_workflow_step = getNodeWorkflowStep(node) FATAL_ERROR = False FATAL_ERROR_STR = "" if "gotrue" in req.params: if not PYPDF_MODULE_PRESENT: del req.params['gotrue'] return self.show_workflow_node(node, req) radio_apply_reset_accept = req.params.get('radio_apply_reset_accept', '') if radio_apply_reset_accept == 'reset': for f in node.files: f_name = f.base_name if f_name.startswith('addpic2pdf_%s_node_%s_' % (unicode(current_workflow_step.id), unicode(node.id))) and f.filetype.startswith('p_document'): logg.info("workflow step addpic2pdf(%s): going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)", current_workflow_step.id, f_name, node.name, node.id, user.login_name, req.ip) node.files.remove(f) db.session.commit() try: os.remove(f.abspath) except: logg.exception("exception in workflow setep addpic2pdf, removing file failed, ignoring") del req.params['gotrue'] return self.show_workflow_node(node, req) elif radio_apply_reset_accept == 'accept': p_document_files = [f for f in node.files if f.filetype == 'p_document' and f.base_name.startswith( 'addpic2pdf_%s_node_%s_' % (unicode(current_workflow_step.id), unicode(node.id)))] if len(p_document_files) > 0: p_document_file = p_document_files[0] document_file = [f for f in node.files if f.filetype == 'document'][0] o_document_file = File(document_file.path, 'o_document', document_file.mimetype) node.files.remove(document_file) node.files.append(o_document_file) o_document_name = o_document_file.base_name for f in node.files: if f.filetype in ['thumb', 'fileinfo', 'fulltext'] or f.filetype.startswith('present'): if os.path.splitext(f.base_name)[0] == os.path.splitext(o_document_name)[0]: new_f = File(f.path, 'o_' + f.filetype, f.mimetype) node.files.remove(f) node.files.append(new_f) new_document_file = File(p_document_file.path, 'document', p_document_file.mimetype) node.files.remove(p_document_file) node.files.append(new_document_file) db.session.commit() node.event_files_changed() del req.params['gotrue'] return self.forwardAndShow(node, True, req) elif radio_apply_reset_accept == 'apply': drag_logo_fullname = req.params.get("input_drag_logo_fullname", None) if not drag_logo_fullname: req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_no_logo_selected")) del req.params['gotrue'] return self.show_workflow_node(node, req) drag_logo_filepath = [f.abspath for f in current_workflow_step.files if f.base_name == drag_logo_fullname][0] pos_cm = req.params.get("input_poffset_cm", "0, 0") x_cm, y_cm = [float(x.strip()) for x in pos_cm.split(",")] pdf_in_filepath = getPdfFilepathForProcessing(current_workflow_step, node) current_pageno = int(req.params.get("input_current_page", "0").strip()) radio_select_targetpages = req.params.get("radio_select_targetpages", "").strip() input_select_targetpages = req.params.get("input_select_targetpages", "").strip() printer_range = [] page_count = get_pdf_pagecount(pdf_in_filepath) _parser_error = False try: if radio_select_targetpages == "current_page": printer_range = [current_pageno] elif radio_select_targetpages == "all": printer_range = range(0, page_count) elif radio_select_targetpages == "pair": printer_range = [x for x in range(0, page_count) if x % 2] if input_select_targetpages: printer_range = [x for x in printer_range if x in parse_printer_range( input_select_targetpages, maximum=page_count + 1)] elif radio_select_targetpages == "impair": printer_range = [x for x in range(0, page_count) if not x % 2] if input_select_targetpages: printer_range = [x for x in printer_range if x in parse_printer_range( input_select_targetpages, maximum=page_count + 1)] elif radio_select_targetpages == "range_only" and input_select_targetpages: printer_range = parse_printer_range(input_select_targetpages, maximum=page_count + 1) except ValueError as e: _parser_error = True if _parser_error: req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_printer_range_error")) del req.params['gotrue'] return self.show_workflow_node(node, req) printer_range = map(int, list(printer_range)) if not printer_range: req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_printer_range_selected_empty")) del req.params['gotrue'] return self.show_workflow_node(node, req) x = x_cm * cm # cm = 28.346456692913385 y = y_cm * cm pic_dpi = get_pic_info(drag_logo_filepath).get('dpi', None) scale = 1.0 if pic_dpi: dpi_x, dpi_y = pic_dpi if dpi_x != dpi_y: req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_logo_dpix_dpiy")) dpi = int(dpi_x) if dpi == 72: scale = 1.0 else: scale = 1.0 * 72.0 / dpi else: dpi = 300 scale = 1.0 * 72.0 / dpi #dpi = 72 #scale = 1.0 tmppath = config.get("paths.datadir") + "tmp/" date_str = format_date().replace('T', '-').replace(' ', '').replace(':', '-') filetempname = tmppath + \ "temp_addpic_pdf_wfs_%s_node_%s_%s_%s_.pdf" % ( unicode(current_workflow_step.id), unicode(node.id), date_str, unicode(random.random())) url = req.params.get('input_drag_logo_url', '') fn_out = filetempname build_logo_overlay_pdf(pdf_in_filepath, drag_logo_filepath, fn_out, x, y, scale=scale, mask='auto', pages=printer_range, follow_rotate=True, url=(" " * ADD_NBSP) + url) for f in node.files: f_name = f.base_name if f_name.startswith('addpic2pdf_%s_node_%s_' % (unicode(current_workflow_step.id), unicode(node.id), )) and f.filetype.startswith('p_document'): logg.info("workflow step addpic2pdf(%s): going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)", current_workflow_step.id, f_name, node.name, node.id, user.login_name, req.ip) node.files.remove(f) with suppress(Exception, warn=False): os.remove(f.abspath) break date_str = format_date().replace('T', '-').replace(' ', '').replace(':', '-') nodeFile = importFileToRealname("_has_been_processed_%s.pdf" % (date_str), filetempname, prefix='addpic2pdf_%s_node_%s_' % ( unicode(current_workflow_step.id), unicode(node.id), ), typeprefix="p_") node.files.append(nodeFile) db.session.commit() with suppress(Exception, warn=False): os.remove(filetempname) del req.params['gotrue'] return self.show_workflow_node(node, req) if "gofalse" in req.params: return self.forwardAndShow(node, False, req) # part of show_workflow_node not handled by "gotrue" and "gofalse" try: pdf_filepath = [f.abspath for f in node.files if f.filetype.startswith('document')][0] error_no_pdf = False except: error_no_pdf = t(lang(req), "admin_wfstep_addpic2pdf_no_pdf_document_for_this_node") if not PYPDF_MODULE_PRESENT or error_no_pdf: error = "" if not PYPDF_MODULE_PRESENT: error += t(lang(req), "admin_wfstep_addpic2pdf_no_pypdf") if error_no_pdf: error += error_no_pdf pdf_dimensions = {'d_pageno2size': {0: [595.275, 841.889]}, 'd_pageno2rotate': {0: 0}} # A4 keep_params = copyDictValues(req.params, {}, KEEP_PARAMS) context = {"key": req.params.get("key", req.session.get("key", "")), "error": error, "node": node, "files": node.files, "wfs": current_workflow_step, "wfs_files": [], "logo_info": {}, "logo_info_list": [], "getImageSize": lambda x: (0, 0), "pdf_page_count": 0, "pdf_dimensions": pdf_dimensions, "json_pdf_dimensions": json.dumps(pdf_dimensions), "keep_params": json.dumps(keep_params), "startpageno": 0, "FATAL_ERROR": 'true', "user": users.getUserFromRequest(req), "prefix": self.get("prefix"), "buttons": self.tableRowButtons(node), "csrf": req.csrf_token.current_token,} return req.getTAL("workflow/addpic2pdf.html", context, macro="workflow_addpic2pdf") try: pdf_dimensions = get_pdf_dimensions(pdf_filepath) pdf_pagecount = get_pdf_pagecount(pdf_filepath) except Exception as e: logg.exception("exception in workflow step addpic2pdf(%s)", current_workflow_step.id) pdf_dimensions = {'d_pages': 0, 'd_pageno2size': (0, 0), 'd_pageno2rotate': 0} pdf_pagecount = 0 FATAL_ERROR = True FATAL_ERROR_STR += " - %s" % (unicode(e)) #wfs_files = [f for f in current_workflow_step.getFiles() if os.path.isfile(f.retrieveFile())] wfs_files0, wfs_files = getFilelist(current_workflow_step, 'logoupload') url_mapping = [line.strip() for line in current_workflow_step.get("url_mapping").splitlines() if line.strip() and line.find("|") > 0] url_mapping = dict(map(lambda x: (x[0].strip(), x[1].strip()), [line.split("|", 1) for line in url_mapping])) logo_info = {} logo_info_list = [] for f in [f for f in wfs_files if f.base_name.startswith('m_upload_logoupload')]: f_path = f.abspath try: _size = list(get_pic_size(f_path)) _dpi = get_pic_dpi(f_path) except Exception as e: logg.exception("exception in workflow step addpic2pdf(%s)", current_workflow_step.id) FATAL_ERROR = True FATAL_ERROR_STR += (" - ERROR loading logo '%s'" % f_path) + unicode(e) continue logo_filename = f.base_name logo_url = "" for key in url_mapping: if logo_filename.find(key) >= 0: logo_url = url_mapping[key] break logo_info[logo_filename.encode('utf-8')] = {'size': _size, 'dpi': _dpi, 'url': logo_url.encode('utf-8')} if _dpi == 'no-info': _dpi = 72.0 logo_info_list.append({'size': _size, 'dpi': _dpi, 'url': logo_url.encode('utf-8')}) if len(logo_info) == 0: logg.error("workflow step addpic2pdf(%s): Error: no logo images found", current_workflow_step.id) FATAL_ERROR = True FATAL_ERROR_STR += " - Error: no logo images found" keep_params = copyDictValues(req.params, {}, KEEP_PARAMS) context = {"key": req.params.get("key", req.session.get("key", "")), "error": req.params.get('addpic2pdf_error', ''), "node": node, "files": node.files, "wfs": current_workflow_step, "wfs_files": wfs_files, "logo_info": logo_info, "logo_info_list": logo_info_list, "getImageSize": get_pic_size, "pdf_page_count": pdf_pagecount, "pdf_dimensions": pdf_dimensions, "json_pdf_dimensions": json.dumps(pdf_dimensions), "keep_params": json.dumps(keep_params), "startpageno": startpageno, "FATAL_ERROR": {False: 'false', True: 'true'}[bool(FATAL_ERROR)], "user": users.getUserFromRequest(req), "prefix": self.get("prefix"), "buttons": self.tableRowButtons(node), "csrf": req.csrf_token.current_token,} if FATAL_ERROR: context["error"] += " - %s" % (FATAL_ERROR_STR) return req.getTAL("workflow/addpic2pdf.html", context, macro="workflow_addpic2pdf")
if req.params.get('action') == "upload": if 'file' in req.params: # plupload realname = mybasename(req.params['file'].filename) tempname = req.params['file'].tempname msg = [] for k in dir(req.params['file']): if k in ['__doc__', '__init__', '__module__', '__str__', 'adddata', 'close', ]: continue msg.append("%r: %r" % (k, getattr(req.params['file'], k))) msg = "... req.params['file'] = %r" % (', '.join(msg)) logger.debug(msg) proceed_to_uploadcomplete = True realname = realname.replace(' ', '_') # check this: import to realnamne or random name ? f = importFileToRealname(realname, tempname) #f = importFile(realname,tempname) n = tree.getNode(req.params.get('id')) n.addFile(f) req.write("") logger.debug("%r|%s.%s: added file to node %r (%r, %r)" % (get_user_id(req), __name__, funcname(), n.id, n.name, n.type)) if not proceed_to_uploadcomplete: return None # upload done -> deliver view of object if proceed_to_uploadcomplete or req.params.get('action') == "uploadcomplete": logger.debug("upload done -> deliver view of object") if proceed_to_uploadcomplete: req.params['file'] = realname
def validate(req, op): path = req.path[1:].split("/") if len(path) == 3 and path[2] == "overview": return showFieldOverview(req) if len(path) == 4 and path[3] == "editor": res = showEditor(req) return res if len(path) == 5 and path[3] == "editor" and path[4] == "show_testnodes": raise NotImplementedError("") template = req.params.get('template', '') testnodes_list = req.params.get('testnodes', '') width = req.params.get('width', '400') item_id = req.params.get('item_id', None) mdt_name = path[1] mask_name = path[2] mdt = q(Metadatatypes).one().children.filter_by(name=mdt_name).one() mask = mdt.children.filter_by(name=mask_name).one() sectionlist = [] for nid in [x.strip() for x in testnodes_list.split(',') if x.strip()]: section_descr = {} section_descr['nid'] = nid section_descr['error_flag'] = '' # in case of no error node = q(Node).get(nid) section_descr['node'] = node if node and node.has_data_access(): try: node_html = mask.getViewHTML( [node], VIEW_DEFAULT, template_from_caller=[template, mdt, mask, item_id]) section_descr['node_html'] = node_html except: logg.exception("exception while evaluating template") error_text = str(sys.exc_info()[1]) template_line = 'for node id ' + ustr( nid) + ': ' + error_text with suppress(Exception, warn=False): m = re.match( r".*line (?P<line>\d*), column (?P<column>\d*)", error_text) if m: mdict = m.groupdict() line = int(mdict.get('line', 0)) column = int(mdict.get('column', 0)) error_text = error_text.replace( 'line %d' % line, 'template line %d' % (line - 1)) template_line = 'for node id ' + ustr( nid ) + '<br/>' + error_text + '<br/><code>' + esc( template.split("\n")[line - 2][0:column - 1] ) + '<span style="color:red">' + esc( template.split("\n")[line - 2][column - 1:]) + '</span></code>' section_descr[ 'error_flag'] = 'Error while evaluating template:' section_descr['node_html'] = template_line elif node and not node.has_data_access(): section_descr['error_flag'] = 'no access' section_descr['node_html'] = '' if node is None: section_descr['node'] = None section_descr['error_flag'] = 'NoSuchNodeError' section_descr['node_html'] = 'for node id ' + ustr(nid) sectionlist.append(section_descr) # remark: error messages will be served untranslated in English # because messages from the python interpreter (in English) will be added return req.getTAL("web/admin/modules/metatype.html", { 'sectionlist': sectionlist, 'csrf': req.csrf_token.current_token }, macro="view_testnodes") if len(path) == 2 and path[1] == "info": return showInfo(req) if "file" in req.params and hasattr( req.params["file"], "filesize") and req.params["file"].filesize > 0: # import scheme from xml-file importfile = req.params.get("file") if importfile.tempname != "": xmlimport(req, importfile.tempname) if req.params.get("acttype", "schema") == "schema": # section for schema for key in req.params.keys(): # create new metadatatype if key.startswith("new"): return MetatypeDetail(req, "") # edit metadatatype elif key.startswith("edit_"): return MetatypeDetail(req, key[5:-2]) # delete metadata elif key.startswith("delete_"): deleteMetaType(key[7:-2]) break # show details for given metadatatype elif key.startswith("detaillist_"): return showDetailList(req, key[11:-2]) # show masklist for given metadatatype elif key.startswith("masks_"): return showMaskList(req, key[6:-2]) # save schema if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return view(req) if req.params.get("mname", "") == "" or req.params.get( "mlongname", "") == "" or req.params.get("mdatatypes", "") == "": return MetatypeDetail(req, req.params.get("mname_orig", ""), 1) # no name was given elif not checkString(req.params.get("mname", "")): return MetatypeDetail( req, req.params.get("mname_orig", ""), 4) # if the name contains wrong characters elif req.params.get("mname_orig", "") != req.params.get( "mname", "") and existMetaType(req.params.get("mname")): return MetatypeDetail(req, req.params.get("mname_orig", ""), 2) # metadata still existing _active = 0 if req.params.get("mactive", "") != "": _active = 1 updateMetaType(req.params.get("mname", ""), description=req.params.get("description", ""), longname=req.params.get("mlongname", ""), active=_active, datatypes=req.params.get("mdatatypes", "").replace(";", ", "), bibtexmapping=req.params.get("mbibtex", ""), citeprocmapping=req.params.get("mciteproc", ""), orig_name=req.params.get("mname_orig", "")) mtype = q(Metadatatype).filter_by( name=req.params.get("mname")).scalar() if mtype: new_ruleset_names = set(req.form.getlist("leftread")) add_remove_rulesets_from_metadatatype(mtype, new_ruleset_names) db.session.commit() elif req.params.get("acttype") == "field": # section for fields for key in req.params.keys(): # create new meta field if key.startswith("newdetail_"): return FieldDetail(req, req.params.get("parent"), "") # edit meta field elif key.startswith("editdetail_"): return FieldDetail(req, req.params.get("parent"), key[11:-2]) # delete metafield: key[13:-2] = pid | n elif key.startswith("deletedetail_"): deleteMetaField(req.params.get("parent"), key[13:-2]) return showDetailList(req, req.params.get("parent")) # change field order up if key.startswith("updetail_"): moveMetaField(req.params.get("parent"), key[9:-2], -1) return showDetailList(req, req.params.get("parent")) # change field order down elif key.startswith("downdetail_"): moveMetaField(req.params.get("parent"), key[11:-2], 1) return showDetailList(req, req.params.get("parent")) if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return showDetailList(req, req.params.get("parent")) if existMetaField(req.params.get("parent"), req.params.get("mname")) and \ (req.params.get("form_op", "") == "save_newdetail" or req.params.get("mname") != req.params.get("mname_orig")): return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 3) # field still existing elif req.params.get("mname", "") == "" or req.params.get( "mlabel", "") == "": return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 1) elif not checkString(req.params.get("mname", "")): # if the name contains wrong characters return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 4) _option = "" for o in req.params.keys(): if o.startswith("option_"): _option += o[7] _fieldvalue = "" if req.params.get("mtype", "") + "_value" in req.params.keys(): _fieldvalue = req.params.get( req.params.get("mtype") + "_value") _filenode = None if "valuesfile" in req.params.keys(): valuesfile = req.params.pop("valuesfile") _filenode = importFileToRealname(valuesfile.filename, valuesfile.tempname) _attr_dict = {} if req.params.get("mtype", "") + "_handle_attrs" in req.params.keys(): attr_names = [ s.strip() for s in req.params.get( req.params.get("mtype", "") + "_handle_attrs").split(",") ] key_prefix = req.params.get("mtype", "") + "_attr_" for attr_name in attr_names: attr_value = req.params.get(key_prefix + attr_name, "") _attr_dict[attr_name] = attr_value updateMetaField(req.params.get("parent", ""), req.params.get("mname", ""), req.params.get("mlabel", ""), req.params.get("orderpos", ""), req.params.get("mtype", ""), _option, req.params.get("mdescription", ""), _fieldvalue, fieldid=req.params.get("fieldid", ""), filenode=_filenode, attr_dict=_attr_dict) return showDetailList(req, req.params.get("parent")) elif req.params.get("acttype") == "mask": # section for masks for key in req.params.keys(): # new mask if key.startswith("newmask_"): return MaskDetails(req, req.params.get("parent"), "") # edit metatype masks elif key.startswith("editmask_"): return MaskDetails(req, req.params.get("parent"), key[9:-2], err=0) # delete mask elif key.startswith("deletemask_"): mtype = getMetaType(req.params.get("parent")) mtype.children.remove(q(Node).get(key[11:-2])) db.session.commit() return showMaskList(req, req.params.get("parent")) # create autmatic mask with all fields elif key.startswith("automask_"): generateMask(getMetaType(req.params.get("parent"))) return showMaskList(req, req.params.get("parent")) # cope selected mask if key.startswith("copymask_"): k = key[9:-2] if k.isdigit(): mask = q(Mask).get(k) else: mtype = getMetaType(req.params.get("parent")) mask = mtype.getMask(k) cloneMask(mask, u"copy_" + mask.name) return showMaskList(req, req.params.get("parent")) if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return showMaskList(req, req.params.get("parent")) if req.params.get("mname", "") == "": return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=1) elif not checkString(req.params.get("mname", "")): # if the name contains wrong characters return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=4) mtype = q(Metadatatype).filter_by( name=q(Node).get(req.params.get("parent", "")).name).one() if req.params.get("form_op") == "save_editmask": mask = mtype.get_mask(req.params.get("mname", "")) # in case of renaming a mask the mask cannot be detected via the new mname # then detect mask via maskid if not mask: mtype = getMetaType(req.params.get("parent")) mask = mtype.children.filter_by( id=req.params.get("maskid", "")).scalar() elif req.params.get("form_op") == "save_newmask": mask = Mask(req.params.get("mname", "")) mtype.children.append(mask) db.session.commit() mask.name = req.params.get("mname") mask.setDescription(req.params.get("mdescription")) mask.setMasktype(req.params.get("mtype")) mask.setSeparator(req.params.get("mseparator")) db.session.commit() if req.params.get("mtype") == "export": mask.setExportMapping(req.params.get("exportmapping") or "") mask.setExportHeader(req.params.get("exportheader")) mask.setExportFooter(req.params.get("exportfooter")) _opt = "" if "types" in req.params.keys(): _opt += "t" if "notlast" in req.params.keys(): _opt += "l" mask.setExportOptions(_opt) db.session.commit() mask.setLanguage(req.params.get("mlanguage", "")) mask.setDefaultMask("mdefault" in req.params.keys()) for r in mask.access_ruleset_assocs.filter_by(ruletype=u'read'): db.session.delete(r) for key in req.params.keys(): if key.startswith("left"): for r in req.params.get(key).split(';'): mask.access_ruleset_assocs.append( NodeToAccessRuleset(ruleset_name=r, ruletype=key[4:])) break db.session.commit() return showMaskList(req, ustr(req.params.get("parent", ""))) return view(req)
del req.params[file_key] filename = file.filename filesize = file.filesize filetempname = file.tempname else: msg = t(lang(req), "no file for this field submitted") errors.append(msg) if filename: diskname = normalizeFilename(filename) nodeFile = importFileToRealname(diskname, filetempname, prefix='m_upload_%s_' % (submitter, ), typeprefix="u_") if not nodeFile: msg = "metadata m_upload: could not create file node for request from '%s'" % ( ustr(req.ip)) errors.append(msg) logg.error(msg) if targetnode and filename: filecount = len(getFilelist(targetnode, submitter)[0]) targetnode.files.append(nodeFile) targetnode.set(submitter, filecount + 1) db.session.commit()
def handle_request(req): user = users.getUserFromRequest(req) access = AccessData(req) errors = [] if "cmd" in req.params: cmd = req.params["cmd"] if cmd == "list_files": targetnodeid = req.params.get("targetnodeid", "") m_upload_field_name = req.params.get("m_upload_field_name", "") n = tree.getNode(targetnodeid) s = {'response': 'response for cmd="%s"' % cmd} filelist, filelist2 = getFilelist(n, m_upload_field_name) filelist = [_t[0:-1] for _t in filelist] s['filelist'] = filelist html_filelist = mkfilelist(n, filelist2, deletebutton=1, language=None, request=req) html_filelist = html_filelist.replace("____FIELDNAME____", "%s" % m_upload_field_name) s['html_filelist'] = html_filelist req.write(req.params.get("jsoncallback") + "(%s)" % json.dumps(s, indent=4)) return 200 elif cmd == 'delete_file': s = {'response': 'response for cmd="%s"' % cmd} f_name = req.params.get('prefixed_filename', '') f_name = f_name[len('delete_'):] targetnodeid = req.params.get("targetnodeid", "") m_upload_field_name = req.params.get("m_upload_field_name", "") n = tree.getNode(targetnodeid) fs = n.getFiles() if not access.hasAccess(n, 'data'): msg = "m_upload: no access for user '%s' to node %s ('%s', '%s') from '%s'" % ( user.name, str(n.id), n.name, n.type, str(req.ip)) logging.getLogger("backend").info(msg) errors.append(msg) s['errors'] = errors req.write(req.params.get("jsoncallback") + "(%s)" % json.dumps(s, indent=4)) return 403 for f in fs: if f.getName() == f_name: msg = "metadata m_upload: going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)" % ( f_name, n.name, str(n.id), user.name, str(req.ip)) logging.getLogger("backend").info(msg) n.removeFile(f) try: os.remove(f.retrieveFile()) except: pass break filecount = len(getFilelist(n, m_upload_field_name)[0]) n.set(m_upload_field_name, filecount) s['errors'] = errors req.write(req.params.get("jsoncallback") + "(%s)" % json.dumps(s, indent=4)) return 200 else: s = {'response': 'response for cmd="%s" not completely implemented feature' % cmd} req.write(req.params.get("jsoncallback") + "(%s)" % json.dumps(s, indent=4)) return 200 filename = None filesize = 0 s = {} if "submitter" in req.params.keys(): submitter = req.params.get("submitter", "").split(';')[0] targetnodeid = req.params.get("targetnodeid_FOR_" + submitter, None) targetnode = None if targetnodeid: try: targetnode = tree.getNode(targetnodeid) except: msg = "metadata m_upload: targetnodeid='%s' for non-existant node for upload from '%s'" % (str(targetnodeid), str(req.ip)) errors.append(msg) logging.getLogger("backend").error(msg) else: msg = "metadata m_upload could not find 'targetnodeid' for upload from '%s'" % str(req.ip) errors.append(msg) logging.getLogger("backend").error(msg) if not access.hasAccess(targetnode, 'data'): msg = "m_upload: no access for user '%s' to node %s ('%s', '%s') from '%s'" % ( user.name, str(targetnode.id), targetnode.name, targetnode.type, str(req.ip)) logging.getLogger("backend").info(msg) errors.append(msg) s['errors'] = errors req.write("%s" % json.dumps(s, indent=4)) return filename = None file_key = "m_upload_file_FOR_" + submitter if file_key in req.params: file = req.params[file_key] del req.params[file_key] filename = file.filename filesize = file.filesize filetempname = file.tempname else: msg = t(lang(req), "no file for this field submitted") errors.append(msg) if filename: diskname = normalizeFilename(filename) nodeFile = importFileToRealname(diskname, filetempname, prefix='m_upload_%s_' % (submitter, ), typeprefix="u_") if nodeFile: imported_filename = nodeFile.getName() imported_filesize = nodeFile.getSize() imported_filepath = nodeFile.retrieveFile() imported_filemimetype = nodeFile.getMimeType() else: msg = "metadata m_upload: could not create file node for request from '%s'" % (str(req.ip)) errors.append(msg) logging.getLogger("backend").error(msg) if targetnode and filename: targetnode.addFile(nodeFile) filecount = len(getFilelist(targetnode, submitter)[0]) targetnode.set(submitter, filecount) copy_report = t(lang(req), "uploaded file: %s; size: %d bytes") % (filename, filesize) else: copy_report = "" else: msg = "metadata m_upload: could not find submitter for request from '%s'" % (str(req.ip)) errors.append(msg) logging.getLogger("backend").error(msg) s = { 'errors': errors, 'copy_report': copy_report, } req.write("%s" % json.dumps(s, indent=4)) return 200
def _finish_change(node, change_file, user, uploadfile, req): if change_file in ["yes", "no"]: # check that the correct filetype is uploaded # note: only the suffix of the filename is checked not the file content uploadfile_type = getMimeType(uploadfile.filename)[1] if uploadfile_type != node.type and uploadfile_type != node.get_upload_filetype( ): req.setStatus(httpstatus.HTTP_NOT_ACCEPTABLE) return # sys files are always cleared to delete remaining thumbnails, presentation images etc. for f in node.files: if f.filetype in node.get_sys_filetypes(): node.files.remove(f) file = importFile(uploadfile.filename, uploadfile.tempname) # add new file file.filetype = node.get_upload_filetype() node.files.append(file) # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname) return attpath = "" for f in node.files: if f.mimetype == "inode/directory": attpath = f.base_name break if change_file == "attfile": # add file as attachment if attpath == "": # no attachment directory existing file = importFileToRealname(uploadfile.filename, uploadfile.tempname) # add new file file.mimetype = "inode/file" file.filetype = "attachment" node.files.append(file) else: # import attachment file into existing attachment directory importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname) if change_file == "addthumb": # create new thumbanil from uploaded file thumbname = os.path.join( getImportDir(), hashlib.md5(ustr(random.random())).hexdigest()[0:8]) + ".thumb" file = importFile(thumbname, uploadfile.tempname) # add new file make_thumbnail_image(file.abspath, thumbname) make_presentation_image(file.abspath, thumbname + "2") if os.path.exists(file.abspath): # remove uploaded original os.remove(file.abspath) for f in node.files: if f.type in ["thumb", "presentation"]: if os.path.exists(f.abspath): os.remove(f.abspath) node.files.remove(f) node.files.append(File(thumbname, "thumb", "image/jpeg")) node.files.append(File(thumbname + "2", "presentation", "image/jpeg")) logg.info("%s changed thumbnail of node %s", user.login_name, node.id) # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname)
def validate(req, op): path = req.path[1:].split("/") if len(path) == 3 and path[2] == "overview": return showFieldOverview(req) if len(path) == 4 and path[3] == "editor": res = showEditor(req) # mask may have been edited: flush masks cache flush_maskcache(req=req) return res if len(path) == 5 and path[3] == "editor" and path[4] == "show_testnodes": template = req.params.get('template', '') testnodes_list = req.params.get('testnodes', '') width = req.params.get('width', '400') item_id = req.params.get('item_id', None) mdt_name = path[1] mask_name = path[2] mdt = tree.getRoot('metadatatypes').getChild(mdt_name) mask = mdt.getChild(mask_name) access = AccessData(req) sectionlist = [] for nid in [x.strip() for x in testnodes_list.split(',') if x.strip()]: section_descr = {} section_descr['nid'] = nid section_descr['error_flag'] = '' # in case of no error try: node = tree.getNode(nid) section_descr['node'] = node if access.hasAccess(node, "data"): try: node_html = mask.getViewHTML([node], VIEW_DEFAULT, template_from_caller=[template, mdt, mask, item_id], mask=mask) section_descr['node_html'] = node_html except: error_text = str(sys.exc_info()[1]) template_line = 'for node id ' + str(nid) + ': ' + error_text try: m = re.match(r".*line (?P<line>\d*), column (?P<column>\d*)", error_text) if m: mdict = m.groupdict() line = int(mdict.get('line', 0)) column = int(mdict.get('column', 0)) error_text = error_text.replace('line %d' % line, 'template line %d' % (line - 1)) template_line = 'for node id ' + str(nid) + '<br/>' + error_text + '<br/><code>' + esc(template.split( "\n")[line - 2][0:column - 1]) + '<span style="color:red">' + esc(template.split("\n")[line - 2][column - 1:]) + '</span></code>' except: pass section_descr['error_flag'] = 'Error while evaluating template:' section_descr['node_html'] = template_line else: section_descr['error_flag'] = 'no access' section_descr['node_html'] = '' except tree.NoSuchNodeError: section_descr['node'] = None section_descr['error_flag'] = 'NoSuchNodeError' section_descr['node_html'] = 'for node id ' + str(nid) sectionlist.append(section_descr) # remark: error messages will be served untranslated in English # because messages from the python interpreter (in English) will be added return req.getTAL("web/admin/modules/metatype.html", {'sectionlist': sectionlist}, macro="view_testnodes") if len(path) == 2 and path[1] == "info": return showInfo(req) if "file" in req.params and hasattr(req.params["file"], "filesize") and req.params["file"].filesize > 0: # import scheme from xml-file importfile = req.params.get("file") if importfile.tempname != "": xmlimport(req, importfile.tempname) if req.params.get("acttype", "schema") == "schema": # section for schema for key in req.params.keys(): # create new metadatatype if key.startswith("new"): return MetatypeDetail(req, "") # edit metadatatype elif key.startswith("edit_"): return MetatypeDetail(req, str(key[5:-2])) # delete metadata elif key.startswith("delete_"): deleteMetaType(key[7:-2]) break # show details for given metadatatype elif key.startswith("detaillist_"): return showDetailList(req, str(key[11:-2])) # show masklist for given metadatatype elif key.startswith("masks_"): return showMaskList(req, str(key[6:-2])) # reindex search index for current schema elif key.startswith("indexupdate_") and "cancel" not in req.params.keys(): schema = tree.getNode(key[12:]) searcher.reindex(schema.getAllItems()) break # save schema if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return view(req) if req.params.get("mname", "") == "" or req.params.get("mlongname", "") == "" or req.params.get("mdatatypes", "") == "": return MetatypeDetail(req, req.params.get("mname_orig", ""), 1) # no name was given elif not checkString(req.params.get("mname", "")): return MetatypeDetail(req, req.params.get("mname_orig", ""), 4) # if the name contains wrong characters elif req.params.get("mname_orig", "") != req.params.get("mname", "") and existMetaType(req.params.get("mname")): return MetatypeDetail(req, req.params.get("mname_orig", ""), 2) # metadata still existing _active = 0 if req.params.get("mactive", "") != "": _active = 1 updateMetaType(req.params.get("mname", ""), description=req.params.get("description", ""), longname=req.params.get("mlongname", ""), active=_active, datatypes=req.params.get("mdatatypes", "").replace(";", ", "), bibtexmapping=req.params.get("mbibtex", ""), citeprocmapping=req.params.get("mciteproc", ""), orig_name=req.params.get("mname_orig", "")) mtype = getMetaType(req.params.get("mname")) if mtype: mtype.setAccess("read", "") for key in req.params.keys(): if key.startswith("left"): mtype.setAccess(key[4:], req.params.get(key).replace(";", ",")) break elif req.params.get("acttype") == "field": # section for fields for key in req.params.keys(): # create new meta field if key.startswith("newdetail_"): return FieldDetail(req, req.params.get("parent"), "") # edit meta field elif key.startswith("editdetail_"): return FieldDetail(req, req.params.get("parent"), key[11:-2]) # delete metafield: key[13:-2] = pid | n elif key.startswith("deletedetail_"): deleteMetaField(req.params.get("parent"), key[13:-2]) return showDetailList(req, req.params.get("parent")) # change field order up if key.startswith("updetail_"): moveMetaField(req.params.get("parent"), key[9:-2], -1) return showDetailList(req, req.params.get("parent")) # change field order down elif key.startswith("downdetail_"): moveMetaField(req.params.get("parent"), key[11:-2], 1) return showDetailList(req, req.params.get("parent")) if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return showDetailList(req, req.params.get("parent")) if existMetaField(req.params.get("parent"), req.params.get("mname")) and req.params.get("form_op", "") == "save_newdetail": return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 3) # field still existing elif req.params.get("mname", "") == "" or req.params.get("mlabel", "") == "": return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 1) elif not checkString(req.params.get("mname", "")): # if the name contains wrong characters return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 4) _option = "" for o in req.params.keys(): if o.startswith("option_"): _option += o[7] _fieldvalue = "" if req.params.get("mtype", "") + "_value" in req.params.keys(): _fieldvalue = str(req.params.get(req.params.get("mtype") + "_value")) _filenode = None if "valuesfile" in req.params.keys(): valuesfile = req.params.pop("valuesfile") _filenode = importFileToRealname(valuesfile.filename, valuesfile.tempname) _attr_dict = {} if req.params.get("mtype", "") + "_handle_attrs" in req.params.keys(): attr_names = [s.strip() for s in req.params.get(req.params.get("mtype", "") + "_handle_attrs").split(",")] key_prefix = req.params.get("mtype", "") + "_attr_" for attr_name in attr_names: attr_value = req.params.get(key_prefix + attr_name, "") _attr_dict[attr_name] = attr_value updateMetaField(req.params.get("parent", ""), req.params.get("mname", ""), req.params.get("mlabel", ""), req.params.get("orderpos", ""), req.params.get("mtype", ""), _option, req.params.get("mdescription", ""), _fieldvalue, fieldid=req.params.get("fieldid", ""), filenode=_filenode, attr_dict=_attr_dict) return showDetailList(req, req.params.get("parent")) elif req.params.get("acttype") == "mask": # mask may have been edited: flush masks cache flush_maskcache(req=req) # section for masks for key in req.params.keys(): # new mask if key.startswith("newmask_"): return MaskDetails(req, req.params.get("parent"), "") # edit metatype masks elif key.startswith("editmask_"): return MaskDetails(req, req.params.get("parent"), key[9:-2], err=0) # delete mask elif key.startswith("deletemask_"): mtype = getMetaType(req.params.get("parent")) mtype.removeChild(tree.getNode(key[11:-2])) return showMaskList(req, req.params.get("parent")) # create autmatic mask with all fields elif key.startswith("automask_"): generateMask(getMetaType(req.params.get("parent"))) return showMaskList(req, req.params.get("parent")) # cope selected mask if key.startswith("copymask_"): mtype = getMetaType(req.params.get("parent")) mask = mtype.getMask(key[9:-2]) cloneMask(mask, "copy_" + mask.getName()) return showMaskList(req, req.params.get("parent")) if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return showMaskList(req, req.params.get("parent")) if req.params.get("mname", "") == "": return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=1) elif not checkString(req.params.get("mname", "")): # if the name contains wrong characters return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=4) mtype = getMetaType(req.params.get("parent", "")) if req.params.get("form_op") == "save_editmask": mask = mtype.getMask(req.params.get("maskid", "")) elif req.params.get("form_op") == "save_newmask": mask = tree.Node(req.params.get("mname", ""), type="mask") mtype.addChild(mask) mask.setName(req.params.get("mname")) mask.setDescription(req.params.get("mdescription")) mask.setMasktype(req.params.get("mtype")) mask.setSeparator(req.params.get("mseparator")) if req.params.get("mtype") == "export": mask.setExportMapping(req.params.get("exportmapping") or "") mask.setExportHeader(req.params.get("exportheader")) mask.setExportFooter(req.params.get("exportfooter")) _opt = "" if "types" in req.params.keys(): _opt += "t" if "notlast" in req.params.keys(): _opt += "l" mask.setExportOptions(_opt) mask.setLanguage(req.params.get("mlanguage", "")) mask.setDefaultMask("mdefault" in req.params.keys()) mask.setAccess("read", "") for key in req.params.keys(): if key.startswith("left"): mask.setAccess(key[4:], req.params.get(key).replace(";", ",")) break return showMaskList(req, str(req.params.get("parent", ""))) return view(req)
def validate(req, op): path = req.path[1:].split("/") if len(path) == 3 and path[2] == "overview": return showFieldOverview(req) if len(path) == 4 and path[3] == "editor": res = showEditor(req) return res if len(path) == 5 and path[3] == "editor" and path[4] == "show_testnodes": raise NotImplementedError("") template = req.params.get('template', '') testnodes_list = req.params.get('testnodes', '') width = req.params.get('width', '400') item_id = req.params.get('item_id', None) mdt_name = path[1] mask_name = path[2] mdt = q(Metadatatypes).one().children.filter_by(name=mdt_name).one() mask = mdt.children.filter_by(name=mask_name).one() sectionlist = [] for nid in [x.strip() for x in testnodes_list.split(',') if x.strip()]: section_descr = {} section_descr['nid'] = nid section_descr['error_flag'] = '' # in case of no error node = q(Node).get(nid) section_descr['node'] = node if node and node.has_data_access(): try: node_html = mask.getViewHTML([node], VIEW_DEFAULT, template_from_caller=[template, mdt, mask, item_id]) section_descr['node_html'] = node_html except: logg.exception("exception while evaluating template") error_text = str(sys.exc_info()[1]) template_line = 'for node id ' + ustr(nid) + ': ' + error_text with suppress(Exception, warn=False): m = re.match(r".*line (?P<line>\d*), column (?P<column>\d*)", error_text) if m: mdict = m.groupdict() line = int(mdict.get('line', 0)) column = int(mdict.get('column', 0)) error_text = error_text.replace('line %d' % line, 'template line %d' % (line - 1)) template_line = 'for node id ' + ustr(nid) + '<br/>' + error_text + '<br/><code>' + esc( template.split( "\n")[line - 2][0:column - 1]) + '<span style="color:red">' + esc( template.split("\n")[line - 2][column - 1:]) + '</span></code>' section_descr['error_flag'] = 'Error while evaluating template:' section_descr['node_html'] = template_line elif node and not node.has_data_access(): section_descr['error_flag'] = 'no access' section_descr['node_html'] = '' if node is None: section_descr['node'] = None section_descr['error_flag'] = 'NoSuchNodeError' section_descr['node_html'] = 'for node id ' + ustr(nid) sectionlist.append(section_descr) # remark: error messages will be served untranslated in English # because messages from the python interpreter (in English) will be added return req.getTAL("web/admin/modules/metatype.html", {'sectionlist': sectionlist, 'csrf': req.csrf_token.current_token}, macro="view_testnodes") if len(path) == 2 and path[1] == "info": return showInfo(req) if "file" in req.params and hasattr(req.params["file"], "filesize") and req.params["file"].filesize > 0: # import scheme from xml-file importfile = req.params.get("file") if importfile.tempname != "": xmlimport(req, importfile.tempname) if req.params.get("acttype", "schema") == "schema": # section for schema for key in req.params.keys(): # create new metadatatype if key.startswith("new"): return MetatypeDetail(req, "") # edit metadatatype elif key.startswith("edit_"): return MetatypeDetail(req, key[5:-2]) # delete metadata elif key.startswith("delete_"): deleteMetaType(key[7:-2]) break # show details for given metadatatype elif key.startswith("detaillist_"): return showDetailList(req, key[11:-2]) # show masklist for given metadatatype elif key.startswith("masks_"): return showMaskList(req, key[6:-2]) # save schema if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return view(req) if req.params.get("mname", "") == "" or req.params.get("mlongname", "") == "" or req.params.get("mdatatypes", "") == "": return MetatypeDetail(req, req.params.get("mname_orig", ""), 1) # no name was given elif not checkString(req.params.get("mname", "")): return MetatypeDetail(req, req.params.get("mname_orig", ""), 4) # if the name contains wrong characters elif req.params.get("mname_orig", "") != req.params.get("mname", "") and existMetaType(req.params.get("mname")): return MetatypeDetail(req, req.params.get("mname_orig", ""), 2) # metadata still existing _active = 0 if req.params.get("mactive", "") != "": _active = 1 updateMetaType(req.params.get("mname", ""), description=req.params.get("description", ""), longname=req.params.get("mlongname", ""), active=_active, datatypes=req.params.get("mdatatypes", "").replace(";", ", "), bibtexmapping=req.params.get("mbibtex", ""), citeprocmapping=req.params.get("mciteproc", ""), orig_name=req.params.get("mname_orig", "")) mtype = q(Metadatatype).filter_by(name=req.params.get("mname")).scalar() if mtype: new_ruleset_names = set(req.form.getlist("leftread")) add_remove_rulesets_from_metadatatype(mtype, new_ruleset_names) db.session.commit() elif req.params.get("acttype") == "field": # section for fields for key in req.params.keys(): # create new meta field if key.startswith("newdetail_"): return FieldDetail(req, req.params.get("parent"), "") # edit meta field elif key.startswith("editdetail_"): return FieldDetail(req, req.params.get("parent"), key[11:-2]) # delete metafield: key[13:-2] = pid | n elif key.startswith("deletedetail_"): deleteMetaField(req.params.get("parent"), key[13:-2]) return showDetailList(req, req.params.get("parent")) # change field order up if key.startswith("updetail_"): moveMetaField(req.params.get("parent"), key[9:-2], -1) return showDetailList(req, req.params.get("parent")) # change field order down elif key.startswith("downdetail_"): moveMetaField(req.params.get("parent"), key[11:-2], 1) return showDetailList(req, req.params.get("parent")) if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return showDetailList(req, req.params.get("parent")) if existMetaField(req.params.get("parent"), req.params.get("mname")) and \ (req.params.get("form_op", "") == "save_newdetail" or req.params.get("mname") != req.params.get("mname_orig")): return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 3) # field still existing elif req.params.get("mname", "") == "" or req.params.get("mlabel", "") == "": return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 1) elif not checkString(req.params.get("mname", "")): # if the name contains wrong characters return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 4) _option = "" for o in req.params.keys(): if o.startswith("option_"): _option += o[7] _fieldvalue = "" if req.params.get("mtype", "") + "_value" in req.params.keys(): _fieldvalue = req.params.get(req.params.get("mtype") + "_value") _filenode = None if "valuesfile" in req.params.keys(): valuesfile = req.params.pop("valuesfile") _filenode = importFileToRealname(valuesfile.filename, valuesfile.tempname) _attr_dict = {} if req.params.get("mtype", "") + "_handle_attrs" in req.params.keys(): attr_names = [s.strip() for s in req.params.get(req.params.get("mtype", "") + "_handle_attrs").split(",")] key_prefix = req.params.get("mtype", "") + "_attr_" for attr_name in attr_names: attr_value = req.params.get(key_prefix + attr_name, "") _attr_dict[attr_name] = attr_value updateMetaField(req.params.get("parent", ""), req.params.get("mname", ""), req.params.get("mlabel", ""), req.params.get("orderpos", ""), req.params.get("mtype", ""), _option, req.params.get("mdescription", ""), _fieldvalue, fieldid=req.params.get("fieldid", ""), filenode=_filenode, attr_dict=_attr_dict) return showDetailList(req, req.params.get("parent")) elif req.params.get("acttype") == "mask": # section for masks for key in req.params.keys(): # new mask if key.startswith("newmask_"): return MaskDetails(req, req.params.get("parent"), "") # edit metatype masks elif key.startswith("editmask_"): return MaskDetails(req, req.params.get("parent"), key[9:-2], err=0) # delete mask elif key.startswith("deletemask_"): mtype = getMetaType(req.params.get("parent")) mtype.children.remove(q(Node).get(key[11:-2])) db.session.commit() return showMaskList(req, req.params.get("parent")) # create autmatic mask with all fields elif key.startswith("automask_"): generateMask(getMetaType(req.params.get("parent"))) return showMaskList(req, req.params.get("parent")) # cope selected mask if key.startswith("copymask_"): k = key[9:-2] if k.isdigit(): mask = q(Mask).get(k) else: mtype = getMetaType(req.params.get("parent")) mask = mtype.getMask(k) cloneMask(mask, u"copy_" + mask.name) return showMaskList(req, req.params.get("parent")) if "form_op" in req.params.keys(): if req.params.get("form_op", "") == "cancel": return showMaskList(req, req.params.get("parent")) if req.params.get("mname", "") == "": return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=1) elif not checkString(req.params.get("mname", "")): # if the name contains wrong characters return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=4) mtype = q(Metadatatype).filter_by(name=q(Node).get(req.params.get("parent", "")).name).one() if req.params.get("form_op") == "save_editmask": mask = mtype.get_mask(req.params.get("mname", "")) # in case of renaming a mask the mask cannot be detected via the new mname # then detect mask via maskid if not mask: mtype = getMetaType(req.params.get("parent")) mask = mtype.children.filter_by(id =req.params.get("maskid", "")).scalar() elif req.params.get("form_op") == "save_newmask": mask = Mask(req.params.get("mname", "")) mtype.children.append(mask) db.session.commit() mask.name = req.params.get("mname") mask.setDescription(req.params.get("mdescription")) mask.setMasktype(req.params.get("mtype")) mask.setSeparator(req.params.get("mseparator")) db.session.commit() if req.params.get("mtype") == "export": mask.setExportMapping(req.params.get("exportmapping") or "") mask.setExportHeader(req.params.get("exportheader")) mask.setExportFooter(req.params.get("exportfooter")) _opt = "" if "types" in req.params.keys(): _opt += "t" if "notlast" in req.params.keys(): _opt += "l" mask.setExportOptions(_opt) db.session.commit() mask.setLanguage(req.params.get("mlanguage", "")) mask.setDefaultMask("mdefault" in req.params.keys()) for r in mask.access_ruleset_assocs.filter_by(ruletype=u'read'): db.session.delete(r) for key in req.params.keys(): if key.startswith("left"): for r in req.params.get(key).split(';'): mask.access_ruleset_assocs.append(NodeToAccessRuleset(ruleset_name=r, ruletype=key[4:])) break db.session.commit() return showMaskList(req, ustr(req.params.get("parent", ""))) return view(req)