added ajax request to server side

This commit is contained in:
tkupek 2016-02-09 20:46:04 +01:00
parent 6614bf8c26
commit 8c2e595e37
3 changed files with 29 additions and 24 deletions

Binary file not shown.

View file

@ -47,7 +47,10 @@ class User(models.Model):
class UserAnswer(models.Model): class UserAnswer(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False) user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
questionId = models.ForeignKey(Question, on_delete=models.CASCADE, null=False) questionId = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
answers = models.ForeignKey(Option, on_delete=models.CASCADE, null=False) answers = models.CharField(max_length=100, null=True)
def __str__(self):
return str(self.user) + " - " + str(self.questionId)
class Meta: class Meta:
unique_together = (('user', 'questionId'),) unique_together = (('user', 'questionId'),)

View file

@ -1,3 +1,4 @@
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
@ -5,8 +6,8 @@ from tkupek_elearning.elearning.models import Setting, Question, Option, UserAns
import pdb import pdb
def home(request):
def home(request):
settings = Setting.objects.filter(active=1) settings = Setting.objects.filter(active=1)
if settings: if settings:
settings = settings[0] settings = settings[0]
@ -21,26 +22,27 @@ def home(request):
def getAnswer(request): def getAnswer(request):
if request.method == 'GET':
# if request.method == 'GET': request_id = request.GET.get('id')
# pdb.set_trace() request_token = request.GET.get('token')
# request_questionId = request.GET.get('id') request_answers = request.GET.get('answers')
# request_answers = request.GET.get('answers')
# request_userToken = request.GET.get('user') question = Question.objects.get(id=request_id)
# user = User.objects.get(token=request_token)
# question = Question.objects.filter(questionId=request_questionId)
# user = User.objects.filter(token=request_userToken) try:
# userAnswer = UserAnswer.objects.get(questionId=question.id, user=user.id)
# userAnswer = UserAnswer.objects.filter(questionId=question.id, user=user.token) except ObjectDoesNotExist:
# if not userAnswer: userAnswer = None
# userAnswer = UserAnswer()
# userAnswer.questionId = question if userAnswer is None:
# userAnswer.user = user userAnswer = UserAnswer()
# userAnswer.answers = "" userAnswer.questionId = question
# userAnswer.save() userAnswer.user = user
# else: userAnswer.answers = request_answers
# return HttpResponse('error:alreadyAnswered'); userAnswer.save()
#
# options = Option.objects.filter(question=question.id) options = Option.objects.filter(question=question.id, correct=True)
#
# return HttpResponse(options) return HttpResponse(options)