11#!/usr/bin/env python
22from datetime import datetime
3- from dateutil . relativedelta import relativedelta
3+
44# the python library for elabftw
55import elabapi_python
66from client import api_client
7+ from dateutil .relativedelta import relativedelta
78
89#####################
910# DESCRIPTION #
1617DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
1718# we want to archive users not logged in after 8 months
1819INACTIVE_PERIOD_MONTHS = 8
20+ # The team ID of the user we want to archive
21+ USER_TEAM_ID = 1
22+
1923
2024def 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
2833usersApi = 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