Skip to content

Commit

Permalink
User delete warning notification
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelagz committed May 22, 2024
1 parent 27f633b commit 64a3612
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -50,6 +59,3 @@ def delete_users(self):
def handle(self, *args, **options):
self.enqueue_users()
self.delete_users()



24 changes: 21 additions & 3 deletions src/auth_and_perms/tasks.py
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")

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 %}
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 %}
24 changes: 24 additions & 0 deletions src/auth_and_perms/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ def send_delete_user_email(user_delete):
)
activate(oldlang)

def send_email_delete_user_warning(user_delete, days, msg):
if hasattr(user_delete, 'profile'):
lang = user_delete.profile.language
oldlang = get_language()
context = {'lang': lang, 'user': user_delete}
activate(lang)
context.update({"available_time": "%s %s" % (days, _(msg))})
send_mail(subject=_("Delete User Notification"),
message=_(""),
recipient_list=[user_delete.email],
from_email=settings.DEFAULT_FROM_EMAIL,
html_message=render_to_string(
"auth_and_perms/mail/"+lang+"/user_delete_warning.html",
context=context
)
)
activate(oldlang)


def merge_information_user(to_delete, to_related):
for field in to_delete._meta.get_fields():
if field.name == 'sga_substance':
Expand Down Expand Up @@ -83,3 +102,8 @@ def delete_user(to_delete, to_related):
def user_management(request, user_base, user_delete, action):
send_email_user_management(request, user_base, user_delete, action)
delete_user(user_delete, user_base)


def warning_notification_delete_user(queryset, days, msg):
for obj in queryset:
send_email_delete_user_warning(obj.user, days, msg)
5 changes: 5 additions & 0 deletions src/auth_and_perms/views/users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.models import User
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse
Expand All @@ -8,10 +9,13 @@
from auth_and_perms.users import user_management


@permission_required('auth_and_perms.can_manage_users')
def users_list(request):
return render(request,"auth_and_perms/users_list.html", context={
"merge_form": MergeUsers()})


@permission_required('auth_and_perms.can_manage_users')
def merge_users(request, user_base, user_delete):
user_base_instance = get_object_or_404(User, pk=user_base)
user_delete_instance = get_object_or_404(User, pk=user_delete)
Expand All @@ -26,6 +30,7 @@ def merge_users(request, user_base, user_delete):
return render(request, "auth_and_perms/merge_users.html", context=context)


@permission_required('auth_and_perms.can_manage_users')
def save_user_merge(request, user_base, user_delete):
user_base_instance = get_object_or_404(User, pk=user_base)
user_delete_instance = get_object_or_404(User, pk=user_delete)
Expand Down
6 changes: 6 additions & 0 deletions src/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -4491,3 +4491,9 @@ msgstr "Por favor use un cliente de lectura html"

msgid "User delete invalid"
msgstr "Usuario a eliminar inválido"

msgid "working days"
msgstr "días hábiles"

msgid "working day"
msgstr "día hábil"

0 comments on commit 64a3612

Please sign in to comment.