tildetown-admin/ttadmin/users/models.py

65 lines
2.2 KiB
Python
Raw Normal View History

2016-11-30 07:48:02 +00:00
import re
2017-01-14 06:17:23 +00:00
from django.db.models import Model
from django.db.models.signals import pre_save
2016-11-20 05:34:38 +00:00
from django.dispatch import receiver
from django.contrib.auth.models import User
2017-01-14 06:17:23 +00:00
from django.db.models import TextField, BooleanField, CharField, ForeignKey
from django.template.loader import get_template
2016-11-20 05:34:38 +00:00
from common.mailing import send_email
from help.models import Ticket
2016-11-30 07:48:02 +00:00
2016-11-22 05:14:47 +00:00
SSH_TYPE_CHOICES = (
('ssh-rsa', 'ssh-rsa',),
('ssh-dss', 'ssh-dss',),
)
2016-11-20 05:34:38 +00:00
2016-11-30 07:48:02 +00:00
2016-11-20 05:34:38 +00:00
class Townie(User):
"""Both an almost normal Django User as well as an abstraction over a
system user."""
2017-01-13 21:22:52 +00:00
class Meta:
verbose_name = 'Townie'
verbose_name_plural = 'Townies'
2016-11-21 07:56:55 +00:00
shell = CharField(max_length=50, default="/bin/bash")
reviewed = BooleanField(default=False)
2016-11-30 07:48:02 +00:00
reasons = TextField(blank=True, null=False, default='')
2016-11-21 07:56:55 +00:00
displayname = CharField(max_length=100, blank=False, null=False)
2016-11-20 05:34:38 +00:00
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))
2017-01-14 06:17:23 +00:00
class Pubkey(Model):
2017-01-14 07:57:50 +00:00
key_type = CharField(max_length=50,
2017-01-14 06:17:23 +00:00
blank=False,
null=False,
choices=SSH_TYPE_CHOICES,
)
key = TextField(blank=False, null=False)
townie = ForeignKey(Townie)
@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
2016-11-20 05:34:38 +00:00
if not existing[0].reviewed and instance.reviewed == True:
instance.send_welcome_email()