-
Notifications
You must be signed in to change notification settings - Fork 12
/
themehandler.py
150 lines (129 loc) · 4.75 KB
/
themehandler.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
'''
Copyright (C) 2014 Muhd Amirul Ashraf bin Mohd Fauzi <[email protected]>
This file is part of Automatic IIUM Schedule Formatter.
Automatic IIUM Schedule Formatter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Automatic IIUM Schedule Formatter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Automatic IIUM Schedule Formatter. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import re
import recaptcha.client.captcha as recapt
recaptcha=recapt
from staticsettings import RECAPTCHA_KEY,RECAPTCHA_PUBLIC_KEY,DEBUG,DEFAULTDATA
try:
import JSON
except ImportError:
import json as JSON
import logging
from flask import url_for,request,render_template
from bootstrap import app
from models import Theme
def list_themes():
if(request.args.get("submit",None)):
arg={}
arg["RECAPTCHA_PUBLIC_KEY"]=RECAPTCHA_PUBLIC_KEY
arg["RECAPTCHA_KEY"]=RECAPTCHA_KEY
return render_template('submittheme.html',**arg)
elif(request.args.get("name",None)):
themename=request.args.get("name")
themedata=Theme.get_by_key_name(themename)
if(themedata==None):
return 'Theme not found',404
thedict=dict()
thedict["name"]=themedata.name
thedict["submitter"]=themedata.submitter
thedict["email"]=themedata.email
thedict["data"]=JSON.loads(themedata.data)
return JSON.dumps(thedict)
else:
themelist=Theme.all()
return render_template( 'themegallery.html' , themes=themelist)
def save_theme():
error=False
errormessage=None
themename=request.form.get("name")
submitter=request.form.get("submitter",None)
template=request.form.get("template",None)
data=request.form.get("data",None)
email=request.form.get("email",None)
rendered=request.form.get("rendered",None)
if(not error):
if(themename==None):
error=True
errormessage="You must enter a theme name"
if(not error):
recaptchallange=request.form.get("recaptcha_challenge_field")
if(not error):
recaptresponse=request.form.get("recaptcha_response_field")
if(not error):
if(submitter==None or submitter==""):
error=True
errormessage="Please enter your name"
if(not error):
if(email==None or email==""):
error=True
errormessage="Please enter your email"
if(not error and not re.match(r"[^@]+@[^@]+\.[^@]+",email)):
error=True
errormessage="Please enter a valid email address"
if(not error):
if(data==None or data==""):
error=True
errormessage="The no data found"
if(not error):
themedata=Theme.get_by_key_name(themename)
if(themedata):
error=True
errormessage="A theme with the same name already exist"
if(not DEBUG):
validation=recaptcha.submit(recaptchallange,recaptresponse,RECAPTCHA_KEY,request.remote_addr)
if(not validation.is_valid):
error=True
errormessage="Please reenter the recaptcha."
if((DEBUG or validation.is_valid) and not error):
newtheme=Theme()
newtheme.name=themename
newtheme.submitter=submitter
newtheme.email=email
newtheme.data=data
newtheme.counter=0
newtheme.rendered=rendered
newtheme.generate_photo()
newtheme.put()
return 'success',200
elif(not error):
return 'Something went wrong',400
else:
return JSON.dumps({ "error": errormessage }),400
@app.route('/scheduleformatter/themes/',methods=['GET'])
def themes():
themelist=Theme.all()
return JSON.dumps([x.simple_to_hash() for x in themelist.all()])
@app.route('/themegallery/',methods=['GET','POST'])
def themegallery():
if(request.method=='GET'):
return list_themes()
elif(request.method=='POST'):
return save_theme()
@app.route('/screenshot/')
def screenshot():
themename=request.args.get("themename",None)
if(not themename):
return 'No theme selected',404
themedata=Theme.get_by_key_name(themename)
if(not themedata):
return "The theme '"+themename+"' does not exist.",404
if(themedata.rendered):
return themedata.rendered
else:
return "<h1>Sorry Thumbnail is not avalable</h1>"
@app.route('/defaultdata/')
def defaultdata():
return DEFAULTDATA