2016-01-31 16:17:37 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-02-14 15:02:26 +01:00
|
|
|
import hashlib
|
|
|
|
import os
|
2016-02-09 19:51:05 +01:00
|
|
|
|
2016-01-31 16:17:37 +01:00
|
|
|
from django.db import models
|
2016-03-05 13:14:33 +01:00
|
|
|
from tinymce.models import HTMLField
|
2016-01-31 16:17:37 +01:00
|
|
|
|
2016-01-31 18:19:45 +01:00
|
|
|
|
2016-03-03 22:00:14 +01:00
|
|
|
def gen_token():
|
|
|
|
return hashlib.sha1(os.urandom(128)).hexdigest()
|
|
|
|
|
|
|
|
|
2016-01-31 18:19:45 +01:00
|
|
|
class Setting(models.Model):
|
|
|
|
title = models.CharField(max_length=100, null=True)
|
2016-03-05 15:02:08 +01:00
|
|
|
footer = models.TextField(null=True, blank=True)
|
|
|
|
message_welcome_user = models.TextField(null=True, blank=True)
|
|
|
|
message_access_denied = models.TextField(null=True, blank=True)
|
|
|
|
message_already_answered = models.TextField(null=True, blank=True)
|
|
|
|
text_answer = models.CharField(max_length=100, null=True, blank=True)
|
|
|
|
text_solution = models.CharField(max_length=100, null=True, blank=True)
|
|
|
|
text_next = models.CharField(max_length=100, null=True, blank=True)
|
|
|
|
popup_completed_title = models.CharField(max_length=100, null=True, blank=True)
|
|
|
|
popup_completed_message = models.TextField(null=True, blank=True)
|
|
|
|
popup_leave_message = models.TextField(null=True, blank=True)
|
|
|
|
logo = models.CharField(max_length=256, null=False, blank=True)
|
|
|
|
active = models.BooleanField(default=False)
|
2016-03-05 13:31:42 +01:00
|
|
|
statistic_token = models.CharField(max_length=40, null=True, default=gen_token, unique=True)
|
2016-01-31 18:19:45 +01:00
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
def __unicode__(self):
|
2016-01-31 18:19:45 +01:00
|
|
|
return self.title
|
|
|
|
|
2016-03-05 15:02:08 +01:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.active:
|
|
|
|
try:
|
|
|
|
temp = Setting.objects.get(active=True)
|
|
|
|
if self != temp:
|
|
|
|
temp.active = False
|
|
|
|
temp.save()
|
|
|
|
except Setting.DoesNotExist:
|
|
|
|
pass
|
|
|
|
super(Setting, self).save(*args, **kwargs)
|
|
|
|
|
2016-01-31 18:19:45 +01:00
|
|
|
|
|
|
|
class Question(models.Model):
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
title = models.CharField(max_length=100, null=True)
|
2016-03-05 13:14:33 +01:00
|
|
|
text = HTMLField(null=True)
|
|
|
|
explanation = HTMLField(null=True)
|
2016-01-31 18:19:45 +01:00
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
def __unicode__(self):
|
2016-01-31 18:19:45 +01:00
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
|
|
class Option(models.Model):
|
2016-02-21 22:41:24 +01:00
|
|
|
text = models.CharField(max_length=256, null=True)
|
2016-01-31 18:19:45 +01:00
|
|
|
correct = models.BooleanField(null=False, default=False)
|
|
|
|
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
|
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
def __unicode__(self):
|
2016-02-09 19:51:05 +01:00
|
|
|
return self.text
|
|
|
|
|
|
|
|
|
|
|
|
class User(models.Model):
|
2016-02-14 15:02:26 +01:00
|
|
|
|
2016-03-05 12:22:46 +01:00
|
|
|
token = models.CharField(max_length=40, null=True, default=gen_token, unique=True)
|
2016-02-09 19:51:05 +01:00
|
|
|
name = models.CharField(max_length=100, null=False)
|
2016-03-05 11:40:40 +01:00
|
|
|
last_seen = models.DateTimeField(null=True, blank=True)
|
2016-03-05 15:12:53 +01:00
|
|
|
completed = models.BooleanField(null=False, default=False)
|
2016-02-09 19:51:05 +01:00
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
def __unicode__(self):
|
2016-02-09 19:51:05 +01:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class UserAnswer(models.Model):
|
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
|
2016-02-09 21:46:52 +01:00
|
|
|
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
|
2016-02-17 23:58:07 +01:00
|
|
|
correct = models.NullBooleanField(null=True)
|
2016-02-09 20:46:04 +01:00
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return str(self.user) + " - " + str(self.question.id)
|
2016-02-09 19:51:05 +01:00
|
|
|
|
|
|
|
class Meta:
|
2016-02-14 16:15:13 +01:00
|
|
|
unique_together = (('user', 'question'),)
|
2016-02-14 17:07:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserAnswerOptions(models.Model):
|
|
|
|
user_answer = models.ForeignKey(UserAnswer, on_delete=models.CASCADE, null=False)
|
|
|
|
option = models.ForeignKey(Option, on_delete=models.CASCADE, null=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('user_answer', 'option'),)
|
2016-02-21 22:41:24 +01:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return str(self.user_answer) + " - " + str(self.option)
|
2016-03-19 17:56:32 +01:00
|
|
|
|