added first public views - no check for access rights at the moment
This commit is contained in:
@@ -10,18 +10,14 @@ from django.forms.models import model_to_dict
|
||||
from django.utils.safestring import mark_safe
|
||||
from filer.fields.file import FilerFileField
|
||||
|
||||
from lib import get_current_user
|
||||
from lib.core.db.models.base import SharedPermissionBase
|
||||
from lib.core.db.models.mixins import DateAware, AuthorAware, DescriptionAware, NameAware
|
||||
|
||||
|
||||
class MediaFile(models.Model):
|
||||
name = models.CharField(max_length=25, unique=True, db_index=True)
|
||||
class MediaFile(NameAware, DateAware, AuthorAware, DescriptionAware):
|
||||
# https://django-filer.readthedocs.io/en/latest/extending_filer.html
|
||||
# https://pypi.org/project/django-thumbnails/
|
||||
file = FilerFileField(on_delete=models.RESTRICT, related_name='media_file')
|
||||
description = models.TextField(null=True, blank=True)
|
||||
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, default=get_current_user)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
@@ -30,13 +26,8 @@ class MediaFile(models.Model):
|
||||
return self.file.name
|
||||
|
||||
|
||||
class Link(models.Model):
|
||||
name = models.CharField(max_length=25, null=True, blank=True, db_index=True)
|
||||
class Link(NameAware, DateAware, AuthorAware, DescriptionAware):
|
||||
url = models.URLField(unique=True, db_index=True)
|
||||
description = models.TextField(null=True, blank=True)
|
||||
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, default=get_current_user)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.url
|
||||
@@ -45,14 +36,9 @@ class Link(models.Model):
|
||||
return self.url
|
||||
|
||||
|
||||
class Level(models.Model):
|
||||
name = models.CharField(max_length=25, unique=True, db_index=True)
|
||||
class Level(NameAware, DateAware, AuthorAware, DescriptionAware):
|
||||
value = models.IntegerField(unique=True, db_index=True)
|
||||
description = models.TextField(null=True, blank=True)
|
||||
color = ColorField(default='#F90F90')
|
||||
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, default=get_current_user)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.value} - {self.name}'
|
||||
@@ -61,13 +47,8 @@ class Level(models.Model):
|
||||
return f'{self.value}'
|
||||
|
||||
|
||||
class Label(models.Model):
|
||||
name = models.CharField(max_length=25, unique=True, db_index=True)
|
||||
description = models.TextField(null=True, blank=True)
|
||||
class Label(NameAware, DateAware, AuthorAware, DescriptionAware):
|
||||
color = ColorField(default='#666666')
|
||||
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, default=get_current_user)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
@@ -76,19 +57,21 @@ class Label(models.Model):
|
||||
return self.name
|
||||
|
||||
|
||||
class Question(models.Model):
|
||||
class SharedQuestion(SharedPermissionBase):
|
||||
class Meta:
|
||||
abstract = False
|
||||
|
||||
|
||||
class Question(DateAware, AuthorAware, DescriptionAware):
|
||||
name = models.CharField(max_length=500, unique=True, db_index=True)
|
||||
question = models.TextField(db_index=True)
|
||||
buzzword = models.CharField(max_length=25, null=True, blank=True)
|
||||
awnser = models.TextField(db_index=True)
|
||||
level = models.ForeignKey(Level, on_delete=models.SET_NULL, null=True)
|
||||
description = models.TextField(null=True, blank=True)
|
||||
labels = models.ManyToManyField(Label, blank=True)
|
||||
medias = models.ManyToManyField(MediaFile, blank=True)
|
||||
links = models.ManyToManyField(Link, blank=True)
|
||||
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
shares = models.ManyToManyField(SharedQuestion, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
@@ -96,23 +79,38 @@ class Question(models.Model):
|
||||
def __repr__(self):
|
||||
return self.name
|
||||
|
||||
def to_view(self, for_players: bool = False):
|
||||
ret = {}
|
||||
ret['name'] = '{}{}'.format(self.name, f'({self.buzzword})' if self.buzzword else '')
|
||||
ret['question'] = self.question
|
||||
if self.description:
|
||||
ret['question'] += ' \n\n------------------------------\n\n' + self.description
|
||||
ret['awnser'] = self.awnser
|
||||
ret['level'] = self.level.value
|
||||
ret['medias'] = []
|
||||
ret['links'] = []
|
||||
for media in self.medias.all():
|
||||
ret.setdefault('medias', []).append(os.path.basename(media.file.path))
|
||||
for link in self.links.all():
|
||||
ret.setdefault('links', []).append(link.url)
|
||||
ret['question'] = mark_safe(ret['question'].replace('\n', '\n<br />'))
|
||||
logging.error(ret)
|
||||
return ret
|
||||
@staticmethod
|
||||
def searchdomain(term, from_quiz=False):
|
||||
return \
|
||||
models.Q(name__icontains=term) \
|
||||
| models.Q(question__icontains=term) \
|
||||
| models.Q(awnser__icontains=term) \
|
||||
| models.Q(buzzword__icontains=term) \
|
||||
| models.Q(labels__name__icontains=term)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_by_tearchterm(term):
|
||||
return Question.objects.filter(Question.searchdomain(term)).annotate(cnt=models.Count('id'))
|
||||
|
||||
|
||||
def to_view(self, for_players: bool = False):
|
||||
ret = {}
|
||||
ret['name'] = '{}{}'.format(self.name, f'({self.buzzword})' if self.buzzword else '')
|
||||
ret['question'] = self.question
|
||||
if self.description:
|
||||
ret['question'] += ' \n\n------------------------------\n\n' + self.description
|
||||
ret['awnser'] = self.awnser
|
||||
ret['level'] = self.level.value
|
||||
ret['medias'] = []
|
||||
ret['links'] = []
|
||||
for media in self.medias.all():
|
||||
ret.setdefault('medias', []).append(os.path.basename(media.file.path))
|
||||
for link in self.links.all():
|
||||
ret.setdefault('links', []).append(link.url)
|
||||
ret['question'] = mark_safe(ret['question'].replace('\n', '\n<br />'))
|
||||
logging.error(ret)
|
||||
return ret
|
||||
|
||||
|
||||
def default_json(o):
|
||||
@@ -121,12 +119,9 @@ def default_json(o):
|
||||
return json.dumps(model_to_dict(o))
|
||||
|
||||
|
||||
class QuestionVersion(models.Model):
|
||||
class QuestionVersion(DateAware, AuthorAware):
|
||||
question = models.ForeignKey(Question, on_delete=models.SET_NULL, null=True)
|
||||
data = models.JSONField()
|
||||
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.question} - {self.created_at}' if self.question else f'{self.data.name} - {self.created_at}'
|
||||
@@ -140,6 +135,7 @@ def versionize_question(sender, instance: Question, *args, **kwargs):
|
||||
data['medias'] = [{'id': m.id, '__str__': str(m)} for m in data['medias']]
|
||||
data['links'] = [{'id': m.id, '__str__': str(m)} for m in data['links']]
|
||||
data['labels'] = [{'id': m.id, '__str__': str(m)} for m in data['labels']]
|
||||
data['shares'] = list(map(str, data['shares']))
|
||||
|
||||
QuestionVersion.objects.create(
|
||||
question=instance,
|
||||
|
||||
Reference in New Issue
Block a user