-
-
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.
Add check_remove_user_notlogin as WIP
- Loading branch information
Showing
13 changed files
with
256 additions
and
46 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/auth_and_perms/management/commands/check_remove_user_notlogin.py
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,61 @@ | ||
from django.conf import settings | ||
from django.core.management import BaseCommand | ||
from django.contrib.auth import get_user_model | ||
from dateutil.relativedelta import relativedelta | ||
from django.utils.timezone import now | ||
from django.contrib.sites.models import Site | ||
from django.test import RequestFactory | ||
|
||
from auth_and_perms.models import DeleteUserList | ||
from auth_and_perms.users import delete_user | ||
from auth_and_perms.users import send_email_user_management | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def enqueue_users(self): | ||
User=get_user_model() | ||
User.objects.all() | ||
year_ago = now() + relativedelta(years=1) | ||
deletelist = [] | ||
for user in User.objects.exclude(username="[email protected]").filter( | ||
deleteuserlist__isnull=True, | ||
last_login__lte=year_ago): | ||
deletelist.append(DeleteUserList(user=user)) | ||
if deletelist: | ||
DeleteUserList.objects.bulk_create(deletelist) | ||
|
||
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() | ||
if site.domain not in settings.ALLOWED_HOSTS: | ||
settings.ALLOWED_HOSTS.append(site.domain) | ||
request = RequestFactory().get('/') | ||
|
||
request.META['HTTP_HOST']=site.domain | ||
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=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) | ||
user_delete.delete() | ||
del_count+=1 | ||
print("Delete users", del_count) | ||
print("Actual to users delete ", DeleteUserList.objects.all().count()) | ||
print("Actual to users ", User.objects.all().count()) | ||
|
||
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 |
---|---|---|
|
@@ -4,9 +4,16 @@ | |
|
||
def create_user_default(apps, schema_editor): | ||
User = apps.get_model('auth', 'User') | ||
User.objects.create(username="[email protected]", email="[email protected]", | ||
Profile = apps.get_model('auth_and_perms', 'Profile') | ||
user=User.objects.create(username="[email protected]", email="[email protected]", | ||
first_name="Usuario preservador de integridad") | ||
|
||
Profile.objects.create( | ||
user = user, | ||
phone_number = "8888888", | ||
id_card = "888888", | ||
job_position = "Integrity", | ||
language = 'es' | ||
) | ||
def delete_user_default(apps, schema_editor): | ||
User = apps.get_model('auth', 'User') | ||
User.objects.filter(username="[email protected]").delete() | ||
|
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,14 @@ | ||
# Generated by Django 4.1.10 on 2024-05-18 19:59 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('auth_and_perms', '0014_create_user_default'), | ||
('auth_and_perms', '0016_addpermissiontoadmingroup'), | ||
] | ||
|
||
operations = [ | ||
] |
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,26 @@ | ||
# Generated by Django 4.1.10 on 2024-05-18 19:59 | ||
|
||
import auth_and_perms.models | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('auth_and_perms', '0017_merge_20240518_1359'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='DeleteUserList', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('creation_date', models.DateTimeField(auto_now=True)), | ||
('expiration_date', models.DateTimeField(default=auth_and_perms.models.user_expiration_date)), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
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
22 changes: 22 additions & 0 deletions
22
src/auth_and_perms/templates/auth_and_perms/mail/en/user_delete_notification.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,22 @@ | ||
{% extends 'gentelella/registration/email_base.html' %} | ||
{% load i18n %} | ||
{% block content %} | ||
{% trans 'Dear' %} {{user_delete.get_full_name}}, | ||
<br><br> | ||
{% trans 'We would like to inform you that the user' %} <b>{{user_delete.username}}</b> {% trans 'was deleted. The reasons for this delete are as follows:' %} | ||
<br><br> | ||
1) {% trans 'Account inactive for a long time.' %} | ||
<br> | ||
2) {% trans 'System purification.' %} | ||
<br><br> | ||
{% trans 'This measure has been taken to optimize our resources, ensure more efficient management, and enhance the security of our services.' %} | ||
{% endif %} | ||
<br><br> | ||
{% trans 'If you have any questions or require further assistance, please do not hesitate to contact our support team. We will be glad to assist you.' %} | ||
<br><br> | ||
{% trans 'Sincerely,' %} | ||
<br><br> | ||
Organilab | ||
</p> | ||
|
||
{% endblock %} |
15 changes: 15 additions & 0 deletions
15
src/auth_and_perms/templates/auth_and_perms/mail/en/user_merge_notification.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,15 @@ | ||
{% extends 'gentelella/registration/email_base.html' %} | ||
{% load i18n %} | ||
{% block content %} | ||
{% trans 'Dear' %} {{user_base.get_full_name}}, | ||
<br><br> | ||
{% trans 'We would like to inform you that an update has been made to your account information. The reason for this update is as follows:' %} | ||
<br><br> | ||
{% trans 'Account consolidation by an administrator: An administrator from your organization has decided to consolidate the accounts that belong to you in order to centralize access. This action aims to simplify account management and provide you with a smoother experience when accessing our resources.' %} | ||
<br><br> | ||
{% trans 'From now on, you can log in to your account using the following username:' %} {{user_base.username}} | ||
<br><br> | ||
{% trans 'If you do not remember your password, you can use the following link to recover it:' %} <a href="{{domain}}{% url 'password_reset' %}" target="_blank">{% trans 'Click here' %}</a> | ||
<br><br> | ||
{% trans 'We appreciate your understanding and cooperation during this account update process. We value your trust in our services and strive to provide you with an optimal experience.' %} | ||
{% endblock content %} |
7 changes: 7 additions & 0 deletions
7
src/auth_and_perms/templates/auth_and_perms/mail/es/user_delete_notification.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,7 @@ | ||
{% extends 'gentelella/registration/email_base.html' %} | ||
{% load i18n %} | ||
{% block content %} | ||
Esto es un adiós <strong>{{user}}</strong>. | ||
|
||
No olvides apoyar a Organilab. | ||
{% endblock %} |
5 changes: 5 additions & 0 deletions
5
src/auth_and_perms/templates/auth_and_perms/mail/es/user_merge_notification.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 %} | ||
Su usuario ha sido mezclado. | ||
{% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.conf import settings | ||
from django.core.mail import send_mail | ||
from django.template.loader import render_to_string | ||
from django.db.models.fields.related import ManyToOneRel | ||
from django.utils.translation import activate, get_language | ||
|
||
def send_email_user_management(request, user_base, user_delete, action): | ||
schema = request.scheme + "://" | ||
if hasattr(user_base, 'profile'): | ||
lang = user_base.profile.language | ||
oldlang = get_language() | ||
context = { | ||
'user_base': user_base, | ||
'user_delete': user_delete, | ||
'domain': schema + request.get_host(), | ||
'action': action | ||
} | ||
activate(lang) | ||
send_mail(subject=_( | ||
"Account Update: Merger with Another Account.") if action == "merge" else _( | ||
"Account Delete"), | ||
message=_("Please use a html reader client"), | ||
recipient_list=[user_base.email], | ||
from_email=settings.DEFAULT_FROM_EMAIL, | ||
html_message=render_to_string( | ||
'auth_and_perms/mail/'+lang+'/user_merge_notification.html', | ||
context=context | ||
) | ||
) | ||
activate(oldlang) | ||
|
||
|
||
def send_delete_user_email(user_delete): | ||
if hasattr(user_delete, 'profile'): | ||
lang = user_delete.profile.language | ||
oldlang = get_language() | ||
context={'lang': lang, | ||
'user': user_delete} | ||
activate(lang) | ||
send_mail(subject=_("Thanks for be part of Organilab, we will miss you."), | ||
message=_("Your account was removed"), | ||
recipient_list=[user_delete.email], | ||
from_email=settings.DEFAULT_FROM_EMAIL, | ||
html_message=render_to_string( | ||
"auth_and_perms/mail/"+lang+"/user_delete_notification.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': | ||
print(field) | ||
if field.name in ['user_permissions', 'groups', 'profile', 'usertotpdevice', | ||
'registrationuser', 'authorizedapplication', 'deleteuserlist', | ||
'auth_token', 'chunked_uploads', 'totpdevice']: | ||
continue | ||
if isinstance(field, ManyToOneRel): | ||
#print(field.name,field.target_field.model, field.field.name) | ||
field.target_field.model.objects.filter(**{field.field.name: to_delete}).update( | ||
**{field.field.name: to_related} | ||
) | ||
if hasattr(to_delete, 'profile') and hasattr(to_related, 'profile'): | ||
del_profile=to_delete.profile | ||
merge_profile = to_related.profile | ||
for field in del_profile._meta.get_fields(): | ||
if field.name in ['profilepermission']: | ||
continue | ||
if isinstance(field, ManyToOneRel): | ||
field.target_field.model.objects.filter( | ||
**{field.field.name: del_profile}).update( | ||
**{field.field.name: merge_profile} | ||
) | ||
|
||
|
||
def delete_user(to_delete, to_related): | ||
merge_information_user(to_delete, to_related) | ||
send_delete_user_email(to_delete) | ||
print("Deleting:", to_delete.username) | ||
print(to_delete.delete()) | ||
|
||
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) |
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