def test_paragraphs(): from docx import Document doc = Document(test_dir / 'test1.docx') summary, reference_csv_details, title = create_upload_variables(doc) summary_detail = summary['Paragraphs'] assert summary_detail[ 'ok'], "Paragraphs has issues but should have no issues" for item in summary_detail['details']: assert item[ 'style_ok'], "Paragraphs has style issues but should have no style issues"
def test_author(): from docx import Document doc = Document(test_dir / 'test1.docx') summary, reference_csv_details, title = create_upload_variables(doc) summary_detail = summary['Authors'] assert summary_detail['ok'], "Author has issues but should have no issues" assert len(summary_detail['details']), "Author is more than one paragraph" details = summary_detail['details'][0] assert details[ 'style_ok'], "Author has style issues but should have no style issues" assert details[ 'title_style_ok'], "Author has strict style issues but should have no strict style issues"
def test_no_abstract(): from docx import Document doc = Document(test_dir / 'correct_a4_margins.docx') summary, reference_csv_details, title = None, None, None try: summary, reference_csv_details, title = create_upload_variables(doc) assert False, 'AbstractNotFoundError not raised' except AbstractNotFoundError as err: assert str( err ) == "Abstract header not found", 'AbstractNotFoundError found but not correct message' # these should be empty because Error raised before values returned assert summary is None, 'summary not empty' assert reference_csv_details is None, 'reference_csv_details not empty'
def upload_from_spms(paper_name, parse_type): base_path = 'C:\\\\Users\Hawke\\\\PycharmProjects\\\\jacow_files\\\\IPAC19 Test Files\\\\' try: spms_summary = '' full_path = base_path+paper_name+'.'+parse_type conference_path = 'C:\\\\Users\\\\Hawke\\\\PycharmProjects\\\\References_ipac19.csv' if parse_type == 'docx': doc = Document(full_path) # check whether tracking on (will raise an error if it is) check_tracking_on(doc) # get variables summary, authors, title = create_upload_variables(doc) spms_summary, reference_csv_details = create_spms_variables(paper_name, authors, title, conference_path) elif parse_type == 'tex': doc = TexSoup(open(full_path, encoding="utf8")) summary, authors, title = create_upload_variables_latex(doc) spms_summary, reference_csv_details = create_spms_variables(paper_name, authors, title, conference_path) print(spms_summary) return spms_summary except (PackageNotFoundError, ValueError): message = f"Failed to open document. Is it a valid {parse_type} document?" except TrackingOnError as err: message = err except OSError: message = f"It seems the file {paper_name} is corrupted" except PaperNotFoundError: message = f"It seems the file {paper_name} has no corresponding entry in the SPMS ({conference_path}) " \ f"references list. Is your filename the same as your Paper name?" except AbstractNotFoundError as err: message = err except Exception: app.logger.exception("Failed to process document") message = f"Failed to process document: {paper_name}" print(message) return message
def upload_common(documents, args): admin = 'DEV_DEBUG' in os.environ and os.environ['DEV_DEBUG'] == 'True' conferences = [ conference.short_name for conference in Conference.query.filter_by( is_active=True).order_by(Conference.display_order.asc()).all() ] if request.method == "POST" and documents.name in request.files: try: filename = documents.save(request.files[documents.name]) paper_name = os.path.splitext(filename)[0] except UploadNotAllowed: return render_template( "upload.html", error= f"Wrong file extension. Please upload {args['extension']} files only", admin=admin, args=args) full_path = documents.path(filename) # set a default conference_id = False # next(iter(conferences)) conference_path = '' if 'conference_id' in request.form and request.form[ "conference_id"] in conferences: conference_id = request.form["conference_id"] conference_path = get_conference_path(conference_id) try: if args['description'] == 'Word': doc = Document(full_path) parse_type = 'docx' metadata = doc.core_properties # check whether tracking on result = check_tracking_on(doc) # get variables to pass to template summary, authors, title = create_upload_variables(doc) if conference_id: spms_summary, reference_csv_details = \ create_spms_variables(paper_name, authors, title, conference_path, conference_id) if spms_summary: summary.update(spms_summary) elif args['description'] == 'Latex': doc = TexSoup(open(full_path, encoding="utf8")) summary, authors, title = create_upload_variables_latex(doc) metadata = [] if conference_id: spms_summary, reference_csv_details = \ create_spms_variables(paper_name, authors, title, conference_path, conference_id) if spms_summary: summary.update(spms_summary) save_log(filename, conference_id, 'OK', locals()) return render_template("upload.html", processed=True, **locals()) except (PackageNotFoundError, ValueError): save_log(filename, conference_id, 'PackageNotFoundError', locals()) return render_template( "upload.html", filename=filename, conferences=conferences, error= f"Failed to open document {filename}. Is it a valid {args['description']} document?", admin=admin, args=args) except TrackingOnError as err: save_log(filename, conference_id, 'TrackingOnError', locals()) return render_template("upload.html", filename=filename, conferences=conferences, error=err, admin=admin, args=args) except OSError: save_log(filename, conference_id, 'OSError', locals()) return render_template( "upload.html", filename=filename, conferences=conferences, error=f"It seems the file {filename} is corrupted", admin=admin, args=args) except PaperNotFoundError: save_log(filename, conference_id, 'PaperNotFoundError', locals()) return render_template( "upload.html", processed=True, **locals(), error= f"It seems the file {filename} has no corresponding entry in the SPMS ({conference_id}) references list. " f"Is your filename the same as your Paper name?") except AbstractNotFoundError as err: save_log(filename, conference_id, 'AbstractNotFoundError', locals()) return render_template("upload.html", filename=filename, conferences=conferences, error=err, admin=admin, args=args) except Exception: # TODO work out why there is an OK log followed by an Exception one. save_log(filename, conference_id, 'Exception', locals()) if app.debug: raise else: app.logger.exception("Failed to process document") return render_template( "upload.html", error=f"Failed to process document: {filename}", conferences=conferences, args=args) finally: os.remove(full_path) return render_template("upload.html", admin=admin, args=args, conferences=conferences)