def notify(self): """Sends an email to the creator of the proposal with a confirmation email. The emails has a link to edit the proposal. """ if not self.code: self.code = random_string(64) for speaker in self.speakers.all(): context = self.notification_email_context(speaker=speaker) send_email(context=context, template="emails/proposals/confirmation.html", subject=_("[%s] Confirmación de propuesta de charla") % settings.CONFERENCE_TITLE, to=speaker.email, from_email=settings.CONTACT_EMAIL) self.notified = True self.save()
def save(self): email = self.cleaned_data["email"] if User.objects.filter(email=email).exists(): user = User.objects.get(email=email) else: try: with transaction.atomic(): user = User.objects.create_user(email=email, password=random_string()) except IntegrityError: user = User.objects.get(email=email) # Create reviewer profile group = Group.objects.get(name=REVIEW_GROUP_NAME) if group not in user.groups.all(): user.groups.add(group) # Sends restore password user.send_restore_password_link() create_reviews(user) return user
def clean_speakers(self): """Parse JSON data with speakers and creates/gets them.""" speakers = json.loads(self.cleaned_data["speakers"]) if not speakers: raise forms.ValidationError(_("Es obligatorio indicar un ponente al menos.")) speakers_models = [] for speaker in speakers: name = speaker.get("name") email = speaker.get("email") if not email and name: raise forms.ValidationError(_("Es falta un correo electrónico.")) if email and not name: raise forms.ValidationError(_("Es falta un nombre.")) if not email and not name: continue try: speaker_model = Speaker.objects.get(user__email=email) speaker_model.name = speaker.get("name", "") speaker_model.save() except Speaker.DoesNotExist: users = User.objects.filter(email=email) first_name = speaker.get("name", "").split(" ")[0] last_name = " ".join(speaker.get("name", "").split(" ")[1:]) if not users.exists(): user = User.objects.create_user( email=email, password=random_string(), first_name=first_name, last_name=last_name, ) else: user = users.first() user.first_name = first_name user.last_name = last_name user.save() speaker_model = Speaker.objects.create(user=user, name=speaker.get("name", "")) speakers_models.append(speaker_model) return speakers_models
def generate_restore_code(self): self.restore_code = random_string(16) self.save()