From dd1366a5095d92aad1bc8dfc206facfd3c79ae99 Mon Sep 17 00:00:00 2001 From: tkupek Date: Sun, 14 Feb 2016 15:02:26 +0100 Subject: [PATCH] added token to user management and message for already answered questions --- db.sqlite3 | Bin 70656 -> 70656 bytes tkupek_elearning/elearning/models.py | 16 ++++- .../elearning/static/css/style.css | 10 +++ .../elearning/static/js/elearning.js | 66 +++++++++++++++--- .../elearning/templates/access_denied.html | 25 +++++++ .../templates/{index.html => elearning.html} | 9 ++- tkupek_elearning/elearning/views.py | 51 +++++++++++--- 7 files changed, 152 insertions(+), 25 deletions(-) create mode 100644 tkupek_elearning/elearning/templates/access_denied.html rename tkupek_elearning/elearning/templates/{index.html => elearning.html} (76%) diff --git a/db.sqlite3 b/db.sqlite3 index ff2ac8a938b98f5fb56ecfbaea73cbf36150970d..41e595aa2f11886dc80ebe65cc36f5f05d1653ed 100644 GIT binary patch delta 3044 zcmah~eQX>@72lb+=I$JS;_okI;;J8!e0#TFcej_)#7=2t$!S6p5;;wDTHmfON6vTd z-a1YiihBtNl}Z7332BI^1pT8G6_uQ;K*$M=QqiJ<(h$)SAV3LK)uJdAiKd7~g*SV) zbB>KzY2WP5%=^vz_`NqzydXaDg7~eT<_m%#d>-EfzH9J3K0Bb|WcWQ=gMUG|j8fPz zH@P1I@)&;JwYz*3nAkfi)Y1z9`aTRm7Utn3Y=(>U0{suPL;GB5mIk3kFkn>ZnH}u# zF4T5C(%MELLaZ;D`Ai`-k-OE(2ipbs1U`Vj!Z+bb zI0`nTU=%)rzr!ElxA1HDCHx#-fuBMd9*1do6!yb*_z^r0--lChe6G|C-aBuK_to~* z03m^1U-tO2o6FiRUv~Pk!dO}I_;dmKVA2a5yt5sC0N;aWz=LCG&sSjv z9)l^EoGVEX7a>IYxaj4ghl_45y13}%qJxWeF50+g<)Wqh{NcU9Itn*BZU7tK}bm`z9&BdoDswKU2eMXhxu!xe}q2 zd^}s)-E_2_)C)!_v^8}15h#t0dfVpP@7rE*oI?Z5nqp&)I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%&P7GPk*=2XL$XONPF0q2}zgx~tiMH+K&0?qHj+o&_F&lFItzvpsalN3LKSQquMT*H`P$Nva9(pC@ zup9}?+70Ynh`oki6{lA)r?6+xa|65L4Gx{^p^Ju2A@!R);uOJk^}l(1qvU@|At&*m zZfax|rM-+r42?7>J?Oedny8*g@H+L1TBzQaNoG*YMw`A z{l)c0zsj~M>kE1=o<}UoQbdV&FSfB|c7i?6Qnl*6EJ)3NsvV`%{N?LfGh+m&aTrgd sjh$l$*iOc3)kRwWM9d(o25AE|(g``Sv?1vE`HT@XE?;d48mAlizlSarpa1{> diff --git a/tkupek_elearning/elearning/models.py b/tkupek_elearning/elearning/models.py index 42bf6fe..e360896 100644 --- a/tkupek_elearning/elearning/models.py +++ b/tkupek_elearning/elearning/models.py @@ -1,16 +1,21 @@ from __future__ import unicode_literals -import uuid +import hashlib +import os +import binascii from django.db import models from django.db.models.signals import post_init, pre_init class Setting(models.Model): title = models.CharField(max_length=100, null=True) - message = models.TextField(null=True) footer = models.TextField(null=True) + message_welcome_user = models.TextField(null=True) + message_access_denied = models.TextField(null=True) + message_already_answered = models.TextField(null=True) button_solution = models.CharField(max_length=100, null=True) + logo = models.CharField(max_length=256, null=True) active = models.BooleanField(unique=True, default=False) def __str__(self): @@ -36,8 +41,13 @@ class Option(models.Model): return self.text +def gen_token(): + return hashlib.sha1(os.urandom(128)).hexdigest() + + class User(models.Model): - token = models.CharField(max_length=32, null=True) + + token = models.CharField(max_length=40, null=True, default=gen_token) name = models.CharField(max_length=100, null=False) def __str__(self): diff --git a/tkupek_elearning/elearning/static/css/style.css b/tkupek_elearning/elearning/static/css/style.css index 2702043..58ff5ad 100644 --- a/tkupek_elearning/elearning/static/css/style.css +++ b/tkupek_elearning/elearning/static/css/style.css @@ -41,3 +41,13 @@ p[id^="explanation_"] { .failed { outline: 4px solid #a60000; } + +.mainLogo { + width: 300px; + height: auto; + float: right; +} + +.message_already_answered { + font-style: italic; +} diff --git a/tkupek_elearning/elearning/static/js/elearning.js b/tkupek_elearning/elearning/static/js/elearning.js index 6a2e8f0..12a0171 100644 --- a/tkupek_elearning/elearning/static/js/elearning.js +++ b/tkupek_elearning/elearning/static/js/elearning.js @@ -1,6 +1,54 @@ +var QueryString = function () { + // This function is anonymous, is executed immediately and + // the return value is assigned to QueryString! + var query_string = {}; + var query = window.location.search.substring(1); + var vars = query.split("&"); + for (var i=0;i + + + + + + {% load staticfiles %} + + + + {{ settings.title }} + + + + +
+ +

{{ settings.title }}

+
+

{{ settings.message_access_denied }}

+ +
+ + + \ No newline at end of file diff --git a/tkupek_elearning/elearning/templates/index.html b/tkupek_elearning/elearning/templates/elearning.html similarity index 76% rename from tkupek_elearning/elearning/templates/index.html rename to tkupek_elearning/elearning/templates/elearning.html index 7158005..10c7359 100644 --- a/tkupek_elearning/elearning/templates/index.html +++ b/tkupek_elearning/elearning/templates/elearning.html @@ -7,15 +7,15 @@ {% load staticfiles %} - {{ settings.title }}
+

{{ settings.title }}

-

{{ settings.message }}

+

{{ settings.message_welcome_user }}


@@ -23,6 +23,10 @@

{{ question.id }}: {{ question.title }}

{{ question.text }}

+

+ {{ settings.message_already_answered }} +

+
{% for option in options %} @@ -44,5 +48,6 @@
+ \ No newline at end of file diff --git a/tkupek_elearning/elearning/views.py b/tkupek_elearning/elearning/views.py index dc452e8..39f7b34 100644 --- a/tkupek_elearning/elearning/views.py +++ b/tkupek_elearning/elearning/views.py @@ -4,20 +4,43 @@ from django.shortcuts import render_to_response from tkupek_elearning.elearning.models import Setting, Question, Option, UserAnswer, User -# import pdb +import pdb def home(request): + + token = request.GET.get('token') + + try: + user = User.objects.get(token=token) + except ObjectDoesNotExist: + user = None + settings = Setting.objects.filter(active=1) if settings: settings = settings[0] - questions_options = {} - questions = Question.objects.all() - for question in questions: - options = Option.objects.filter(question=question.id) - questions_options[question] = options + if user is not None: + settings.message_welcome_user = settings.message_welcome_user.replace('{username}', user.name) - return render_to_response('index.html', {'settings': settings, 'questions_options': questions_options}) + questions_options = {} + questions = Question.objects.all() + for question in questions: + options = Option.objects.filter(question=question.id) + + user_answer = get_user_answer(question, user) + + if user_answer is None: + question.enable = True + else: + question.enable = False + + questions_options[question] = options + questions_options[question] = options + + return render_to_response('elearning.html', {'settings': settings, 'questions_options': questions_options}) + + else: + return render_to_response('access_denied.html', {'settings': settings}) def get_answer(request): @@ -30,10 +53,7 @@ def get_answer(request): question = Question.objects.get(id=request_id) user = User.objects.get(token=request_token) - try: - user_answer = UserAnswer.objects.get(question=question.id, user=user.id) - except ObjectDoesNotExist: - user_answer = None + user_answer = get_user_answer(question, user) if user_answer is None: user_answer = UserAnswer() @@ -52,3 +72,12 @@ def get_answer(request): options_id = options_id[:-1] return HttpResponse(options_id) + + +def get_user_answer(question, user): + try: + user_answer = UserAnswer.objects.get(question=question.id, user=user.id) + except ObjectDoesNotExist: + user_answer = None + + return user_answer; \ No newline at end of file