Repository: LMFDB/lmfdb

Issue:
CMF download issues
From the flasklog:
```
Exception on /ModularForm/GL2/Q/holomorphic/download_newform_to_magma/46350.2.a.g [GET]
Traceback (most recent call last):
  File "/home/sage/sage-9.7/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sage/sage-9.7/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/flask/app.py", line 1520, in full_dispatch_request
    return self.finalize_request(rv)
  File "/home/sage/sage-9.7/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/flask/app.py", line 1539, in finalize_request
    response = self.make_response(rv)
  File "/home/sage/sage-9.7/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/flask/app.py", line 1695, in make_response
    raise TypeError(
TypeError: The view function for 'cmf.download_newform_to_magma' did not return a valid response. The function either returned None or ended without a return statement.
[2024-05-29 05:45:06 UTC] 500 error on URL https://www.lmfdb.org/ModularForm/GL2/Q/holomorphic/download_newform_to_magma/46350.2.a.g ()
```

and 

```
Exception on /ModularForm/GL2/Q/holomorphic/download_full_space/44370.2 [GET]
Traceback (most recent call last):
  File "/home/sage/sage-9.7/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sage/sage-9.7/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/flask/app.py", line 1519, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/sage/sage-9.7/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/flask/app.py", line 1517, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/sage/sage-9.7/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/flask/app.py", line 1503, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/lmfdb/lmfdb-git-web/lmfdb/classical_modular_forms/main.py", line 657, in download_full_space
    return CMF_download().download_full_space(label)
  File "/home/lmfdb/lmfdb-git-web/lmfdb/classical_modular_forms/download.py", line 349, in download_full_space
    data['newspaces'] = [spc['label'] for spc, forms in space.decomp]
  File "/home/lmfdb/lmfdb-git-web/lmfdb/classical_modular_forms/download.py", line 349, in <listcomp>
    data['newspaces'] = [spc['label'] for spc, forms in space.decomp]
KeyError: 'label'
[2024-05-29 05:45:42 UTC] 500 error on URL https://www.lmfdb.org/ModularForm/GL2/Q/holomorphic/download_full_space/44370.2 ()
```


Discussion:

Resolving patch:
diff --git a/lmfdb/classical_modular_forms/download.py b/lmfdb/classical_modular_forms/download.py
--- a/lmfdb/classical_modular_forms/download.py
+++ b/lmfdb/classical_modular_forms/download.py
@@ -1,8 +1,10 @@
 from ast import literal_eval
+from sage.all import prime_range, previous_prime
 from flask import url_for, redirect, abort
 from lmfdb import db
 from psycodict.encoding import Json
 from lmfdb.utils import Downloader, flash_error
+from lmfdb.characters.TinyConrey import ConreyCharacter
 from lmfdb.classical_modular_forms.web_newform import WebNewform
 from lmfdb.classical_modular_forms.web_space import WebNewformSpace, WebGamma1Space
 
@@ -15,14 +17,22 @@ class CMF_download(Downloader):
 
     def _get_hecke_nf(self, label):
         proj = ['ap', 'hecke_ring_rank', 'hecke_ring_power_basis','hecke_ring_numerators', 'hecke_ring_denominators', 'field_poly','hecke_ring_cyclotomic_generator', 'hecke_ring_character_values', 'maxp']
+        print(label)
         data = db.mf_hecke_nf.lucky({'label':label}, proj)
-        if not data:
-            return None
+        print(data)
+        if data is None:
+            f = db.mf_newforms.lookup(label,projection=["level","char_orbit_label","dim","traces"])
+            if f["dim"]==1:
+                vals = ConreyCharacter(f["level"], db.char_dirichlet.lookup("%s.%s"%(f["level"],f["char_orbit_label"]),projection="first")).values_gens
+                vals = [[v[0],[1] if v[1]==0 else [-1]] for v in vals]
+                aps = [[f["traces"][p-1]] for p in prime_range(len(f["traces"])+1)]
+                data = { 'hecke_ring_cyclotomic_generator': 0, 'hecke_ring_character_values': vals, 'hecke_ring_power_basis': True, 'field_poly': [0,1], 'maxp': previous_prime(len(f["traces"])), 'ap': aps }
+            else:
+                return None
         # Make up for db_backend currently deleting Nones
         for elt in proj:
             if elt not in data:
                 data[elt] = None
-
         return data
 
     def _get_traces(self, label):
@@ -142,7 +152,7 @@ def download_qexp(self, label, lang='sage'):
             lang = self.languages.get(lang, self.languages['sage'])
         hecke_nf = self._get_hecke_nf(label)
         if hecke_nf is None:
-            return abort(404, "No q-expansion found for %s" % label)
+            return abort(404, "q-expansion not available for newform %s" % label)
 
         aps = hecke_nf['ap']
         level, weight = map(int, label.split('.')[:2])
@@ -509,7 +519,7 @@ def _magma_ExtendMultiplicatively(self):
                 '    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...',
                 '    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);',
                 '    recursion := Coefficients(1/(1 - X*T + Y*T^2));',
-                '    coeffs := [F!0: i in [1..(prec+1)]];',
+                '    coeffs := [F!0: i in [1..prec]];',
                 '    coeffs[1] := 1; //a_1',
                 '    for i := 1 to #primes do',
                 '        p := primes[i];',


Test patch:
diff --git a/lmfdb/classical_modular_forms/test_cmf2.py b/lmfdb/classical_modular_forms/test_cmf2.py
--- a/lmfdb/classical_modular_forms/test_cmf2.py
+++ b/lmfdb/classical_modular_forms/test_cmf2.py
@@ -28,7 +28,7 @@ def test_download_qexp(self):
             assert str(out) == exp
         for label in ['212.2.k.a', '887.2.a.b']:
             page = self.tc.get('/ModularForm/GL2/Q/holomorphic/download_qexp/{}'.format(label), follow_redirects=True)
-            assert 'No q-expansion found for {}'.format(label) in page.get_data(as_text=True)
+            assert 'q-expansion not available for newform {}'.format(label) in page.get_data(as_text=True)
 
     def test_download(self):
         r"""