浏览代码

[enh] display errors

also tried flask's flash feature but flask creates session cookies if it
isn't flushed. Avoiding session cookies to preserve privacy
Adam Tauber 8 年前
父节点
当前提交
832cf37a97
共有 2 个文件被更改,包括 25 次插入6 次删除
  1. 15
    0
      searx/templates/oscar/base.html
  2. 10
    6
      searx/webapp.py

+ 15
- 0
searx/templates/oscar/base.html 查看文件

1
+{% from 'oscar/macros.html' import icon %}
1
 <!DOCTYPE html>
2
 <!DOCTYPE html>
2
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"{% if rtl %} dir="rtl"{% endif %}>
3
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"{% if rtl %} dir="rtl"{% endif %}>
3
 <head>
4
 <head>
54
 <body>
55
 <body>
55
     {% include 'oscar/navbar.html' %}
56
     {% include 'oscar/navbar.html' %}
56
     <div class="container">
57
     <div class="container">
58
+    {% if errors %}
59
+        <div class="alert alert-danger fade in" role="alert">
60
+            <button class="close" data-dismiss="alert" type="button">
61
+                <span aria-hidden="true">×</span>
62
+                <span class="sr-only">{{ _('Close') }}</span>
63
+            </button>
64
+            <strong class="lead">{{ icon('info-sign') }} {{ _('Error!') }}</strong>
65
+            <ul>
66
+            {% for message in errors %}
67
+                <li>{{ message }}</li>
68
+            {% endfor %}
69
+            </ul>
70
+        </div>
71
+    {% endif %}
57
 
72
 
58
     {% block site_alert_error %}
73
     {% block site_alert_error %}
59
     {% endblock %}
74
     {% endblock %}

+ 10
- 6
searx/webapp.py 查看文件

344
 
344
 
345
     kwargs['cookies'] = request.cookies
345
     kwargs['cookies'] = request.cookies
346
 
346
 
347
+    kwargs['errors'] = request.errors
348
+
347
     kwargs['instance_name'] = settings['general']['instance_name']
349
     kwargs['instance_name'] = settings['general']['instance_name']
348
 
350
 
349
     kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab')
351
     kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab')
364
 
366
 
365
 @app.before_request
367
 @app.before_request
366
 def pre_request():
368
 def pre_request():
367
-    # merge GET, POST vars
369
+    request.errors = []
370
+
368
     preferences = Preferences(themes, categories.keys(), engines, plugins)
371
     preferences = Preferences(themes, categories.keys(), engines, plugins)
369
     try:
372
     try:
370
         preferences.parse_cookies(request.cookies)
373
         preferences.parse_cookies(request.cookies)
371
     except:
374
     except:
372
-        # TODO throw error message to the user
373
-        logger.warning('Invalid config')
375
+        request.errors.append(gettext('Invalid settings, please edit your preferences'))
374
     request.preferences = preferences
376
     request.preferences = preferences
375
 
377
 
378
+    # merge GET, POST vars
376
     # request.form
379
     # request.form
377
     request.form = dict(request.form.items())
380
     request.form = dict(request.form.items())
378
     for k, v in request.args.items():
381
     for k, v in request.args.items():
397
     Supported outputs: html, json, csv, rss.
400
     Supported outputs: html, json, csv, rss.
398
     """
401
     """
399
 
402
 
400
-    if not request.args and not request.form:
403
+    if request.form.get('q') is None:
401
         return render(
404
         return render(
402
             'index.html',
405
             'index.html',
403
         )
406
         )
410
         # search = Search(search_query) #  without plugins
413
         # search = Search(search_query) #  without plugins
411
         search = SearchWithPlugins(search_query, request)
414
         search = SearchWithPlugins(search_query, request)
412
         result_container = search.search()
415
         result_container = search.search()
413
-    except Exception:
416
+    except:
417
+        request.errors.append(gettext('search error'))
414
         logger.exception('search error')
418
         logger.exception('search error')
415
         return render(
419
         return render(
416
             'index.html',
420
             'index.html',
573
         try:
577
         try:
574
             request.preferences.parse_form(request.form)
578
             request.preferences.parse_form(request.form)
575
         except ValidationException:
579
         except ValidationException:
576
-            # TODO use flash feature of flask
580
+            request.errors.append(gettext('Invalid settings, please edit your preferences'))
577
             return resp
581
             return resp
578
         return request.preferences.save(resp)
582
         return request.preferences.save(resp)
579
 
583