added token to user management and message for already answered questions

This commit is contained in:
tkupek 2016-02-14 15:02:26 +01:00
parent a6589b9e84
commit dd1366a509
7 changed files with 152 additions and 25 deletions

Binary file not shown.

View file

@ -1,16 +1,21 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import uuid import hashlib
import os
import binascii
from django.db import models from django.db import models
from django.db.models.signals import post_init, pre_init from django.db.models.signals import post_init, pre_init
class Setting(models.Model): class Setting(models.Model):
title = models.CharField(max_length=100, null=True) title = models.CharField(max_length=100, null=True)
message = models.TextField(null=True)
footer = 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) button_solution = models.CharField(max_length=100, null=True)
logo = models.CharField(max_length=256, null=True)
active = models.BooleanField(unique=True, default=False) active = models.BooleanField(unique=True, default=False)
def __str__(self): def __str__(self):
@ -36,8 +41,13 @@ class Option(models.Model):
return self.text return self.text
def gen_token():
return hashlib.sha1(os.urandom(128)).hexdigest()
class User(models.Model): 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) name = models.CharField(max_length=100, null=False)
def __str__(self): def __str__(self):

View file

@ -41,3 +41,13 @@ p[id^="explanation_"] {
.failed { .failed {
outline: 4px solid #a60000; outline: 4px solid #a60000;
} }
.mainLogo {
width: 300px;
height: auto;
float: right;
}
.message_already_answered {
font-style: italic;
}

View file

@ -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<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
}();
window.onload = init();
function init() {
enable_disable_question();
}
function enable_disable_question() {
var elements = document.querySelectorAll("p[id*='enable-']");
for(var i = 0; i < elements.length; i++) {
var element = elements[i];
var questionId = element.getAttribute("data-questionId");
if(element.getAttribute("data-value") == "False") {
element.className = element.className.replace("hide", "");
//document.getElementById("showSolution_" + questionId).disabled = true;
//get_answer(element.getAttribute("data-questionId"));
} else {
}
}
}
function toggle_solution(id) function toggle_solution(id)
{ {
element = document.getElementById("explanation_" + id); var element = document.getElementById("explanation_" + id);
if (element.className == "show") { if (element.className == "show") {
element.className = "hide"; element.className = "hide";
} else { } else {
@ -10,7 +58,7 @@ function toggle_solution(id)
function show_solution(id) function show_solution(id)
{ {
element = document.getElementById("explanation_" + id); var element = document.getElementById("explanation_" + id);
if (element.className == "hide") { if (element.className == "hide") {
element.className = "show"; element.className = "show";
} }
@ -18,7 +66,7 @@ function show_solution(id)
function get_answer(id) { function get_answer(id) {
showSolutionButton = document.getElementById("showSolution_" + id); var showSolutionButton = document.getElementById("showSolution_" + id);
showSolutionButton.disabled = true; showSolutionButton.disabled = true;
show_solution(id); show_solution(id);
@ -35,11 +83,11 @@ function get_answer(id) {
} }
} }
var questionId=encodeURIComponent(id) var questionId=encodeURIComponent(id);
var token=encodeURIComponent("1ea6de64cf5a11e5ada41c6f6525891e") var token=encodeURIComponent(QueryString.token);
var answers=encodeURIComponent(getCheckboxAnswers(id)) var answers=encodeURIComponent(getCheckboxAnswers(id));
answerRequest.open("GET", "api?id="+questionId+"&token="+token+"&answers="+answers, true) answerRequest.open("GET", "/api?id="+questionId+"&token="+token+"&answers="+answers, true)
answerRequest.send(null) answerRequest.send(null)
} }
@ -91,7 +139,6 @@ function parseResponse(id, responseText) {
} }
} }
function ajaxRequest() { function ajaxRequest() {
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
@ -110,3 +157,4 @@ function ajaxRequest() {
else else
return false return false
} }

View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link href="{% static 'images/favicon.ico' %}" rel="shortcut icon">
<script type="text/javascript" src="{% static 'js/elearning.js' %}"></script>
<title>{{ settings.title }}</title>
</head>
<body>
<div class="container">
<img src="{{ settings.logo }}" class="mainLogo">
<h1>{{ settings.title }}</h1>
<hr/>
<p>{{ settings.message_access_denied }}</p>
</div>
</body>
</html>

View file

@ -7,15 +7,15 @@
{% load staticfiles %} {% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/style.css' %}"> <link rel="stylesheet" href="{% static 'css/style.css' %}">
<link href="{% static 'images/favicon.ico' %}" rel="shortcut icon"> <link href="{% static 'images/favicon.ico' %}" rel="shortcut icon">
<script type="text/javascript" src="{% static 'js/elearning.js' %}"></script>
<title>{{ settings.title }}</title> <title>{{ settings.title }}</title>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<img src="{{ settings.logo }}" class="mainLogo">
<h1>{{ settings.title }}</h1> <h1>{{ settings.title }}</h1>
<p>{{ settings.message }}</p> <p>{{ settings.message_welcome_user }}</p>
<hr/> <hr/>
<div class="margin50" ></div> <div class="margin50" ></div>
@ -23,6 +23,10 @@
<h2>{{ question.id }}: {{ question.title }}</h2> <h2>{{ question.id }}: {{ question.title }}</h2>
<p>{{ question.text }}</p> <p>{{ question.text }}</p>
<p id="enable-{{ question.id }}" data-value="{{ question.enable }}" data-questionId= "{{ question.id }}" class="message_already_answered hide">
{{ settings.message_already_answered }}
</p>
<form action="#"> <form action="#">
{% for option in options %} {% for option in options %}
@ -44,5 +48,6 @@
</div> </div>
<script type="text/javascript" src="{% static 'js/elearning.js' %}"></script>
</body> </body>
</html> </html>

View file

@ -4,20 +4,43 @@ from django.shortcuts import render_to_response
from tkupek_elearning.elearning.models import Setting, Question, Option, UserAnswer, User from tkupek_elearning.elearning.models import Setting, Question, Option, UserAnswer, User
# import pdb import pdb
def home(request): 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) settings = Setting.objects.filter(active=1)
if settings: if settings:
settings = settings[0] settings = settings[0]
if user is not None:
settings.message_welcome_user = settings.message_welcome_user.replace('{username}', user.name)
questions_options = {} questions_options = {}
questions = Question.objects.all() questions = Question.objects.all()
for question in questions: for question in questions:
options = Option.objects.filter(question=question.id) 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 questions_options[question] = options
return render_to_response('index.html', {'settings': settings, 'questions_options': questions_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): def get_answer(request):
@ -30,10 +53,7 @@ def get_answer(request):
question = Question.objects.get(id=request_id) question = Question.objects.get(id=request_id)
user = User.objects.get(token=request_token) user = User.objects.get(token=request_token)
try: user_answer = get_user_answer(question, user)
user_answer = UserAnswer.objects.get(question=question.id, user=user.id)
except ObjectDoesNotExist:
user_answer = None
if user_answer is None: if user_answer is None:
user_answer = UserAnswer() user_answer = UserAnswer()
@ -52,3 +72,12 @@ def get_answer(request):
options_id = options_id[:-1] options_id = options_id[:-1]
return HttpResponse(options_id) 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;