2024-01-16 14:29:03 +01:00
|
|
|
from typing import Any, List
|
2021-06-18 00:06:02 +02:00
|
|
|
|
2023-03-04 01:52:14 +01:00
|
|
|
from django.db.models import QuerySet
|
2021-06-18 00:06:02 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
2024-01-29 13:47:19 +01:00
|
|
|
from corporate.lib.activity import format_date_for_activity_reports, make_table
|
2021-06-18 00:06:02 +02:00
|
|
|
from zerver.decorator import require_server_admin
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models import UserActivity, UserProfile
|
|
|
|
from zerver.models.users import get_user_profile_by_id
|
2021-06-18 00:06:02 +02:00
|
|
|
|
|
|
|
|
2022-06-23 20:44:31 +02:00
|
|
|
def get_user_activity_records(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
) -> QuerySet[UserActivity]:
|
2021-06-18 00:06:02 +02:00
|
|
|
fields = [
|
|
|
|
"query",
|
|
|
|
"client__name",
|
|
|
|
"count",
|
|
|
|
"last_visit",
|
|
|
|
]
|
|
|
|
|
2024-01-16 14:29:03 +01:00
|
|
|
records = (
|
|
|
|
UserActivity.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
)
|
|
|
|
.order_by("-last_visit")
|
|
|
|
.select_related("client")
|
|
|
|
.only(*fields)
|
2021-06-18 00:06:02 +02:00
|
|
|
)
|
|
|
|
return records
|
|
|
|
|
|
|
|
|
2024-01-16 14:29:03 +01:00
|
|
|
@require_server_admin
|
|
|
|
def get_user_activity(request: HttpRequest, user_profile_id: int) -> HttpResponse:
|
|
|
|
user_profile = get_user_profile_by_id(user_profile_id)
|
|
|
|
records = get_user_activity_records(user_profile)
|
|
|
|
|
2021-06-18 00:06:02 +02:00
|
|
|
cols = [
|
2024-01-16 14:29:03 +01:00
|
|
|
"Query",
|
|
|
|
"Client",
|
|
|
|
"Count",
|
|
|
|
"Last visit",
|
2021-06-18 00:06:02 +02:00
|
|
|
]
|
|
|
|
|
2022-06-23 20:44:31 +02:00
|
|
|
def row(record: UserActivity) -> List[Any]:
|
2021-06-18 00:06:02 +02:00
|
|
|
return [
|
|
|
|
record.query,
|
|
|
|
record.client.name,
|
|
|
|
record.count,
|
|
|
|
format_date_for_activity_reports(record.last_visit),
|
|
|
|
]
|
|
|
|
|
|
|
|
rows = list(map(row, records))
|
2024-01-16 14:29:03 +01:00
|
|
|
title = f"{user_profile.delivery_email} ({user_profile.realm.name})"
|
|
|
|
content = make_table(title, cols, rows)
|
2021-06-18 00:06:02 +02:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2024-01-29 14:53:53 +01:00
|
|
|
"corporate/activity/activity.html",
|
2024-01-16 14:29:03 +01:00
|
|
|
context=dict(
|
|
|
|
data=content,
|
|
|
|
title=title,
|
|
|
|
is_home=False,
|
|
|
|
),
|
2021-06-18 00:06:02 +02:00
|
|
|
)
|