From c95c8fe8e7fae78e7d4c58f9a14eb5ba5f5487eb Mon Sep 17 00:00:00 2001 From: orertrr Date: Tue, 30 Jul 2024 21:07:26 +0800 Subject: [PATCH 1/4] feat: add dietary habit statistics information on project page Signed-off-by: orertrr --- module/users.py | 22 ++++++++++++- templates/project_statistics.html | 50 ++++++++++++++++++++++++++++++ templates/project_teams_index.html | 6 ++++ view/project.py | 24 ++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 templates/project_statistics.html diff --git a/module/users.py b/module/users.py index 91667c1..a693e38 100644 --- a/module/users.py +++ b/module/users.py @@ -7,7 +7,7 @@ from models.oauth_db import OAuthDB from models.users_db import PolicySignedDB, TobeVolunteerDB, UsersDB -from module.dietary_habit import DietaryHabitItemsValue +from module.dietary_habit import DietaryHabitItemsName, DietaryHabitItemsValue from module.mattermost_bot import MattermostTools from module.skill import TobeVolunteerStruct from structs.users import PolicyType @@ -425,6 +425,26 @@ def marshal_dietary_habit(user_infos: dict[str, Any]) -> Generator[dict[str, Any yield data + @staticmethod + def get_dietary_habit_statistics(uids: list[str]): + ''' Get dietary habit statistics by given uids ''' + result: dict[str, int] = { } + + for item in DietaryHabitItemsName: + result[item.value] = 0 + + user_infos = User.get_info(uids=uids) + for user_info in user_infos.values(): + if "profile_real" not in user_info or \ + "dietary_habit" not in user_info["profile_real"]: + continue + + for habit in user_info["profile_real"]["dietary_habit"]: + habit_enum_name = DietaryHabitItemsValue(habit).name + result[DietaryHabitItemsName[habit_enum_name].value] += 1 + + return result + class TobeVolunteer: ''' TobeVolunteer ''' diff --git a/templates/project_statistics.html b/templates/project_statistics.html new file mode 100644 index 0000000..03a8796 --- /dev/null +++ b/templates/project_statistics.html @@ -0,0 +1,50 @@ +{% extends "base.html" %} +{% block head_title %}{{project.name}} 統計資訊{% endblock %} +{% block body %} +
+
+

+ {{project.name}} +

+
+

{{project.desc}}

+ {% if editable %} + + {% endif %} +
+
+
+
+
+ +
+
+

飲食習慣人數分布

+ + + + + + + {% for habit, count in habit_statistics.items() %} + + + + + {% endfor %} + +
習慣人數
{{ habit }}{{ count }}
+
+
+
+
+{% endblock %} diff --git a/templates/project_teams_index.html b/templates/project_teams_index.html index 679216e..7004c5c 100644 --- a/templates/project_teams_index.html +++ b/templates/project_teams_index.html @@ -20,6 +20,12 @@

+

總人數:{{total}}

diff --git a/view/project.py b/view/project.py index 410a9cd..fc97eb2 100644 --- a/view/project.py +++ b/view/project.py @@ -496,6 +496,30 @@ def team_page(pid: str) -> str | ResponseBase: ) +@VIEW_PROJECT.route('//statistics', methods=('GET',)) +def project_statictics(pid: str) -> str | ResponseBase: + ''' Statistics page ''' + project = Project.get(pid) + if not project: + return Response('no data', status=404) + + editable = g.user['account']['_id'] in project.owners + + all_users = set() + for team in Team.list_by_pid(pid=pid): + if team.chiefs: + all_users.update(team.chiefs) + if team.members: + all_users.update(team.members) + + habit_statistics = User.get_dietary_habit_statistics(uids=list(all_users)) + + return render_template('./project_statistics.html', + project=project.dict(by_alias=True), + habit_statistics=habit_statistics, + editable=editable) + + @VIEW_PROJECT.route('//form_traffic_mapping', methods=('GET', 'POST')) def project_form_traffic_mapping(pid: str) -> str | ResponseBase: ''' Project form traffic mapping ''' From 8848c2273debbe08259a1fa7f9ae284787661247 Mon Sep 17 00:00:00 2001 From: orertrr Date: Tue, 30 Jul 2024 21:08:44 +0800 Subject: [PATCH 2/4] feat: add clothes statistics information on project page Signed-off-by: orertrr --- module/form.py | 21 +++++++++++++++++++++ templates/project_statistics.html | 17 +++++++++++++++++ view/project.py | 2 ++ 3 files changed, 40 insertions(+) diff --git a/module/form.py b/module/form.py index 1860b67..21a93bc 100644 --- a/module/form.py +++ b/module/form.py @@ -262,6 +262,27 @@ def all_clothes(pid: str) -> Generator[dict[str, Any], None, None]: ''' yield from FormDB().find({'case': 'clothes', 'pid': pid}) + @staticmethod + def get_clothes_statistics(pid: str) -> dict[str, int]: + ''' Get clothes statistics by given pid ''' + result = { + 'XS': 0, + 'S': 0, + 'M': 0, + 'L': 0, + 'XL': 0, + '2XL': 0, + '3XL': 0, + '4XL': 0, + '5XL': 0, + '6XL': 0, + } + + for form in Form.all_clothes(pid=pid): + result[form['data']['clothes']] += 1 + + return result + @staticmethod def update_parking_card(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]: ''' Update parking card diff --git a/templates/project_statistics.html b/templates/project_statistics.html index 03a8796..d47122c 100644 --- a/templates/project_statistics.html +++ b/templates/project_statistics.html @@ -44,6 +44,23 @@

飲食習慣人數分布

+
+

衣服尺寸人數分布

+ + + + + + + {% for clothes_size, count in clothes_statistics.items() %} + + + + + {% endfor %} + +
尺寸人數
{{ clothes_size }}{{ count }}
+
diff --git a/view/project.py b/view/project.py index fc97eb2..a394d3c 100644 --- a/view/project.py +++ b/view/project.py @@ -513,10 +513,12 @@ def project_statictics(pid: str) -> str | ResponseBase: all_users.update(team.members) habit_statistics = User.get_dietary_habit_statistics(uids=list(all_users)) + clothes_statistics = Form.get_clothes_statistics(pid=pid) return render_template('./project_statistics.html', project=project.dict(by_alias=True), habit_statistics=habit_statistics, + clothes_statistics=clothes_statistics, editable=editable) From 0e8037d96efcd2e2e1a20b83400e59ae7b10195e Mon Sep 17 00:00:00 2001 From: orertrr Date: Tue, 30 Jul 2024 21:09:31 +0800 Subject: [PATCH 3/4] feat: add accmodation statistics information on project page Signed-off-by: orertrr --- module/form.py | 13 +++++++++++++ templates/project_statistics.html | 24 ++++++++++++++++++++++++ view/project.py | 2 ++ 3 files changed, 39 insertions(+) diff --git a/module/form.py b/module/form.py index 21a93bc..5a9d2dd 100644 --- a/module/form.py +++ b/module/form.py @@ -450,6 +450,19 @@ def get(pid: str) -> Generator[dict[str, Any], None, None]: ''' yield from FormDB().find({'case': 'accommodation', 'pid': pid, 'data.key': {'$ne': 'no'}}) + @staticmethod + def get_statistics(pid: str) -> dict[str, int]: + ''' Get accommodation statistics by given pid ''' + result = { + 'yes': 0, + 'yes-longtraffic': 0, + } + + for form in FormAccommodation.get(pid=pid): + result[form['data']['key']] += 1 + + return result + @staticmethod def update_room(pid: str, uid: str, room: str, change_key: bool = True) -> dict[str, Any]: ''' Update room no diff --git a/templates/project_statistics.html b/templates/project_statistics.html index d47122c..c1a9235 100644 --- a/templates/project_statistics.html +++ b/templates/project_statistics.html @@ -61,6 +61,30 @@

衣服尺寸人數分布

+
+

住宿人數

+ + + + + + + {% for accmmodation_option, count in accommodation_statictics.items() %} + {% if accmmodation_option == 'yes' %} + + + + + {% elif accmmodation_option == 'yes-longtraffic' %} + + + + + {% endif %} + {% endfor %} + +
住宿選項人數
需要住宿{{ count }}
需要住宿,且通勤時間大於一小時{{ count }}
+
diff --git a/view/project.py b/view/project.py index a394d3c..8c5578c 100644 --- a/view/project.py +++ b/view/project.py @@ -514,11 +514,13 @@ def project_statictics(pid: str) -> str | ResponseBase: habit_statistics = User.get_dietary_habit_statistics(uids=list(all_users)) clothes_statistics = Form.get_clothes_statistics(pid=pid) + accommodation_statictics = FormAccommodation.get_statistics(pid=pid) return render_template('./project_statistics.html', project=project.dict(by_alias=True), habit_statistics=habit_statistics, clothes_statistics=clothes_statistics, + accommodation_statictics=accommodation_statictics, editable=editable) From 9a6390dc9162a470d821e65e4750561881e83c95 Mon Sep 17 00:00:00 2001 From: orertrr Date: Tue, 30 Jul 2024 23:20:05 +0800 Subject: [PATCH 4/4] fix: add return type annotation Signed-off-by: orertrr --- module/users.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/users.py b/module/users.py index a693e38..00a132c 100644 --- a/module/users.py +++ b/module/users.py @@ -426,7 +426,7 @@ def marshal_dietary_habit(user_infos: dict[str, Any]) -> Generator[dict[str, Any yield data @staticmethod - def get_dietary_habit_statistics(uids: list[str]): + def get_dietary_habit_statistics(uids: list[str]) -> dict[str, int]: ''' Get dietary habit statistics by given uids ''' result: dict[str, int] = { }