Skip to content

Commit

Permalink
submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeboucas committed Feb 24, 2021
1 parent 98e8012 commit 492664d
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
66 changes: 60 additions & 6 deletions flaski/apps/main/submissions.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 13 additions & 8 deletions flaski/apps/routes/submissions.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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.."

Expand Down Expand Up @@ -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:
Expand All @@ -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 <a href="https://github.com/mpg-age-bioinformatics/submissions/blob/main/README.md#submissions" class="alert-link">here</a>.')
flash(initial_message)
return render_template('apps/submissions.html', apps=apps) #, **session["plot_arguments"])
22 changes: 21 additions & 1 deletion flaski/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,"[email protected]"],
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='[email protected]',\
attatchment=submission_file )
4 changes: 2 additions & 2 deletions flaski/templates/apps/submissions.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block app_title %}

<div class="d-none d-sm-inline-block form-inline mr-auto ml-md-3 my-2 my-md-0 mw-100">
<h1 class="h3 mb-0 text-gray-800">DAVID</h1>
<h1 class="h3 mb-0 text-gray-800">Submissions</h1>
</div>

{% endblock %}
Expand Down Expand Up @@ -36,7 +36,7 @@ <h1 class="h3 mb-0 text-gray-800">DAVID</h1>

<div id="" style="overflow-y:auto;height:calc(100vh - 80px);">

<form action="{{ url_for('david') }}" method="post" enctype="multipart/form-data">
<form action="{{ url_for('submissions') }}" method="post" enctype="multipart/form-data">

<!-- -------------------- INPUT FILE SELECTION ---------------------- -->
<!-- Heading -->
Expand Down
6 changes: 6 additions & 0 deletions flaski/templates/email/submissions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>Files have been submitted by {{ user.firstname }} for {{ submission_type }} analysis.</p>
<p>Submission form attached.</p>

<br>
<p>Cheers!</p>
<p>Flaski</p>
7 changes: 7 additions & 0 deletions flaski/templates/email/submissions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Files have been submitted by {{ user.firstname }} for {{ submission_type }} analysis.

Submission form attached.

Cheers!

Flaski

0 comments on commit 492664d

Please sign in to comment.