def post(self, request, *args, **kwargs): #pylint:disable=too-many-locals,unused-argument serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) if is_authenticated(self.request): user = self.request.user else: user_model = get_user_model() email = serializer.validated_data.get('email', None) try: user = user_model.objects.get(email=email) except user_model.DoesNotExist: #pylint:disable=unused-variable first_name, mid, last_name = full_name_natural_split( serializer.validated_data.get('full_name', None)) user = user_model(email=email, first_name=first_name, last_name=last_name) message = serializer.validated_data.get('message', '') provider = serializer.validated_data.get('provider', self.provider) items = [] for key, value in six.iteritems(serializer.data): if value and not (key in serializer.validated_data or key in ( 'captcha', 'g-recaptcha-response', 'csrfmiddlewaretoken')): items += [(key, value)] if message: items += [("Message", message)] if not user.email: return Response({'detail': _("Thank you for the feedback. Please feel free to leave your contact"\ " information next time so we can serve you better.")}) if provider: provider = get_object_or_404(Organization, slug=provider) else: provider = self.provider try: contact_requested.send(sender=__name__, provider=provider, user=user, reason=items, request=self.request) return Response({ 'detail': _("Your request has been sent. We will reply within 24 hours. Thank you." ) }) except (SMTPException, socket.error) as err: LOGGER.exception("%s on page %s", err, self.request.get_raw_uri()) return Response({'detail': _("Sorry, there was an issue sending your request for information"\ " to '%(full_name)s <%(email)s>'.") % { 'full_name': provider.full_name, 'email': provider.email}})
def form_valid(self, form): if is_authenticated(self.request): user = self.request.user else: user_model = get_user_model() email = form.cleaned_data.get('email', None) try: user = user_model.objects.get(email=email) except user_model.DoesNotExist: #pylint:disable=unused-variable first_name, mid, last_name = full_name_natural_split( form.cleaned_data.get('full_name', None)) user = user_model(email=email, first_name=first_name, last_name=last_name) message = form.cleaned_data.get('message', '') provider = form.cleaned_data.get('provider', self.provider) items = [] for key, value in six.iteritems(form.data): if value and not (key in form.cleaned_data or key in ( 'captcha', 'g-recaptcha-response', 'csrfmiddlewaretoken')): items += [(key, value)] if message: items += [("Message", message)] if user.email: if provider: provider = get_object_or_404(Organization, slug=provider) else: provider = self.provider try: contact_requested.send(sender=__name__, provider=provider, user=user, reason=items, request=self.request) messages.info( self.request, _("Your request has been sent. We will reply within 24 hours. Thank you." )) except (SMTPException, socket.error) as err: LOGGER.exception("%s on page %s", err, self.request.get_raw_uri()) messages.error(self.request, _("Sorry, there was an issue sending your request for information"\ " to '%(full_name)s <%(email)s>'.") % { 'full_name': provider.full_name, 'email': provider.email}) else: messages.warning(self.request, _("Thank you for the feedback. Please feel free to leave your contact"\ " information next time so we can serve you better.")) return http.HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): if self.get_survey(): # If we end-up creating Response records but the account constraint # is different between the front-end proxy and app, that would blow # up, even when no survey is present. response = super(ContactView, self).form_valid(form) else: response = None if self.request.user.is_authenticated(): user = self.request.user else: user_model = get_user_model() email = form.cleaned_data.get('email', None) try: user = user_model.objects.get(email=email) except user_model.DoesNotExist: #pylint:disable=unused-variable first_name, mid, last_name = full_name_natural_split( form.cleaned_data.get('full_name', None)) user = user_model(email=email, first_name=first_name, last_name=last_name) message = form.cleaned_data.get('message', '') provider = form.cleaned_data.get('provider', None) items = [] if self.object: for answer in self.object.answers.order_by('rank'): if answer.text: question = answer.question items += [(question.text, answer.text)] if message: items += [("Message", message)] if user.email: if provider: provider = get_object_or_404(Organization, slug=provider) else: provider = self.provider try: contact_requested.send(sender=__name__, provider=provider, user=user, reason=items, request=self.request) messages.info( self.request, _("Your request has been sent. We will reply within 24 hours. Thank you." )) except (SMTPException, socket.error) as err: LOGGER.exception("%s on page %s", err, self.request.get_raw_uri()) messages.error(self.request, _("Sorry, there was an issue sending your request for information"\ " to '%(full_name)s <%(email)s>'.") % { 'full_name': provider.full_name, 'email': provider.email}) else: messages.warning(self.request, _("Thank you for the feedback. Please feel free to leave your contact"\ " information next time so we can serve you better.")) if not response: response = http.HttpResponseRedirect(self.get_success_url()) return response