Repository: LMFDB/lmfdb

Issue:
Server error when searching ecnf by j-invariant
Searching elliptic curves over number fields by j-invariant without specifying the field produces a server error. (It also gives the message: "Error: 0 is not a valid input for j-invariant. You must specify a field when searching by j-invariant".)

For example, https://www.lmfdb.org/EllipticCurve/?jinv=0.


Discussion:
Just checking, is this issue just about solving the server error, or also about adding support for searching for rational j-invariants across multiple number fields?
I'm not sure. Certainly the server error needs to be fixed, but should we allow users to search for elliptic curves with rational j-invariants defined over any number field? @edgarcosta didn't think this would be hard to do: just loop over all degrees of number fields, where the j-invariant will be represented by [j, 0, 0, ..., 0].
There are two parts:
- fix the server error
- feature request to be able to search on rational j-invariants

perhaps the last item deserve it's own issue
It is possible to search by j-invariant (rational or not) if one specifies the field.  The error message when the field is omitted is quite clear, it is perhaps wrong to describing itself as a "server error".  The same happens if one puts in an invalid field label such as a.b.c.d.  There, we do want to distinguish between entering as a field label something syntactically wrong (such as 'a.b.c.d') from something in principle correct but which does not refer to any actual field (e.g. 2.2.5.2) and something which refers to a valid field over which we happen to have no curves (e.g. 2.0.19.1).  Probably the second case is too hard except for quadratics.

I have no interest in searching for rational j over all fields.  We can already search for base-change curves only -- not the same but obviously includes all those with rational j, and probably more interesting.  So I would vote against the new issue, but if someone wanted to create it of course they can.
@JohnCremona, 
If you click on https://beta.lmfdb.org/EllipticCurve/?jinv=0
you will get an error message
```
Error: 0 is not a valid input for j-invariant. You must specify a field when searching by j-invariant
```
and it produces a server error, as there is an uncaught raised exception:
```
The page you are looking for has produced a server error. 
```
<img width="734" alt="Screen Shot 2019-12-20 at 06 26 34" src="https://user-images.githubusercontent.com/322822/71252068-fb7da680-22f1-11ea-8765-97d17c3baa88.png">

Here is the traceback
```
Exception on /EllipticCurve/ [GET]
Traceback (most recent call last):
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/main.py", line 172, in index
    return elliptic_curve_search(request.args)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/utils/search_wrapper.py", line 88, in __call__
    data = self.make_query(info, random)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/utils/search_wrapper.py", line 51, in make_query
    return render_template(self.template, info=info, title=err_title, **template_kwds)
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/templates/ecnf-search-results.html", line 1, in top-level template code
    {% extends 'homepage.html' %}
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/templates/homepage.html", line 19, in top-level template code
    <div class="{{ [L,'nodisplay','code','codebox'] | join(' ')}}">{% for line in lines %}{{prompt}}:&nbsp;{{line}}<br /><div style="margin: 0; padding: 0; height: 0;">&nbsp;</div>{# this div is a workaround for a copy-tex bug where if a code block followed a hidden code block followed by math was selected and copied the hidden block would be also it is important that it has display: block #}{% endfor %}</div>
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/templates/base.html", line 135, in top-level template code
    {% block body -%}{%- endblock body %}
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/templates/homepage.html", line 244, in block "body"
    {% block content -%}
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/templates/ecnf-search-results.html", line 32, in block "content"
    {% if info.query.number %}
  File "/home/sage/sage-8.8/local/lib/python2.7/site-packages/jinja2/environment.py", line 430, in getattr
    return getattr(obj, attribute)
UndefinedError: 'dict object' has no attribute 'query'
[2019-12-20 11:15:34 UTC] 500 error on URL http://beta.lmfdb.org/EllipticCurve/?jinv=0 ("'dict object' has no attribute 'query'",)
```
Yes I was not doubting that a server error occurs.  I'm sure that it did not cause a server error when I implemented this feature and wrote the nice error message.  When this has been fixed there should be a test for it.

Resolving patch:
diff --git a/lmfdb/ecnf/main.py b/lmfdb/ecnf/main.py
--- a/lmfdb/ecnf/main.py
+++ b/lmfdb/ecnf/main.py
@@ -17,7 +17,7 @@
     to_dict, flash_error,
     parse_ints, parse_noop, nf_string_to_label, parse_element_of,
     parse_nf_string, parse_nf_elt, parse_bracketed_posints,
-    search_wrap)
+    search_wrap, parse_rational)
 from lmfdb.number_fields.number_field import field_pretty
 from lmfdb.number_fields.web_number_field import nf_display_knowl, WebNumberField
 from lmfdb.ecnf import ecnf_page
@@ -499,9 +499,14 @@ def elliptic_curve_search(info, query):
             info['jinv'] = info['jinv'].replace('phi','a')
         if info.get('field','').strip() == '2.0.4.1':
             info['jinv'] = info['jinv'].replace('i','a')
-    parse_nf_elt(info,query,'jinv',name='j-invariant')
-    if query.get('jinv'):
-        query['jinv'] =','.join(query['jinv'])
+        if not 'a' in info['jinv'] and not info.get('field'): # rational j-invariant allowed for any field
+            parse_rational(info, query, 'jinv', name='j-invariant')
+            if query.get('jinv'):
+                query['jinv'] = {'$regex': '^' + query['jinv'] + '(,0)*$'} # nf elements like j,0,0,0
+        else: # j-invariant is a number field element
+            parse_nf_elt(info, query, 'jinv', name='j-invariant')
+            if query.get('jinv'):
+                query['jinv'] = ','.join(query['jinv'])
 
     if 'include_isogenous' in info and info['include_isogenous'] == 'off':
         info['number'] = 1


Test patch: