Skip to content

Commit

Permalink
Add check_remove_user_notlogin as WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
luisza committed May 18, 2024
1 parent 7b3cedb commit 9a606c7
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 46 deletions.
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()



11 changes: 9 additions & 2 deletions src/auth_and_perms/migrations/0014_create_user_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions src/auth_and_perms/migrations/0017_merge_20240518_1359.py
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 = [
]
26 changes: 26 additions & 0 deletions src/auth_and_perms/migrations/0018_deleteuserlist.py
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)),
],
),
]
9 changes: 9 additions & 0 deletions src/auth_and_perms/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import random
import uuid

from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission, User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.validators import RegexValidator
from django.db import models
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from django_otp.plugins.otp_totp.models import TOTPDevice

Expand Down Expand Up @@ -133,6 +135,13 @@ class AuthorizedApplication(models.Model):
notification_url = models.URLField()
token = models.TextField()

def user_expiration_date():
return now()+relativedelta(months=1)

class DeleteUserList(models.Model):
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
creation_date = models.DateTimeField(auto_now=True)
expiration_date = models.DateTimeField(default=user_expiration_date)

class ImpostorLog(models.Model):
impostor = models.ForeignKey(
Expand Down
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 %}
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 %}
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 %}
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 %}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,5 @@
<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.' %}
{% else %}
{% 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 %}
86 changes: 86 additions & 0 deletions src/auth_and_perms/users.py
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)
26 changes: 0 additions & 26 deletions src/auth_and_perms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,6 @@ def send_email(request, user):
)


def send_email_user_management(request, user_base, user_delete, action):
schema = request.scheme + "://"

context = {
'user_base': user_base,
'user_delete': user_delete,
'domain': schema + request.get_host(),
'action': action
}
send_mail(subject=_(
"Account Update: Merger with Another Account.") if action == "merge" else _(
"Account Delete"),
message="Por favor use un visor de html",
recipient_list=[user_base.email],
from_email=settings.DEFAULT_FROM_EMAIL,
html_message=render_to_string(
'auth_and_perms/user_management_notification.html',
context=context
)
)


def user_management(request, user_base, user_delete, action):
send_email_user_management(request, user_base, user_delete, action)
user_delete.delete()


def get_ip_address(request):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/auth_and_perms/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.utils.translation import gettext_lazy as _

from auth_and_perms.forms import MergeUsers, UserForm
from auth_and_perms.utils import user_management
from auth_and_perms.users import user_management


def users_list(request):
Expand Down

0 comments on commit 9a606c7

Please sign in to comment.