1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import random
from collections import OrderedDict
from string import ascii_letters, digits
from django import forms
from django.conf import settings
from django.contrib.auth.models import User, Group
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.template import loader, Context
from .models import UserProfile
class ProfileForm(forms.Form):
email = forms.EmailField(label='Private email (not shown publicly):',
help_text="Used for out-of-date notifications, etc.")
passwd1 = forms.CharField(label='New Password', required=False,
widget=forms.PasswordInput)
passwd2 = forms.CharField(label='Confirm Password', required=False,
widget=forms.PasswordInput)
def clean(self):
if self.cleaned_data['passwd1'] != self.cleaned_data['passwd2']:
raise forms.ValidationError('Passwords do not match.')
return self.cleaned_data
class UserProfileForm(forms.ModelForm):
def clean_pgp_key(self):
data = self.cleaned_data['pgp_key']
# strip 0x prefix if provided; store uppercase
if data.startswith('0x'):
data = data[2:]
return data.upper()
class Meta:
model = UserProfile
exclude = ('allowed_repos', 'user', 'latin_name')
class NewUserForm(forms.ModelForm):
username = forms.CharField(max_length=30)
private_email = forms.EmailField()
first_name = forms.CharField(required=False)
last_name = forms.CharField(required=False)
groups = forms.ModelMultipleChoiceField(required=False,
queryset=Group.objects.all())
class Meta:
model = UserProfile
exclude = ('picture', 'user')
def __init__(self, *args, **kwargs):
super(NewUserForm, self).__init__(*args, **kwargs)
# Hack ourself so certain fields appear first
old = self.fields
self.fields = OrderedDict()
keys = ('username', 'private_email', 'first_name', 'last_name',
'alias', 'public_email')
for key in keys:
self.fields[key] = old[key]
for key, val in old.items():
if key not in keys:
self.fields[key] = old[key]
def clean_username(self):
username = self.cleaned_data['username']
if User.objects.filter(username=username).exists():
raise forms.ValidationError(
"A user with that username already exists.")
return username
def save(self, commit=True):
profile = super(NewUserForm, self).save(False)
pwletters = ascii_letters + digits
password = ''.join([random.choice(pwletters) for _ in xrange(8)])
user = User.objects.create_user(username=self.cleaned_data['username'],
email=self.cleaned_data['private_email'], password=password)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
# sucks that the MRM.add() method can't take a list directly... we have
# to resort to dirty * magic.
user.groups.add(*self.cleaned_data['groups'])
profile.user = user
if commit:
profile.save()
self.save_m2m()
template = loader.get_template('devel/new_account.txt')
ctx = Context({
'site': Site.objects.get_current(),
'user': user,
'password': password,
})
send_mail("Your new archweb account",
template.render(ctx),
settings.BRANDING_EMAIL,
[user.email],
fail_silently=False)
# vim: set ts=4 sw=4 et:
|