from django import forms 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 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'] class CreateUser(forms.ModelForm): class Meta: model = User fields = [ 'username', 'first_name', 'last_name', 'email', ] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) 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, ) 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' ]