From 8c2e595e3795d01208a39e68b326f41412335d46 Mon Sep 17 00:00:00 2001 From: tkupek Date: Tue, 9 Feb 2016 20:46:04 +0100 Subject: [PATCH] added ajax request to server side --- db.sqlite3 | Bin 70656 -> 70656 bytes tkupek_elearning/elearning/models.py | 5 ++- tkupek_elearning/elearning/views.py | 48 ++++++++++++++------------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/db.sqlite3 b/db.sqlite3 index 4978da742e8e875d4b7afad528d40e5c397f1aea..875621cdd91f436872397fb5b5244d730fdf4bf5 100644 GIT binary patch delta 1138 zcmZ`%U1%It6uxuMEXnSs`4_U{ZHC^RtuAtc`IiMcYQHg(}UojqxFK=6vVg z!}&huoH=JM@@Fpc?`{izObFSBuAy&1pO}j(n2g<}F1)os1CdCz=_$a3)qsA!x1D(C z3J+%?3Rzfy3-B`h46F1BU4iym^$K;vQPS5P29;x9E{A*gP&*8AX3jK9g=}FmJySMI z6ia&MQXQRPfEbrKVH75dX(N-*7Sg%mWQgtczR88H3*M>Y10gt1;BUAOzr$bf5E^h3 zuEURT6~2Y9!Gh0W9zKE(;Jg*&v(V=Pj`M~b3_2Ka(C?s+Sts8>!Fd8}@Cg2d2XGH% ze}h|a1FpdjD7uIZeFZi64CYaA7TDq}-x1=$%>``;*xp;W^GZQi|iu!HmIN8fc#vloBi~w1JJ-!(bq^a}QrQvl6B&#O5u;E(X_m?n;ccUI z?6^@HkVJ7%c>T!O*kE<`P3PW1=odT2j@LiWFBBQPp*8?M|yZ+)62d z@8NwIhhBP(o}-5-tyNdKuCb7t_>Xd1ICn~VtrQmyWeZb6Okf|bY !jk0<&Atxm< zE-9KMCH4f7r#n1(18=NYGV{Xz>GDi|Gj>AL^w9vaM;*4Zft@MliY1|(tq4ZmtSFl) zNV+B;^rOOCjzVIC0^53N>-Z)-S<@9|j}Li6?jF0}Be-oTXT^l##0m3QC6&pTnfU*@ V#$oD7QH#q7MHx<5;}1XC`47U*5M%%V delta 611 zcmX|8T}YEr7(VAbpKfl+a9d9L5g8k`Qu>j#tfphp&qZAX6}g1*)xPH1oH8{pgz!kv zO;q2GP!e<##BK`KcU6Jyhd>HRK~PD(D2R$e2%?Ktb_UWp=Q+I3IS((-InP*58Otem zw>I1-gdCGNC}EZ8(OcW)VcREGBbgbCawmcOKoV?U&{%<2+15=gY+b=PT9Lp@OyVeJ z@f8lXj$EFvuuZw-9s3K(U9{IomH~EwMOm4-`$%ST_$uHhpSWPPZ@2|`Ek0W^9MAj z^A6j1&k>W$@258|4yl(?nsh7|Z5OHAO`@&XWm40FBU)xqguHI?zGxAr@6=PDNEFXm zuBn;Jy=rD>z$Y5Kjq=iNAL>tPX}8bE!$Ffv^;147R!dvN&C+IZvUKZNi=r6Hg+De2 z9U}^vA*_?lqfZty_VLCpu}}(9zgU})Pbr6KNDP#N^uPwUin;P0b2V`}EW*6J xqs}X<~j=yA(+NJMDqL;jL6z!&p$3e#XkT5 diff --git a/tkupek_elearning/elearning/models.py b/tkupek_elearning/elearning/models.py index f90d6b6..d1e55c4 100644 --- a/tkupek_elearning/elearning/models.py +++ b/tkupek_elearning/elearning/models.py @@ -47,7 +47,10 @@ class User(models.Model): class UserAnswer(models.Model): user = models.ForeignKey(User, 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: unique_together = (('user', 'questionId'),) \ No newline at end of file diff --git a/tkupek_elearning/elearning/views.py b/tkupek_elearning/elearning/views.py index bdffe0e..bec9e7b 100644 --- a/tkupek_elearning/elearning/views.py +++ b/tkupek_elearning/elearning/views.py @@ -1,3 +1,4 @@ +from django.core.exceptions import ObjectDoesNotExist from django.http import HttpResponse from django.shortcuts import render_to_response @@ -5,8 +6,8 @@ from tkupek_elearning.elearning.models import Setting, Question, Option, UserAns import pdb -def home(request): +def home(request): settings = Setting.objects.filter(active=1) if settings: settings = settings[0] @@ -21,26 +22,27 @@ def home(request): def getAnswer(request): + if request.method == 'GET': - # if request.method == 'GET': - # pdb.set_trace() - # request_questionId = request.GET.get('id') - # request_answers = request.GET.get('answers') - # request_userToken = request.GET.get('user') - # - # question = Question.objects.filter(questionId=request_questionId) - # user = User.objects.filter(token=request_userToken) - # - # userAnswer = UserAnswer.objects.filter(questionId=question.id, user=user.token) - # if not userAnswer: - # userAnswer = UserAnswer() - # userAnswer.questionId = question - # userAnswer.user = user - # userAnswer.answers = "" - # userAnswer.save() - # else: - # return HttpResponse('error:alreadyAnswered'); - # - # options = Option.objects.filter(question=question.id) - # - # return HttpResponse(options) + request_id = request.GET.get('id') + request_token = request.GET.get('token') + request_answers = request.GET.get('answers') + + question = Question.objects.get(id=request_id) + user = User.objects.get(token=request_token) + + try: + userAnswer = UserAnswer.objects.get(questionId=question.id, user=user.id) + except ObjectDoesNotExist: + userAnswer = None + + if userAnswer is None: + userAnswer = UserAnswer() + userAnswer.questionId = question + userAnswer.user = user + userAnswer.answers = request_answers + userAnswer.save() + + options = Option.objects.filter(question=question.id, correct=True) + + return HttpResponse(options)