Skip to content

Commit

Permalink
Merge pull request #75 from Evgeniy-Golodnykh/develop
Browse files Browse the repository at this point in the history
add cron script to delete inactive users
  • Loading branch information
Evgeniy-Golodnykh authored Feb 9, 2025
2 parents db8a182 + 929f19f commit 9097543
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
30 changes: 26 additions & 4 deletions blackfox/api/cron.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime as dt
import logging
import time

Expand All @@ -18,8 +19,13 @@

User = get_user_model()

error_message = 'Updating data for user "{user}" failed with error "{err}"'
successful_message = 'Fatsecret data for user "{user}" successfully updated'
fooddiary_autoupdate_error_message = (
'Updating data for user "{user}" failed with error "{err}"'
)
fooddiary_autoupdate_successful_message = (
'Fatsecret data for user "{user}" successfully updated'
)
delete_inactive_user_message = 'Inactive user "{user}" has been deleted'


def fooddiary_autoupdate():
Expand All @@ -34,8 +40,24 @@ def fooddiary_autoupdate():
try:
objs = get_fooddiary_objects(user)
except Exception as err:
logging.error(error_message.format(user=user.username, err=err))
logging.error(fooddiary_autoupdate_error_message.format(
user=user.username, err=err
))
continue
FoodDiary.objects.bulk_create(objs=objs)
logging.info(successful_message.format(user=user.username))
logging.info(fooddiary_autoupdate_successful_message.format(
user=user.username
))
time.sleep(1)


def delete_inactive_users():
"""A function for Cron to delete inactive users."""

inactive_users = User.objects.filter(Q(is_active=False))
for user in inactive_users:
if (dt.date.today() - user.date_joined.date()).days > 1:
User.objects.filter(id=user.id).delete()
logging.info(delete_inactive_user_message.format(
user=user.username
))
1 change: 1 addition & 0 deletions blackfox/blackfox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@

CRONJOBS = [
('0 1 * * *', 'api.cron.fooddiary_autoupdate', '>> /var/log/cron.log 2>&1'),
('0 2 * * *', 'api.cron.delete_inactive_users', '>> /var/log/cron.log 2>&1'),
]

SPECTACULAR_SETTINGS = {
Expand Down

0 comments on commit 9097543

Please sign in to comment.