예제 #1
0
def take_survey(request, survey_id):
    survey = get_object_or_404(Survey, pk=survey_id)
    questions = survey.question_set.all()
    AnswerFormset = get_answer_formset(questions)

    formset = AnswerFormset(request.POST or None, queryset=Answer.objects.none())
    if formset.is_valid():
        answers = formset.save(commit=False)
        response = Response(survey=survey)
        response.save()
        for answer in answers:
            answer.response = response
            answer.save()    
        return HttpResponseRedirect(reverse('simple_survey.views.list_surveys'))
        
    for index in range(len(questions)):
        question = questions[index]
        form = formset.forms[index]
        form.fields['answer'].label = question.question
        form.fields['question'].initial = question
        
    responseParameters = {
        "survey" : survey,
        "formset" : formset,
    }
    return render_to_response('simple_survey/take_survey.html', responseParameters, context_instance=RequestContext(request))
예제 #2
0
 def _on_rmq_message(self, *args, **kwargs):
   if 'rmq_message' in kwargs:
     rmq_message = kwargs['rmq_message']
   else:
     return
   if rmq_message is not None:
     channel, user, message, plugin_name, plugin_response = \
         self._process_rmq_message(rmq_message)
     if channel is not None:
       if user is not None:
         if message is not None:
           if plugin_name is not None:
             if plugin_response is not None:
               is_approved = True if channel.is_secure is False \
                   else False
               response = Response(
                   text=plugin_response,
                   from_plugin=plugin_name,
                   in_response_to=message,
                   to_channel=channel,
                   to_user=user,
                   is_approved=is_approved,
                   is_sent=False,
               )
               response.save()
예제 #3
0
def answer(request, poll_id, user_uuid):
    poll = get_object_or_404(Poll, pk=poll_id)
    participant = get_object_or_404(Participant, unique_id=user_uuid)
    if participant.completed:
        return redirect(reverse('polls:thanks',
                                args=(poll.id,
                                      user_uuid,))) 
    questions = poll.question_set.all()
    if request.method == 'POST':
        form = DetailForm(request.POST, questions=questions)
        if form.is_valid():
            to_delete = Response.objects.filter(participant=participant)
            to_delete.delete()
            for choice_id in form.answers():
                response = Response(participant=participant,
                                    choice_id=choice_id)
                response.save()
            return HttpResponseRedirect(reverse('polls:sign',
                                                args=(poll.id,
                                                      user_uuid,)))
    else:
        form = DetailForm(questions=questions)
    return render(request, 'polls/detail.html',
                  {
                      'poll': poll,
                      'form': form,
                      'user_uuid': user_uuid,
                  })
예제 #4
0
 def post(self, request):
     if request.POST.get('response', False):
         response = Response()
         question = Question.objects.get(id=request.POST['question'])
         response.message = request.POST['response']
         response.question = question
         response.user = request.user
         response.save()
     return HttpResponseRedirect(request.META['HTTP_REFERER'])
예제 #5
0
 def mutate(self, info, **kwargs):
     req_kw = kwargs['request']
     request_instance = RequestModel.objects.get(**req_kw)
     kwargs['request'] = request_instance
     del kwargs['request']
     new_response_instance = ResponseModel(**kwargs)
     print 'new response instance: '
     print new_response_instance
     new_response_instance.save()
     return CreateResponse(response=new_response_instance)
예제 #6
0
 def mutate(self, info, **kwargs):
     print "getting request info: "
     req_kw = kwargs['request']
     print req_kw
     request_instance = RequestModel.objects.get(**req_kw)
     print "request instance: "
     print request_instance.id
     #kwargs['request'] = request_instance
     del kwargs['request']
     new_response_instance = ResponseModel(**kwargs)
     new_response_instance.request = request_instance
     print 'new response instance: '
     print new_response_instance
     new_response_instance.save()
     return CreateResponse(response=new_response_instance)
예제 #7
0
def respond(request):
	"""
	Request handler when someone posts a response
	1. Add response content to the database
	2. Send push notification to client device
	3. Update the credit of the responder
	"""
	if request.method == 'POST':
		json_data = json.loads(request.body)

		try:
			thread_id = json_data['thread_id']
			response_content = json_data['content']
			device_id = json_data['device_id']
		except KeyError:
			print "Error: A posted response did not have a JSON object with the required properties"
		else:
			# check that the thread id and the device ids are valid
			thread = Thread.objects.filter(id=thread_id)
			device = Device.objects.filter(device_id=device_id)

			print "Passed parameter validation"
			print thread.count()
			print device.count()

			if thread.exists() and device.exists():
				# add response to database
				response = Response(thread=thread[0], responder_device=device[0], response_content=response_content)
				response.save()

				# add update to the other device
				asker_device = thread[0].asker_device
				answerer_device = thread[0].answerer_device
				
				print "Thread and device actually exist"
				print device_id
				print asker_device.device_id
				print answerer_device.device_id

				if asker_device.device_id == device_id:
					ResponseUpdates.add_update(answerer_device, response)
					print "Adding an update to the answerers queue"
					
				elif answerer_device.device_id == device_id:
					ResponseUpdates.add_update(asker_device, response)
					print "Adding an update to the askers queue"

				return HttpResponse(json.dumps({}), content_type="application/json")
예제 #8
0
def submit_item(request):
    post_data = json.loads(request.body)
    try:
        ip = get_client_ip(request)
        email = post_data['email']
        response = Response.objects.filter(email=email, ip=ip).first()
        if response is None:
            response = Response(email=email, ip=ip)
        items_list = post_data['items_list']
        for item in items_list:
            value = post_data[item]
            setattr(response, item, value)
        response.save()

    except (KeyError):
        return HttpResponse(-1)
    return HttpResponse(1)
예제 #9
0
 def _run_once(self, *_, **kwargs):
   try:
     rmq_message = kwargs.get('rmq_message', '{}') or '{}'
     rmq_message = json.loads(rmq_message)
     if len(rmq_message) == 0:
       return None
     channel = Channel.get(
         Channel.id == rmq_message['channel_id']  # noqa pylint:disable=no-member
     )
     if channel.is_subscribed is False:
       return None
     user = User.get(
         User.id == rmq_message['user_id']  # noqa pylint:disable=no-member
     )
     message = Message.get(
         Message.id == rmq_message['message_id']  # noqa pylint:disable=no-member
     )
     plugin_name = rmq_message['plugin_name']
     plugin_response = rmq_message['plugin_response']
     if len(plugin_name) == 0:
       return None
     if len(plugin_response) == 0:
       return None
     is_approved = True if channel.is_secure is False \
         else False
     response = Response(
         text=plugin_response,
         from_plugin=plugin_name,
         in_response_to=message,
         to_channel=channel,
         to_user=user,
         is_approved=is_approved,
         is_sent=False,
     )
     lock_generator = self.locker.make_lock_generator('response')
     try:
       lock_generator.next()
       response.save()
     except Exception as error:
       self.error(error)
     finally:
       lock_generator.next()
   except Exception as error:
     self.error(error)
     return None
예제 #10
0
파일: app.py 프로젝트: t20/skore
def add_response():
    username = request.form.get('username')
    username = username or 'anonymous'
    board_id = request.form.get('board_id')
    items = request.form.getlist('item')

    # print 'Items: {} // {}'.format(type(items), items)
    items = [int(x) for x in items if x]
    # print 'Items: {} // {}'.format(type(items), items)

    response = Response(username=username, board_id=int(board_id),
            items=items)
    saved = response.save()

    # print 'saved response? : {}'.format(saved)
    if saved == True:
        flash('Response saved. Thanks {}.'.format(username))
    else:
        flash('Could not save response')

    return redirect(url_for('response', response_id=response.id))