Added rate limiting on api update requests.
This commit is contained in:
parent
a78bd4257f
commit
fe5596cd64
@ -67,7 +67,7 @@ $("#get-help").click (e) ->
|
||||
|
||||
$("#api-refresh").click (e) ->
|
||||
e.preventDefault();
|
||||
$.post("{{ url_for('refresh_api') }}");
|
||||
$.post("/refresh");
|
||||
refreshQuestion()
|
||||
|
||||
$ ->
|
||||
|
@ -90,7 +90,7 @@
|
||||
|
||||
$("#api-refresh").click(function(e) {
|
||||
e.preventDefault();
|
||||
$.post("{{ url_for('refresh_api') }}");
|
||||
$.post("/refresh");
|
||||
return refreshQuestion();
|
||||
});
|
||||
|
||||
|
@ -6,7 +6,7 @@ from flask.ext.wtf import Form
|
||||
import requests
|
||||
from wtforms import StringField
|
||||
from wtforms.validators import DataRequired
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_pyfile('config.py')
|
||||
@ -93,12 +93,15 @@ class User(db.Model):
|
||||
self.vocabulary = json.dumps(items)
|
||||
|
||||
def update_all(self):
|
||||
self.parse_radicals_and_userdata()
|
||||
self.parse_kanji()
|
||||
self.parse_vocabulary()
|
||||
self.last_updated = datetime.utcnow()
|
||||
db.session.add(self)
|
||||
db.session.commit()
|
||||
if (datetime.now() - self.last_updated) > timedelta(hours=1):
|
||||
self.parse_radicals_and_userdata()
|
||||
self.parse_kanji()
|
||||
self.parse_vocabulary()
|
||||
self.last_updated = datetime.utcnow()
|
||||
db.session.add(self)
|
||||
db.session.commit()
|
||||
else:
|
||||
raise ValueError('Cannot refresh now, try again later.')
|
||||
|
||||
|
||||
@login_manager.user_loader
|
||||
@ -173,8 +176,11 @@ def get_items():
|
||||
|
||||
@app.route('/refresh', methods=['POST'])
|
||||
def refresh_api():
|
||||
current_user.update_all()
|
||||
return "202"
|
||||
try:
|
||||
current_user.update_all()
|
||||
return "Refresh Successful", 202
|
||||
except ValueError as err:
|
||||
return str(err), 500
|
||||
|
||||
|
||||
@app.route('/logout')
|
||||
|
Loading…
Reference in New Issue
Block a user