Skip to content

Commit

Permalink
[change] Changed the delete_old_radiusbatch_users command to use days…
Browse files Browse the repository at this point in the history
… as well openwisp#525

Added the option to delete users by specifying days or months.If nothing is provided 18 months will be taken as default.

Fixes openwisp#525
  • Loading branch information
kaushikaryan04 committed Jul 9, 2024
1 parent 5283833 commit c755252
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
5 changes: 3 additions & 2 deletions docs/source/user/management_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ This command deactivates expired user accounts which were created temporarily
--------------------------------

This command deletes users that have expired (and should have been deactivated by
``deactivate_expired_users``) for more than the specified ``<duration_in_months>``.
``deactivate_expired_users``) for more than the duration specified by ``--older-than-days <duration_in_days>``
or ``--older-than-months `<duration_in_months>``.

.. code-block:: shell
./manage.py delete_old_radiusbatch_users --older-than-months <duration_in_months>
./manage.py delete_old_radiusbatch_users --older-than-days <duration_in_days>
Note that the default duration is set to 18 months.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,29 @@ def add_arguments(self, parser):
parser.add_argument(
'--older-than-days',
action='store',
type=int,
help='Delete users that are older than days provided',
)
parser.add_argument(
'--older-than-months',
action='store',
type=int,
default=BATCH_DELETE_EXPIRED,
help='delete users which have expired before this time',
help='Delete users that are older than months provided',
)

def handle(self, *args, **options):
days = now() - timedelta(days=int(options['older_than_days']))
batches = RadiusBatch.objects.filter(expiration_date__lt=days)
days = options.get('older_than_days')
months = options.get('older_than_months')

if days is not None:
threshold_date = now() - timedelta(days=days)
else:
threshold_date = now() - timedelta(days=30 * months)

batches = RadiusBatch.objects.filter(expiration_date__lt=threshold_date)
time_period = threshold_date.strftime('%Y-%m-%d %H:%M:%S')

for b in batches:
b.delete()
self.stdout.write(
f'Deleted accounts older than {options["older_than_days"]} days'
)
self.stdout.write(f'Deleted accounts older than {time_period}')
2 changes: 1 addition & 1 deletion openwisp_radius/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_default_password_reset_url(urls):
DEFAULT_SECRET_FORMAT = get_settings_value('DEFAULT_SECRET_FORMAT', 'NT-Password')
DISABLED_SECRET_FORMATS = get_settings_value('DISABLED_SECRET_FORMATS', [])
BATCH_DEFAULT_PASSWORD_LENGTH = get_settings_value('BATCH_DEFAULT_PASSWORD_LENGTH', 8)
BATCH_DELETE_EXPIRED = get_settings_value('BATCH_DELETE_EXPIRED', 30 * 18)
BATCH_DELETE_EXPIRED = get_settings_value('BATCH_DELETE_EXPIRED', 18)
BATCH_MAIL_SUBJECT = get_settings_value('BATCH_MAIL_SUBJECT', 'Credentials')
BATCH_MAIL_SENDER = get_settings_value('BATCH_MAIL_SENDER', settings.DEFAULT_FROM_EMAIL)
API_AUTHORIZE_REJECT = get_settings_value('API_AUTHORIZE_REJECT', False)
Expand Down

0 comments on commit c755252

Please sign in to comment.