From 9f03001b0d62794331cbde6ac7ed45fc9790c34d Mon Sep 17 00:00:00 2001 From: padamchopra Date: Mon, 8 Jan 2018 17:29:28 +0530 Subject: [PATCH 1/5] Even fork with scorelab/Bassa --- .travis.yml | 2 +- scripts/debian/setup-mysql.sh | 15 +++ ui/.eslintrc.yml | 1 - ui/gulp/build.js | 4 +- ui/package.json | 2 +- ui/src/app/login/controllers/LoginCtrl.js | 7 +- ui/src/app/stylesheets/_custom.scss | 140 +++++++++++++++++++++- ui/src/app/views/login.html | 52 ++++---- ui/src/app/views/signup.html | 54 +++++---- 9 files changed, 215 insertions(+), 62 deletions(-) create mode 100755 scripts/debian/setup-mysql.sh diff --git a/.travis.yml b/.travis.yml index e56fc02f..19e49239 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,4 +32,4 @@ after_success: docker push scoreucsc/bassa; curl -X POST 'https://hooks.microbadger.com/images/scoreucsc/bassa/Wbi5zehDMooS3ri2bgC8LETGGuQ='; fi - + diff --git a/scripts/debian/setup-mysql.sh b/scripts/debian/setup-mysql.sh new file mode 100755 index 00000000..8f73130e --- /dev/null +++ b/scripts/debian/setup-mysql.sh @@ -0,0 +1,15 @@ +#!/bin/bash +mysql_root_password='s57d46857f968t79pho'; +port="3006"; +echo ""; +echo "Installing MySQL on Debian based Linux" +echo ""; +sudo apt-get update; +sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password '$mysql_root_password; +sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password '$mysql_root_password; +sudo apt-get -y install mysql-server; +sudo sed -i "s/port.*/port = $port/" /etc/mysql/mysql.conf.d/mysqld.cnf; +sudo service mysql stop; +sudo service mysql start; +echo ""; +echo "MySQL Installed Successfully"; diff --git a/ui/.eslintrc.yml b/ui/.eslintrc.yml index b99dfa04..36b7af3d 100644 --- a/ui/.eslintrc.yml +++ b/ui/.eslintrc.yml @@ -1,2 +1 @@ extends: airbnb-base - diff --git a/ui/gulp/build.js b/ui/gulp/build.js index 203d56fc..575714df 100644 --- a/ui/gulp/build.js +++ b/ui/gulp/build.js @@ -13,7 +13,7 @@ gulp.task('partials', function () { paths.src + '/{app,components}/**/*.html', paths.tmp + '/{app,components}/**/*.html' ]) - .pipe($.minifyHtml({ + .pipe($.htmlmin({ empty: true, spare: true, quotes: true @@ -52,7 +52,7 @@ gulp.task('html', ['inject', 'partials'], function () { .pipe($.useref()) .pipe($.revReplace()) .pipe(htmlFilter) - .pipe($.minifyHtml({ + .pipe($.htmlmin({ empty: true, spare: true, quotes: true diff --git a/ui/package.json b/ui/package.json index 66777b0b..af23f9f7 100644 --- a/ui/package.json +++ b/ui/package.json @@ -56,7 +56,7 @@ "gulp-jshint": "~1.9.0", "gulp-karma": "0.0.5", "gulp-load-plugins": "~0.7.1", - "gulp-minify-html": "~0.1.7", + "gulp-htmlmin": "~1.3.0", "gulp-ng-annotate": "~1.0.0", "gulp-protractor": "~0.0.11", "gulp-rename": "~1.2.0", diff --git a/ui/src/app/login/controllers/LoginCtrl.js b/ui/src/app/login/controllers/LoginCtrl.js index 80a56ddc..f633ef01 100755 --- a/ui/src/app/login/controllers/LoginCtrl.js +++ b/ui/src/app/login/controllers/LoginCtrl.js @@ -10,12 +10,11 @@ $scope.incorrectCredentials = false; $scope.unApproved = false; UserService.login($scope.user, function(status) { - if (status.state === 200) { - localStorage.setItem("user", JSON.stringify($scope.user)); + if (status.state == 200) { $state.go('home.dashboard'); - } else if(status.state === 401) { + } else if(status.state == 401) { $scope.unApproved = true; - } else if(status.state === 403) { + } else if(status.state == 403) { $scope.incorrectCredentials = true; } }); diff --git a/ui/src/app/stylesheets/_custom.scss b/ui/src/app/stylesheets/_custom.scss index 3548b85b..fbc06b1c 100755 --- a/ui/src/app/stylesheets/_custom.scss +++ b/ui/src/app/stylesheets/_custom.scss @@ -180,6 +180,49 @@ md-backdrop.md-sidenav-backdrop { margin-top: 0px; } +#signup-button{ + border-color: #757575; + border-width: 7px; + color: #757575; + font-weight: bold; + margin-left: auto; + margin-right: 7px; + margin-top: 15PX; + cursor: pointer; +} + +.input-container{ + margin:1px; +} + +.login-content{ + width: 23%; +} + +.login-button{ + font-weight:bolder; + margin: 0 6px 6px 0; +} + +.alert{ + padding:14px; + background-color:#F2DEDE; + border-color: #EBCCD1; + color: #B94A48; + border-radius: 4px; +} + +.closebtn{ + margin-left: 15px; + color: #B94A48; + font-weight: bold; + float: right; + font-size: 22px; + line-height: 20px; + cursor: pointer; + transition: 0.3s; +} + // Dashboard .add-download-box { background-color: white; @@ -210,7 +253,6 @@ md-backdrop.md-sidenav-backdrop { padding-bottom: 0px; } - .alert { padding:14px; background-color:grey; color: white; } @@ -224,4 +266,98 @@ md-backdrop.md-sidenav-backdrop { line-height: 20px; cursor: pointer; transition: 0.3s; -} \ No newline at end of file +} + +//Signup design + +form { + width: 380px; + margin: 4em auto; + padding: 3em 2em 2em 2em; + background: #fafafa; + border: 1px solid #ebebeb; + box-shadow: rgba(0,0,0,0.14902) 0px 1px 1px 0px,rgba(0,0,0,0.09804) 0px 1px 2px 0px; +} + +.group { + position: relative; + margin-bottom: 45px; +} + + +/* Underline */ + +.bar { + position: relative; + display: block; + width: 100%; +} + +.bar:before, .bar:after { + content: ''; + height: 2px; + width: 0; + bottom: 1px; + position: absolute; + background: #757575; + transition: all 0.2s ease; +} + +.bar:before { left: 50%; } + +.bar:after { right: 50%; } + + + +/* Highlight */ + +.highlight { + position: absolute; + height: 60%; + width: 100px; + top: 25%; + left: 0; + pointer-events: none; + opacity: 0.5; +} + + + +.has-error { + color: #f39c12; +} + +.has-success { + color: #18bc9c; +} + + +.inputStyle { + font-size: 18px; + padding: 10px 10px 10px 5px; + -webkit-appearance: none; + display: block; + background: #fafafa; + color: #636363; + width: 100%; + border: none; + border-radius: 0; + border-bottom: 1px solid #757575; +} + +.inputStyle:focus { outline: none; } + +/* active */ + +.inputStyle:focus ~ label, .inputStyle.used ~ label { + top: -20px; + transform: scale(.75); left: -2px; + /* font-size: 14px; */ + color: #4a89dc; +} + +.inputStyle:focus ~ .bar:before, .inputStyle:focus ~ .bar:after { width: 50%; } + +.inputStyle:focus ~ .highlight { + animation: inputHighlighter 0.3s ease; +} diff --git a/ui/src/app/views/login.html b/ui/src/app/views/login.html index 88d721d1..75cfc8a4 100755 --- a/ui/src/app/views/login.html +++ b/ui/src/app/views/login.html @@ -1,27 +1,25 @@ -
- -

Login to Bassa

-
- - - - - - - - -
-
-
- × - Incorrect Credentials -
-
- × - Please confirm your account -
-
- Login - Signup -
-
+
+ + +
+ × Incorrect Credentials +
+
+ × Please confirm your account +
+
\ No newline at end of file diff --git a/ui/src/app/views/signup.html b/ui/src/app/views/signup.html index ae375da9..fd1ead83 100755 --- a/ui/src/app/views/signup.html +++ b/ui/src/app/views/signup.html @@ -1,26 +1,32 @@
-

Signup for a Bassa account

-
- - - - - - - - - - - - - - - - -
-
-
- Back - Signup -
+

Sign up for Bassa

+ +
+
+ +

Username should be more than 3 characters and less than 15

+
+ +
+ +

Please Enter a valid email address

+
+ +
+ +

Please Enter at least 8 characters

+
+ +
+ +

Password did not match!

+
+ +
+ Back + Signup +
+
+
+ From af72fc9e2258a5c129abea5e7bb2c755e22730e8 Mon Sep 17 00:00:00 2001 From: padamchopra Date: Mon, 8 Jan 2018 18:16:10 +0530 Subject: [PATCH 2/5] Break Rest.py --- components/core/Routes/Download.py | 169 ++++++++++++++++++ components/core/{REST.py => Routes/User.py} | 184 +------------------- components/core/Routes/index.py | 2 + components/core/Server.py | 48 +++++ 4 files changed, 224 insertions(+), 179 deletions(-) create mode 100644 components/core/Routes/Download.py rename components/core/{REST.py => Routes/User.py} (64%) create mode 100644 components/core/Routes/index.py create mode 100644 components/core/Server.py diff --git a/components/core/Routes/Download.py b/components/core/Routes/Download.py new file mode 100644 index 00000000..f8131661 --- /dev/null +++ b/components/core/Routes/Download.py @@ -0,0 +1,169 @@ +import sys, os +dir_path = os.getcwd() +sys.path.append(dir_path) + +from flask import Flask +from flask.ext.cors import CORS +from flask import send_file, send_from_directory +from flask import request, jsonify, abort, Response, g +from flask_socketio import SocketIO, join_room +from Auth import * +from Models import * +from DownloadManager import * +import json, urllib.request, urllib.error, urllib.parse, os, _thread +from multiprocessing import Process +from DownloadDaemon import starter +from EMail import send_mail +from gevent import monkey + + +@server.route('/download/start') +def start(): + try: + token = request.headers['key'] + if str(token)!=server.config['SECRET_KEY']: + return "{'error':'not authorized'}", 403 + global p + p = Process(target=starter, args=(socketio,)) + p.start() + return '{"status":"' + str(p.pid) + '"}' + except Exception as e: + return '{"error":"' + e.message + '"}',400 + +@socketio.on('join', namespace='/progress') +def on_join(data): + room = data['room'] + if room != '': + join_room(room) + +@server.route('/download/kill') +def kill(): + try: + token = request.headers['key'] + if str(token)!=server.config['SECRET_KEY']: + return "{'error':'not authorized'}", 403 + if p is not None: + p.terminate() + p.join() + jsonreq = json.dumps({'jsonrpc':'2.0', 'id':'qwer', 'method':'aria2.pauseAll'}) + jsonreq = jsonreq.encode('ascii') + c = urllib.request.urlopen('http://localhost:6800/jsonrpc', jsonreq) + if verbose: + print(c) + if not p.is_alive(): + return '{"status":"success"}' + else: + return '{"error":"error"}' + except Exception as e: + return '{"error":"' + e.message + '"}',400 + + +@server.route('/api/download', methods=['POST']) +def add_download_request(): + token = token_validator(request.headers['token']) + if token is not None: + data = request.get_json(force=True) + try: + if check_if_bandwidth_exceeded(g.user.userName): + resp = Response(response='{"quota":"exceeded"}', status=400) + else: + newDownload = Download(data['link'], g.user.userName) + status = add_download(newDownload) + if status == "success": + resp = Response(response='{"status":"'+ status + '"}', status=200) + else: + resp = Response(response='{"error":"' + status + '"}', status=400) + except Exception as e: + resp = Response(response='{"error":"' + e.message + '"}', status=400) + resp.headers['token'] = token + resp.headers['Access-Control-Expose-Headers'] = 'token' + return resp + elif token is not None: + return '{"error":"not authorized"}', 403 + else: + return '{"error":"token error"}', 403 + + +@server.route('/api/download/', methods=['DELETE']) +def remove_download_request(id): + token = token_validator(request.headers['token']) + if token is not None: + try: + status = remove_download(id, g.user.userName) + if status == "success": + resp = Response(response='{"status":"'+ status + '"}', status=200) + else: + resp = Response(response='{"error":"' + status + '"}', status=400) + except Exception as e: + resp = Response(response='{"error":"' + e.message + '"}', status=400) + resp.headers['token'] = token + resp.headers['Access-Control-Expose-Headers'] = 'token' + return resp + elif token is not None: + return '{"error":"not authorized"}', 403 + else: + return '{"error":"token error"}', 403 + + +@server.route('/api/download/rate/', methods=['POST']) +def rate_download_request(id): + token = token_validator(request.headers['token']) + if token is not None: + data = request.get_json(force=True) + try: + status = rate_download(id, g.user.userName, data['rate']) + if status == "success": + resp = Response(response='{"status":"'+ status + '"}', status=200) + else: + resp = Response(response='{"error":"' + status + '"}', status=400) + except Exception as e: + resp = Response(response='{"error":"' + e.message + '"}', status=400) + resp.headers['token'] = token + resp.headers['Access-Control-Expose-Headers'] = 'token' + return resp + elif token is not None: + return '{"error":"not authorized"}', 403 + else: + return '{"error":"token error"}', 403 + + +@server.route('/api/downloads/', methods=['GET']) +def get_downloads_request(limit): + token = token_validator(request.headers['token']) + if token is not None : + try: + status = get_downloads(int(limit)) + if not isinstance(status, str): + resp = Response(response=json.dumps(status), status=200) + else: + resp = Response(response='{"error":"' + status + '"}', status=400) + except Exception as e: + resp = Response(response='{"error":"' + e.message + '"}', status=400) + resp.headers['token'] = token + resp.headers['Access-Control-Expose-Headers'] = 'token' + return resp + elif token is not None: + return '{"error":"not authorized"}', 403 + else: + return '{"error":"token error"}', 403 + +@server.route('/api/download/', methods=['GET']) +def get_download(id): + token = token_validator(request.headers['token']) + if token is not None : + try: + status = get_download_path(int(id)) + if status is not None and status!="db connection error": + if verbose: + print(status) + return send_file(status,as_attachment=True, mimetype='multipart/form-data') + else: + return '{"error":"file not found"}', 404 + except Exception as e: + resp = Response(response="{'error':'" + e.message + "'}", status=400) + return resp + elif token is not None: + return '{"error":"not authorized"}', 403 + else: + return '{"error":"token error"}', 403 + diff --git a/components/core/REST.py b/components/core/Routes/User.py similarity index 64% rename from components/core/REST.py rename to components/core/Routes/User.py index 5fa8bbc5..615f0bef 100644 --- a/components/core/REST.py +++ b/components/core/Routes/User.py @@ -1,3 +1,7 @@ +import sys, os +dir_path = os.getcwd() +sys.path.append(dir_path) + from flask import Flask from flask.ext.cors import CORS from flask import send_file, send_from_directory @@ -10,77 +14,7 @@ from multiprocessing import Process from DownloadDaemon import starter from EMail import send_mail -import sys - from gevent import monkey -monkey.patch_all(ssl=False) - -server = Flask(__name__) -server.config['SECRET_KEY'] = "123456789" -socketio = SocketIO(server, debug=True, logger=True, engineio_logger=True, ping_timeout=600) -cors = CORS(server) -p = None -verbose = False - -if len(sys.argv) == 2 and sys.argv[1] == '-v': - verbose = True - -def token_validator(token): - user = verify_auth_token(token, server.config['SECRET_KEY']) - if user != None: - g.user = user - token = generate_auth_token(user, server.config['SECRET_KEY']) - return token - return None - - -@server.route('/ui/') -def serve_ui(path): - return send_from_directory(os.path.dirname(os.path.realpath(__file__))+"/ui", path) - -@server.route('/ui/') -def serve_ui1(): - return send_file(os.path.dirname(os.path.realpath(__file__))+"/ui/index.html") - -@server.route('/download/start') -def start(): - try: - token = request.headers['key'] - if str(token)!=server.config['SECRET_KEY']: - return "{'error':'not authorized'}", 403 - global p - p = Process(target=starter, args=(socketio,)) - p.start() - return '{"status":"' + str(p.pid) + '"}' - except Exception as e: - return '{"error":"' + e.message + '"}',400 - -@socketio.on('join', namespace='/progress') -def on_join(data): - room = data['room'] - if room != '': - join_room(room) - -@server.route('/download/kill') -def kill(): - try: - token = request.headers['key'] - if str(token)!=server.config['SECRET_KEY']: - return "{'error':'not authorized'}", 403 - if p is not None: - p.terminate() - p.join() - jsonreq = json.dumps({'jsonrpc':'2.0', 'id':'qwer', 'method':'aria2.pauseAll'}) - jsonreq = jsonreq.encode('ascii') - c = urllib.request.urlopen('http://localhost:6800/jsonrpc', jsonreq) - if verbose: - print(c) - if not p.is_alive(): - return '{"status":"success"}' - else: - return '{"error":"error"}' - except Exception as e: - return '{"error":"' + e.message + '"}',400 @server.route('/api/login', methods=['POST']) @@ -309,75 +243,6 @@ def unblock_user_request(username): return '{"error":"token error"}', 403 -@server.route('/api/download', methods=['POST']) -def add_download_request(): - token = token_validator(request.headers['token']) - if token is not None: - data = request.get_json(force=True) - try: - if check_if_bandwidth_exceeded(g.user.userName): - resp = Response(response='{"quota":"exceeded"}', status=400) - else: - newDownload = Download(data['link'], g.user.userName) - status = add_download(newDownload) - if status == "success": - resp = Response(response='{"status":"'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) - except Exception as e: - resp = Response(response='{"error":"' + e.message + '"}', status=400) - resp.headers['token'] = token - resp.headers['Access-Control-Expose-Headers'] = 'token' - return resp - elif token is not None: - return '{"error":"not authorized"}', 403 - else: - return '{"error":"token error"}', 403 - - -@server.route('/api/download/', methods=['DELETE']) -def remove_download_request(id): - token = token_validator(request.headers['token']) - if token is not None: - try: - status = remove_download(id, g.user.userName) - if status == "success": - resp = Response(response='{"status":"'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) - except Exception as e: - resp = Response(response='{"error":"' + e.message + '"}', status=400) - resp.headers['token'] = token - resp.headers['Access-Control-Expose-Headers'] = 'token' - return resp - elif token is not None: - return '{"error":"not authorized"}', 403 - else: - return '{"error":"token error"}', 403 - - -@server.route('/api/download/rate/', methods=['POST']) -def rate_download_request(id): - token = token_validator(request.headers['token']) - if token is not None: - data = request.get_json(force=True) - try: - status = rate_download(id, g.user.userName, data['rate']) - if status == "success": - resp = Response(response='{"status":"'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) - except Exception as e: - resp = Response(response='{"error":"' + e.message + '"}', status=400) - resp.headers['token'] = token - resp.headers['Access-Control-Expose-Headers'] = 'token' - return resp - elif token is not None: - return '{"error":"not authorized"}', 403 - else: - return '{"error":"token error"}', 403 - - @server.route('/api/user/downloads/', methods=['GET']) def get_downloads_user_request(limit): token = token_validator(request.headers['token']) @@ -399,46 +264,6 @@ def get_downloads_user_request(limit): return '{"error":"token error"}', 403 -@server.route('/api/downloads/', methods=['GET']) -def get_downloads_request(limit): - token = token_validator(request.headers['token']) - if token is not None : - try: - status = get_downloads(int(limit)) - if not isinstance(status, str): - resp = Response(response=json.dumps(status), status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) - except Exception as e: - resp = Response(response='{"error":"' + e.message + '"}', status=400) - resp.headers['token'] = token - resp.headers['Access-Control-Expose-Headers'] = 'token' - return resp - elif token is not None: - return '{"error":"not authorized"}', 403 - else: - return '{"error":"token error"}', 403 - -@server.route('/api/download/', methods=['GET']) -def get_download(id): - token = token_validator(request.headers['token']) - if token is not None : - try: - status = get_download_path(int(id)) - if status is not None and status!="db connection error": - if verbose: - print(status) - return send_file(status,as_attachment=True, mimetype='multipart/form-data') - else: - return '{"error":"file not found"}', 404 - except Exception as e: - resp = Response(response="{'error':'" + e.message + "'}", status=400) - return resp - elif token is not None: - return '{"error":"not authorized"}', 403 - else: - return '{"error":"token error"}', 403 - @server.route('/api/user/heavy', methods=['GET']) def get_topten_heaviest_users(): token = token_validator(request.headers['token']) @@ -458,3 +283,4 @@ def get_topten_heaviest_users(): return '{"error":"not authorized"}', 403 else: return '{"error":"token error"}', 403 + diff --git a/components/core/Routes/index.py b/components/core/Routes/index.py new file mode 100644 index 00000000..ca0f298b --- /dev/null +++ b/components/core/Routes/index.py @@ -0,0 +1,2 @@ +import User; +import Download; diff --git a/components/core/Server.py b/components/core/Server.py new file mode 100644 index 00000000..24c3b5a5 --- /dev/null +++ b/components/core/Server.py @@ -0,0 +1,48 @@ +from flask import Flask +from flask.ext.cors import CORS +from flask import send_file, send_from_directory +from flask import request, jsonify, abort, Response, g +from flask_socketio import SocketIO, join_room +from Auth import * +from Models import * +from DownloadManager import * +import json, urllib.request, urllib.error, urllib.parse, os, _thread +from multiprocessing import Process +from DownloadDaemon import starter +from EMail import send_mail +import sys, os +from gevent import monkey + +dir_path = os.getcwd() +sys.path.append(dir_path+'/Routes') +import index + +monkey.patch_all(ssl=False) + +server = Flask(__name__) +server.config['SECRET_KEY'] = "123456789" +socketio = SocketIO(server, debug=True, logger=True, engineio_logger=True, ping_timeout=600) +cors = CORS(server) +p = None +verbose = False + +if len(sys.argv) == 2 and sys.argv[1] == '-v': + verbose = True + +def token_validator(token): + user = verify_auth_token(token, server.config['SECRET_KEY']) + if user != None: + g.user = user + token = generate_auth_token(user, server.config['SECRET_KEY']) + return token + return None + + +@server.route('/ui/') +def serve_ui(path): + return send_from_directory(os.path.dirname(os.path.realpath(__file__))+"/ui", path) + +@server.route('/ui/') +def serve_ui1(): + return send_file(os.path.dirname(os.path.realpath(__file__))+"/ui/index.html") + From 81820a060aa9db9b35a7badb99287d93b60afd3f Mon Sep 17 00:00:00 2001 From: padamchopra Date: Mon, 8 Jan 2018 20:35:12 +0530 Subject: [PATCH 3/5] Remove os re-imports --- components/core/Routes/Download.py | 3 +-- components/core/Routes/User.py | 5 +---- components/core/Server.py | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/components/core/Routes/Download.py b/components/core/Routes/Download.py index f8131661..f00d7307 100644 --- a/components/core/Routes/Download.py +++ b/components/core/Routes/Download.py @@ -10,13 +10,12 @@ from Auth import * from Models import * from DownloadManager import * -import json, urllib.request, urllib.error, urllib.parse, os, _thread +import json, urllib.request, urllib.error, urllib.parse, _thread from multiprocessing import Process from DownloadDaemon import starter from EMail import send_mail from gevent import monkey - @server.route('/download/start') def start(): try: diff --git a/components/core/Routes/User.py b/components/core/Routes/User.py index 615f0bef..92f157b4 100644 --- a/components/core/Routes/User.py +++ b/components/core/Routes/User.py @@ -10,10 +10,7 @@ from Auth import * from Models import * from DownloadManager import * -import json, urllib.request, urllib.error, urllib.parse, os, _thread -from multiprocessing import Process -from DownloadDaemon import starter -from EMail import send_mail +import urllib.request, urllib.error, urllib.parse, _thread from gevent import monkey diff --git a/components/core/Server.py b/components/core/Server.py index 24c3b5a5..4a9a32fc 100644 --- a/components/core/Server.py +++ b/components/core/Server.py @@ -10,7 +10,7 @@ from multiprocessing import Process from DownloadDaemon import starter from EMail import send_mail -import sys, os +import sys from gevent import monkey dir_path = os.getcwd() From d1b96e2cf9891dcbc8f718ead101baec1754d919 Mon Sep 17 00:00:00 2001 From: padamchopra Date: Mon, 8 Jan 2018 23:26:55 +0530 Subject: [PATCH 4/5] Fix file location acquiring in python --- components/core/Routes/Download.py | 2 +- components/core/Routes/User.py | 2 +- components/core/Server.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/components/core/Routes/Download.py b/components/core/Routes/Download.py index f00d7307..8406dbb1 100644 --- a/components/core/Routes/Download.py +++ b/components/core/Routes/Download.py @@ -1,5 +1,5 @@ import sys, os -dir_path = os.getcwd() +dir_path = os.path.abspath(__file__)[:-19] sys.path.append(dir_path) from flask import Flask diff --git a/components/core/Routes/User.py b/components/core/Routes/User.py index 92f157b4..2a2c3614 100644 --- a/components/core/Routes/User.py +++ b/components/core/Routes/User.py @@ -1,5 +1,5 @@ import sys, os -dir_path = os.getcwd() +dir_path = os.path.abspath(__file__)[:-15] sys.path.append(dir_path) from flask import Flask diff --git a/components/core/Server.py b/components/core/Server.py index 4a9a32fc..7463f690 100644 --- a/components/core/Server.py +++ b/components/core/Server.py @@ -13,7 +13,7 @@ import sys from gevent import monkey -dir_path = os.getcwd() +dir_path = os.path.abspath(__file__)[:-10] sys.path.append(dir_path+'/Routes') import index From ecfe69697e49d162757c76e78cf3854282f409e6 Mon Sep 17 00:00:00 2001 From: padamchopra Date: Tue, 9 Jan 2018 16:03:16 +0530 Subject: [PATCH 5/5] Combine if/else statements for resp var --- components/core/Routes/Download.py | 20 +++--------- components/core/Routes/User.py | 52 +++++++----------------------- 2 files changed, 15 insertions(+), 57 deletions(-) diff --git a/components/core/Routes/Download.py b/components/core/Routes/Download.py index 8406dbb1..6524768e 100644 --- a/components/core/Routes/Download.py +++ b/components/core/Routes/Download.py @@ -68,10 +68,7 @@ def add_download_request(): else: newDownload = Download(data['link'], g.user.userName) status = add_download(newDownload) - if status == "success": - resp = Response(response='{"status":"'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response='{"status":"'+ status + '"}', status= (200 if status == "success" else 400)) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -89,10 +86,7 @@ def remove_download_request(id): if token is not None: try: status = remove_download(id, g.user.userName) - if status == "success": - resp = Response(response='{"status":"'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response='{"status":"'+ status + '"}', status= (200 if status == "success" else 400)) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -111,10 +105,7 @@ def rate_download_request(id): data = request.get_json(force=True) try: status = rate_download(id, g.user.userName, data['rate']) - if status == "success": - resp = Response(response='{"status":"'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response='{"status":"'+ status + '"}', status= (200 if status == "success" else 400)) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -132,10 +123,7 @@ def get_downloads_request(limit): if token is not None : try: status = get_downloads(int(limit)) - if not isinstance(status, str): - resp = Response(response=json.dumps(status), status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response=((json.dumps(status),status=200) if not isinstance(status, str) else ('{"error":"' + status + '"}', status=400))) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token diff --git a/components/core/Routes/User.py b/components/core/Routes/User.py index 2a2c3614..c88b9d4f 100644 --- a/components/core/Routes/User.py +++ b/components/core/Routes/User.py @@ -41,7 +41,7 @@ def regular_user_request(): status = add_regular_user(newUser) if status == "success": resp = Response(response='{"status": "'+ status + '"}', status=200) - # _thread.start_new_thread(send_mail, (data['email'],"Hi\n Your Bassa account will be approved after it has been approved by an admin.")) + # _thread.start_new_thread(send_mail, (data['email'],"Hi\n Your Bassa account will be activated after it has been approved by an admin.")) else: resp = Response(response='{"error":"' + status + '"}', status=400) except Exception as e: @@ -79,10 +79,7 @@ def remove_user_request(username): if token is not None and g.user.auth == AuthLeval.ADMIN: try: status = remove_user(username) - if status == "success": - resp = Response(response='{"status": "'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response='{"status":"'+ status + '"}', status= (200 if status == "success" else 400)) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -102,10 +99,7 @@ def update_user_request(username): try: newUser = User(data['user_name'], data['password'], int(data['auth']), data['email']) status = update_user(newUser, username) - if status == "success": - resp = Response(response='{"status": "'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response='{"status":"'+ status + '"}', status= (200 if status == "success" else 400)) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -123,10 +117,7 @@ def get_users_request(): if token is not None and g.user.auth == AuthLeval.ADMIN: try: status = get_users() - if not isinstance(status, str): - resp = Response(response=json.dumps(status), status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response=((json.dumps(status),status=200) if not isinstance(status, str) else ('{"error":"' + status + '"}', status=400))) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -143,10 +134,7 @@ def get_user_signup_requests(): if token is not None and g.user.auth == AuthLeval.ADMIN: try: status = get_signup_requests() - if not isinstance(status, str): - resp = Response(response=json.dumps(status), status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response=((json.dumps(status),status=200) if not isinstance(status, str) else ('{"error":"' + status + '"}', status=400))) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -163,10 +151,7 @@ def approve_user_request(username): if token is not None and g.user.auth == AuthLeval.ADMIN: try: status = approve_user(username) - if status == "success": - resp = Response(response='{"status": "'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response='{"status":"'+ status + '"}', status= (200 if status == "success" else 400)) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -183,10 +168,7 @@ def get_blocked_users_request(): if token is not None and g.user.auth == AuthLeval.ADMIN: try: status = get_blocked_users() - if not isinstance(status, str): - resp = Response(response=json.dumps(status), status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response=((json.dumps(status),status=200) if not isinstance(status, str) else ('{"error":"' + status + '"}', status=400))) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -204,10 +186,7 @@ def block_user_request(username): if token is not None and g.user.auth == AuthLeval.ADMIN: try: status = block_user(username) - if status == "success": - resp = Response(response='{"status": "'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response='{"status":"'+ status + '"}', status= (200 if status == "success" else 400)) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -225,10 +204,7 @@ def unblock_user_request(username): if token is not None and g.user.auth == AuthLeval.ADMIN: try: status = unblock_user(username) - if status == "success": - resp = Response(response='{"status": "'+ status + '"}', status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response='{"status":"'+ status + '"}', status= (200 if status == "success" else 400)) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -246,10 +222,7 @@ def get_downloads_user_request(limit): if token is not None : try: status = get_downloads_user(g.user.userName, int(limit)) - if not isinstance(status, str): - resp = Response(response=json.dumps(status), status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response=((json.dumps(status),status=200) if not isinstance(status, str) else ('{"error":"' + status + '"}', status=400))) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token @@ -267,10 +240,7 @@ def get_topten_heaviest_users(): if token is not None and g.user.auth == AuthLeval.ADMIN: try: status = get_heavy_users() - if not isinstance(status, str): - resp = Response(response=json.dumps(status), status=200) - else: - resp = Response(response='{"error":"' + status + '"}', status=400) + resp = Response(response=((json.dumps(status),status=200) if not isinstance(status, str) else ('{"error":"' + status + '"}', status=400))) except Exception as e: resp = Response(response='{"error":"' + e.message + '"}', status=400) resp.headers['token'] = token