-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
103 lines (87 loc) · 3.18 KB
/
main.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
import sys
import os
import shutil
from flask import Flask, render_template, request, redirect
from flask import *
from flask import send_file, send_from_directory, safe_join, abort
from copy import copy
from main import app as Application
import barcode
from barcode.charsets import code39
from barcode.writer import ImageWriter
app = Flask(__name__, template_folder='templates')
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.after_request
def add_header(response):
response.cache_control.max_age = 100
return response
@app.route('/', methods=['GET'])
def root():
path = os.path.join(app.root_path, 'static', 'image.png')
delete_path = os.path.join(app.root_path, "static")
error = None
for i in os.listdir(delete_path):
if i.startswith('image'): # not to remove other images
os.remove(path)
return render_template("index.html", error=error)
def is_nric_valid(nric):
nricCheckDigits = 'JZIHGFEDCBA'
finCheckDigits = 'XWUTRQPNMLK'
weights = [2, 7, 6, 5, 4, 3, 2]
if len(nric) != 9:
return False
firstChar = nric[0].upper()
digits = nric[1:8]
lastChar = nric[8].upper()
if firstChar not in "STFG" or not digits.isdigit():
return False
total = 0
if firstChar in "GT":
total += 4
for i in range(7):
total += int(digits[i]) * weights[i]
if firstChar in "ST":
return nricCheckDigits[total % 11] == lastChar
return finCheckDigits[total % 11] == lastChar
@app.route('/generate', methods=['GET'])
def generate():
if "nric" in request.args:
nric = request.args.get('nric')
nric = str(nric)
path = os.path.join(app.root_path, 'static', 'image.png')
delete_path = os.path.join(app.root_path, "static")
filename = "image"
for i in os.listdir(delete_path):
if i.startswith('image'): # not to remove other images
os.remove(path)
if nric == '':
error = '<Empty Input>'
return render_template("index.html", error=error)
elif is_nric_valid(nric) == True:
code39 = barcode.Code39(nric, writer=ImageWriter(), add_checksum=False)
os.chdir(delete_path)
image = code39.save(filename)
# a = os.path.abspath(image)
# b = delete_path
# shutil.move(a,b)
return render_template("barcode.html", nric=nric)
else:
error = '<Invalid NRIC>'
check = True
return render_template("index.html", error=error)
else:
error = '<Empty Input>'
return render_template("index.html", error=error)
if __name__ == '__main__':
app.run(debug=False, use_reloader=True)