-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
186 lines (156 loc) · 6.03 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from flask import Flask, render_template, redirect, session,request,jsonify,send_file
from flask_wtf import FlaskForm
from wtforms import SubmitField, StringField, PasswordField
import db
import time
import os
import math
app = Flask(__name__)
app.config['SECRET_KEY'] = "find@me1290"
plans={"free":500,"pro":1000,"gold":5000,"platinum":10000}
class LoginForm(FlaskForm):
email = StringField("Email")
password = PasswordField("Password")
submit = SubmitField("Log in")
class RegisterForm(FlaskForm):
email = StringField("Email")
password = PasswordField("Password")
cpassword = PasswordField("Password")
submit = SubmitField("Register")
@app.route("/authUser", methods=['POST'])
def authUser():
form = LoginForm()
email = str(form.email.data)
password = str(form.password.data)
status=db.login_user(email,password)
if status:
session['logged_in']=True
session['email']=email
return redirect('profile')
else:
error="Invalid email or password"
form = LoginForm()
regForm = RegisterForm()
return render_template("members.html",error=error,form=form, rform=regForm)
@app.route("/registerUser", methods=['POST'])
def registerUser():
status=""
data = request.get_json()
if data['password']!=data['cpassword']:
status="password and confirm password does not match"
else:
status=db.register_user(data['email'],data['password'])
time.sleep(1) ## to see the spinner loading add this or remove this for fast response
try:
base_dir=os.path.abspath(os.path.dirname(__file__))
os.mkdir(os.path.join(base_dir,"static","files",data['email']))
except Exception as e:
print(str(e))
response = {'status': status}
return jsonify(response)
@app.route("/Logoutuser",methods=['POST'])
def logout():
data=request.get_json()
email=data['email']
status="error"
response={}
if email==session['email']:
session.pop('logged_in')
session.pop('email')
status="success"
response["logged out"]=status
return jsonify(response)
response['logged out']=status
return jsonify(response)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/members", methods=['POST', 'GET'])
def members():
try:
if session['logged_in']:
return redirect('profile')
except Exception as e:
print("Some error")
form = LoginForm()
regForm = RegisterForm()
error=""
return render_template("members.html", form=form, rform=regForm,error=error)
@app.route("/pricing")
def pricing():
return render_template("pricing.html")
@app.route("/profile")
def profile():
if 'logged_in' in session:
plan_name=db.get_plan_type(session['email'])
plan_size=plans[plan_name]
print(plan_size)
try:
base_dir=os.path.abspath(os.path.dirname(__file__))
user_files=os.listdir(os.path.join(base_dir,"static","files",session['email']))
user_files_size=[]
utilized_size=0.0
for items in user_files:
size1=os.path.getsize(os.path.join(base_dir,"static","files",session['email'],items))
size=str(round(size1/(1024*1024),3))
user_files_size.append([items,size])
utilized_size+=float(size)
except Exception as e:
print(str(e))
session['utilized_size']=utilized_size
session['plan_size']=plan_size
plan_usage_percentage=math.ceil((int(utilized_size)/plan_size)*100)
return render_template("profile.html",files=user_files_size,utilized_size=math.ceil(utilized_size),plan_usage_percentage=plan_usage_percentage,plan_size=plan_size,plan_name=plan_name)
else:
return redirect('members')
@app.route("/uploadFile",methods=['POST','GET'])
def upload_file():
response={}
try:
base_dir=os.path.abspath(os.path.dirname(__file__))
file_size=float(request.form.get('f_size'))
file_size1=str(round(file_size/(1024*1024),3))
f_size=float(file_size1)
if (session['utilized_size']+f_size)>session['plan_size']:
response['status']="Limit crossed"
return jsonify(response)
f=request.files['file']
f.save(os.path.join(base_dir,"static","files",session['email'],f.filename))
response={'status':''+str(f.filename)+str(" was succesfully uploaded")}
return jsonify(response)
except Exception as e:
response['status']="Server error: "+str(e)
return jsonify(response)
@app.route("/downloadfile/<filename>",methods=['GET','POST'])
def download_file(filename):
base_dir=os.path.abspath(os.path.dirname(__file__))
file_path=os.path.join(base_dir,"static","files",session['email'],filename)
return send_file(file_path, as_attachment=True)
@app.route("/deletefile",methods=['GET','POST'])
def delete_file():
data=request.get_json()
file_name=data['filename']
respone={"status":"An error occurred while deleting"}
if 'logged_in' in session:
base_dir=os.path.abspath(os.path.dirname(__file__))
try:
file_path=os.path.join(base_dir,"static","files",session['email'],file_name)
os.remove(file_path)
respone['status']=""+str(file_name)+" deleted successfully"
return jsonify(respone)
except Exception as e:
respone['status']="Error "+str(e)
return jsonify(respone)
@app.route("/changePlan",methods=['POST'])
def change_plan():
response={"status":"error"}
try:
email=session['email']
data=request.get_json()
plan_name=data['plan_name']
status=db.change_plan(email,plan_name)
response['status']=status
return jsonify(response)
except Exception as e:
response['status']=str(e)
return jsonify(response)