-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
72 lines (57 loc) · 2.58 KB
/
utils.py
File metadata and controls
72 lines (57 loc) · 2.58 KB
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
import os
import jdatetime
import re
from app import app
from flask import redirect, url_for, flash
from flask_login import current_user
from functools import wraps
from werkzeug.utils import secure_filename
#=================================== CREATE EMAIL ADMIN VALID ===============================================
# regex = re.compile(r"([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\"([]!#-[^-~ \t]|(\\[\t -~]))+\")@([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\[[\t -Z^-~]*])")
regex = re.compile(r"admin@([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\[[\t -Z^-~]*])")
def isvalid_email_admin(email):
return False if re.fullmatch(regex, email) else True
#========================================== END =============================================================
#====================================== CREATE ADMIN REQUIRED ===============================================
def admin_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if current_user.role == 0:
return f(*args, **kwargs)
else:
# flash("You need to be an admin to view this page.", 'danger')
return redirect(url_for('admin.index'))
return wrap
#========================================== END =============================================================
def allow_extension(filename):
ext = filename[-3:]
extension = {'png', 'jpg'}
if not ext in extension:
return False
return True
#====================================== UPLOADING IMAGE ===============================================
def save_image(image, app_name, url, form):
if image.filename == '':
flash('you not select a image properly, please try again', 'warning')
return redirect(url_for(url, form=form))
if image:
print('image is -----> ', image)
filename = image.filename
file_secure = secure_filename(filename)
if not allow_extension(file_secure):
flash('this extension for image file is not allowed', 'warning')
return redirect(url_for(url, form=form))
folder = os.path.join(app.config['UPLOAD_DIR'], app_name, str(jdatetime.date.today()))
print('folder is -----> ', folder)
try:
os.makedirs(folder)
except Exception as e:
# flash(f'error {e} is happened, please try again', 'warning')
pass
finally:
file = os.path.join(folder, file_secure)
print('file is ----> ', file)
image.save(file)
flash('your image is uploaded successfully', 'success')
return True
return False