@@ -0,0 +1,2287 @@
+from anymail.exceptions import AnymailError%0Afrom datetime import datetime, timedelta%0Afrom django.core.management.base import BaseCommand%0Afrom django.contrib.auth.models import User%0Afrom django.db.models import Q%0Aimport logging%0A%0Alogger = logging.getLogger(__name__)%0A%0AWARNING_TEMPLATE = %22%22%22%0AHi there %7B%7D - %0A%0AYou have not logged in to seqr in %7B%7D days. Unless you log in within the next %7B%7D days, your account will be deactivated.%0A%22%22%22%0A%0ADEACTIVATE_TEMPLATE = %22%22%22%0AHi there %7B%7D - %0A%0AYou have not logged in to seqr in %7B%7D days, and therefore your account has been deactivated.%0APlease feel free to reach out to the seqr team if you would like your account reinstated.%0A%22%22%22%0A%0Aclass Command(BaseCommand):%0A%0A%0A    def handle(self, *args, **options):%0A        logger.info('Checking for inactive users')%0A%0A        now = datetime.now()%0A        required_login = now - timedelta(days=90)%0A        warn_login = required_login + timedelta(days=30)%0A%0A        users = User.objects.filter(Q(is_staff=True) %7C Q(is_superuser=True)).filter(is_active=True)%0A        deactivate_users = users.filter(last_login__lt=required_login)%0A        warn_users = users.filter(last_login__lt=warn_login, last_login__gt=required_login)%0A%0A        for user in warn_users:%0A            logger.info('Warning %7B%7D of impending account inactivation'.format(user.email))%0A            days_login = (now - user.last_login.replace(tzinfo=None)).days%0A            email_content = WARNING_TEMPLATE.format(user.get_full_name(), days_login, 90 - days_login)%0A            try:%0A                user.email_user('Warning: seqr account inactivation', email_content)%0A            except AnymailError as e:%0A                logger.error('Unable to send email: %7B%7D'.format(e))%0A%0A        for user in deactivate_users:%0A            logger.info('Inactivating account for %7B%7D'.format(user.email))%0A            user.is_active = False%0A            user.save()%0A%0A            days_login = (now - user.last_login.replace(tzinfo=None)).days%0A            email_content = DEACTIVATE_TEMPLATE.format(user.get_full_name(), days_login)%0A            try:%0A                user.email_user('Warning: seqr account deactivated', email_content)%0A            except AnymailError as e:%0A                logger.error('Unable to send email: %7B%7D'.format(e))%0A%0A        logger.info('Inactive user check complete')%0A%0A

add comand to detect inactive users