-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
292 lines (254 loc) · 9.74 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# from datetime import datetime
import os
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class TinyWebDB(db.Model):
__tablename__ = 'tinywebdb'
tag = db.Column(db.String, primary_key=True, nullable=False)
value = db.Column(db.String, nullable=False)
# The 'date' column is needed for deleting older entries, so not really required
# date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
db.create_all()
db.session.commit()
@app.route('/')
def main_method():
return 'AsteroidDB works perfectly!'
# -------------------------
# Store Value
# - Store a value by using tag.
# -------------------------
@app.route('/store', methods=['POST'])
def store_a_value():
tag = request.form['tag']
value = request.form['value']
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if tag:
if tag == 'dbpass':
return return_error('Not possible to do any action to password record!')
else:
# --------------------
if getpassword:
password = request.form['pass']
if password != getpassword.value:
return return_error('Wrong password!')
# --------------------
existing_tag = TinyWebDB.query.filter_by(tag=tag).first()
if existing_tag:
existing_tag.value = value
db.session.commit()
else:
data = TinyWebDB(tag=tag, value=value)
db.session.add(data)
db.session.commit()
return jsonify(action="STORED", tag=tag, value=value)
return return_error('Tag is not specified!')
# -------------------------
# Get Value
# - Get the value from tag.
# -------------------------
@app.route('/get', methods=['POST'])
def get_value():
tag = request.form['tag']
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if tag:
if tag == 'dbpass':
return return_error('Not possible to do any action to password record!')
else:
# --------------------
if getpassword:
password = request.form['pass']
if password != getpassword.value:
return return_error('Wrong password!')
# --------------------
value = TinyWebDB.query.filter_by(tag=tag).first().value
return jsonify(action="GOT", tag=tag, value=value)
return return_error('Not found the tag!')
# -------------------------
# Get All Data
# - Return everything from database. This method doesn't include password record tag and value.
# -------------------------
@app.route('/auth/data', methods=['POST'])
def get_data():
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if getpassword:
# --------------------
password = request.form['pass']
if password != getpassword.value:
return return_error('Wrong password!')
# --------------------
tags = TinyWebDB.query.all()
datalist = []
for tg in tags:
if tg.tag != 'dbpass':
datalist.append([tg.tag, tg.value])
return jsonify(action="DATA", data=datalist)
return return_error('You need to set a password first to use this feature!')
# -------------------------
# Get All Tags
# - Return all tags from database. This method doesn't include password record tag.
# -------------------------
@app.route('/getall', methods=['POST'])
def get_all():
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if getpassword:
# --------------------
password = request.form['pass']
if password != getpassword.value:
return return_error('Wrong password!')
# --------------------
tags = TinyWebDB.query.all()
taglist = []
for tg in tags:
if tg.tag != 'dbpass':
taglist.append(tg.tag)
return jsonify(action="TAGS", tag=taglist)
# -------------------------
# Delete Record
# - Delete a record from tag.
# -------------------------
@app.route('/delete', methods=['POST'])
def delete_entry():
tag = request.form['tag']
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if tag:
if tag == 'dbpass':
return return_error('Not possible to do any action to password record!')
else:
# --------------------
if getpassword:
password = request.form['pass']
if password != getpassword.value:
return return_error('Wrong password!')
# --------------------
deleted = TinyWebDB.query.filter_by(tag=tag).first()
db.session.delete(deleted)
db.session.commit()
return jsonify(action="DELETED", tag=tag)
return return_error('Not found the tag!')
# -------------------------
# Format Database
# - Deletes every record from database, and remove password protection.
# -------------------------
@app.route('/format', methods=['POST'])
def delete_all():
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if getpassword:
# --------------------
password = request.form['pass']
if password != getpassword.value:
return return_error('Wrong password!')
# --------------------
try:
count = db.session.query(TinyWebDB).delete()
db.session.commit()
return jsonify(action="FORMATTED", count=count)
except:
db.session.rollback()
return return_error('Something went wrong while performing this action.')
# -------------------------
# Set/Change Password
# - If you set a password, you need to type a password when you modify the database.
# - The password will be saved in the same table along with other data called "dbpass".
# - If you forgot the password, there is no way to recover it.
# -------------------------
@app.route('/auth/password', methods=['POST'])
def set_key():
newpassword = request.form['newpass']
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if newpassword:
if getpassword:
oldpassword = request.form['oldpass']
if getpassword.value == oldpassword:
getpassword.value = newpassword
db.session.commit()
return jsonify(action="CHANGED PASSWORD", oldpass=oldpassword, newpass=newpassword)
else:
return return_error('Wrong old password!')
else:
data = TinyWebDB(tag='dbpass', value=newpassword)
db.session.add(data)
db.session.commit()
return jsonify(action="SET PASSWORD", newpass=newpassword)
return return_error('No new password is specified!')
# -------------------------
# Remove Password
# - If you type your current password, requests won't require pass parameter anymore. And your password will be deleted.
# -------------------------
@app.route('/auth/unlock', methods=['POST'])
def remove_key():
password = request.form['pass']
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if getpassword:
if getpassword.value == password:
deleted = TinyWebDB.query.filter_by(tag='dbpass').first()
db.session.delete(deleted)
db.session.commit()
return jsonify(action="DELETED PASSWORD", password=password)
else:
return return_error('Wrong password!')
return return_error('You need to set a password first to use this feature!')
# -------------------------
# Is Password True?
# - Useful for applications. Returns 'true' if password is correct. Otherwise; 'false'.
# -------------------------
@app.route('/istrue', methods=['POST'])
def is_true():
password = request.form['pass']
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if getpassword:
# --------------------
if password != getpassword.value:
return jsonify(action="IS CORRECT",result=False)
# --------------------
return jsonify(action="IS CORRECT",result=True)
# -------------------------
# Count All Records
# - Returns a number that tells you how many records there are in database.
# -------------------------
@app.route('/count')
def count_all():
tags = TinyWebDB.query.all()
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
resl = len(tags)
if getpassword:
resl = resl - 1
return jsonify(action="COUNT", count=resl)
# -------------------------
# Version
# - Shows the tagged version of AsteroidDB instance.
# -------------------------
@app.route('/version')
def version():
return jsonify(action="VERSION", result="1.1")
# -------------------------
# Is Locked?
# - Gives information about current lock status.
# -------------------------
@app.route('/islocked')
def is_locked():
getpassword = TinyWebDB.query.filter_by(tag='dbpass').first()
if getpassword:
return jsonify(action="IS LOCKED",result=True)
else:
return jsonify(action="IS LOCKED",result=False)
@app.errorhandler(405)
def method_not_allowed(e):
return jsonify(action="ERROR",result="Method is not allowed!"), 405
@app.errorhandler(404)
def not_found(e):
return jsonify(action="ERROR",result="The requested URL was not found on the AsteroidDB instance!"), 404
@app.errorhandler(500)
def internal_error(error):
db.session.rollback()
return jsonify(action="ERROR",result="Internal server error!"), 500
# Returns error.
def return_error(message):
response = jsonify(action="ERROR",result=message)
response.status_code = 400
return response
if __name__ == '__main__':
app.run()