Skip to content

Commit f68e616

Browse files
authored
examples: fix archive user example (#51)
Fix patch request body for archiving user
1 parent 08e3189 commit f68e616

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

examples/18-archive-users.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env python
22
from datetime import datetime
3-
from dateutil.relativedelta import relativedelta
3+
44
# the python library for elabftw
55
import elabapi_python
66
from client import api_client
7+
from dateutil.relativedelta import relativedelta
78

89
#####################
910
# DESCRIPTION #
@@ -16,6 +17,9 @@
1617
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
1718
# we want to archive users not logged in after 8 months
1819
INACTIVE_PERIOD_MONTHS = 8
20+
# The team ID of the user we want to archive
21+
USER_TEAM_ID = 1
22+
1923

2024
def should_be_archived(date_string):
2125
date_format = "%Y-%m-%d %H:%M:%S"
@@ -24,6 +28,7 @@ def should_be_archived(date_string):
2428
threshold_date = current_date - relativedelta(months=INACTIVE_PERIOD_MONTHS)
2529
return input_date < threshold_date
2630

31+
2732
# Load the users api
2833
usersApi = elabapi_python.UsersApi(api_client)
2934

@@ -37,7 +42,16 @@ def should_be_archived(date_string):
3742
# also prevent archival of sysadmin accounts (server will throw error anyway)
3843
if user.last_login is not None and user.is_sysadmin != 1:
3944
if should_be_archived(user.last_login):
40-
print(f'Archiving user {user.email}. Last login: {user.last_login}')
41-
usersApi.patch_user(user.userid, body={'action': 'archive'})
45+
print(f"Archiving user {user.email}. Last login: {user.last_login}")
46+
usersApi.patch_user(
47+
user.userid,
48+
body={
49+
'action': 'patchuser2team',
50+
'team': USER_TEAM_ID,
51+
'target': 'is_archived',
52+
'content': 1,
53+
},
54+
)
55+
# The user can un-archive by flipping the "content" value to 0
4256

4357
# TODO: fix last_login info is not provided anymore when user is not admin!

0 commit comments

Comments
 (0)