@ratelimit(rate='1200/h', method=None, block=True)
def surveyform(request, assignment_id):
    assignment = get_object_or_404(Assignment, form_slug=assignment_id)
    
    # This variable is no longer used. It was initially developed
    # as a reference to the FormRequest for a separate view that would
    # change the object's page_loaded boolean to true.
    request_key = str(assignment.id) + str(hex(random.randint(100000, 999999)))
    # When behind a proxy, the first one is the client IP, otherwise it is in REMOTE_ADDR
    ip_address = request.META.get('HTTP_X_FORWARDED_FOR', '') or request.META.get('REMOTE_ADDR')
    referer = request.META.get('HTTP_REFERER') or ''
    user_agent = request.META.get('HTTP_USER_AGENT') or ''
    try:
        form_request = FormRequest.objects.create(key=request_key, assignment=assignment,
                                                    ip_address=ip_address, referer=referer,
                                                    user_agent=user_agent)
    except:
        form_request = FormRequest.objects.create(key=request_key, assignment=assignment,
                                                    ip_address='0.0.0.0', referer='Unknown',
                                                    user_agent='Unknown')
    
    form_request.save()

    complete_check = (list(assignment.questions.questions.all().order_by('id').values_list('id', flat=True))
        == list(assignment.answers.all().order_by('question__question__id').values_list('question__question__id', flat=True))
        and Comment.objects.filter(assignment=assignment).exists())

    if complete_check:
        return HttpResponseRedirect('/checkup/thanks/' + assignment.form_slug + '/')
    else:
        Answer.objects.filter(assignment=assignment).delete()
        Comment.objects.filter(assignment=assignment).delete()
    
    if request.method == 'POST':
        form = SurveyForm(request.POST, assignment=assignment)
        
        if form.is_valid():
            for key, value in form.cleaned_data.items():
                if 'question' in key:
                    question = Question.objects.get(pk=int(key.split('-')[1]))
                    group = assignment.questions
                    # We store the QuestionGroupOrder with the answer
                    # so we have access to questions and answers
                    # IN ORDER from the assignment model later
                    group_order = QuestionGroupOrder.objects.get(
                        question=question, group=group)
                    answer = Answer.objects.get_or_create(assignment=assignment, 
                        question=group_order, 
                        answer=Choice.objects.get(pk=int(value)))
                elif 'comment' in key:
                    comment = Comment.objects.get_or_create(assignment=assignment,
                        comment=value)

            complete_check = (list(assignment.questions.questions.all().order_by('id').values_list('id', flat=True))
                == list(assignment.answers.all().order_by('question__question__id').values_list('question__question__id', flat=True))
                and Comment.objects.filter(assignment=assignment).exists())

            if complete_check:
                assignment.survey_complete = True
                assignment.save()

            email_message = assignment.respondent.first_name + ' ' + assignment.respondent.last_name
            email_message += ' responded to the survey: ' + assignment.survey.name + '\n\n'
            email_message += 'Here are the respondent\'s answer(s): \n'
            for answer in assignment.answers.all():
                email_message += answer.question.question.question + '\n'
                email_message += answer.answer.choice + '\n'
            email_message += '\n'
            email_message += 'Here is the respondent\'s comment (if any): \n'
            email_message += assignment.comment.comment + '\n'

            try:
                send_mail('CheckUp survey form submitted!', 
                            email_message, 
                            settings.DEFAULT_FROM_EMAIL,
                            [assignment.reporter.user.email], 
                            fail_silently=True)
            except __HOLE__:
                #If they didn't set up their server.
                pass

            return HttpResponseRedirect('/checkup/thanks/' + assignment.form_slug + '/')
    else:
        form = SurveyForm(assignment=assignment)

    context = {
        'form' : form,
        'assignment' : assignment,
        'base_template': base_template
        }
    return render(request, 'checkup/surveyform.html', context)
dataset/ETHPy150Open newsday/newstools-checkup/checkup/views.py/surveyform