def serve_post_response(request, reporter):
    username = reporter.urn_tel
    run_id = request.POST['run_id']

    if request.POST.get('password'):
        user = activate_user(request, reporter)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)
        send_message_to_rapidpro({'from': username, 'text': 'next'})
    else:
        send_message_to_rapidpro({'from': username, 'text': request.POST['send']})

    is_complete = is_registration_complete(run_id)
    if is_complete:
        got_messages = True
        msgs = ["Thank you for joining us. Please click or tap below to take the latest U-Report poll."]
    else:
        msgs, got_messages = get_messages_for_user(username)
    if not got_messages:
        return serve_timeout_message(request, msgs)

    is_password = has_password_keyword(msgs, username)
    post_password = get_post_password_status(request, is_password)
    show_latest_poll_link = post_password > ON_PASSWORD

    return render(request, 'register.html', {
        'is_complete': is_complete,
        'messages': msgs,
        'submission': request.POST.get('send') or None,
        'is_password': is_password,
        'post_password': post_password,
        'show_latest_poll_link': show_latest_poll_link,
        'uuid': reporter.uuid,
        'run_id': run_id})
def serve_get_response(request, uuid):
    username = Ureporter.objects.get(uuid=uuid).urn_tel
    if user_is_authenticated(request):
        return get_already_registered_message(request)
    else:
        send_message_to_rapidpro({'from': username, 'text': settings.RAPIDPRO_REGISTER_TRIGGER})
        return render(request, 'register.html', {'messages': get_messages_for_user(username), 'uuid': uuid})
def serve_get_response(request, reporter):
    username = reporter.urn_tel

    if user_is_authenticated(request):
        return get_already_registered_message(request)
    else:
        send_message_to_rapidpro({'from': username, 'text': s.RAPIDPRO_REGISTER_TRIGGER})

        messages, got_messages = get_messages_for_user(username)
        return render(request, 'register.html', {
            'messages': messages,
            'uuid': reporter.uuid,
            'run_id': get_run_id(reporter)})
def serve_post_response(request, uuid, ureporter):
    username = Ureporter.objects.get(uuid=uuid).urn_tel
    if request.POST.get('password'):
        activate_user(request, ureporter)
        send_message_to_rapidpro({'from': username, 'text': 'next'})
    else:
        send_message_to_rapidpro({'from': username, 'text': request.POST['send']})
    msgs = get_messages_for_user(username)
    return render(request, 'register.html', {
        'messages': msgs,
        'submission': request.POST.get('send') or None,
        'is_password': has_password_keyword(msgs, username),
        'uuid': uuid})
def serve_post_response(request, poll_id, flow_info, username, uuid):
    send_message_to_rapidpro({'from': username, 'text': request.POST['send']})
    run_is_complete = is_run_complete(flow_info['flow_uuid'], uuid)
    if run_is_complete:
        save_last_poll_taken(username, poll_id)
    msgs = get_messages_for_user(username)
    title = flow_info['title']
    return render(request, 'poll_response.html', {
        'messages': msgs,
        'poll_id': poll_id,
        'title': title,
        'is_complete': run_is_complete,
        'submission': request.POST.get('send')})
def serve_post_response(request, poll_id):
    username = request.user
    uuid = Ureporter.objects.get(user__username=username).uuid
    flow_info = json.loads(request.POST['flow_info'])
    run_id = request.POST['run_id']
    send_message_to_rapidpro({'from': username, 'text': request.POST['send']})
    current_time = current_datetime_to_rapidpro_formatted_date()
    run_complete_before_messages = is_current_run_complete_before_messages(flow_info['flow_uuid'], uuid, run_id)

    if run_complete_before_messages:
        got_messages = True
        msgs = ["Poll Completed"]
    else:
        msgs, got_messages = get_messages_for_user(username)

    if not got_messages and not run_complete_before_messages:
        return render_timeout_message(request, msgs)
    else:
        if not run_complete_before_messages:
            run_complete_after_messages = is_current_run_complete_after_messages(flow_info['flow_uuid'], uuid, run_id, current_time)
        else:
            run_complete_after_messages = run_complete_before_messages

        run_is_complete = run_complete_before_messages or run_complete_after_messages

        if run_is_complete:
            ureporter = Ureporter.objects.get(user__username=username)
            ureporter.set_last_poll_taken(poll_id)
        return render(request, 'poll_response.html', {
            'messages': msgs,
            'poll_id': poll_id,
            'flow_info': json.dumps(flow_info),
            'title': flow_info['title'],
            'run_id': run_id,
            'is_complete': run_is_complete,
            'submission': request.POST.get('send')})
 def form_valid(self, form):
     reporter = Ureporter.objects.get(user=self.request.user)
     data = {'text': form.cleaned_data['message'], 'from': reporter.urn_tel}
     send_message_to_rapidpro(data)
     messages.info(self.request, _('Thank you change maker for sending U-Report this message'))
     return HttpResponseRedirect(self.get_success_url())
 def form_valid(self, form):
     reporter = Ureporter.objects.get(user=self.request.user)
     data = {"text": form.cleaned_data["message"], "from": reporter.urn_tel}
     send_message_to_rapidpro(data)
     messages.info(self.request, "Thank you change maker for sending ureport this message")
     return HttpResponseRedirect(self.get_success_url())