Просмотр исходного кода

Merge pull request #429 from sh4nks/improve-management-panel

Improve management panel
Peter Justin 7 лет назад
Родитель
Сommit
705e1c0d0a

+ 35 - 20
flaskbb/management/views.py

@@ -355,7 +355,8 @@ class BanUser(MethodView):
     def post(self, user_id=None):
     def post(self, user_id=None):
         if not Permission(CanBanUser, identity=current_user):
         if not Permission(CanBanUser, identity=current_user):
             flash(
             flash(
-                _("You do not have the permissions to ban this user."), "danger"
+                _("You do not have the permissions to ban this user."),
+                "danger"
             )
             )
             return redirect(url_for("management.overview"))
             return redirect(url_for("management.overview"))
 
 
@@ -366,11 +367,11 @@ class BanUser(MethodView):
             data = []
             data = []
             users = User.query.filter(User.id.in_(ids)).all()
             users = User.query.filter(User.id.in_(ids)).all()
             for user in users:
             for user in users:
-                # don't let a user ban himself and do not allow a moderator to ban
-                # a admin user
-                if (current_user.id == user.id
-                        or Permission(IsAdmin, identity=user)
-                        and Permission(Not(IsAdmin), current_user)):
+                # don't let a user ban himself and do not allow a moderator
+                # to ban a admin user
+                if (current_user.id == user.id or
+                        Permission(IsAdmin, identity=user) and
+                        Permission(Not(IsAdmin), current_user)):
                     continue
                     continue
 
 
                 elif user.ban():
                 elif user.ban():
@@ -435,7 +436,8 @@ class UnbanUser(MethodView):
                             "type": "unban",
                             "type": "unban",
                             "reverse": "ban",
                             "reverse": "ban",
                             "reverse_name": _("Ban"),
                             "reverse_name": _("Ban"),
-                            "reverse_url": url_for("management.ban_user", user_id=user.id)
+                            "reverse_url": url_for("management.ban_user",
+                                                   user_id=user.id)
                         }
                         }
                     )
                     )
 
 
@@ -529,7 +531,8 @@ class DeleteGroup(MethodView):
     def post(self, group_id=None):
     def post(self, group_id=None):
         if request.is_xhr:
         if request.is_xhr:
             ids = request.get_json()["ids"]
             ids = request.get_json()["ids"]
-            if not (set(ids) & set(["1", "2", "3", "4", "5"])):
+            # TODO: Get rid of magic numbers
+            if not (set(ids) & set(["1", "2", "3", "4", "5", "6"])):
                 data = []
                 data = []
                 for group in Group.query.filter(Group.id.in_(ids)).all():
                 for group in Group.query.filter(Group.id.in_(ids)).all():
                     group.delete()
                     group.delete()
@@ -557,7 +560,7 @@ class DeleteGroup(MethodView):
             )
             )
 
 
         if group_id is not None:
         if group_id is not None:
-            if group_id <= 5:  # there are 5 standard groups
+            if group_id <= 6:  # there are 6 standard groups
                 flash(
                 flash(
                     _(
                     _(
                         "You cannot delete the standard groups. "
                         "You cannot delete the standard groups. "
@@ -610,7 +613,8 @@ class EditForum(MethodView):
         if form.validate_on_submit():
         if form.validate_on_submit():
             form.save()
             form.save()
             flash(_('Forum updated.'), 'success')
             flash(_('Forum updated.'), 'success')
-            return redirect(url_for('management.edit_forum', forum_id=forum.id))
+            return redirect(url_for('management.edit_forum',
+                                    forum_id=forum.id))
         else:
         else:
             if forum.moderators:
             if forum.moderators:
                 form.moderators.data = ','.join(
                 form.moderators.data = ','.join(
@@ -868,6 +872,22 @@ class DeleteReport(MethodView):
         return redirect(url_for("management.reports"))
         return redirect(url_for("management.reports"))
 
 
 
 
+class CeleryStatus(MethodView):
+    decorators = [allows.requires(IsAtleastModerator)]
+
+    def get(self):
+        celery_inspect = celery.control.inspect()
+        try:
+            celery_running = True if celery_inspect.ping() else False
+        except Exception:
+            # catching Exception is bad, and just catching ConnectionError
+            # from redis is also bad because you can run celery with other
+            # brokers as well.
+            celery_running = False
+
+        return jsonify(celery_running=celery_running, status=200)
+
+
 class ManagementOverview(MethodView):
 class ManagementOverview(MethodView):
     decorators = [allows.requires(IsAtleastModerator)]
     decorators = [allows.requires(IsAtleastModerator)]
 
 
@@ -887,15 +907,6 @@ class ManagementOverview(MethodView):
             order_by(Report.id.desc()).\
             order_by(Report.id.desc()).\
             count()
             count()
 
 
-        celery_inspect = celery.control.inspect()
-        try:
-            celery_running = True if celery_inspect.ping() else False
-        except Exception:
-            # catching Exception is bad, and just catching ConnectionError
-            # from redis is also bad because you can run celery with other
-            # brokers as well.
-            celery_running = False
-
         python_version = "{}.{}.{}".format(
         python_version = "{}.{}.{}".format(
             sys.version_info[0], sys.version_info[1], sys.version_info[2]
             sys.version_info[0], sys.version_info[1], sys.version_info[2]
         )
         )
@@ -914,7 +925,6 @@ class ManagementOverview(MethodView):
             # components
             # components
             "python_version": python_version,
             "python_version": python_version,
             "celery_version": celery_version,
             "celery_version": celery_version,
-            "celery_running": celery_running,
             "flask_version": flask_version,
             "flask_version": flask_version,
             "flaskbb_version": flaskbb_version,
             "flaskbb_version": flaskbb_version,
             # plugins
             # plugins
@@ -1177,6 +1187,11 @@ def flaskbb_load_blueprints(app):
     )
     )
     register_view(
     register_view(
         management,
         management,
+        routes=['/celerystatus'],
+        view_func=CeleryStatus.as_view('celery_status')
+    )
+    register_view(
+        management,
         routes=['/'],
         routes=['/'],
         view_func=ManagementOverview.as_view('overview')
         view_func=ManagementOverview.as_view('overview')
     )
     )

+ 3 - 0
flaskbb/templates/management/groups.html

@@ -58,12 +58,15 @@
                         <a href="{{ url_for('management.edit_group', group_id = group.id) }}" class="btn btn-icon">
                         <a href="{{ url_for('management.edit_group', group_id = group.id) }}" class="btn btn-icon">
                             <span class="fa fa-pencil text-primary" data-toggle="tooltip" data-placement="top" title="{% trans %}Edit{% endtrans %}"></span>
                             <span class="fa fa-pencil text-primary" data-toggle="tooltip" data-placement="top" title="{% trans %}Edit{% endtrans %}"></span>
                         </a>
                         </a>
+                        {# only display "Delete" if group is not part of the standard groups. #}
+                        {% if group.id > 6 %}
                         <form class="inline-form" id="delete-{{group.id}}" method="post" action="{{ url_for('management.delete_group', group_id=group.id) }}">
                         <form class="inline-form" id="delete-{{group.id}}" method="post" action="{{ url_for('management.delete_group', group_id=group.id) }}">
                             <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                             <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                             <button class="btn btn-icon" name="confirmDialog" data-toggle="tooltip" data-placement="top" title="{% trans %}Delete{% endtrans %}">
                             <button class="btn btn-icon" name="confirmDialog" data-toggle="tooltip" data-placement="top" title="{% trans %}Delete{% endtrans %}">
                                 <span class="fa fa-trash text-danger"></span>
                                 <span class="fa fa-trash text-danger"></span>
                             </button>
                             </button>
                         </form>
                         </form>
+                        {% endif %}
                     </div>
                     </div>
                 </div>
                 </div>
             {% else %}
             {% else %}

+ 47 - 20
flaskbb/templates/management/overview.html

@@ -19,22 +19,14 @@
             <div class="settings-content">
             <div class="settings-content">
                 <div class="stats">
                 <div class="stats">
                     <div class="row stats-row">
                     <div class="row stats-row">
-                        <div class="col-md-12 col-sm-12 col-xs-12">
-                            {# TODO: I have a feeling that this can be done so much nicer #}
-                            {% if not celery_running %}
-                            <div class="alert-message alert-message-danger">
-                                <h4>{% trans %}There is a problem.{% endtrans %}</h4>
-                                <p>{% trans %}Celery is <strong>not</strong> running.{% endtrans %}</p>
-                                <p>{% trans %}You can start celery with this command:{% endtrans %}</p>
-                                <pre>flaskbb --config {{ current_app.config["CONFIG_PATH"] }} celery worker</pre>
-                            </div>
-                            {% elif unread_reports > 0 %}
+                        <div class="overview-notifications col-md-12 col-sm-12 col-xs-12">
+                            {% if unread_reports > 0 %}
                             <div class="alert-message alert-message-warning">
                             <div class="alert-message alert-message-warning">
                                 <h4>{% trans %}There is something that wants your attention.{% endtrans %}</h4>
                                 <h4>{% trans %}There is something that wants your attention.{% endtrans %}</h4>
                                 <p>{% trans url=url_for('management.unread_reports') %}You have <a href="{{ url }}">{{ unread_reports }} unread reports</a>.{% endtrans %}</p>
                                 <p>{% trans url=url_for('management.unread_reports') %}You have <a href="{{ url }}">{{ unread_reports }} unread reports</a>.{% endtrans %}</p>
                             </div>
                             </div>
                             {% else %}
                             {% else %}
-                            <div class="alert-message alert-message-success">
+                            <div id="overview-no-notifications" class="alert-message alert-message-success">
                                 <h4>{% trans %}Everything seems alright.{% endtrans %}</h4>
                                 <h4>{% trans %}Everything seems alright.{% endtrans %}</h4>
                                 <p>{% trans %}No new notifications.{% endtrans %}</p>
                                 <p>{% trans %}No new notifications.{% endtrans %}</p>
                             </div>
                             </div>
@@ -118,13 +110,10 @@
                             </div>
                             </div>
                             -->
                             -->
                             <div class="row stats-item">
                             <div class="row stats-item">
-                                <div class="key pull-left">Celery</div><div class="value pull-right">
-                                {% if celery_running %}
-                                    <span class="text-success"><strong>running</strong></span>
-                                {% else %}
-                                <span class="text-danger"><strong>not running</strong></span>
-                                {% endif %}
-                                {{ celery_version }}
+                                <div class="key pull-left">Celery</div>
+                                <div class="value pull-right">
+                                    <span id="celery-status" class="text-warning">checking status</span>
+                                    {{ celery_version }}
                                 </div>
                                 </div>
                             </div>
                             </div>
                             <div class="row stats-item">
                             <div class="row stats-item">
@@ -150,10 +139,14 @@
                                 <div class="value pull-right">
                                 <div class="value pull-right">
                                     {% if not plugin.enabled %}
                                     {% if not plugin.enabled %}
                                         <span class="text-danger">not enabled</span>
                                         <span class="text-danger">not enabled</span>
-                                    {% elif plugin.enabled and plugin.is_installed %}
+                                    {% elif plugin.enabled %}
+                                        {% if plugin.is_installed %}
                                         <span class="text-success">enabled &amp; installed</span>
                                         <span class="text-success">enabled &amp; installed</span>
-                                    {% elif plugin.enabled and not plugin.is_installed %}
+                                        {% elif not plugin.is_installable %}
+                                        <span class="text-success">enabled</span>
+                                        {% elif not plugin.is_installed %}
                                         <span class="text-warning">not installed</span>
                                         <span class="text-warning">not installed</span>
+                                        {% endif %}
                                     {% endif %}
                                     {% endif %}
                                     {{ plugin.version }}
                                     {{ plugin.version }}
                                 </div>
                                 </div>
@@ -167,3 +160,37 @@
     </div>
     </div>
 </div>
 </div>
 {% endblock %}
 {% endblock %}
+
+{% block scripts %}
+<script>
+
+function celery_not_running_notification() {
+    content = "<div class='alert-message alert-message-danger'>" +
+        "<h4>{% trans %}There is a problem.{% endtrans %}</h4>" +
+        "<p>{% trans %}Celery is <strong>not</strong> running.{% endtrans %}</p>" +
+        "<p>{% trans %}You can start celery with this command:{% endtrans %}</p>" +
+        "<pre>flaskbb --config {{ current_app.config["CONFIG_PATH"] }} celery worker</pre>" +
+        "</div>";
+
+    // replace the no notifications notice with ours
+    if(document.getElementById("#overview-no-notifications") == null) {
+        $("#overview-no-notifications").replaceWith(content);
+    } else {
+        $(".overview-notifications").append(content);
+    }
+}
+
+$(document).ready(function () {
+    var $celerystatus = $('#celery-status');
+
+    $.getJSON('/admin/celerystatus', function(data) {
+        if(data.celery_running) {
+            $celerystatus.replaceWith("<span id='celery-status' class='text-success'><strong>running</strong></span>");
+        } else {
+            $celerystatus.replaceWith("<span id='celery-status' class='text-danger'><strong>not running</strong></span>");
+            celery_not_running_notification()
+        }
+    });
+});
+</script>
+{% endblock %}