Esempio n. 1
0
def prov_log_action(request):
    #TODO better error handling and feedback
    status = "ok"
    message = "This is not correct."
    
    if not request.user.is_active:
        json_data = json.dumps({"message":"You are currently not logged in. Please login to continue.", "status":"logged_out"})            
        return HttpResponse(json_data, mimetype="application/json")
    
    #if request.is_ajax():
    if request.method == 'POST':
        serial = request.POST.get('serial', None)
        action = request.POST.get('action', None) #'move' or 'click'
        node = request.POST.get('node', None)
        x = request.POST.get('x', None)
        y = request.POST.get('y', None)
        attribute = request.POST.get('attribute', None)
        position = request.POST.get('position', None)
        inactive = json.loads(request.POST.get('inactive', None))
        if attribute == "none":
            attribute = None
        try:
            state = json.loads(request.POST.get('state', None))
        except:
            state = None
        
        #TODO log everything, including clicking
        cronDocumentInstance = None
        mopDocumentInstance = None
        provenance = Provenance.objects.get(serial=serial)
        if provenance.type == Provenance.TYPE_CRON:
            documentInstance = CronDocumentInstance.objects.get(cronDocument=provenance.document, cron=request.user.cron)
            cronDocumentInstance = documentInstance
            if cronDocumentInstance.cronDocument.autoSolve:
                inactive = False
        elif provenance.type == Provenance.TYPE_MOP_INSTANCE:
            documentInstance = MopDocumentInstance.objects.get(randomizedDocument=provenance.randomizedDocument, mop=request.user.mop)
            mopDocumentInstance = documentInstance
        else:
            message = "no document instance found"
            error = True
        
        if documentInstance is not None:
            try:
                stored_data = json.loads(documentInstance.provenanceState)
            except:
                stored_data = []
            
            updated = False
            if action == 'move':
                if not inactive:
                    for data in stored_data:
                        try:
                            if data['node'] == node:
                                data['x'] = x
                                data['y'] = y
                                updated = True
                                break
                        except:
                            pass
                    if not updated:
                        stored_data.append({"node":node, "x":x, "y":y})
                    documentInstance.provenanceState = json.dumps(stored_data)
                    documentInstance.save()
                    message = 'position updated'
                else:
                    message = 'position ignored as is inactive'
                error = False
                logAction = ProvLog.ACTION_MOVE
                
            
            elif action == 'click':
                if attribute == 'mop:__url':
                    message = 'media opened registered'
                    error = False
                    logAction = ProvLog.ACTION_MEDIA
                else:    
                    if not inactive:
                        for data in stored_data:
                            try:
                                if data['position'] == position:
                                    if state and attribute is not None:
                                        data['selected_node'] = node
                                        data['selected_attribute'] = attribute
                                        updated = True
                                        break
                                    elif state and attribute is None:
                                        data['selected_node'] = node
                                        data['selected_attribute'] = None
                                        updated = True
                                        break
                                    elif not state and attribute is not None:
                                        data['selected_node'] = node
                                        data['selected_attribute'] = None
                                        updated = True
                                        break
                                    elif not state and attribute is None:
                                        data['selected_node'] = None
                                        data['selected_attribute'] = None
                                        updated = True
                                        break
                            except:
                                pass
                        if not updated:
                            stored_data.append({"position":position, "selected_node":node, "selected_attribute":attribute})
                        documentInstance.provenanceState = json.dumps(stored_data)
                        documentInstance.save()
                        message = 'click registered'
                    else:
                        message = 'click ignored as is inactive'
                    error = False
                    logAction = ProvLog.ACTION_CLICK
            logging.log_prov(action=logAction, cronDocumentInstance=cronDocumentInstance, mopDocumentInstance=mopDocumentInstance, node1=node, attribute1=attribute, x=x, y=y, selected=state, inactive=inactive)
        
        if error:
            status = "error"
        json_data = json.dumps({"message":message, "status":status})            
        return HttpResponse(json_data, mimetype="application/json") 
Esempio n. 2
0
def prov_check(request):
    
    correct = False
    message = "This is not correct."
    status = "ok"
    
    if not request.user.is_active:
        json_data = json.dumps({"message":"You are currently not logged in. Please login to continue.", "status":"logged_out"})            
        return HttpResponse(json_data, mimetype="application/json")

    #if request.is_ajax():
    if request.method == 'POST':
        serial = request.POST.get('serial', "")
        post_node1 = request.POST.get('node1', "")
        post_node2 = request.POST.get('node2', "")
        post_attribute1 = request.POST.get('attribute1', "")
        post_attribute2 = request.POST.get('attribute2', "")
        is_empty = json.loads(request.POST.get('is_empty', False))
        is_test = json.loads(request.POST.get('is_test', False))
        
        player_attribute1 = makeAttributeString(post_node1, post_attribute1)
        player_attribute2 = makeAttributeString(post_node2, post_attribute2)
        
        try:
            provenance = Provenance.objects.get(serial=serial)
            try: 
                attribute1_json = json.loads(provenance.attribute1)
                attribute2_json = json.loads(provenance.attribute2)
            except:
                attribute1_json = []
                attribute2_json = []
        #if we have no proper serial in the post, then we are probably looking at a randomized MOP_TEMPLATE
        except Provenance.DoesNotExist:
            provenance = Provenance.objects.get(id=request.session['prov_id'])
            try:
                attribute1_json = json.loads(request.session['attribute1'])
                attribute2_json = json.loads(request.session['attribute2'])
            except:
                attribute1_json = None
                attribute2_json = None
                
            print 'got stuff from the session cookie'
        
        stored_attribute1_list = []
        stored_attribute2_list = []
        
        if provenance.attribute1 is None and provenance.attribute2 is None:
            if is_empty:
                correct = True
        
        else:
            for a_json in attribute1_json:
                stored_attribute1_list.append(makeAttributeString(a_json['node'], a_json['attribute']))
            for a_json in attribute2_json:
                stored_attribute2_list.append(makeAttributeString(a_json['node'], a_json['attribute']))
            
            if player_attribute1 in stored_attribute1_list and player_attribute2 in stored_attribute2_list:
                correct = True
            elif player_attribute1 in stored_attribute2_list and player_attribute2 in stored_attribute1_list:
                correct = True
       
        stars = None
        if is_test and request.user.is_staff:
            if correct:
                message = "Test feedback: Yes, this is where the inconsistency is!"
            else:
                message = "Test feedback: No, this is not correct."
            close_prov = False
        else:
            cronDocumentInstance = None
            mopDocumentInstance = None
            if provenance.type == Provenance.TYPE_CRON:
                #TODO check properly for cron-user and if Instance exists
                cronDocumentInstance = CronDocumentInstance.objects.get(cronDocument=provenance.document, cron=request.user.cron)
                logging.log_action(ActionLog.ACTION_CRON_PROVENANCE_SUBMIT, cron=cronDocumentInstance.cron, cronDocumentInstance=cronDocumentInstance, cronDocumentInstanceCorrect=correct)
                if correct:
                    cronDocumentInstance.solved = True
                    cronDocumentInstance.save()
                    close_prov = True
                    stars = cronDocumentInstance.getStars()
                    attempts = cronDocumentInstance.failedAttempts + 1
                    
                    if stars == 3:
                        message = "Excellent job! This will be reflected in your agent profile."
                    elif stars == 2:
                        message = "It took you %s tries, but good job anyway. Your agent profile has been updated." % attempts
                    elif stars == 1:
                        message = "Yes, you did it. It did take %s attempts, but the result is what counts. Your success has been logged in your agent profile." % attempts
                    
                    
                else:
                    cronDocumentInstance.increaseFailedAttempts()
                    close_prov = False
                    if cronDocumentInstance.failedAttempts >= 50:
                        message = "Yeah. Not it."
                    elif cronDocumentInstance.failedAttempts >= 30:
                        message = "Honestly, now you are just guessing, right? Contact HQ, they might have uncovered an additional clue!"
                    elif cronDocumentInstance.failedAttempts >= 10:
                        message = "You are trying very hard, but this is not it. Maybe send a message to HQ to see if they have more intel."
                    elif is_empty:
                        message = "No, we are pretty sure that something is wrong with this data. Please keep investigating."
                    else:
                        message = "The data you submitted does not seem suspicious. Please keep investigating."
                    
                  
            elif provenance.type == Provenance.TYPE_MOP_INSTANCE:
                message = "Provenance modification saved. Please submit document now."  
                #TODO check properly for mop-user and if Instance exists
                mopDocumentInstance = MopDocumentInstance.objects.get(randomizedDocument=provenance.randomizedDocument, mop=request.user.mop)
                mopDocumentInstance.modified = True
                mopDocumentInstance.correct = correct
                mopDocumentInstance.save()
                logging.log_action(ActionLog.ACTION_MOP_PROVENANCE_SUBMIT, mop=mopDocumentInstance.mop, mopDocumentInstance=mopDocumentInstance, mopDocumentInstanceCorrect=correct)
                tutorial.checkProvenance(mopDocumentInstance.mop.mopTracker, mopDocumentInstance.correct)
                close_prov = True
            logging.log_prov(action=ProvLog.ACTION_SUBMIT, cronDocumentInstance=cronDocumentInstance, mopDocumentInstance=mopDocumentInstance, node1=post_node1, node2=post_node2, attribute1=post_attribute1, attribute2=post_attribute2, empty=is_empty, correct=correct)

        
        json_data = json.dumps({"close_prov":close_prov, "status":status, "message":message, "stars":stars})

        return HttpResponse(json_data, mimetype="application/json")