def profile(): if request.method == 'GET': try: response = requests.get(BASE_API_URL + f'/profile', headers=Token.get_header_access()) except requests.exceptions.RequestException: return render_template("error.html") json = response.json() return render_template("profile.html", username=json['username'], email=json['email'], usage=json['usage']) data = request.form request_data = {"email": data['email'], "username": data['username']} try: response = requests.post(BASE_API_URL + f'/profile', headers=Token.get_header_access(), json=request_data) except requests.exceptions.RequestException: return render_template("error.html") json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) elif 'success' in json: flash(json['success']) return redirect(request.url)
def manage_collection_file(collection_id, filename): response = None if request.method == 'GET': try: response = requests.get(BASE_API_URL+f"/collection/{collection_id}/{filename}", headers=Token.get_header_access()) except requests.exceptions.RequestException: flash("[ERROR] Unexpected error") file_content = response.text if response else "Unexpected error" return render_template('file-show.html', filename=filename, filecontent=file_content) try: response = requests.delete(BASE_API_URL + f"/collection/{collection_id}/{filename}", headers=Token.get_header_access()) except requests.exceptions.RequestException: flash("[ERROR] Unexpected error") if response: json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) if 'success' in json: flash(json['success']) return "finished process"
def collection(collection_id): base_link = f"{BASE_API_URL}/collection/{collection_id}" if request.method == 'GET': try: collection_json = requests.get(base_link, headers=Token.get_header_access()).json() files_json = requests.get(base_link + "/filelist", headers=Token.get_header_access()).json() except requests.exceptions.RequestException: return render_template("error.html") return render_template("collection.html", collection=collection_json, files=files_json) elif request.method == 'POST': data = request.form request_data = { "name": data['name'], "description": data['description'] } try: response = requests.put(BASE_API_URL + f'/collection/{collection_id}', headers=Token.get_header_access(), json=request_data) except requests.exceptions.RequestException: return render_template("error.html") json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) elif 'success' in json: flash(json['success']) return redirect(request.url) elif request.method == 'DELETE': response = None try: response = requests.delete(BASE_API_URL + f"/collection/{collection_id}", headers=Token.get_header_access()) except requests.exceptions.RequestException: flash("[ERROR] Unexpected error") if response: json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) if 'success' in json: flash(json['success']) return "process finished"
def register_collection(): if request.method == 'GET': return render_template("collection_register.html") data = request.form request_data = { "name": data['name'], "description": data['description'] } try: response = requests.post(BASE_API_URL + '/collection', headers=Token.get_header_access(), json=request_data) except requests.exceptions.RequestException: return render_template("error.html") json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) return redirect(request.url) elif 'success' in json: flash(json['success']) return redirect(url_for('home.home'))
def password_reset(): if request.method == 'GET': return render_template("change-password.html") data = request.form request_data = { "actual_password": data['actual-password'], "new_password": data['new-password'] } check_password = check_password_complexity(data['new-password'], data['confirm-password']) if 'error' in check_password: flash("[ERROR] " + check_password['error']) return redirect(url_for("home.profile")) try: response = requests.post(BASE_API_URL + '/change-password', headers=Token.get_header_access(), json=request_data) except requests.exceptions.RequestException: return render_template("error.html") json = response.json() if 'success' not in json: flash("[ERROR] " + json['error']) else: flash(json['success']) return redirect(url_for("home.profile"))
def search(collection_id, *, keywords=None, file_search=None): try: if keywords: response = requests.get(BASE_API_URL + f'/collection/{collection_id}/search', headers=Token.get_header_access(), json={'search': keywords}) else: response = requests.get(BASE_API_URL + f'/collection/{collection_id}/search', headers=Token.get_header_access(), files={'file': file_search}) except requests.exceptions.RequestException: return render_template("error.html") json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) return redirect(url_for('home.home')) return render_template("result.html", results=json['solution'], keywords=keywords if keywords else file_search[0], collection_id=collection_id)
def macro_collections(): response = requests.get(f"{BASE_API_URL}/collection", headers=Token.get_header_access()) try: collections = response.json()['collections'] except KeyError: return [] return collections
def logout(): try: response = requests.post(BASE_API_URL + "/logout", headers=Token.get_header_access()) except requests.exceptions.RequestException: return render_template("error.html") if 'success' not in response.json(): return render_template("error.html") Token.access_token = None Token.refresh_token = None return redirect(url_for("auth.login"))
def process_collection(collection_id): try: response = requests.get(BASE_API_URL+f"/collection/{collection_id}/process", headers=Token.get_header_access()) except requests.exceptions.RequestException: flash("[ERROR] Unexpected error") return render_template("error.html") json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) if 'success' in json: flash(json['success']) return "collection processed"
def advanced_with_file(): if request.method == 'GET': return render_template('advanced-search-file.html') else: data = request.form collection_id = data.get("collection") file_up = request.files.getlist("file") params = get_params_advanced(data) kwargs = dict() if collection_id: for f in file_up: if f.filename: try: file_search = (f.filename, f.stream, f.mimetype) response = requests.get(BASE_API_URL + f'/collection/{collection_id}/search', headers=Token.get_header_access(), files={'file': file_search}, params=params) except requests.exceptions.RequestException: return render_template("error.html") json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) return render_template("advanced-search-file.html", collection_id=collection_id, params=params) return render_template("result.html", results=json['solution'], keywords=f.filename, collection_id=collection_id) else: flash("[ERROR] You need to put one file.") if collection_id: kwargs['collection_id'] = int(collection_id) break if not collection_id: flash("[ERROR] You need to select one collection.") kwargs['params'] = params return render_template('advanced-search-file.html', **kwargs)
def upload_files(collection_id): file_up = request.files.getlist("file") for file in file_up: send_file = {"file": (file.filename, file.stream, file.mimetype)} try: response = requests.post(BASE_API_URL + f"/collection/{collection_id}/upload", headers=Token.get_header_access(), files=send_file) except requests.exceptions.RequestException: flash("[ERROR] Unexpected error.") return redirect(url_for("collection.collection", collection_id=collection_id)) json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) elif 'success' in json: flash(json['success']) return redirect(url_for("collection.collection", collection_id=collection_id))
def advanced_text(): collection_id = request.args.get("collection") keywords = request.args.get("keywords") kwargs = dict() params = get_params_advanced(request.args) if collection_id and keywords: try: response = requests.get(BASE_API_URL + f'/collection/{collection_id}/search', headers=Token.get_header_access(), json={'search': keywords}, params=params) except requests.exceptions.RequestException: return render_template("error.html") json = response.json() if 'error' in json: flash("[ERROR] " + json['error']) return render_template("advanced.html", collection_id=collection_id, keywords=keywords, params=params) return render_template("result.html", results=json['solution'], keywords=keywords, collection_id=collection_id) if not collection_id: flash("[ERROR] You need to select one collection.") if keywords: kwargs['keywords'] = keywords if not keywords: flash("[ERROR] You need to type some keywords") if collection_id: kwargs['collection_id'] = int(collection_id) kwargs['params'] = params return render_template("advanced.html", **kwargs)