示例#1
0
    def get_object_list(self, request):
        #Store KA exercise content load in UserButton table
        if request.GET['key']:
            kusername = get_username(request.GET['key'])
        if kusername:
            if  Exercise.objects.filter(name=request.GET['name']).count()==1:
                kexercise = Exercise.objects.get(name=request.GET['name'])

                with transaction.commit_on_success():
                    kbook = Books.objects.get(book_name= request.GET['book'])
                    user_data, created = UserData.objects.get_or_create(user=kusername,book=kbook)
                module = get_module(request.GET['module'])

                if kexercise and module:
                    with transaction.commit_on_success():
                        user_module, exist =  UserModule.objects.get_or_create(user=kusername, book=kbook,module=module)
                    text = 'User loaded %s exercise' %request.GET['name']
                    user_button,correct = log_button_action(
                        kusername,
                        kexercise,
                        module,
                        kbook,
                        'load-ka',
                        text,
                        time.time()*1000,
                        None,
                        request.user_agent.browser.family,
                        request.user_agent.browser.version_string,
                        request.user_agent.os.family,
                        request.user_agent.os.version_string,
                        request.user_agent.device.family,
                        request.META['REMOTE_ADDR'],
                        )

        return super(ExerciseResource, self).get_object_list(request)
示例#2
0
    def logavbutton(self, request, **kwargs):
        print request.POST
        for key,value in request.POST.iteritems():
        #if request.POST['key']:
        #    kusername = get_username(request.POST['key'])

        #if kusername:
        #    actions = simplejson.loads(request.POST['actions'])
        #actions = simplejson.loads(request.POST)
            actions = json.loads(key) 
            number_logs = 0
            for act in actions:
                if 'score[total]' in request.POST:
                    streak = request.POST['score[total]']
                else:
                    streak = 0
                 
                with transaction.commit_on_success():
                    kusername, created = User.objects.get_or_create(username=act['user'])

                if  Exercise.objects.filter(name=act['av']).count()==1:
                    kexercise = Exercise.objects.get(name=act['av'])
                else:
                    with transaction.commit_on_success():
                        kexercise = Exercise(name=act['av'], streak=streak)
                        kexercise.save()

                with transaction.commit_on_success():
                    kbook = Books.objects.get(book_name= act['book']) #request.POST['book'])
                    user_data, created = UserData.objects.get_or_create(user=kusername,book=kbook)
                module = get_module(act['module'])

                if kexercise and module:
                    with transaction.commit_on_success():
                        user_module, exist =  UserModule.objects.get_or_create(user=kusername, book=kbook,module=module)
                    user_button,correct = log_button_action(
                        kusername,
                        kexercise,
                        module,
                        kbook,
                        act['type'],
                        act['desc'],
                        act['tstamp'],
                        act['uiid'],
                        request.user_agent.browser.family,
                        request.user_agent.browser.version_string,
                        request.user_agent.os.family,
                        request.user_agent.os.version_string,
                        request.user_agent.device.family,
                        request.META['REMOTE_ADDR'],
                        )
                    if correct:
                        number_logs += 1

        if number_logs == len(actions):
                return self.create_response(request, {'success': True, 'message': 'all button action logged'})
        else:
                return self.create_response(request, {'success': False, 'error': 'not all button action logged'}, HttpBadRequest)
        return self.create_response(request, {'success': False, 'error': 'unauthorized action'}, HttpUnauthorized)