Skip to content

Commit 74d3f96

Browse files
authored
Merge pull request LMFDB#7125 from roed-math/remove-dead-ajax-helpers
Remove obsolete callback-AJAX machinery
2 parents f86285f + 22d8a89 commit 74d3f96

2 files changed

Lines changed: 1 addition & 122 deletions

File tree

lmfdb/app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,6 @@ def WhiteListedRoutes():
788788
'alive',
789789
'api',
790790
'bigpicture',
791-
'callback_ajax',
792791
'citation',
793792
'contact',
794793
'editorial-board',

lmfdb/utils/utilities.py

Lines changed: 1 addition & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import cmath
22
import math
33
import os
4-
import random
54
import re
65
import tempfile
7-
import time
86
from copy import copy
97
from itertools import islice
108
from types import GeneratorType
@@ -35,7 +33,7 @@
3533
from sage.misc.functional import round
3634
from sage.structure.element import Element
3735

38-
from lmfdb.app import app, is_beta, is_debug_mode, _url_source
36+
from lmfdb.app import is_beta, is_debug_mode, _url_source
3937

4038

4139
def integer_divisors(n):
@@ -802,124 +800,6 @@ def flash_success(msg, *args):
802800
flash(Markup(msg % tuple("<span style='color:black'>%s</span>" % escape(x) for x in args)), "success")
803801

804802

805-
################################################################################
806-
# Ajax utilities
807-
################################################################################
808-
809-
# LinkedList is used in Ajax below
810-
class LinkedList():
811-
__slots__ = ('value', 'next', 'timestamp')
812-
813-
def __init__(self, value, nxt):
814-
self.value = value
815-
self.next = nxt
816-
self.timestamp = time.time()
817-
818-
def append(self, value):
819-
self.next = LinkedList(value, self)
820-
return self.next
821-
822-
823-
class AjaxPool():
824-
def __init__(self, size=1e4, expiration=3600):
825-
self._size = size
826-
self._key_list = self._head = LinkedList(None, None)
827-
self._expiration = expiration
828-
self._all = {}
829-
830-
def get(self, key, value=None):
831-
return self._all.get(key, value)
832-
833-
def __contains__(self, key):
834-
return key in self._all
835-
836-
def __setitem__(self, key, value):
837-
self._key_list = self._key_list.append(key)
838-
self._all[key] = value
839-
840-
def __getitem__(self, key):
841-
res = self._all[key]
842-
self.purge()
843-
return res
844-
845-
def __delitem__(self, key):
846-
del self._all[key]
847-
848-
def pop_key(self):
849-
head = self._head
850-
if head.next is None:
851-
return None
852-
else:
853-
key = head.value
854-
self._head = head.next
855-
return key
856-
857-
def purge(self):
858-
if self._size:
859-
while len(self._all) > self._size:
860-
key = self.pop_key()
861-
if key in self._all:
862-
del self._all[key]
863-
if self._expiration:
864-
oldest = time.time() - self._expiration
865-
while self._head.timestamp < oldest:
866-
key = self.pop_key()
867-
if key in self._all:
868-
del self._all[key]
869-
870-
871-
pending = AjaxPool()
872-
def ajax_url(callback, *args, **kwds):
873-
if '_ajax_sticky' in kwds:
874-
_ajax_sticky = kwds.pop('_ajax_sticky')
875-
else:
876-
_ajax_sticky = False
877-
if not isinstance(args, tuple):
878-
args = args,
879-
nonce = hex(random.randint(0, 1 << 128))
880-
pending[nonce] = callback, args, kwds, _ajax_sticky
881-
return url_for('ajax_result', id=nonce)
882-
883-
884-
@app.route('/callback_ajax/<id>')
885-
def ajax_result(id):
886-
if id in pending:
887-
f, args, kwds, _ajax_sticky = pending[id]
888-
if not _ajax_sticky:
889-
del pending[id]
890-
return f(*args, **kwds)
891-
else:
892-
return "<expired>"
893-
894-
895-
def ajax_more(callback, *arg_list, **kwds):
896-
from .web_display import web_latex
897-
inline = kwds.get('inline', True)
898-
text = kwds.get('text', 'more')
899-
nonce = hex(random.randint(0, 1 << 128))
900-
if inline:
901-
args = arg_list[0]
902-
arg_list = arg_list[1:]
903-
if isinstance(args, tuple):
904-
res = callback(*arg_list)
905-
elif isinstance(args, dict):
906-
res = callback(**args)
907-
else:
908-
res = callback(args)
909-
res = web_latex(res)
910-
else:
911-
res = ''
912-
if arg_list:
913-
url = ajax_url(ajax_more, callback, *arg_list, inline=True, text=text)
914-
return """<span id='%(nonce)s'>%(res)s <a onclick="$('#%(nonce)s').load('%(url)s', function() { renderMathInElement($('#%(nonce)s').get(0),katexOpts);}); return false;" href="#">%(text)s</a></span>""" % locals()
915-
else:
916-
return res
917-
918-
919-
def image_src(G):
920-
return ajax_url(image_callback, G, _ajax_sticky=True)
921-
922-
923803
def image_callback(G):
924804
P = G.plot()
925805
_, filename = tempfile.mkstemp('.png')

0 commit comments

Comments
 (0)