2024-07-12 02:30:17 +02:00
|
|
|
from typing import Any
|
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-08-28 16:39:03 +02:00
|
|
|
from corporate.lib.activity import (
|
|
|
|
ActivityHeaderEntry,
|
|
|
|
format_optional_datetime,
|
|
|
|
make_table,
|
|
|
|
user_support_link,
|
|
|
|
)
|
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)
|
|
|
|
|
2024-08-28 16:39:03 +02:00
|
|
|
title = f"{user_profile.full_name}"
|
2021-06-18 00:06:02 +02:00
|
|
|
cols = [
|
2024-01-16 14:29:03 +01:00
|
|
|
"Query",
|
|
|
|
"Client",
|
|
|
|
"Count",
|
2024-02-05 19:07:13 +01:00
|
|
|
"Last visit (UTC)",
|
2021-06-18 00:06:02 +02:00
|
|
|
]
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def row(record: UserActivity) -> list[Any]:
|
2021-06-18 00:06:02 +02:00
|
|
|
return [
|
|
|
|
record.query,
|
|
|
|
record.client.name,
|
|
|
|
record.count,
|
2024-02-05 19:07:13 +01:00
|
|
|
format_optional_datetime(record.last_visit),
|
2021-06-18 00:06:02 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
rows = list(map(row, records))
|
2024-08-28 16:39:03 +02:00
|
|
|
|
|
|
|
header_entries = []
|
|
|
|
header_entries.append(ActivityHeaderEntry(name="Email", value=user_profile.delivery_email))
|
|
|
|
header_entries.append(ActivityHeaderEntry(name="Realm", value=user_profile.realm.name))
|
|
|
|
|
|
|
|
user_support = user_support_link(user_profile.delivery_email)
|
|
|
|
|
|
|
|
content = make_table(title, cols, rows, header=header_entries, title_link=user_support)
|
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
|
|
|
)
|