Пример #1
0
def respondchannel(request, pk1, pk2, num):
    context = {}
    if request.user.is_authenticated():
        print 'response'
        print request.GET.get('response')
        new_response = Response()
        print num
        if int(num) == 1:
            new_response.is_resp_to_reply = False
        else:
            new_response.is_resp_to_reply = True
        print 'New resp' + str(new_response.is_resp_to_reply)
        new_response.text = request.GET.get('response')
        new_response.channel = Channel.objects.get(pk=pk1)
        new_response.original_comment = Comment.objects.get(pk=pk2)
        new_response.user = request.user
        new_response.save()
        return HttpResponseRedirect('/channel_detail/%s' % pk1)

    else:
        print 'anonymous user'
        return HttpResponseRedirect('/please_login/')
    return render_to_response('channel_detail.html',
                              context,
                              context_instance=RequestContext(request))
Пример #2
0
    def get_next_task_view(self):
        self.client.login(username="******", password="******")
        ## get the next task
        self.task_expectation.check(self)
        ## tweak the database to put a response and a review in
        response = Response(task=self.t,
                            user=self.user,
                            start_time=timezone.now())
        response.full_clean()
        response.save()
        review = Review(response=response,
                        comment="I have reviewed this task.")
        review.full_clean()
        review.save()
        ## check to see if next_task now redirects to review
        ## if we actually execute the view code, we will get an error,
        ## because the task associated with the review has no result;
        ## therefore the code below is a bit hackish
        review_target = WebTarget("GET",
                                  main.views.base.next_task,
                                  statuses=(301, 302))

        def redirects_to_review(input_context, output_context):
            return "/review/next/" in output_context["__redirected_to__"]

        review_expectation = ViewExpectation(
            Conditions.post(redirects_to_review), review_target)
        review_expectation.check(self)
        ## tweak the database to complete the review
        review.complete = True
        review.full_clean()
        review.save()
        ## try getting the next task again
        self.task_expectation.check(self)
Пример #3
0
    def get_next_task_view(self):
        self.client.login(username="******", password="******")
        ## get the next task
        self.task_expectation.check(self)
        ## tweak the database to put a response and a review in
        response = Response(task=self.t, user=self.user, start_time=datetime.datetime.now())
        response.full_clean()
        response.save()
        review = Review(response=response, comment="I have reviewed this task.")
        review.full_clean()
        review.save()
        ## check to see if next_task now redirects to review
        ## if we actually execute the view code, we will get an error,
        ## because the task associated with the review has no result;
        ## therefore the code below is a bit hackish
        review_target = WebTarget("GET", main.views.base.next_task, statuses=(301, 302))

        def redirects_to_review(input_context, output_context):
            return "/review/next/" in output_context["__redirected_to__"]

        review_expectation = ViewExpectation(Conditions.post(redirects_to_review), review_target)
        review_expectation.check(self)
        ## tweak the database to complete the review
        review.complete = True
        review.full_clean()
        review.save()
        ## try getting the next task again
        self.task_expectation.check(self)
def crowd_respond_to_study(request, study_id):
    # There are two ways the client makes ajax calls. Once at the beginning just to get the first segment,
    # and also after a response is made to record the response AND get a new segment
    if request.is_ajax():

        # This is true if the ajax is returning a response
        if not request.POST.get('segment_ajax') is None:
            seg = Segment.objects.get(id=request.POST.get('segment_id'))
            new_response = Response(segment=seg,
                                    user=request.user,
                                    label_id=request.POST.get('label_id'))
            new_response.save()
            check_segment(seg)
        prev_responses = list(
            Response.objects.filter(
                worker_id=request.user.id).values('segment_id'))
        high_priorities = Segment.objects.filter(
            study_id=study_id,
            status='high_priority').exclude(id__in=prev_responses)

        if high_priorities.exists():
            available_segments = high_priorities
        else:
            available_segments = Segment.objects.filter(study_id=study_id,
                                                        status='low_priority')

        chosen_segment = available_segments[random.randint(
            0,
            len(available_segments) - 1)]
        data = {
            'seg_id': chosen_segment.id,
            'file_name': chosen_segment.file.filename,
            'start': float(chosen_segment.start),
            'stop': float(chosen_segment.stop),
            'duration': float(chosen_segment.duration),
        }
        return render_to_json_response(data)
    study_worker = StudyWorker.objects.filter(study_id=study_id,
                                              worker_id=request.user.id)
    if study_worker is None:
        return redirect('/')
    template = loader.get_template('main/respond_to_study.html')
    labels = Label.objects.filter(study_id=study_id)
    context = {
        'study': Study.objects.get(id=study_id),
        'labels': labels,
        'remaining': 17
    }
    return HttpResponse(template.render(context, request))
Пример #5
0
def respondchannel(request, pk1, pk2, num):
    context = {}
    if request.user.is_authenticated():
        print 'response'
        print request.GET.get('response')
        new_response = Response() 
        print num
        if int(num) == 1:
            new_response.is_resp_to_reply = False
        else:
            new_response.is_resp_to_reply = True
        print  'New resp' + str(new_response.is_resp_to_reply)
        new_response.text = request.GET.get('response')
        new_response.channel = Channel.objects.get(pk=pk1)
        new_response.original_comment = Comment.objects.get(pk=pk2)
        new_response.user = request.user
        new_response.save()
        return HttpResponseRedirect('/channel_detail/%s' %pk1) 
   
    else:  
        print 'anonymous user'
        return HttpResponseRedirect('/please_login/')
    return render_to_response('channel_detail.html', context, context_instance=RequestContext(request))
Пример #6
0
def parsePrompt(chunk):
    global tab, vars, storyName, promptIdCounter

    #print(chunk)
    #find the offset, and then change the chunk to a standard form
    offset = 0
    for c in chunk[0]:
        if c == tab:
            offset += 1
        else:
            break

    #standardize
    for i in range(len(chunk)):
        chunk[i] = chunk[i][offset:]

    #print(chunk)

    prompt = Prompt(promptText=chunk[0],
                    storyId=storyName,
                    option1="",
                    promptId=promptIdCounter)
    promptIdCounter += 1
    prompt.save()

    #starting with first line of first option, iterate through the lines
    for j in range(1, len(chunk)):
        line = chunk[j]
        #print(line)

        # check if the line is set off by a tab
        # if not, then we found an option
        if line[0] != tab and prompt.option1 == "":
            #set the option1 for the prompt to this line
            prompt.option1 = line
            #then create the response object
            option1 = Response(storyId=storyName,
                               option1or2="1",
                               previousPrompt=prompt,
                               ifStatement="")
            print("response created for option1", option1)
            option1.save()
            optionsToPassUp1 = [option1]
            #now we start from what comes under the option
            #these sublines will be offset by 1 tab
            k = j + 1
            for notk in range(j + 1, len(chunk)):
                subline = chunk[k]
                #print("subline1",[subline.split()])
                #if there is no tab, then we have reached the end
                if subline[0] != tab:
                    #print('move on')
                    break
                #this means that we have found the nextPrompt
                elif subline[1] == tab:
                    #print('go deeper')
                    countForNextPrompt = 0
                    for l in range(k, len(chunk) + 1):
                        #print(chunk[l])
                        # the nextPrompt will be in atleast 2 deep, anything shallower is not a part of it
                        if l == len(chunk) or chunk[l][1] != tab:
                            #print(k,l)
                            option1.nextPrompt, subOptionsToPassUp1 = parsePrompt(
                                chunk[k:l])
                            print("retrieved response from deeper option1")
                            optionsToPassUp1.pop(-1)
                            optionsToPassUp1.extend(subOptionsToPassUp1)
                            #print(optionsToPassUp1)
                            #print("returned up")
                            k = l - 1
                            #print("option1: ",option1)
                            option1.save()
                            break
                #if statement
                elif subline[1:3] == "if" and subline[-1] == ":":
                    if option1.ifStatement == "":
                        option1.ifStatement = subline[1:]
                    else:
                        option1.save()
                        option1 = Response(storyId=storyName,
                                           option1or2="1",
                                           previousPrompt=prompt)
                        print("created an if response for options 1", option1)
                        option1.ifStatement = subline[1:]
                        option1.save()
                        optionsToPassUp1.append(option1)
                #set variable statement
                elif len(subline[1:].split()) > 2 and subline[1:].split(
                )[1] == ":=" and subline.split()[0][1:] in vars:
                    option1.setVariable = subline[1:]
                #otherwise, it's (hopefully) the response text, if any
                else:
                    #print("response text: ",subline)
                    option1.responseText = subline[1:]
                k += 1
                if k >= len(chunk): break
            option1.save()

        #for option 2
        elif line[0] != tab and prompt.option1 != "":
            #set the option2 for the prompt to this line
            prompt.option2 = line
            #then create the response object
            option2 = Response(storyId=storyName,
                               option1or2="2",
                               previousPrompt=prompt,
                               ifStatement="")
            print("response created for option2", option2)
            option2.save()
            optionsToPassUp2 = [option2]
            #now we start from what comes under the option
            #these sublines will be offset by 1 tab
            k = j + 1
            for notk in range(j + 1, len(chunk)):
                subline = chunk[k]
                #print("subline2",[subline.split()])
                #if there is no tab, then we have reached the end
                if subline[0] != tab:
                    #print('move on')
                    break
                #this means that we have found the nextPrompt
                elif subline[1] == tab:
                    print('go deeper')
                    print(chunk)
                    countForNextPrompt = 0
                    for l in range(k, len(chunk) + 1):
                        #print(chunk[l])
                        # the nextPrompt will be in atleast 2 deep, anything shallower is not a part of it
                        if l == len(chunk) or chunk[l][1] != tab:
                            #print(k,l)
                            option2.nextPrompt, subOptionsToPassUp2 = parsePrompt(
                                chunk[k:l])
                            print("retrieved response from deeper option2")
                            optionsToPassUp2.pop(-1)
                            optionsToPassUp2.extend(subOptionsToPassUp2)
                            #print(optionsToPassUp2)
                            print("returned up")
                            k = l - 1
                            #print("option1: ",option1)
                            option2.save()
                            break
                #if statement
                elif subline[1:3] == "if" and subline[-1] == ":":
                    if option2.ifStatement == "":
                        option2.ifStatement = subline[1:]
                    else:
                        option2.save()
                        option2 = Response(storyId=storyName,
                                           option1or2="2",
                                           previousPrompt=prompt)
                        print("created an if response for options 2", option2)
                        option2.ifStatement = subline[1:]
                        option2.save()
                        optionsToPassUp2.append(option2)
                #set variable statement
                elif len(subline[1:].split()) > 2 and subline[1:].split(
                )[1] == ":=" and subline.split()[0][1:] in vars:
                    option2.setVariable = subline[1:]
                #otherwise, it's (hopefully) the response text, if any
                else:
                    #print("response text: ",subline)
                    option2.responseText = subline[1:]
                k += 1
                if k >= len(chunk): break
            option2.save()

    prompt.save()
    optionsToPassUp2.extend(optionsToPassUp1)
    return prompt, optionsToPassUp2