Example #1
0
def update_provider(request, pk):
    try:
        slug = request.session['survey_slug']
    except KeyError:
        logger.warning('Expired session, could not find "survey_slug"')
        return HttpResponseRedirect('/expired/')

    if request.method == 'POST':

        if pk is not None:
            vas = VASProvider.objects.get(pk=pk)
            form = VASProviderForm(request.POST, instance=vas)
        else:
            form = VASProviderForm(request.POST)

        if form.is_valid():
            response_data = {}

            vas = form.save()
            if not vas.respondent:
                survey_instance = Survey.objects.get(slug=slug)
                vas.respondent = survey_instance
                vas.save()

            response_data['id'] = vas.id
            response_data['companyName'] = vas.company_name
            response_data['contactName'] = vas.contact_name or '-'
            response_data['contactEmail'] = vas.contact_email or '-'

            return JsonResponse(response_data, status=200)

        else:
            return JsonResponse({'errors': form.errors, 'data': form}, status=400)

    return JsonResponse({'Error': 'Can only handle POST requests...'}, status=403)
Example #2
0
def create_provider(request):
    try:
        slug = request.session['survey_slug']
    except KeyError:
        logger.warning('Expired session, could not find "survey_slug"')
        return HttpResponseRedirect('/expired/')

    if request.method == 'POST':

        logger.info(request.POST)
        form = VASProviderForm(request.POST)

        if form.is_valid():
            logger.info('Form is valid!')
            response_data = {}

            vas = form.save()
            if not vas.respondent:
                survey_instance = Survey.objects.get(slug=slug)
                vas.respondent = survey_instance
                vas.save()

            response_data['id'] = vas.id
            response_data['companyName'] = vas.company_name
            response_data['contactName'] = vas.contact_name or '-'
            response_data['contactEmail'] = vas.contact_email or '-'

            return JsonResponse(response_data)

        else:
            logger.info(form.errors)

    return JsonResponse({'Error': 'Can only handle POST requests...'})