2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
from django.db.models import Q
|
2018-12-17 16:19:18 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import UserStatus
|
2018-12-17 16:19:18 +01:00
|
|
|
|
|
|
|
|
2020-08-07 04:58:22 +02:00
|
|
|
def get_user_info_dict(realm_id: int) -> Dict[str, Dict[str, Any]]:
|
2019-01-21 19:06:03 +01:00
|
|
|
rows = UserStatus.objects.filter(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile__realm_id=realm_id,
|
|
|
|
user_profile__is_active=True,
|
2019-01-21 19:06:03 +01:00
|
|
|
).exclude(
|
|
|
|
Q(status=UserStatus.NORMAL) &
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
Q(status_text=''),
|
2019-01-21 19:06:03 +01:00
|
|
|
).values(
|
|
|
|
'user_profile_id',
|
|
|
|
'status',
|
|
|
|
'status_text',
|
|
|
|
)
|
|
|
|
|
2020-09-02 08:14:51 +02:00
|
|
|
user_dict: Dict[str, Dict[str, Any]] = {}
|
2019-01-21 19:06:03 +01:00
|
|
|
for row in rows:
|
|
|
|
away = row['status'] == UserStatus.AWAY
|
|
|
|
status_text = row['status_text']
|
|
|
|
user_id = row['user_profile_id']
|
|
|
|
|
2020-09-02 08:14:51 +02:00
|
|
|
dct = {}
|
2019-01-21 19:06:03 +01:00
|
|
|
if away:
|
|
|
|
dct['away'] = away
|
|
|
|
if status_text:
|
|
|
|
dct['status_text'] = status_text
|
2018-12-17 16:19:18 +01:00
|
|
|
|
2020-08-07 04:58:22 +02:00
|
|
|
user_dict[str(user_id)] = dct
|
2019-01-21 19:06:03 +01:00
|
|
|
|
|
|
|
return user_dict
|
2018-12-17 16:19:18 +01:00
|
|
|
|
2019-01-21 18:19:59 +01:00
|
|
|
def update_user_status(user_profile_id: int,
|
2019-01-21 19:06:03 +01:00
|
|
|
status: Optional[int],
|
|
|
|
status_text: Optional[str],
|
2019-01-21 18:19:59 +01:00
|
|
|
client_id: int) -> None:
|
2018-12-17 16:19:18 +01:00
|
|
|
|
|
|
|
timestamp = timezone_now()
|
|
|
|
|
2019-01-21 18:19:59 +01:00
|
|
|
defaults = dict(
|
|
|
|
client_id=client_id,
|
|
|
|
timestamp=timestamp,
|
2018-12-17 16:19:18 +01:00
|
|
|
)
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
if status is not None:
|
|
|
|
defaults['status'] = status
|
|
|
|
|
|
|
|
if status_text is not None:
|
|
|
|
defaults['status_text'] = status_text
|
2019-01-21 18:19:59 +01:00
|
|
|
|
|
|
|
UserStatus.objects.update_or_create(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=user_profile_id,
|
2019-01-21 18:19:59 +01:00
|
|
|
defaults=defaults,
|
|
|
|
)
|