def get_knowl(self, ID, fields=None, beta=None, allow_deleted=False, timestamp=None): if fields is None: fields = ['id'] + self._default_fields if timestamp is not None: selecter = SQL( "SELECT {0} FROM kwl_knowls WHERE id = %s AND timestamp = %s LIMIT 1" ).format(SQL(", ").join(map(Identifier, fields))) cur = self._execute(selecter, [ID, timestamp]) if cur.rowcount > 0: return dict(zip(fields, cur.fetchone())) else: return None if beta is None: beta = is_beta() selecter = SQL( "SELECT {0} FROM kwl_knowls WHERE id = %s AND status >= %s ORDER BY timestamp DESC LIMIT 1" ).format(SQL(", ").join(map(Identifier, fields))) if not beta: cur = self._execute(selecter, [ID, 1]) if cur.rowcount > 0: return {k: v for k, v in zip(fields, cur.fetchone())} cur = self._execute(selecter, [ID, -2 if allow_deleted else 0]) if cur.rowcount > 0: return dict(zip(fields, cur.fetchone()))
def ids_referencing(self, knowlid, old=False, beta=None): """ Returns all ids that reference the given one. Note that if running on prod, and the reviewed version of a knowl doesn't reference knowlid but a more recent beta version does, it will be included in the results even though the displayed knowl will not include a reference. INPUT: - ``knowlid`` -- a knowl id in the database - ``old`` -- whether to include knowls that used to reference this one, but no longer do. - ``beta`` -- if False, use the most recent positively reviewed knowl, rather than the most recent. """ values = [0, -2, [knowlid]] if old: selecter = SQL( "SELECT DISTINCT ON (id) id FROM kwl_knowls WHERE status >= %s AND type != %s AND links @> %s" ) else: if beta is None: beta = is_beta() if not beta: # Have to make sure we do display references where the the most recent positively reviewed knowl does reference this, but the most recent beta does not. selecter = SQL( "SELECT id FROM (SELECT DISTINCT ON (id) id, links FROM kwl_knowls WHERE status > %s AND type != %s ORDER BY id, timestamp DESC) knowls WHERE links @> %s" ) cur = self._execute(selecter, values) good_ids = [rec[0] for rec in cur] # Have to make sure that we don't display knowls as referencing this one when the most recent positively reviewed knowl doesn't but the most recent beta knowl does. selecter = SQL( "SELECT id FROM (SELECT DISTINCT ON (id) id, links FROM kwl_knowls WHERE status > %s AND type != %s ORDER BY id, timestamp DESC) knowls WHERE NOT (links @> %s)" ) cur = self._execute(selecter, values) bad_ids = [rec[0] for rec in cur] # We also need new knowls that have never been reviewed selecter = SQL( "SELECT id FROM (SELECT DISTINCT ON (id) id, links FROM kwl_knowls WHERE status >= %s AND type != %s ORDER BY id, timestamp DESC) knowls WHERE links @> %s" ) cur = self._execute(selecter, values) if not beta and not old: new_ids = [rec[0] for rec in cur if rec[0] not in bad_ids] return sorted(set(new_ids + good_ids)) else: return [rec[0] for rec in cur]
def random_url(): routes = [ "L/", "L/", "L/", "ModularForm/GL2/Q/holomorphic/", "ModularForm/GL2/Q/Maass/", "ModularForm/GL2/TotallyReal/", "ModularForm/GL2/ImaginaryQuadratic/", "EllipticCurve/Q/", "EllipticCurve/", "Genus2Curve/Q/", "HigherGenus/C/Aut/", "Variety/Abelian/Fq/", "NumberField/", "LocalNumberField/", "Character/Dirichlet/", "ArtinRepresentation/", "GaloisGroup/", "SatoTateGroup/" ] if is_beta(): routes += [ "ModularForm/GSp/Q/", "Belyi/", "Motive/Hypergeometric/Q/", "Lattice/" ] route = routes[randint(0,len(routes)-1)] if route == "ModularForm/GL2/Q/holomorphic/": ind = randint(0,1) if ind == 0: route += "random" else: route += "random_space" elif route == "Motive/Hypergeometric/Q/": ind = randint(0,1) if ind == 0: route += "random_motive" else: route += "random_family" else: route += "random" return route
def show(ID): timestamp = request.args.get('timestamp') if timestamp is not None: timestamp = timestamp_in_ms_to_datetime(timestamp) k = Knowl(ID, timestamp=timestamp, showing=True) if k.exists(): r = render_knowl(ID, footer="0", raw=True) title = k.title or "'%s'" % k.id if not is_beta(): if k.status == 0: title += " (awaiting review)" else: title += " (reviewed)" else: if current_user.is_admin() and k.exists(allow_deleted=True): k = Knowl(ID, showing=True, allow_deleted=True) r = render_knowl(ID, footer="0", raw=True, allow_deleted=True) title = (k.title or "'%s'" % k.id) + " (DELETED)" else: return abort(404, "No knowl found with the given id") for elt in k.edit_history: # We will be printing these within a javascript ` ` string # so need to escape backticks elt['content'] = json.dumps(elt['content']) # Modify the comments list to add information on whether this user can delete if k.type != -2: for i, (cid, author, timestamp) in enumerate(k.comments): can_delete = (current_user.is_admin() or current_user.get_id() == author) author_name = userdb.lookup(author)["full_name"] k.comments[i] = (cid, author_name, timestamp, can_delete) if k.type == 2: caturl = url_for('.index', category=k.category, column="on") else: caturl = url_for('.index', category=k.category) b = get_bread([(k.category, caturl), ('%s' % title, url_for('.show', ID=ID))]) return render_template(u"knowl-show.html", title=title, k=k, cur_username=current_user.get_id(), render=r, bread=b)
def get_knowl(self, ID, fields=None, beta=None, allow_deleted=False, timestamp=None): if fields is None: fields = ['id'] + self._default_fields if timestamp is not None: selecter = SQL("SELECT {0} FROM kwl_knowls WHERE id = %s AND timestamp = %s LIMIT 1").format(SQL(", ").join(map(Identifier, fields))) cur = self._execute(selecter, [ID, timestamp]) if cur.rowcount > 0: return {k:v for k,v in zip(fields, cur.fetchone())} else: return None if beta is None: beta = is_beta() selecter = SQL("SELECT {0} FROM kwl_knowls WHERE id = %s AND status >= %s ORDER BY timestamp DESC LIMIT 1").format(SQL(", ").join(map(Identifier, fields))) if not beta: cur = self._execute(selecter, [ID, 1]) if cur.rowcount > 0: return {k:v for k,v in zip(fields, cur.fetchone())} cur = self._execute(selecter, [ID, -2 if allow_deleted else 0]) if cur.rowcount > 0: return {k:v for k,v in zip(fields, cur.fetchone())}
def code_snippet_knowl(D, full=True): r""" INPUT: - ``D`` -- a dictionary with the following keys - ``filename`` -- a filename within the lmfdb repository - ``code`` -- a list of code lines (without trailing \n) - ``lines`` -- (optional) a list of line numbers - ``full`` -- if False, display only the filename rather than the full path. """ filename = D['filename'] code = D['code'] lines = D.get('lines') code = '\n'.join(code).replace('<', '<').replace('>', '>').replace( '"', '"') if is_debug_mode(): branch = "master" elif is_beta(): branch = "dev" else: branch = "web" url = "%s%s/%s" % (_url_source, branch, filename) link_text = "%s on Github" % (filename) if not full: filename = filename.split('/')[-1] if lines: if len(lines) == 1: label = '%s (line %s)' % (filename, lines[0]) else: lines = sorted(lines) label = '%s (lines %s-%s)' % (filename, lines[0], lines[-1]) url += "#L%s" % lines[0] else: label = filename inner = u"<div>\n<pre></pre>\n</div>\n<div align='right'><a href='%s' target='_blank'>%s</a></div>" inner = inner % (url, link_text) return ur'<a title="[code]" knowl="dynamic_show" pretext="%s" kwargs="%s">%s</a>' % ( code, inner, label)
def show(ID): timestamp = request.args.get('timestamp') if timestamp is not None: timestamp = timestamp_in_ms_to_datetime(timestamp) k = Knowl(ID, timestamp=timestamp, showing=True) if k.exists(): r = render_knowl(ID, footer="0", raw=True) title = k.title or "'%s'" % k.id if not is_beta(): if k.status == 0: title += " (awaiting review)" else: title += " (reviewed)" else: if current_user.is_admin() and k.exists(allow_deleted=True): k = Knowl(ID, showing=True, allow_deleted=True) r = render_knowl(ID, footer="0", raw=True, allow_deleted=True) title = (k.title or "'%s'" % k.id) + " (DELETED)" else: return abort(404, "No knowl found with the given id") for elt in k.edit_history: # We will be printing these within a javascript ` ` string # so need to escape backticks elt['content'] = json.dumps(elt['content']) # Modify the comments list to add information on whether this user can delete if k.type != -2: for i, (cid, author, timestamp) in enumerate(k.comments): can_delete = (current_user.is_admin() or current_user.get_id() == author) author_name = userdb.lookup(author)["full_name"] k.comments[i] = (cid, author_name, timestamp, can_delete) b = get_bread([(k.category, url_for('.index', category=k.category)), ('%s' % title, url_for('.show', ID=ID))]) return render_template(u"knowl-show.html", title=title, k=k, cur_username=current_user.get_id(), render=r, bread=b)
def code_snippet_knowl(D, full=True): r""" INPUT: - ``D`` -- a dictionary with the following keys - ``filename`` -- a filename within the lmfdb repository - ``code`` -- a list of code lines (without trailing \n) - ``lines`` -- (optional) a list of line numbers - ``full`` -- if False, display only the filename rather than the full path. """ filename = D['filename'] code = D['code'] lines = D.get('lines') code = '\n'.join(code).replace('<','<').replace('>','>').replace('"', '"') if is_debug_mode(): branch = "master" elif is_beta(): branch = "dev" else: branch = "web" url = "%s%s/%s" % (_url_source, branch, filename) link_text = "%s on Github" % (filename) if not full: filename = filename.split('/')[-1] if lines: if len(lines) == 1: label = '%s (line %s)' % (filename, lines[0]) else: lines = sorted(lines) label = '%s (lines %s-%s)' % (filename, lines[0], lines[-1]) url += "#L%s" % lines[0] else: label = filename inner = u"<div>\n<pre></pre>\n</div>\n<div align='right'><a href='%s' target='_blank'>%s</a></div>" inner = inner % (url, link_text) return ur'<a title="[code]" knowl="dynamic_show" pretext="%s" kwargs="%s">%s</a>'%(code, inner, label)
def ids_referencing(self, knowlid, old=False, beta=None): """Returns all ids that reference the given one. Note that if running on prod, and the reviewed version of a knowl doesn't reference knowlid but a more recent beta version does, it will be included in the results even though the displayed knowl will not include a reference. INPUT: - ``knowlid`` -- a knowl id in the database - ``old`` -- whether to include knowls that used to reference this one, but no longer do. - ``beta`` -- if False, use the most recent positively reviewed knowl, rather than the most recent. """ values = [0, -2, [knowlid]] if old: selecter = SQL("SELECT DISTINCT ON (id) id FROM kwl_knowls WHERE status >= %s AND type != %s links @> %s") else: if beta is None: beta = is_beta() if not beta: # Have to make sure we do display references where the the most recent positively reviewed knowl does reference this, but the most recent beta does not. selecter = SQL("SELECT id FROM (SELECT DISTINCT ON (id) id, links FROM kwl_knowls WHERE status > %s AND type != %s ORDER BY id, timestamp DESC) knowls WHERE links @> %s") cur = self._execute(selecter, values) good_ids = [rec[0] for rec in cur] # Have to make sure that we don't display knowls as referencing this one when the most recent positively reviewed knowl doesn't but the most recent beta knowl does. selecter = SQL("SELECT id FROM (SELECT DISTINCT ON (id) id, links FROM kwl_knowls WHERE status > %s AND type != %s ORDER BY id, timestamp DESC) knowls WHERE NOT (links @> %s)") cur = self._execute(selecter, values) bad_ids = [rec[0] for rec in cur] # We also need new knowls that have never been reviewed selecter = SQL("SELECT id FROM (SELECT DISTINCT ON (id) id, links FROM kwl_knowls WHERE status >= %s AND type != %s ORDER BY id, timestamp DESC) knowls WHERE links @> %s") cur = self._execute(selecter, values) if not beta and not old: new_ids = [rec[0] for rec in cur if rec[0] not in bad_ids] return sorted(set(new_ids + good_ids)) else: return [rec[0] for rec in cur]
def render_knowl(ID, footer=None, kwargs=None, raw=False, k=None, allow_deleted=False, timestamp=None): """ this method renders the given Knowl (ID) to insert it dynamically in a website. It is intended to be used by an AJAX call, but should do a similar job server-side only, too. Note, that the used knowl-render.html template is *not* based on any globally defined website and just creates a small and simple html snippet! the keyword 'raw' is used in knowledge.show and knowl_inc to include *just* the string and not the response object. """ # logger.debug("kwargs: %s", request.args) kwargs = kwargs or dict(((k, v) for k, v in request.args.items())) # logger.debug("kwargs: %s" , kwargs) if timestamp is None: # fetch and convert the ms timestamp to datetime try: timestamp = timestamp_in_ms_to_datetime(int(kwargs['timestamp'])) except KeyError: pass if k is None: try: k = Knowl(ID, allow_deleted=allow_deleted, timestamp=timestamp) except Exception: logger.critical("Failed to render knowl %s" % ID) errmsg = "Sorry, the knowledge database is currently unavailable." return errmsg if raw else make_response(errmsg) # If we are rendering a reviewed knowl on nonbeta, # we always include the timestamp if timestamp is None and k.status == 1 and not is_beta(): kwargs['timestamp'] = k.ms_timestamp # kw_params is inserted *verbatim* into the url_for(...) function inside the template # the idea is to pass the keyword arguments of the knowl further along the chain # of links, in this case the title and the permalink! # so, this kw_params should be plain python, e.g. "a=1, b='xyz'" kw_params = ', '.join(('%s="%s"' % (k, v) for k, v in kwargs.items())) logger.debug("kw_params: %s" % kw_params) # this is a very simple template based on no other template to render one single Knowl # for inserting into a website via AJAX or for server-side operations. if request.method == "POST": con = request.form['content'] foot = footer or request.form['footer'] elif request.method == "GET": con = request.args.get("content", k.content) foot = footer or request.args.get("footer", "1") # authors = [] # for a in k.author_links(): # authors.append("<a href='%s'>%s</a>" % # (url_for('users.profile', userid=a['_id']), a['full_name'] or a['_id'] )) # authors = ', '.join(authors) render_me = u"""\ {%% include "knowl-defs.html" %%} {%% from "knowl-defs.html" import KNOWL with context %%} {%% from "knowl-defs.html" import KNOWL_LINK with context %%} {%% from "knowl-defs.html" import KNOWL_INC with context %%} {%% from "knowl-defs.html" import TEXT_DATA with context %%} <div class="knowl">""" if foot == "1": render_me += """\ <div class="knowl-header"> <a href="{{ url_for('.show', ID='%(ID)s', %(kw_params)s ) }}">%(title)s</a> </div>""" % { 'ID': k.id, 'title': (k.title or k.id), 'kw_params': kw_params } render_me += """<div><div class="knowl-content">%(content)s</div></div>""" review_status = "" if foot == "1": render_me += """\ <div class="knowl-footer"> <a href="{{ url_for('.show', ID='%(ID)s', %(kw_params)s) }}">permalink</a> {%% if user_is_authenticated %%} · <a href="{{ url_for('.edit', ID='%(ID)s') }}">edit</a> {%% endif %%} %(review_status)s </div>""" # """ · Authors: %(authors)s """ if k.status == 0 and k.type != -2: review_status = """· (awaiting review)""" render_me += "</div>" # render_me = render_me % {'content' : con, 'ID' : k.id } con = md_preprocess(con) # markdown enabled render_me = render_me % { 'content': md.convert(con), 'ID': k.id, 'review_status': review_status, 'kw_params': kw_params } #, 'authors' : authors } # Pass the text on to markdown. Note, backslashes need to be escaped for # this, but not for the javascript markdown parser # logger.debug("rendering template string:\n%s" % render_me) # TODO improve the error message # so that the user has a clue. Most likely, the {{ KNOWL('...') }} has the wrong syntax! try: data = render_template_string(render_me, k=k, **kwargs) if raw: # note, this is just internally for the .show method, raw rendering # doesn't exist right now and will wrap this into a make_reponse! return data resp = make_response(data) # cache if it is a usual GET if request.method == 'GET': resp.headers['Cache-Control'] = 'max-age=%d, public' % ( _cache_time, ) resp.headers['Access-Control-Allow-Origin'] = '*' return resp except Exception as e: return "ERROR in the template: %s. Please edit it to resolve the problem." % e
def render_knowl(ID, footer=None, kwargs=None, raw=False, k=None, allow_deleted=False, timestamp=None): """ this method renders the given Knowl (ID) to insert it dynamically in a website. It is intended to be used by an AJAX call, but should do a similar job server-side only, too. Note, that the used knowl-render.html template is *not* based on any globally defined website and just creates a small and simple html snippet! the keyword 'raw' is used in knowledge.show and knowl_inc to include *just* the string and not the response object. """ # logger.debug("kwargs: %s", request.args) kwargs = kwargs or dict(((k, v) for k, v in request.args.iteritems())) # logger.debug("kwargs: %s" , kwargs) if timestamp is None: # fetch and convert the ms timestamp to datetime try: timestamp = timestamp_in_ms_to_datetime(int(kwargs['timestamp'])) except KeyError: pass if k is None: try: k = Knowl(ID, allow_deleted=allow_deleted, timestamp=timestamp) except Exception: logger.critical("Failed to render knowl %s"%ID) errmsg = "Sorry, the knowledge database is currently unavailable." return errmsg if raw else make_response(errmsg) # If we are rendering a reviewed knowl on nonbeta, # we always include the timestamp if timestamp is None and k.status == 1 and not is_beta(): kwargs['timestamp'] = k.ms_timestamp; # kw_params is inserted *verbatim* into the url_for(...) function inside the template # the idea is to pass the keyword arguments of the knowl further along the chain # of links, in this case the title and the permalink! # so, this kw_params should be plain python, e.g. "a=1, b='xyz'" kw_params = ', '.join(('%s="%s"' % (k, v) for k, v in kwargs.iteritems())) logger.debug("kw_params: %s" % kw_params) # this is a very simple template based on no other template to render one single Knowl # for inserting into a website via AJAX or for server-side operations. if request.method == "POST": con = request.form['content'] foot = footer or request.form['footer'] elif request.method == "GET": con = request.args.get("content", k.content) foot = footer or request.args.get("footer", "1") # authors = [] # for a in k.author_links(): # authors.append("<a href='%s'>%s</a>" % # (url_for('users.profile', userid=a['_id']), a['full_name'] or a['_id'] )) # authors = ', '.join(authors) render_me = u"""\ {%% include "knowl-defs.html" %%} {%% from "knowl-defs.html" import KNOWL with context %%} {%% from "knowl-defs.html" import KNOWL_LINK with context %%} {%% from "knowl-defs.html" import KNOWL_INC with context %%} {%% from "knowl-defs.html" import TEXT_DATA with context %%} <div class="knowl">""" if foot == "1": render_me += """\ <div class="knowl-header"> <a href="{{ url_for('.show', ID='%(ID)s', %(kw_params)s ) }}">%(title)s</a> </div>""" % {'ID': k.id, 'title': (k.title or k.id), 'kw_params': kw_params} render_me += """<div><div class="knowl-content">%(content)s</div></div>""" review_status = "" if foot == "1": render_me += """\ <div class="knowl-footer"> <a href="{{ url_for('.show', ID='%(ID)s', %(kw_params)s) }}">permalink</a> {%% if user_is_authenticated %%} · <a href="{{ url_for('.edit', ID='%(ID)s') }}">edit</a> {%% endif %%} %(review_status)s </div>""" # """ · Authors: %(authors)s """ if k.status == 0 and k.type != -2: review_status = """· (awaiting review)""" render_me += "</div>" # render_me = render_me % {'content' : con, 'ID' : k.id } con = md_preprocess(con) # markdown enabled render_me = render_me % {'content': md.convert(con), 'ID': k.id, 'review_status': review_status, 'kw_params': kw_params} #, 'authors' : authors } # Pass the text on to markdown. Note, backslashes need to be escaped for # this, but not for the javascript markdown parser # logger.debug("rendering template string:\n%s" % render_me) # TODO improve the error message # so that the user has a clue. Most likely, the {{ KNOWL('...') }} has the wrong syntax! try: data = render_template_string(render_me, k=k, **kwargs) if raw: # note, this is just internally for the .show method, raw rendering # doesn't exist right now and will wrap this into a make_reponse! return data resp = make_response(data) # cache if it is a usual GET if request.method == 'GET': resp.headers['Cache-Control'] = 'max-age=%d, public' % (_cache_time,) resp.headers['Access-Control-Allow-Origin'] = '*' return resp except Exception, e: return "ERROR in the template: %s. Please edit it to resolve the problem." % e