site_tps/users/forms.py

109 lines
2.6 KiB
Python
Raw Normal View History

2018-03-08 23:53:38 +00:00
from django import forms
2018-03-18 10:36:52 +00:00
from django.utils.encoding import force_bytes
from django.core.mail import send_mail
from django.template import loader
from django.contrib.auth.tokens import default_token_generator
from django.utils.http import urlsafe_base64_encode
from django.contrib.sites.shortcuts import get_current_site
2018-03-08 23:53:38 +00:00
from django.contrib.auth.models import User
from .models import School
class CreateSchool(forms.ModelForm):
class Meta:
model = School
fields = ['name', 'admin']
class EditName(forms.ModelForm):
class Meta:
model = School
fields = ['name']
2018-03-08 23:53:38 +00:00
class CreateUser(forms.ModelForm):
class Meta:
model = User
2018-03-10 13:48:48 +00:00
fields = [
'username',
'first_name',
'last_name',
'email',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2018-03-18 10:36:52 +00:00
def save(self, request):
user = super().save(commit=None)
user.password = ''
user.save()
site = get_current_site(request)
context = {
'email': user.email,
'domain': site.domain,
'site_name': site.name,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'user': user,
'token': default_token_generator.make_token(user),
'protocol': 'https',
}
self.send_mail(context, user.email)
return user
def send_mail(self, context, to_email):
"""
Send a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = "Inscription sur Il était une fois dans l'Est"
body = loader.render_to_string("users/welcome_user.txt", context)
from_email = "noreply.festart@rezometz.org"
send_mail(
subject,
body,
from_email,
[to_email],
fail_silently=False,
)
2018-03-08 23:53:38 +00:00
class EditPhone(forms.ModelForm):
class Meta:
model = School
fields = ['phone']
class EditLogo(forms.ModelForm):
class Meta:
model = School
fields = ['logo']
class EditMail(forms.ModelForm):
class Meta:
model = User
fields = ['email']
class EditJury1(forms.ModelForm):
class Meta:
model = School
fields = [
'first_name_j1',
'last_name_j1',
'phone_j1',
'mail_j1'
]
class EditJury2(forms.ModelForm):
class Meta:
model = School
fields = [
'first_name_j2',
'last_name_j2',
'phone_j2',
'mail_j2'
]