-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
55 lines (50 loc) · 1.87 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
from flask import Flask, request, send_file, session, render_template, jsonify, flash, session
import os
import json
from collections import defaultdict
from sqlalchemy.dialects import registry
registry.register("cockroachdb", "cockroachdb.sqlalchemy.dialect",
"CockroachDBDialect")
import firebase_admin
from firebase_admin import credentials, db
from history_actions import HistoryActions
app = Flask(__name__)
app.secret_key = "asdfasfdasfdsafasddfsadfasdfsadfdas"
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
LIMIT = 10
database = HistoryActions(os.environ['DATABASE_URL'])
cred = credentials.Certificate(json.loads(os.environ['FIREBASE_KEY']))
firebase_admin.initialize_app(cred, {'databaseURL': 'https://sketch2code-877ea-default-rtdb.firebaseio.com/'})
userHistory = defaultdict(list)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/history', methods=['GET', 'POST'])
def history():
ip = request.environ['REMOTE_ADDR']
print("ip====",ip)
if request.method == 'GET':
try:
res = db.reference('/ip').get()
temp = []
for v in res:
temp.append(res[v])
res = temp
except:
res = []
return jsonify({'ip_address': res}), 200
elif request.method == 'POST':
title = request.get_json().get('title', '')
code = request.get_json().get('code', '')
db.reference()
db.reference('/ip').push({'title': title, 'code': code})
sz = len(db.reference("/ip").get())
if sz > LIMIT:
diff = sz - LIMIT
keys = db.reference("/ip").get(shallow=True)
for v in keys:
if diff == 0: break
db.reference("/ip/" + v).delete()
return jsonify(success=True), 200
if __name__ == '__main__':
app.run(debug=True) #debug=True so that caching doesn't occur