Beispiel #1
0
def user_exercise(request, string):
    code = ''
    user = None
    if request.user.is_authenticated():
        user = request.user
    
    if request.method == 'POST':
        try:
            exercise = Exercise.objects.get(name=string)
        except Exercise.DoesNotExist:
            exercise = Exercise(name=string)
            exercise.save()
        
        new_code = Code(user_id = user,
                        exercise_id = exercise,
                        content = request.POST['code'],
                        engine = request.POST['engine'],
                        isRevert = request.POST['isRevert']
                        )
        if user == None:
            new_code.session_key = request.session.session_key
        
        new_code.save()
            # cache.set(string, request.POST['new_code'])
            # print cache.get(string)
        return HttpResponse(new_code.id)
    
    else: #GET 
        if request.user.is_authenticated(): 
          if not code:
            try:
               exercise = Exercise.objects.get(name=string)
               exists = True 
            except Exercise.DoesNotExist:
               return HttpResponseNotFound('page not found')
            if exists:
               user_exercise_code = Code.objects.filter(user_id=request.user, exercise_id= exercise).order_by('-id')[:1]
               if user_exercise_code.exists():
                  code = user_exercise_code[0].content
                  engine = user_exercise_code[0].engine
                  # cache.set(string, code) 
               else:
                   return HttpResponseNotFound('page not found')
          data = simplejson.dumps({'code': code, 'engine': engine})
          return HttpResponse(data, mimetype = 'application/json')
    return HttpResponseNotFound('page not found')
Beispiel #2
0
 def test_manually_post_code_exercise(self):
     count = Exercise.objects.count()
     e = Exercise(name='testing')
     e.save()
     new_count = Exercise.objects.count()
     self.assertEqual(count+1, new_count)