From 414841f7d4cb0365f3144c6f994bf7786cd2ae5a Mon Sep 17 00:00:00 2001
From: thomaswoehlke <thomas.woehlke@gmail.com>
Date: Thu, 27 May 2021 21:21:03 +0200
Subject: [PATCH] Refactoring: vaccination

---
 .../blueprints/app_all/all_model.py           |  2 +-
 .../blueprints/data_ecdc/ecdc_model.py        | 93 ++++++++++++++++---
 .../blueprints/data_ecdc/ecdc_views.py        | 47 +++++-----
 .../all/ecdc_date_reported_all_table.html     | 14 +--
 ...rted_one_notification_rate_pagination.html |  7 +-
 .../ecdc/ecdc_data/ecdc_data_table.html       | 72 +-------------
 .../ecdc/ecdc_data/ecdc_data_table_head.html  | 65 +++++++++++++
 7 files changed, 181 insertions(+), 119 deletions(-)
 create mode 100644 src/flask_covid19/blueprints/data_ecdc/templates/ecdc/ecdc_data/ecdc_data_table_head.html

diff --git a/src/flask_covid19/blueprints/app_all/all_model.py b/src/flask_covid19/blueprints/app_all/all_model.py
index 2c5a8cec..8f885b35 100644
--- a/src/flask_covid19/blueprints/app_all/all_model.py
+++ b/src/flask_covid19/blueprints/app_all/all_model.py
@@ -425,7 +425,7 @@ class BlueprintFactTable(BlueprintFactTableTimeSeries):
         pass
 
     @classmethod
-    def get_by_date_reported_and_location(cls, date_reported: BlueprintDateReported, location: BlueprintLocation):
+    def get_by_date_reported_and_location(cls, date_reported: BlueprintDateReported, location: BlueprintLocation, page: int):
         pass
 
 
diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py
index fb8a562c..b9084c51 100644
--- a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py
+++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py
@@ -106,7 +106,13 @@ class EcdcCountry(BlueprintLocation):
         )).one_or_none()
 
     @classmethod
-    def find_by_location_group(cls, location_group: EcdcContinent, page: int):
+    def find_by_location_group(cls, location_group: EcdcContinent):
+        return db.session.query(cls)\
+            .filter(cls.location_group == location_group)\
+            .all()
+
+    @classmethod
+    def get_by_location_group(cls, location_group: EcdcContinent, page: int):
         return db.session.query(cls)\
             .filter(cls.location_group == location_group)\
             .paginate(page, per_page=ITEMS_PER_PAGE)
@@ -143,31 +149,88 @@ class EcdcData(BlueprintFactTable):
     cumulative_number_for_14_days_of_covid19_cases_per_100000 = db.Column(db.Float, nullable=False)
 
     @classmethod
-    def find_by_date_reported(cls, date_reported: EcdcDateReported, page: int):
+    def __query_by_date_reported(cls, date_reported: EcdcDateReported):
         return db.session.query(cls).filter(
-            cls.date_reported_id == date_reported.id)\
-            .order_by(cls.cumulative_number_for_14_days_of_covid19_cases_per_100000.desc())\
-            .paginate(page, per_page=ITEMS_PER_PAGE)
+            cls.date_reported_id == date_reported.id) \
+            .order_by(cls.cumulative_number_for_14_days_of_covid19_cases_per_100000.desc())
 
     @classmethod
-    def find_by_date_reported_order_by_notification_rate(cls, date_reported: EcdcDateReported, page: int):
+    def __query_by_date_reported_order_by_notification_rate(cls, date_reported: EcdcDateReported):
         return db.session.query(cls).filter(
             cls.date_reported_id == date_reported.id) \
-            .order_by(cls.cumulative_number_for_14_days_of_covid19_cases_per_100000.desc()) \
+            .order_by(cls.cumulative_number_for_14_days_of_covid19_cases_per_100000.desc())
+
+    @classmethod
+    def __query_by_date_reported_order_by_deaths(cls, date_reported: EcdcDateReported):
+        return db.session.query(cls).filter(cls.date_reported_id == date_reported.id) \
+            .order_by(cls.deaths.desc())
+
+    @classmethod
+    def __query_by_date_reported_order_by_cases(cls, date_reported: EcdcDateReported):
+        return db.session.query(cls)\
+            .filter(cls.date_reported == date_reported) \
+            .order_by(cls.cases.desc())
+
+    @classmethod
+    def __query_by_location(cls, location: EcdcCountry):
+        return db.session.query(cls)\
+            .filter(cls.location == location) \
+            .order_by(cls.date_reported.desc())
+
+    @classmethod
+    def __query_by_date_reported_and_location(cls, date_reported: EcdcDateReported, location: EcdcCountry):
+        return db.session.query(cls) \
+            .filter(and_((cls.location == location), (cls.date_reported == date_reported)))
+
+    @classmethod
+    def find_by_date_reported(cls, date_reported: EcdcDateReported):
+        return cls.__query_by_date_reported(date_reported).all()
+
+    @classmethod
+    def get_by_date_reported(cls, date_reported: EcdcDateReported, page: int):
+        return cls.__query_by_date_reported(date_reported)\
             .paginate(page, per_page=ITEMS_PER_PAGE)
 
     @classmethod
-    def find_by_date_reported_order_by_deaths_weekly(cls, date_reported: EcdcDateReported, page: int):
-        return db.session.query(cls).filter(
-            cls.date_reported_id == date_reported.id) \
-            .order_by(cls.deaths.desc()) \
+    def find_by_date_reported_order_by_notification_rate(cls, date_reported: EcdcDateReported):
+        return cls.__query_by_date_reported_order_by_notification_rate(date_reported).all()
+
+    @classmethod
+    def get_by_date_reported_order_by_notification_rate(cls, date_reported: EcdcDateReported, page: int):
+        return cls.__query_by_date_reported_order_by_notification_rate(date_reported)\
             .paginate(page, per_page=ITEMS_PER_PAGE)
 
     @classmethod
-    def find_by_date_reported_order_by_cases_weekly(cls, date_reported: EcdcDateReported, page: int):
-        return db.session.query(cls).filter(cls.date_reported == date_reported) \
+    def find_by_date_reported_order_by_deaths(cls, date_reported: EcdcDateReported):
+        return cls.__query_by_date_reported_order_by_deaths_weekly(date_reported).all()
+
+    @classmethod
+    def get_by_date_reported_order_by_deaths(cls, date_reported: EcdcDateReported, page: int):
+        return cls.__query_by_date_reported_order_by_deaths_weekly(date_reported)\
             .paginate(page, per_page=ITEMS_PER_PAGE)
 
     @classmethod
-    def find_by_location(cls, location: EcdcCountry, page: int):
-        return db.session.query(cls).filter(cls.location == location).paginate(page, per_page=ITEMS_PER_PAGE)
+    def find_by_date_reported_order_by_cases(cls, date_reported: EcdcDateReported):
+        return cls.__query_by_date_reported_order_by_cases_weekly(date_reported).all()
+
+    @classmethod
+    def get_by_date_reported_order_by_cases(cls, date_reported: EcdcDateReported, page: int):
+        return cls.__query_by_date_reported_order_by_cases_weekly(date_reported)\
+            .paginate(page, per_page=ITEMS_PER_PAGE)
+
+    @classmethod
+    def find_by_location(cls, location: EcdcCountry):
+        return cls.__query_by_location(location).all()
+
+    @classmethod
+    def get_by_location(cls, location: EcdcCountry, page: int):
+        return cls.__query_by_location(location).paginate(page, per_page=ITEMS_PER_PAGE)
+
+    @classmethod
+    def find_by_date_reported_and_location(cls, date_reported: EcdcDateReported, location: EcdcCountry):
+        return cls.__query_by_date_reported_and_location(date_reported, location).one_or_none()
+
+    @classmethod
+    def get_by_date_reported_and_location(cls, date_reported: EcdcDateReported, location: EcdcCountry, page: int):
+        return cls.__query_by_date_reported_and_location(date_reported, location).one()
+
diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_views.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_views.py
index e8ac7a6e..e4d5a4b6 100644
--- a/src/flask_covid19/blueprints/data_ecdc/ecdc_views.py
+++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_views.py
@@ -77,44 +77,43 @@ def url_ecdc_date_reported_all(page=1):
         page_data=page_data,
         page_info=page_info)
 
-
-@app_ecdc.route('/date_reported/<int:europe_date_reported_id>/page/<int:page>')
-@app_ecdc.route('/date_reported/<int:europe_date_reported_id>')
-@app_ecdc.route('/date_reported/notification_rate/<int:europe_date_reported_id>/page/<int:page>')
-@app_ecdc.route('/date_reported/notification_rate/<int:europe_date_reported_id>')
-def url_ecdc_date_reported_one_notification_rate(europe_date_reported_id, page=1):
+@app_ecdc.route('/date_reported/<int:date_reported_id>/page/<int:page>')
+@app_ecdc.route('/date_reported/<int:date_reported_id>')
+@app_ecdc.route('/date_reported/notification_rate/<int:date_reported_id>/page/<int:page>')
+@app_ecdc.route('/date_reported/notification_rate/<int:date_reported_id>')
+def url_ecdc_date_reported_one_notification_rate(date_reported_id, page=1):
     page_info = WebPageContent('ECDC', "date_reported")
-    europe_date_reported = EcdcDateReported.get_by_id(europe_date_reported_id)
-    page_data = EcdcData.find_by_date_reported_order_by_notification_rate(europe_date_reported, page)
+    ecdc_date_reported = EcdcDateReported.get_by_id(date_reported_id)
+    page_data = EcdcData.get_by_date_reported_order_by_notification_rate(ecdc_date_reported, page)
     return render_template(
         'ecdc/date_reported/notification/ecdc_date_reported_one_notification_rate.html',
-        europe_date_reported=europe_date_reported,
+        ecdc_date_reported=ecdc_date_reported,
         page_data=page_data,
         page_info=page_info)
 
 
-@app_ecdc.route('/date_reported/deaths_weekly/<int:europe_date_reported_id>/page/<int:page>')
-@app_ecdc.route('/date_reported/deaths_weekly/<int:europe_date_reported_id>')
-def url_ecdc_date_reported_one_deaths_weekly(europe_date_reported_id, page=1):
+@app_ecdc.route('/date_reported/deaths_weekly/<int:date_reported_id>/page/<int:page>')
+@app_ecdc.route('/date_reported/deaths_weekly/<int:date_reported_id>')
+def url_ecdc_date_reported_one_deaths_weekly(date_reported_id, page=1):
     page_info = WebPageContent('ECDC', "date_reported")
-    europe_date_reported = EcdcDateReported.get_by_id(europe_date_reported_id)
-    page_data = EcdcData.find_by_date_reported_order_by_deaths_weekly(europe_date_reported, page)
+    ecdc_date_reported = EcdcDateReported.get_by_id(date_reported_id)
+    page_data = EcdcData.get_by_date_reported_order_by_deaths(ecdc_date_reported, page)
     return render_template(
         'ecdc/date_reported/deaths/ecdc_date_reported_one_deaths_weekly.html',
-        europe_date_reported=europe_date_reported,
+        ecdc_date_reported=ecdc_date_reported,
         page_data=page_data,
         page_info=page_info)
 
 
-@app_ecdc.route('/date_reported/cases_weekly/<int:europe_date_reported_id>/page/<int:page>')
-@app_ecdc.route('/date_reported/cases_weekly/<int:europe_date_reported_id>')
-def url_ecdc_date_reported_one_cases_weekly(europe_date_reported_id, page=1):
+@app_ecdc.route('/date_reported/cases_weekly/<int:date_reported_id>/page/<int:page>')
+@app_ecdc.route('/date_reported/cases_weekly/<int:date_reported_id>')
+def url_ecdc_date_reported_one_cases_weekly(date_reported_id, page=1):
     page_info = WebPageContent('ECDC', "date_reported")
-    europe_date_reported = EcdcDateReported.get_by_id(europe_date_reported_id)
-    page_data = EcdcData.find_by_date_reported_order_by_cases_weekly(europe_date_reported, page)
+    ecdc_date_reported = EcdcDateReported.get_by_id(date_reported_id)
+    page_data = EcdcData.get_by_date_reported_order_by_cases(ecdc_date_reported, page)
     return render_template(
         'ecdc/date_reported/cases/ecdc_date_reported_one_cases_weekly.html',
-        europe_date_reported=europe_date_reported,
+        ecdc_date_reported=ecdc_date_reported,
         page_data=page_data,
         page_info=page_info)
 
@@ -135,7 +134,7 @@ def url_ecdc_continent_all(page=1):
 def url_ecdc_continent_one(continent_id: int, page=1):
     page_info = WebPageContent('ECDC', "continent")
     continent = EcdcContinent.get_by_id(continent_id)
-    page_data = EcdcCountry.find_by_location_group(continent, page)
+    page_data = EcdcCountry.get_by_location_group(continent, page)
     return render_template(
         'ecdc/continent/one/ecdc_continent_one.html',
         continent=continent,
@@ -159,7 +158,7 @@ def url_ecdc_country_all(page=1):
 def url_ecdc_country_one(country_id, page=1):
     page_info = WebPageContent('ECDC', "country")
     europe_country = EcdcCountry.get_by_id(country_id)
-    page_data = EcdcData.find_by_location(europe_country, page)
+    page_data = EcdcData.get_by_location(europe_country, page)
     return render_template(
         'ecdc/country/one/ecdc_country_one.html',
         europe_country=europe_country,
@@ -175,7 +174,7 @@ def url_ecdc_country_germany(page=1):
     if europe_country is None:
         flash('country: Germany not found in Database', category='error')
         return redirect(url_for('ecdc.url_ecdc_tasks'))
-    page_data = EcdcData.find_by_location(europe_country, page)
+    page_data = EcdcData.get_by_location(europe_country, page)
     return render_template(
         'ecdc/country/germany/ecdc_country_germany.html',
         europe_country=europe_country,
diff --git a/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/date_reported/all/ecdc_date_reported_all_table.html b/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/date_reported/all/ecdc_date_reported_all_table.html
index fc0c499e..61e9b4fb 100644
--- a/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/date_reported/all/ecdc_date_reported_all_table.html
+++ b/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/date_reported/all/ecdc_date_reported_all_table.html
@@ -9,24 +9,24 @@
             </tr>
         </thead>
         <tbody>
-        {% for europe_date_reported in page_data.items %}
+        {% for ecdc_date_reported in page_data.items %}
             <tr>
                 <td class="text-right">
-                    {{ europe_date_reported.get_name_for_weekday() }}
+                    {{ ecdc_date_reported.get_name_for_weekday() }}
                 </td>
                 <td class="text-left">
-                    <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_notification_rate', europe_date_reported_id=europe_date_reported.id) }}">
-                        {{ europe_date_reported }}
+                    <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_notification_rate', date_reported_id=ecdc_date_reported.id) }}">
+                        {{ ecdc_date_reported }}
                     </a>
                 </td>
                 <td class="text-right">
-                    {{ europe_date_reported.week_of_year }}
+                    {{ ecdc_date_reported.week_of_year }}
                 </td>
                 <td class="text-left">
-                    {{ europe_date_reported.year }}
+                    {{ ecdc_date_reported.year }}
                 </td>
                 <td class="text-left">
-                    {{ europe_date_reported.day_of_year }}
+                    {{ ecdc_date_reported.day_of_year }}
                 </td>
             </tr>
         {% endfor %}
diff --git a/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/date_reported/notification/ecdc_date_reported_one_notification_rate_pagination.html b/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/date_reported/notification/ecdc_date_reported_one_notification_rate_pagination.html
index 06e66841..10755f52 100644
--- a/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/date_reported/notification/ecdc_date_reported_one_notification_rate_pagination.html
+++ b/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/date_reported/notification/ecdc_date_reported_one_notification_rate_pagination.html
@@ -4,8 +4,7 @@
                     <li class="page-item">
                         <a class="page-link"
                            href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_notification_rate',
-                           europe_date_reported_id=europe_date_reported.id,
-                           page=page_data.prev_num) }}">
+                           date_reported_id=ecdc_date_reported.id, page=page_data.prev_num) }}">
                             Previous
                         </a>
                     </li>
@@ -16,7 +15,7 @@
                                 <li class="page-item">
                                     <a class="page-link"
                                        href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_notification_rate',
-                                        europe_date_reported_id=europe_date_reported.id,
+                                        date_reported_id=ecdc_date_reported.id,
                                         page=page_num) }}">
                                         {{ page_num }}
                                     </a>
@@ -36,7 +35,7 @@
                     <li class="page-item">
                         <a class="page-link"
                            href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_notification_rate',
-                                        europe_date_reported_id=europe_date_reported.id, page=page_data.next_num) }}">
+                                        date_reported_id=ecdc_date_reported.id, page=page_data.next_num) }}">
                             Next
                         </a>
                     </li>
diff --git a/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/ecdc_data/ecdc_data_table.html b/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/ecdc_data/ecdc_data_table.html
index 28946e5b..990d9350 100644
--- a/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/ecdc_data/ecdc_data_table.html
+++ b/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/ecdc_data/ecdc_data_table.html
@@ -1,78 +1,11 @@
     <table class="table table-hover table-striped table-dark">
         <thead class="table-secondary">
-            <tr>
-                <th scope="col">
-                    <a href="{{ url_for('ecdc.url_ecdc_date_reported_all') }}">
-                        date rep
-                    </a>
-                </th>
-                <th scope="col">year_week</th>
-                <th scope="col">
-                    {% if europe_date_reported %}
-                        <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_cases_weekly',
-                            europe_date_reported_id=europe_date_reported.id,
-                            page=page_data.page) }}">
-                            deaths weekly
-                        </a>
-                    {% endif %}
-                    {% if europe_country %}
-                        deaths weekly
-                    {% endif %}
-                </th>
-                <th scope="col">
-                    {% if europe_date_reported %}
-                        <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_cases_weekly',
-                            europe_date_reported_id=europe_date_reported.id,
-                            page=page_data.page) }}">
-                            cases weekly
-                        </a>
-                    {% endif %}
-                    {% if europe_country %}
-                        cases weekly
-                    {% endif %}
-                </th>
-                <th scope="col">
-                    {% if europe_date_reported %}
-                        <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_cases_weekly',
-                                    europe_date_reported_id=europe_date_reported.id,
-                                    page=page_data.page) }}">
-                            notification rate per 100000 population 14days
-                        </a>
-                    {% endif %}
-                    {% if europe_country %}
-                        notification rate per 100000 population 14days
-                    {% endif %}
-                </th>
-                <th scope="col">
-                    <a href="{{ url_for( 'ecdc.url_ecdc_country_all') }}">
-                        countries and territories
-                    </a>
-                </th>
-                <th scope="col">population data 2019</th>
-                <th scope="col">
-                    <a href="{{ url_for('ecdc.url_ecdc_country_all') }}">
-                        geo id
-                    </a>
-                </th>
-                <th scope="col">
-                    <a href="{{ url_for('ecdc.url_ecdc_country_all') }}">
-                        country territory code
-                    </a>
-                </th>
-                <th scope="col">
-                    <a href="{{ url_for('ecdc.url_ecdc_continent_all') }}">
-                        continent
-                    </a>
-                </th>
-            </tr>
+            {% include 'ecdc/ecdc_data/ecdc_data_table_head.html' %}
         </thead>
         <tbody>
         {% for ecdc_data in page_data.items %}
             <tr>
                 <td>
-                    <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_notification_rate',
-                                    date_reported_id=ecdc_data.date_reported.id,
-                                    page=page_data.page) }}">
                     <a href="/ecdc/date_reported/{{ ecdc_data.date_reported.id }}">
                         {{ ecdc_data.date_reported.datum }}
                     </a>
@@ -109,4 +42,7 @@
             </tr>
         {% endfor %}
         </tbody>
+        <tfoot>
+            {% include 'ecdc/ecdc_data/ecdc_data_table_head.html' %}
+        </tfoot>
     </table>
\ No newline at end of file
diff --git a/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/ecdc_data/ecdc_data_table_head.html b/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/ecdc_data/ecdc_data_table_head.html
new file mode 100644
index 00000000..edaabb30
--- /dev/null
+++ b/src/flask_covid19/blueprints/data_ecdc/templates/ecdc/ecdc_data/ecdc_data_table_head.html
@@ -0,0 +1,65 @@
+            <tr>
+                <th scope="col">
+                    <a href="{{ url_for('ecdc.url_ecdc_date_reported_all') }}">
+                        date rep
+                    </a>
+                </th>
+                <th scope="col">year_week</th>
+                <th scope="col">
+                    {% if europe_date_reported %}
+                        <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_cases_weekly',
+                            europe_date_reported_id=europe_date_reported.id,
+                            page=page_data.page) }}">
+                            deaths weekly
+                        </a>
+                    {% endif %}
+                    {% if europe_country %}
+                        deaths weekly
+                    {% endif %}
+                </th>
+                <th scope="col">
+                    {% if europe_date_reported %}
+                        <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_cases_weekly',
+                            date_reported_id=date_reported.id,
+                            page=page_data.page) }}">
+                            cases weekly
+                        </a>
+                    {% endif %}
+                    {% if europe_country %}
+                        cases weekly
+                    {% endif %}
+                </th>
+                <th scope="col">
+                    {% if europe_date_reported %}
+                        <a href="{{ url_for( 'ecdc.url_ecdc_date_reported_one_cases_weekly',
+                                    date_reported_id=date_reported.id,
+                                    page=page_data.page) }}">
+                            notification rate per 100000 population 14days
+                        </a>
+                    {% endif %}
+                    {% if europe_country %}
+                        notification rate per 100000 population 14days
+                    {% endif %}
+                </th>
+                <th scope="col">
+                    <a href="{{ url_for( 'ecdc.url_ecdc_country_all') }}">
+                        countries and territories
+                    </a>
+                </th>
+                <th scope="col">population data 2019</th>
+                <th scope="col">
+                    <a href="{{ url_for('ecdc.url_ecdc_country_all') }}">
+                        geo id
+                    </a>
+                </th>
+                <th scope="col">
+                    <a href="{{ url_for('ecdc.url_ecdc_country_all') }}">
+                        country territory code
+                    </a>
+                </th>
+                <th scope="col">
+                    <a href="{{ url_for('ecdc.url_ecdc_continent_all') }}">
+                        continent
+                    </a>
+                </th>
+            </tr>
-- 
GitLab