diff --git a/epdb/admin.py b/epdb/admin.py index 475053e8..1df5db04 100644 --- a/epdb/admin.py +++ b/epdb/admin.py @@ -1,5 +1,8 @@ +import logging + from django.conf import settings as s from django.contrib import admin +from django.contrib import messages from .models import ( AdditionalInformation, @@ -29,6 +32,8 @@ from .models import ( Package = s.GET_PACKAGE_MODEL() +logger = logging.getLogger(__name__) + class AdditionalInformationAdmin(admin.ModelAdmin): pass @@ -45,6 +50,113 @@ class UserAdmin(admin.ModelAdmin): "date_joined", ] + actions = ["send_welcome_mail", "send_affiliation_mail"] + + @admin.action(description="Send welcome mail") + def send_welcome_mail(self, request, queryset): + from django.core.mail import EmailMultiAlternatives + + tpl = """Hello {username}, + + Your account has been successfully activated. + + To log in, please visit + https://envipath.org/password_reset/ + and request a new password. + + If you have any questions or feedback, feel free to visit our community forum at + https://community.envipath.org/. + You do not need to register again for the forum - you can log in using your enviPath account by clicking "Log In" and then "Log in with enviPath." + + Best regards, + + The enviPath Team""" + + users = [] + + for user in queryset: + if user.is_active: + logger.info(f"{user.username} already active - not sending mail again") + continue + try: + msg = EmailMultiAlternatives( + "Your enviPath Account Is Now Active", + tpl.format(username=user.username), + "admin@envipath.org", + [user.email], + bcc=["admin@envipath.org"], + ) + + msg.send(fail_silently=False) + + user.is_active = True + user.password = "ASDF" + user.save() + + users.append(user) + logger.info(f"{user.username} -> {user.email} mail sent") + except Exception as e: + logger.info(f"Error sending mail to {user.username}: {e}") + + self.message_user( + request, f"Sent welcome mail to {[u.email for u in users]}", messages.SUCCESS + ) + + @admin.action(description="Send affiliation mail") + def send_affiliation_mail(self, request, queryset): + from django.core.mail import EmailMultiAlternatives + + tpl = """Dear {username}, + +Thank you for your interest in enviPath! + +Please note that the public enviPath system is intended for non-commercial use only. +We see that you registered using the email address {email}. +If possible, we kindly ask you to register using an official email address that reflects your affiliation (e.g., a university, NGO, or research organization). + +If you would like us to update your account, simply reply to this email and let us know which address we should use. +We will then change it in our system, and you will receive a password reset email at the new address. + +If you are registering with a company email address and are interested in commercial use, you are very welcome to book a meeting with us so we can discuss how we can best support you. +To book a meeting, please visit https://envipath.com/book + +If changing to an affiliation email address is not possible, please contact us at registration@envipath.org + +Best regards, + +enviPath team""" + + users = [] + + for user in queryset: + if user.is_active or user.contacted: + logger.info( + f"{user.username} already active or already contacted - not sending mail again" + ) + continue + try: + msg = EmailMultiAlternatives( + "Regarding your enviPath registration", + tpl.format(username=user.username, email=user.email), + "admin@envipath.org", + [user.email], + bcc=["admin@envipath.org"], + ) + + msg.send(fail_silently=False) + + user.contacted = True + user.save() + + users.append(user) + logger.info(f"{user.username} -> {user.email} affiliation mail sent") + except Exception as e: + logger.info(f"Error sending mail to {user.username}: {e}") + + self.message_user( + request, f"Sent affiliation mail to {[u.email for u in users]}", messages.SUCCESS + ) + class UserPackagePermissionAdmin(admin.ModelAdmin): pass diff --git a/epdb/migrations/0024_user_contacted.py b/epdb/migrations/0024_user_contacted.py new file mode 100644 index 00000000..403be979 --- /dev/null +++ b/epdb/migrations/0024_user_contacted.py @@ -0,0 +1,17 @@ +# Generated by Django 6.0.3 on 2026-04-21 19:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("epdb", "0023_alter_compoundstructure_options_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="user", + name="contacted", + field=models.BooleanField(blank=True, null=True), + ), + ] diff --git a/epdb/models.py b/epdb/models.py index 67c2c397..311b148c 100644 --- a/epdb/models.py +++ b/epdb/models.py @@ -75,6 +75,7 @@ class User(AbstractUser): blank=False, ) is_reviewer = models.BooleanField(default=False) + contacted = models.BooleanField(null=True, blank=True) USERNAME_FIELD = "email" REQUIRED_FIELDS = ["username"]