-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
86 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,9 @@ | |
from django.utils.timezone import now | ||
from django.contrib.sites.models import Site | ||
from django.test import RequestFactory | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
from auth_and_perms.models import DeleteUserList | ||
from auth_and_perms.users import delete_user | ||
from auth_and_perms.users import delete_user, send_email_delete_user_warning | ||
from auth_and_perms.users import send_email_user_management | ||
|
||
|
||
|
@@ -20,14 +20,21 @@ def enqueue_users(self): | |
deletelist = [] | ||
for user in User.objects.exclude(username="[email protected]").filter( | ||
deleteuserlist__isnull=True, | ||
last_login__lte=year_ago): | ||
last_login__gte=year_ago): | ||
deletelist.append(DeleteUserList(user=user)) | ||
if deletelist: | ||
DeleteUserList.objects.bulk_create(deletelist) | ||
|
||
for obj in deletelist: | ||
send_email_delete_user_warning(obj.user, "30", "working days") | ||
|
||
print("Actual: ", User.objects.all().count()) | ||
print("Remove: ", len(deletelist)) | ||
|
||
def get_now(self): | ||
# fixme: remove this and put now() only | ||
return now() #+ relativedelta(months=1, days=1) | ||
|
||
def delete_users(self): | ||
User = get_user_model() | ||
site = Site.objects.all().last() | ||
|
@@ -39,8 +46,10 @@ def delete_users(self): | |
request.META['SERVER_PORT'] = "80" if settings.DEBUG else "443" | ||
user_base = User.objects.filter(username="[email protected]").first() | ||
del_count = 0 | ||
for user_delete in DeleteUserList.objects.filter(expiration_date__lte=now()): | ||
for user_delete in DeleteUserList.objects.filter(expiration_date__lte= | ||
self.get_now()): | ||
send_email_user_management(request, user_base, user_delete, "delete") | ||
print(user_delete.user.username) | ||
delete_user(user_delete.user, user_base) | ||
del_count += 1 | ||
print("Delete users", del_count) | ||
|
@@ -50,6 +59,3 @@ def delete_users(self): | |
def handle(self, *args, **options): | ||
self.enqueue_users() | ||
self.delete_users() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import importlib | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
from django.conf import settings | ||
from django.db.models import ExpressionWrapper, Q, F, BooleanField | ||
|
||
from dateutil.relativedelta import relativedelta | ||
from django.utils.timezone import now | ||
from auth_and_perms.models import DeleteUserList | ||
from auth_and_perms.users import send_email_delete_user_warning, \ | ||
warning_notification_delete_user | ||
|
||
app = importlib.import_module(settings.CELERY_MODULE).app | ||
|
||
@app.task() | ||
def update_delete_users_list(): | ||
recent_last_login = ExpressionWrapper(Q(last_login__gte=F('creation_date')), | ||
output_field=BooleanField()) | ||
DeleteUserList.objects.annotate(equal=recent_last_login).filter(equal=True).delete() | ||
DeleteUserList.objects.annotate(user_recent_last_login=recent_last_login).filter( | ||
user_recent_last_login=True).delete() | ||
|
||
@app.task() | ||
def send_notification_warning_delete_user(): | ||
eight_working_days = now() + relativedelta(days=8) | ||
one_working_day = now() + relativedelta(days=1) | ||
|
||
eight_days_queryset = DeleteUserList.objects.filter( | ||
expiration_date=eight_working_days) | ||
|
||
one_day_queryset = DeleteUserList.objects.filter(expiration_date=one_working_day) | ||
|
||
warning_notification_delete_user(eight_days_queryset, "8", "working days") | ||
warning_notification_delete_user(one_day_queryset, "1", "working day") | ||
|
5 changes: 5 additions & 0 deletions
5
src/auth_and_perms/templates/auth_and_perms/mail/en/user_delete_warning.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% extends 'gentelella/registration/email_base.html' %} | ||
{% load i18n %} | ||
{% block content %} | ||
{% trans 'Dear' %} {{user.get_full_name|default_if_none:user.username}}, | ||
{% endblock %} |
12 changes: 12 additions & 0 deletions
12
src/auth_and_perms/templates/auth_and_perms/mail/es/user_delete_warning.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{% extends 'gentelella/registration/email_base.html' %} | ||
{% load i18n %} | ||
{% block content %} | ||
Estimado/a {{user.get_full_name|default_if_none:user.username}}, | ||
<br><br> | ||
|
||
Por este medio le hacemos saber que usted cuenta con un año de inactividad en nuestra plataforma Organilab y | ||
cuenta con {{available_time}} para volver a ingresar antes de que su usuario sea eliminado. | ||
|
||
<br><br> | ||
En caso de que no recuerde su contraseña, puede utilizar el siguiente enlace para recuperarla: <a href="{{domain}}{% url 'password_reset' %}" target="_blank">Clic aquí</a> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters