From df41da4860ab93cbdfe3b8382dc89627139c485f Mon Sep 17 00:00:00 2001
From: Rebecca Law
Date: Wed, 28 Dec 2016 14:44:53 +0000
Subject: [PATCH] [WIP] Attempt at adding a form to the platform admin page is
not working well for me here. But I want to commit it so I can look at it
again.
---
app/main/forms.py | 17 ++++++-
app/main/views/platform_admin.py | 52 ++++++++++++++++-----
app/notify_client/service_api_client.py | 1 +
app/templates/views/platform-admin.html | 7 +++
tests/app/main/views/test_platform_admin.py | 5 +-
5 files changed, 64 insertions(+), 18 deletions(-)
diff --git a/app/main/forms.py b/app/main/forms.py
index 57599ee78..1a4c577f7 100644
--- a/app/main/forms.py
+++ b/app/main/forms.py
@@ -16,8 +16,8 @@ from wtforms import (
HiddenField,
IntegerField,
RadioField,
- FieldList
-)
+ FieldList,
+ DateField)
from wtforms.fields.html5 import EmailField, TelField
from wtforms.validators import (DataRequired, Email, Length, Regexp, Optional)
@@ -516,3 +516,16 @@ class Whitelist(Form):
max_entries=5,
label="Mobile numbers"
)
+
+
+class DateFilterForm(Form):
+ start_date = DateField("Start Date", [validators.optional()])
+ end_date = StringField("End Date", [validators.optional()])
+
+ def validate(self):
+ print("****In validate")
+ if self.start_date.data and not self.end_date.data:
+ print("***** false {}".format(type(self.end_date.errors)))
+ raise ValidationError('Both required')
+ else:
+ return True
diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py
index d6494620f..6c5fb8f06 100644
--- a/app/main/views/platform_admin.py
+++ b/app/main/views/platform_admin.py
@@ -5,33 +5,62 @@ from flask import (
request
)
from flask_login import login_required
+from wtforms import ValidationError
from app import service_api_client
from app.main import main
+from app.main.forms import DateFilterForm
from app.utils import user_has_permissions
from app.statistics_utils import get_formatted_percentage
-@main.route("/platform-admin")
+@main.route("/platform-admin", methods=['GET', 'POST'])
@login_required
@user_has_permissions(admin_override=True)
def platform_admin():
+ form = DateFilterForm()
include_from_test_key = request.args.get('include_from_test_key') != 'False'
# specifically DO get inactive services
api_args = {'detailed': True}
if not include_from_test_key:
api_args['include_from_test_key'] = False
- services = service_api_client.get_services(api_args)['data']
- return render_template(
- 'views/platform-admin.html',
- include_from_test_key=include_from_test_key,
- **get_statistics(sorted(
- services,
- key=lambda service: (service['active'], service['created_at']),
- reverse=True
- ))
- )
+ if form.validate_on_submit():
+ start_date = form.start_date.data
+ end_date = form.end_date.data
+ if start_date:
+ print(start_date)
+ print(end_date)
+ api_args['start_date'] = start_date
+ if not end_date:
+ raise ValidationError(message='requires end date', field =form.end_date)
+ api_args['end_date'] = end_date
+
+ services = service_api_client.get_services(api_args)['data']
+
+ return render_template(
+ 'views/platform-admin.html',
+ include_from_test_key=include_from_test_key,
+ form=form,
+ **get_statistics(sorted(
+ services,
+ key=lambda service: (service['active'], service['created_at']),
+ reverse=True
+ ))
+ )
+ else:
+ services = service_api_client.get_services(api_args)['data']
+
+ return render_template(
+ 'views/platform-admin.html',
+ include_from_test_key=include_from_test_key,
+ form=form,
+ **get_statistics(sorted(
+ services,
+ key=lambda service: (service['active'], service['created_at']),
+ reverse=True
+ ))
+ )
def get_statistics(services):
return {
@@ -64,7 +93,6 @@ def create_global_stats(services):
for stat in stats.values():
stat['failure_rate'] = get_formatted_percentage(stat['failed'], stat['requested'])
-
return stats
diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py
index e974890db..a4f0ef9d2 100644
--- a/app/notify_client/service_api_client.py
+++ b/app/notify_client/service_api_client.py
@@ -60,6 +60,7 @@ class ServiceAPIClient(NotifyAdminAPIClient):
"""
Retrieve a list of services.
"""
+ print(params_dict)
return self.get('/service', params=params_dict)
def get_active_services(self, params_dict=None):
diff --git a/app/templates/views/platform-admin.html b/app/templates/views/platform-admin.html
index 52eb48d4b..b5a94cd10 100644
--- a/app/templates/views/platform-admin.html
+++ b/app/templates/views/platform-admin.html
@@ -1,4 +1,6 @@
{% extends "withoutnav_template.html" %}
+{% from "components/textbox.html" import textbox %}
+{% from "components/page-footer.html" import page_footer %}
{% from "components/big-number.html" import big_number, big_number_with_status %}
{% from "components/message-count-label.html" import message_count_label %}
{% from "components/table.html" import mapping_table, field, stats_fields, row_group, row, right_aligned_field_heading, hidden_field_heading, text_field %}
@@ -94,6 +96,11 @@
{% else %}
Excluding test keys (change)
{% endif %}
+
diff --git a/tests/app/main/views/test_platform_admin.py b/tests/app/main/views/test_platform_admin.py
index 6f6fd5411..8956115d3 100644
--- a/tests/app/main/views/test_platform_admin.py
+++ b/tests/app/main/views/test_platform_admin.py
@@ -1,14 +1,11 @@
-from datetime import date
-
from flask import url_for
-from freezegun import freeze_time
import pytest
from bs4 import BeautifulSoup
from tests.conftest import mock_get_user
from tests import service_json
-from app.main.views.platform_admin import get_statistics, format_stats_by_service, create_global_stats
+from app.main.views.platform_admin import format_stats_by_service, create_global_stats
def test_should_redirect_if_not_logged_in(app_):