From 492664d0a0a50ecd082c9c97a81feb60b45a5d85 Mon Sep 17 00:00:00 2001 From: Jorge Boucas Date: Wed, 24 Feb 2021 15:20:38 +0100 Subject: [PATCH] submissions --- docker-compose.yml | 2 +- flaski/apps/main/submissions.py | 66 ++++++++++++++++++++++--- flaski/apps/routes/submissions.py | 21 +++++--- flaski/email.py | 22 ++++++++- flaski/templates/apps/submissions.html | 4 +- flaski/templates/email/submissions.html | 6 +++ flaski/templates/email/submissions.txt | 7 +++ 7 files changed, 110 insertions(+), 18 deletions(-) create mode 100644 flaski/templates/email/submissions.html create mode 100644 flaski/templates/email/submissions.txt diff --git a/docker-compose.yml b/docker-compose.yml index 048cd26ce..4bc504df7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,10 +41,10 @@ services: dockerfile: services/server/dockerfiles/Dockerfile restart: always volumes: + - ./utils/dev.py:/flaski/flaski/apps/external.py # this needs to be the first volume as otherwise it will be overwritten by `- ./:/flaski` - data:/flaski_data/users - ./:/flaski - ~/flaski_private:/flaski_private:ro - - ./utils/dev.py:/flaski/flaski/apps/external.py environment: - LOGS=/var/log/flaski/ - SECRET_KEY=${SECRET_KEY} diff --git a/flaski/apps/main/submissions.py b/flaski/apps/main/submissions.py index 75b52247a..576216f2c 100644 --- a/flaski/apps/main/submissions.py +++ b/flaski/apps/main/submissions.py @@ -1,9 +1,63 @@ -def check_rnaseq(df): - status=True - msg=None +import pandas as pd +import io +from werkzeug.utils import secure_filename +import re + + +def check_rnaseq(EXC): + if "samples" not in EXC.sheet_names: + status=False + msg="Could not find sample information - 'samples' sheet - in the submission file." + return status, msg + metadata=EXC.parse("RNAseq") + email=metadata[ metadata["Field"] == "email"][ "Value" ].values[0] + email=str(email).rstrip().lstrip() + email=email.split(",") + email=[ re.search("([^@|\s]+@[^@]+\.[^@|\s]+)",e,re.I) for e in email ] + email=[ e.group(1) for e in email if e ] + if not email : + status=False + msg="Contact email is not a valid email. Please provide a valid email in the 'email' field of your submission file." + return status, msg + nas=metadata[metadata["Value"].isna()]["Field"].tolist() + if nas: + status=False + msg="The following fields require a valid value: {fields} ".format(fields=", ".join(nas) ) + return status, msg + + status="RNAseq" + msg="Submission successuful. Please check for email confirmation." return status, msg -def submission_check(df, submission_type="RNAseq"): - if submission_type=="RNAseq": - status, msg=check_rnaseq(df) +def submission_check(inputfile): + valid_submissions=["RNAseq"] + + filename = secure_filename(inputfile.filename) + fileread = inputfile.read() + filestream=io.BytesIO(fileread) + extension=filename.rsplit('.', 1)[1].lower() + if extension != "xlsx": + status=False + msg="Wrong file extension detected." + return status, msg + + EXC=pd.ExcelFile(filestream) + sheets=EXC.sheet_names + submission_type=[ s for s in sheets if s in valid_submissions ] + if len(submission_type) > 1 : + status=False + msg="More than one submission type detected." + return status, msg + + elif len(submission_type) == 0 : + status=False + msg="This submission file did not contain a valid submission sheet. Make sure you do not change the sheet names when editing the submission file." + return status, msg + + if submission_type[0]=="RNAseq": + status, msg=check_rnaseq(EXC) + else: + status=False + msg="Submission failed." + return status, msg \ No newline at end of file diff --git a/flaski/apps/routes/submissions.py b/flaski/apps/routes/submissions.py index a75bbeba2..7ffadddde 100644 --- a/flaski/apps/routes/submissions.py +++ b/flaski/apps/routes/submissions.py @@ -1,4 +1,5 @@ from flask import render_template, Flask, Response, request, url_for, redirect, session, send_file, flash, jsonify +from flask import Markup from flaski import app from werkzeug.utils import secure_filename from flask_session import Session @@ -7,9 +8,9 @@ from datetime import datetime from flaski import db from werkzeug.urls import url_parse -from flaski.apps.main.submission import submission_check #, submission_defaults +from flaski.apps.main.submissions import submission_check #, submission_defaults from flaski.models import User, UserLogging -from flaski.email import send_exception_email +from flaski.email import send_exception_email, send_submission_email from flaski.routines import session_to_file, check_session_app, handle_exception, read_request, read_tables, allowed_file, read_argument_file, read_session_file @@ -45,6 +46,7 @@ def submissions(): reset_info=check_session_app(session,"submissions",apps) if reset_info: flash(reset_info,'error') + # INITIATE SESSION session["filename"]="Select file.." @@ -72,25 +74,27 @@ def submissions(): # flash(msg,"info") # if not request.files["inputsessionfile"] and not request.files["inputargumentsfile"] : - plot_arguments=read_request(request) + # plot_arguments=read_request(request) inputfile = request.files["inputfile"] if inputfile: filename = secure_filename(inputfile.filename) - if allowed_file(inputfile.filename): - df=read_tables(inputfile) + if not allowed_file(inputfile.filename): + msg="This file is not allowed." + flash(msg,"error") + return render_template('/apps/submissions.html', apps=apps) # CALL FIGURE FUNCTION - status, msg=submission_check(df) + status, msg=submission_check(inputfile) if not status: flash(msg,"error") return render_template('/apps/submissions.html', apps=apps) #, **plot_arguments) if status: flash(msg) + send_submission_email(user=current_user, submission_type=status, submission_file=inputfile) return render_template('/apps/submissions.html', apps=apps) #, **plot_arguments) - # return render_template('/apps/submissions.html', apps=apps, **plot_arguments) except Exception as e: @@ -99,5 +103,6 @@ def submissions(): return render_template('/apps/submissions.html', apps=apps) #, **plot_arguments) else: - + initial_message=Markup('For submitting samples please follow the instructions in here.') + flash(initial_message) return render_template('apps/submissions.html', apps=apps) #, **session["plot_arguments"]) diff --git a/flaski/email.py b/flaski/email.py index 7d604609d..d9e3aa167 100644 --- a/flaski/email.py +++ b/flaski/email.py @@ -3,15 +3,23 @@ from flaski import app from flask_mail import Message from flaski import mail +from werkzeug.utils import secure_filename + def send_async_email(app, msg): with app.app_context(): mail.send(msg) -def send_email(subject, sender, recipients, text_body, html_body, reply_to): +def send_email(subject, sender, recipients, text_body, html_body, reply_to, attatchment=None): msg = Message(subject, sender=sender, recipients=recipients, reply_to = reply_to) msg.body = text_body msg.html = html_body + if attatchment: + msg.attach( + secure_filename(attatchment.filename), + 'application/octect-stream', + attatchment.read()) + Thread(target=send_async_email, args=(app, msg)).start() def send_password_reset_email(user): @@ -80,3 +88,15 @@ def send_help_email(user,eapp,emsg,etime,session_file): html_body=render_template('email/app_help.html', user=user, eapp=eapp, emsg=emsg_html, etime=etime, session_file=session_file),\ reply_to=user.email ) + +def send_submission_email(user,submission_type,submission_file): + with app.app_context(): + send_email('[Flaski][Automation][{submission_type}] Files have been submited for analysis.'.format(submission_type=submission_type), + sender=app.config['MAIL_USERNAME'], + recipients=[user.email,"bioinformatics@age.mpg.de"], + text_body=render_template('email/submissions.txt', + user=user, submission_type=submission_type), + html_body=render_template('email/submissions.html', + user=user, submission_type=submission_type),\ + reply_to='bioinformatics@age.mpg.de',\ + attatchment=submission_file ) \ No newline at end of file diff --git a/flaski/templates/apps/submissions.html b/flaski/templates/apps/submissions.html index 0a47e9340..48f177cad 100644 --- a/flaski/templates/apps/submissions.html +++ b/flaski/templates/apps/submissions.html @@ -3,7 +3,7 @@ {% block app_title %}
-

DAVID

+

Submissions

{% endblock %} @@ -36,7 +36,7 @@

DAVID

-
+ diff --git a/flaski/templates/email/submissions.html b/flaski/templates/email/submissions.html new file mode 100644 index 000000000..db16033ab --- /dev/null +++ b/flaski/templates/email/submissions.html @@ -0,0 +1,6 @@ +

Files have been submitted by {{ user.firstname }} for {{ submission_type }} analysis.

+

Submission form attached.

+ +
+

Cheers!

+

Flaski

\ No newline at end of file diff --git a/flaski/templates/email/submissions.txt b/flaski/templates/email/submissions.txt new file mode 100644 index 000000000..e7681771a --- /dev/null +++ b/flaski/templates/email/submissions.txt @@ -0,0 +1,7 @@ +Files have been submitted by {{ user.firstname }} for {{ submission_type }} analysis. + +Submission form attached. + +Cheers! + +Flaski \ No newline at end of file