Skip to content

Commit 14790ec

Browse files
committed
major changes
1 parent 7e99c91 commit 14790ec

File tree

13 files changed

+689
-0
lines changed

13 files changed

+689
-0
lines changed

Database.db

16 KB
Binary file not shown.

app.py

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
# Imports necessary libraries
2+
from flask import Flask, render_template, request, redirect, jsonify, make_response
3+
from flask_limiter import Limiter
4+
from flask_limiter.util import get_remote_address
5+
from werkzeug.utils import secure_filename
6+
from werkzeug.security import generate_password_hash, check_password_hash
7+
import os
8+
import json
9+
import uuid
10+
from datetime import datetime, timedelta
11+
from functools import wraps
12+
from flask_sqlalchemy import SQLAlchemy
13+
# jwt imports
14+
import jwt
15+
16+
# Define the app
17+
app = Flask(__name__)
18+
19+
20+
# secret key
21+
app.config['SECRET_KEY'] = 'Internetix' #didn't used env because of convinience
22+
23+
24+
# database name
25+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///Database.db'
26+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
27+
28+
# intializing sqlalchemy
29+
db = SQLAlchemy(app)
30+
31+
# databse model
32+
33+
34+
class User(db.Model):
35+
id = db.Column(db.Integer, primary_key=True)
36+
public_id = db.Column(db.String(50), unique=True)
37+
name = db.Column(db.String(100))
38+
email = db.Column(db.String(70), unique=True)
39+
password = db.Column(db.String(80))
40+
41+
# limiter configs
42+
43+
44+
limiter = Limiter(app, key_func=get_remote_address,
45+
default_limits=['5 per minute'])
46+
47+
48+
# Jwt decorator
49+
50+
def token_required(f):
51+
@wraps(f)
52+
def decorated(*args, **kwargs):
53+
token = None
54+
# cheking token exist or not
55+
56+
if 'x-access-token' in request.headers:
57+
token = request.headers['x-access-token']
58+
59+
if not token:
60+
return jsonify({'message': 'please provide token'}), 401
61+
62+
try:
63+
# decoding token
64+
data = jwt.decode(token, app.config['SECRET_KEY'],algorithms=["HS256"])
65+
current_user = User.query.filter_by(
66+
public_id=data['public_id']).first()
67+
68+
except Exception as e:
69+
return jsonify({
70+
'message': 'Token invalid',
71+
'error': e
72+
}), 401
73+
74+
return f(current_user, *args, **kwargs)
75+
76+
return decorated
77+
78+
79+
@app.get('/user')
80+
@token_required
81+
def get_all_users(current_user):
82+
# querying the database
83+
# for all the entries in it
84+
users = User.query.all()
85+
86+
# converting the query objects
87+
# to list of jsons
88+
output = []
89+
for user in users:
90+
# appending the user data json
91+
# to the response list
92+
output.append({
93+
'public_id': user.public_id,
94+
'name': user.name,
95+
'email': user.email
96+
})
97+
98+
return jsonify({'users': output})
99+
100+
101+
@app.post('/login')
102+
def login():
103+
# creates dictionary of form data
104+
auth = request.form
105+
print(auth)
106+
107+
if not auth or not auth.get('email') or not auth.get('password'):
108+
# returns 401 if any email or / and password is missing
109+
return make_response(
110+
'Could not verify',
111+
401,
112+
{'WWW-Authenticate': 'Basic realm ="Login required !!"'}
113+
)
114+
115+
user = User.query\
116+
.filter_by(email=auth.get('email'))\
117+
.first()
118+
119+
if not user:
120+
# returns 401 if user does not exist
121+
return make_response(
122+
'Could not verify',
123+
401,
124+
{'WWW-Authenticate': 'Basic realm ="User does not exist !!"'}
125+
)
126+
127+
if check_password_hash(user.password, auth.get('password')):
128+
# generates the JWT Token
129+
token = jwt.encode({
130+
'public_id': user.public_id,
131+
'exp': datetime.utcnow() + timedelta(minutes=30)
132+
}, app.config['SECRET_KEY'])
133+
134+
return make_response(jsonify({'token': token}), 201)
135+
# returns 403 if password is wrong
136+
return make_response(
137+
'Could not verify',
138+
403,
139+
{'WWW-Authenticate': 'Basic realm ="Wrong Password !!"'}
140+
)
141+
142+
143+
# signup route
144+
@app.post('/signup')
145+
def signup():
146+
# creates a dictionary of the form data
147+
data = request.form
148+
# gets name, email and password
149+
name, email = data.get('name'), data.get('email')
150+
password = data.get('password')
151+
152+
# checking for existing user
153+
user = User.query\
154+
.filter_by(email=email)\
155+
.first()
156+
if not user:
157+
# database ORM object
158+
user = User(
159+
public_id=str(uuid.uuid4()),
160+
name=name,
161+
email=email,
162+
password=generate_password_hash(password)
163+
)
164+
# insert user
165+
db.session.add(user)
166+
db.session.commit()
167+
168+
return make_response('Successfully registered.', 201)
169+
else:
170+
# returns 202 if user already exists
171+
return make_response('User already exists. Please Log in.', 202)
172+
173+
174+
# Get a welcoming message once you start the server.
175+
@app.route('/')
176+
def home():
177+
return render_template('home.html')
178+
179+
@app.route('/takepictures')
180+
def takepictures():
181+
return render_template('takepictures.html')
182+
183+
184+
@app.route('/showimage/<filename>')
185+
def showimage(filename):
186+
# return render_template('showimage.html')
187+
return f"FILE NAME : <strong> {filename} </strong>"
188+
189+
190+
@app.post('/upload')
191+
@limiter.limit("5/minute")
192+
def upload_file():
193+
try:
194+
if(request.files['my_file']):
195+
try:
196+
file = request.files['my_file']
197+
upload_directory = f"{os.getcwd()}/uploads"
198+
filepath = os.path.exists(upload_directory)
199+
if not filepath:
200+
os.mkdir(upload_directory)
201+
file.save(
202+
f"{upload_directory}/{secure_filename(file.filename)}")
203+
else:
204+
# return f"{url :/showimage/{file.filename}'}"
205+
file.save(
206+
f"{upload_directory}/{secure_filename(file.filename)}")
207+
208+
return json.dumps({"url": f"/showimage/{file.filename}"})
209+
except Exception as e:
210+
return json.dumps("{message: something went wrong}")
211+
else:
212+
return json.dumps("{message:provide a file}")
213+
214+
except Exception as e:
215+
return json.dumps(f'{e}, Provide a file')
216+
217+
@app.post('/uploadWithAuth')
218+
@token_required
219+
@limiter.limit("5/minute")
220+
def upload_file_with_auth(current_user):
221+
try:
222+
if(request.files['my_file']):
223+
try:
224+
file = request.files['my_file']
225+
upload_directory = f"{os.getcwd()}/uploads"
226+
filepath = os.path.exists(upload_directory)
227+
if not filepath:
228+
os.mkdir(upload_directory)
229+
file.save(
230+
f"{upload_directory}/{secure_filename(file.filename)}")
231+
else:
232+
# return f"{url :/showimage/{file.filename}'}"
233+
file.save(
234+
f"{upload_directory}/{secure_filename(file.filename)}")
235+
236+
return json.dumps({"url": f"/showimage/{file.filename}"})
237+
except Exception as e:
238+
return json.dumps("{message: something went wrong}")
239+
else:
240+
return json.dumps("{message:provide a file}")
241+
242+
except Exception as e:
243+
return json.dumps(f'{e}, Provide a file')
244+
245+
246+
@app.post('/capture')
247+
def capture():
248+
file = request.files['capture_img']
249+
if(file):
250+
upload_directory = f"{os.getcwd()}/static/imguploads"
251+
filepath = os.path.exists(upload_directory)
252+
if not filepath:
253+
os.mkdir(upload_directory)
254+
file.save(f"{upload_directory}/{secure_filename(file.filename)}")
255+
else:
256+
file.save(f"{upload_directory}/{secure_filename(file.filename)}")
257+
return jsonifyk({"message":"image uploaded"})
258+
259+
260+
@app.get('/showallpictures')
261+
def showallpictures():
262+
list_of_images = os.listdir('static/imguploads')
263+
url_list = []
264+
for images in list_of_images:
265+
url = f"imguploads/{images}"
266+
url_list.append(url)
267+
268+
return render_template('getallpictures.html',src=url_list)
269+
270+
# If the file is run directly,start the app.
271+
if __name__ == '__main__':
272+
app.run()

requirements.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cffi
2+
click
3+
commonmark
4+
cryptography
5+
Deprecated
6+
Flask
7+
Flask-Limiter
8+
flask-localtunnel
9+
Flask-SQLAlchemy
10+
greenlet
11+
itsdangerous
12+
Jinja2
13+
jwt
14+
limits
15+
MarkupSafe
16+
packaging
17+
pip
18+
pycparser
19+
Pygments
20+
PyJWT
21+
pyparsing
22+
rich
23+
setuptools
24+
SQLAlchemy
25+
typing_extensions
26+
uuid
27+
Werkzeug
28+
wrapt

static/css/main.css

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
@import url('https://fonts.googleapis.com/css2?family=Uchen&display=swap');
2+
*{
3+
margin:0;
4+
padding: 0;
5+
}
6+
7+
body{
8+
background: rgb(188, 232, 247);
9+
font-family: Georgia, 'Times New Roman', Times, serif;
10+
font-family: 'Uchen', serif;
11+
width: 100vw;
12+
height: 100vh;
13+
}
14+
15+
.getallpic{
16+
max-width: 300px;
17+
max-height: 200px;
18+
}
19+
20+
Button {
21+
background-color: #4CAF50; /* Green */
22+
border: none;
23+
color: white;
24+
padding: 15px 32px;
25+
text-align: center;
26+
text-decoration: none;
27+
display: inline-block;
28+
font-size: 16px;
29+
cursor: pointer;
30+
}
31+
32+
Button:hover{
33+
background-color: black;
34+
35+
36+
}
37+
38+
label{
39+
font-size: medium;
40+
}
41+
42+
input[type=button], input[type=submit], input[type=file],#file-upload-button {
43+
background-color: #04AA6D;
44+
border: none;
45+
color: white;
46+
padding: 16px 32px;
47+
text-decoration: none;
48+
margin: 4px 2px;
49+
cursor: pointer;
50+
}
51+
52+
label{
53+
display: block;
54+
text-align: center;
55+
}
56+
57+
.form-wrapper{
58+
width:100vw;
59+
height: 100vh;
60+
display: flex;
61+
align-items: center;
62+
justify-content: center;
63+
flex-direction: column;
64+
65+
}
66+
67+
input{
68+
display: block;
69+
}
70+
71+
.form {
72+
display: flex;
73+
align-items: center;
74+
justify-content: center;
75+
flex-direction: column;
76+
77+
}

static/css/showimg.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
body{
4+
5+
background: #000;
6+
}

0 commit comments

Comments
 (0)