Commit 754d0157 authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Code refactoring

parent 9afa96a9
......@@ -5,27 +5,30 @@ import string
ALLOWED_EXTENSIONS = ['fa', 'fasta', 'fna', 'fa.gz', 'fasta.gz', 'fna.gz']
def allowed_file(filename):
return '.' in filename and \
(filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS or ".".join(filename.rsplit('.', 2)[1:]).lower()
in ALLOWED_EXTENSIONS)
class Functions:
@staticmethod
def allowed_file(filename):
return '.' in filename and \
(filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS or ".".join(filename.rsplit('.', 2)[1:]).lower()
in ALLOWED_EXTENSIONS)
def random_string(s_len):
"""
Generate a random string
:param s_len: length of the string to generate
:return: the random string
"""
return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(s_len)])
@staticmethod
def random_string(s_len):
"""
Generate a random string
:param s_len: length of the string to generate
:return: the random string
"""
return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(s_len)])
def get_valid_uploaded_filename(filename, folder):
file_query_s = os.path.join(folder, filename)
i = 2
filename_orig = filename
while os.path.exists(file_query_s):
filename = str(i) + "_" + filename_orig
@staticmethod
def get_valid_uploaded_filename(filename, folder):
file_query_s = os.path.join(folder, filename)
i += 1
return filename
i = 2
filename_orig = filename
while os.path.exists(file_query_s):
filename = str(i) + "_" + filename_orig
file_query_s = os.path.join(folder, filename)
i += 1
return filename
......@@ -9,7 +9,7 @@ from config_reader import AppConfigReader
from pony.orm import db_session, select
from database import db, Job
from lib.Fasta import Fasta
from lib.functions import allowed_file
from lib.functions import Functions
import requests
import wget
......@@ -85,7 +85,7 @@ class JobManager:
else:
filename = None
if filename is not None:
allowed = allowed_file(filename)
allowed = Functions.allowed_file(filename)
if not allowed:
job = Job.get(id_job=self.id_job)
job.status = "error"
......
#!/usr/bin/env python3
import os
import time
import datetime
import shutil
......@@ -7,7 +8,7 @@ from flask import Flask, render_template, request, url_for, jsonify, session
from lib.paf import Paf
from config_reader import AppConfigReader
from lib.job_manager import JobManager
from lib.functions import *
from lib.functions import Functions
from lib.upload_file import UploadFile
from lib.Fasta import Fasta
......@@ -50,9 +51,9 @@ def main():
@app.route("/run", methods=['GET'])
def run():
session["user_tmp_dir"] = random_string(5) + "_" + \
session["user_tmp_dir"] = Functions.random_string(5) + "_" + \
datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S')
id_job = random_string(5) + "_" + datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S')
id_job = Functions.random_string(5) + "_" + datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S')
if "id_job" in request.args:
id_job = request.args["id_job"]
email = ""
......@@ -180,10 +181,10 @@ def upload():
folder_files = os.path.join(app.config["UPLOAD_FOLDER"], folder)
if not os.path.exists(folder_files):
os.makedirs(folder_files)
filename = get_valid_uploaded_filename(filename, folder_files)
filename = Functions.get_valid_uploaded_filename(filename, folder_files)
mime_type = files.content_type
if not allowed_file(files.filename):
if not Functions.allowed_file(files.filename):
result = UploadFile(name=filename, type_f=mime_type, size=0, not_allowed_msg="File type not allowed")
shutil.rmtree(folder_files)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment