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
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):

View file

@ -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;
}

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)
{
element = document.getElementById("explanation_" + id);
var element = document.getElementById("explanation_" + id);
if (element.className == "show") {
element.className = "hide";
} else {
@ -10,7 +58,7 @@ function toggle_solution(id)
function show_solution(id)
{
element = document.getElementById("explanation_" + id);
var element = document.getElementById("explanation_" + id);
if (element.className == "hide") {
element.className = "show";
}
@ -18,7 +66,7 @@ function show_solution(id)
function get_answer(id) {
showSolutionButton = document.getElementById("showSolution_" + id);
var showSolutionButton = document.getElementById("showSolution_" + id);
showSolutionButton.disabled = true;
show_solution(id);
@ -35,11 +83,11 @@ function get_answer(id) {
}
}
var questionId=encodeURIComponent(id)
var token=encodeURIComponent("1ea6de64cf5a11e5ada41c6f6525891e")
var answers=encodeURIComponent(getCheckboxAnswers(id))
var questionId=encodeURIComponent(id);
var token=encodeURIComponent(QueryString.token);
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)
}
@ -91,7 +139,6 @@ function parseResponse(id, responseText) {
}
}
function ajaxRequest() {
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
@ -109,4 +156,5 @@ function ajaxRequest() {
return new XMLHttpRequest()
else
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 %}
<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>
<p>{{ settings.message }}</p>
<p>{{ settings.message_welcome_user }}</p>
<hr/>
<div class="margin50" ></div>
@ -23,6 +23,10 @@
<h2>{{ question.id }}: {{ question.title }}</h2>
<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="#">
{% for option in options %}
@ -44,5 +48,6 @@
</div>
<script type="text/javascript" src="{% static 'js/elearning.js' %}"></script>
</body>
</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
# 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;