From ba05ec340cbfa43c8793c743a6f964c4fdded49c Mon Sep 17 00:00:00 2001 From: nathaniel smith Date: Sat, 21 Jan 2017 00:35:00 -0800 Subject: [PATCH] implement user welcome emails emails trigger when they're marked as reviewed --- ttadmin/users/admin.py | 8 ++++ ttadmin/users/models.py | 43 ++++++++++++------- .../users/templates/users/welcome_email.txt | 13 ++++++ 3 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 ttadmin/users/templates/users/welcome_email.txt diff --git a/ttadmin/users/admin.py b/ttadmin/users/admin.py index 3212471..899e6a0 100644 --- a/ttadmin/users/admin.py +++ b/ttadmin/users/admin.py @@ -10,8 +10,16 @@ admin.site.unregister(Group) class PubkeyInline(admin.TabularInline): model = Pubkey +def bulk_review(madmin, req, qs): + for townie in qs: + townie.reviewed = True + townie.save() + +bulk_review.short_description = 'mark selected townies as reviewed' + @admin.register(Townie) class TownieAdmin(admin.ModelAdmin): inlines = [PubkeyInline] list_display = ('reviewed', 'username', 'email') ordering = ('reviewed',) + actions = (bulk_review,) diff --git a/ttadmin/users/models.py b/ttadmin/users/models.py index 3bf5aeb..af1b6cc 100644 --- a/ttadmin/users/models.py +++ b/ttadmin/users/models.py @@ -1,11 +1,14 @@ import re from django.db.models import Model -from django.db.models.signals import post_save +from django.db.models.signals import pre_save from django.dispatch import receiver from django.contrib.auth.models import User from django.db.models import TextField, BooleanField, CharField, ForeignKey +from django.template.loader import get_template +from common.mailing import send_email +from help.models import Ticket SSH_TYPE_CHOICES = ( ('ssh-rsa', 'ssh-rsa',), @@ -24,14 +27,22 @@ class Townie(User): reasons = TextField(blank=True, null=False, default='') displayname = CharField(max_length=100, blank=False, null=False) - # TODO consider a generic ensure method that syncs this model with the - # system. there will likely be things besides shell that we want to keep - # track of in the DB. - def ensure_shell(self): - """Runs chsh for the user to set their shell to whatever self.shell - is.""" - raise NotImplementedError() - + def send_welcome_email(self, admin_name='vilmibm'): + welcome_tmpl = get_template('users/welcome_email.txt') + context = { + 'username': self.username, + 'admin_name': admin_name, + } + text = welcome_tmpl.render(context) + from_address = '{}@tilde.town'.format(admin_name) + success = send_email(self.email, text, subject='tilde.town!', frum=from_address) + if not success: + Ticket.objects.create(name='system', + email='root@tilde.town', + issue_type='other', + issue_text='was not able to send welcome email to {} ({})'.format( + self.username, + self.email)) class Pubkey(Model): key_type = CharField(max_length=50, @@ -43,11 +54,11 @@ class Pubkey(Model): townie = ForeignKey(Townie) -@receiver(post_save, sender=Townie) -def sync_system_state(sender, instance, created, **kwargs): - if created: - print('TODO would create new user on system') - else: - print('TODO would update existing user on system') +@receiver(pre_save, sender=Townie) +def on_townie_pre_save(sender, instance, **kwargs): + existing = Townie.objects.filter(username=instance.username) + if not existing: # we're making a new user + return - return + if not existing[0].reviewed and instance.reviewed == True: + instance.send_welcome_email() diff --git a/ttadmin/users/templates/users/welcome_email.txt b/ttadmin/users/templates/users/welcome_email.txt new file mode 100644 index 0000000..b782a20 --- /dev/null +++ b/ttadmin/users/templates/users/welcome_email.txt @@ -0,0 +1,13 @@ +Welcome to tilde.town, ~{{username}}! + +Please take a moment to review our code of conduct: https://tilde.town/~wiki/conduct.html + +and login with: + +ssh -i /path/to/private/key {{username}}@tilde.town + +File a help ticket if you have problems logging in (or other issues): https://cgi.tilde.town/help/tickets + +See you on the server!! + +~{{admin_name}}