def ismoduleproficient(self, request, **kwargs): if request.POST['key']: print request.POST['key'] kusername = get_username(request.POST['key']) if kusername: kmodule = get_module(request.POST['module']) kbook = get_book(request.POST['book']) user_module = get_user_module(kusername, kbook, kmodule) if user_module is not None: with transaction.commit_on_success(): user_data, created = UserData.objects.get_or_create(user=kusername, book=kbook) update_module_proficiency(user_data, request.POST['module'], None) return self.create_response(request, {'proficient': user_module.is_proficient_at()}) return self.create_response(request, {'proficient': False}) return self.create_response(request, {'error': 'unauthorized action'}, HttpUnauthorized)
def loadmodule(self, request, **kwargs): if request.POST['key']: kusername = get_username(request.POST['key']) if kusername: response = {} #get or create Book & link a book to user kbook = get_book(request.POST['book']) if kbook is None: with transaction.commit_on_success(): kbook, added = Books.objects.get_or_create(book_name= request.POST['book'], book_url = request.POST['url']) ubook, created = UserBook.objects.get_or_create(user=kusername, book=kbook) else: #link a book to user ubook = get_user_book(kusername, kbook) if ubook is None: with transaction.commit_on_success(): ubook, created = UserBook.objects.get_or_create(user=kusername, book=kbook) #get or create module kmodule = get_module(request.POST['module']) if kmodule is None: with transaction.commit_on_success(): kmodule, added = Module.objects.get_or_create(name=request.POST['module']) response[kmodule.name] = False #get or create exercises mod_exes = simplejson.loads(request.POST['exercises']) print mod_exes exers_ = [] for mod_exe in mod_exes: if Exercise.objects.filter(name=mod_exe['exercise']).count() == 0: # Add new exercise with transaction.commit_on_success(): kexercise, added = Exercise.objects.get_or_create(name=mod_exe['exercise'], covers="dsa", description=mod_exe['name'], ex_type=mod_exe['type'], streak=mod_exe['threshold']) else: # Update existing exercise with transaction.commit_on_success(): kexercise = get_exercise(mod_exe['exercise']) kexercise.covers="dsa" kexercise.description=mod_exe['name'] kexercise.ex_type=mod_exe['type'] kexercise.streak=Decimal(mod_exe['threshold']) kexercise.save() #add exercise in list of module exercises exers_.append(kexercise) if UserData.objects.filter(user=kusername, book=kbook).count() > 0: user_data = UserData.objects.get(user=kusername, book=kbook) u_prof = user_data.is_proficient_at(kexercise) #check student progress -- KA exercises user_exercise = get_user_exercise(user=kusername, exercise=kexercise) if user_exercise is not None: u_prog = user_exercise.progress if user_exercise.is_proficient() and not user_data.is_proficient_at(kexercise): user_data.earned_proficiency(kexercise.id) user_data.save() u_prof = True else: u_prog = 0 #Link exercise to module and books only if the exercise is required if BookModuleExercise.components.filter(book=kbook, module=kmodule, exercise=kexercise).count() == 0 and mod_exe['required']: with transaction.commit_on_success(): bme = models.BookModuleExercise(book=kbook, module=kmodule, exercise=kexercise, points=mod_exe['points']) bme.save() response[kexercise.name] = {'proficient': u_prof, 'progress': u_prog} # Remove exercises that are no longer part of this book / module for exer in BookModuleExercise.components.get_mod_exercise_list(kbook,kmodule): #get_exercise_list(kbook): if exer not in exers_: BookModuleExercise.components.filter(book=kbook, module=kmodule, exercise=exer).delete() #check module proficiency user_module = get_user_module(user=kusername, book=kbook, module=kmodule) if user_module is not None: with transaction.commit_on_success(): user_data, created = UserData.objects.get_or_create(user=kusername,book = kbook) update_module_proficiency(user_data, request.POST['module'], None) #Module proficiency response response[kmodule.name] = user_module.is_proficient_at() return self.create_response(request, response) return self.create_response(request, {'error': 'unauthorized action'}, HttpUnauthorized)