You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

94 lines
3.4 KiB

# coding=utf-8
from __future__ import unicode_literals
from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from courses.models import Achievements, Course, CourseMap, Diploma
from finance.models import Bill
from storage.models import Storage
ACTION_CHOICES = (
(0, 'try'),
(1, 'yes'),
(2, 'no'),
(3, 'favorite'),
(4, 'watch'),
(5, 'like'),
(6, 'dislike'),
(7, 'comment'),
(8, 'start'),
(9, 'end'),
(10, 'create'),
(11, 'update'),
(12, 'delete'),
)
class Journal(models.Model):
thread = models.ForeignKey(to='Thread', verbose_name='Тред')
user = models.ForeignKey(
to=settings.AUTH_USER_MODEL, verbose_name='Инициатор действия или тот, на ком действие инициируется'
)
content_type = models.ForeignKey(to=ContentType)
extra_data = models.TextField(null=True, blank=True)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
action_type = models.SmallIntegerField(choices=ACTION_CHOICES)
date = models.DateTimeField(auto_now=True)
files = models.ManyToManyField(to=Storage, blank=True)
def __str__(self):
return '%d Пользователь %s %s %s' % (self.id, self.user.email, self.get_action_type_display(), self.thread.key)
class Thread(models.Model):
key = models.CharField(max_length=200)
text = models.TextField(default='', verbose_name='Описание треда')
is_staff = models.BooleanField(default=False, verbose_name='Админская ли табличка')
is_recurse = models.BooleanField(default=False, verbose_name='Поле аптимизации поиска')
subscribers = models.ManyToManyField(to=settings.AUTH_USER_MODEL, verbose_name='Подписчики', blank=True)
groups = models.ManyToManyField(to=Group, verbose_name='Группы подписчиков', blank=True)
check_subscribe = models.BooleanField(default=True, verbose_name='Проверять ли подписки')
parent = models.ManyToManyField(to='self', blank=True, symmetrical=False)
x = models.SmallIntegerField(default=300)
y = models.SmallIntegerField(default=300)
def get_journals(self, **filter_extra):
threads = [i for i in self.thread_set.all()].append(self)
return Journal.objects.filter(thread_in=threads, **filter_extra).order_by('-date')
def check_perm(self, user):
return (user in self.subscribers.all()) or bool(sum([int(i.check_perm(user)) for i in self.parent.all()]))
def child_thread_count(self):
if self.is_recurse:
return self.thread_set.count()
return sum([i.child_thread_count() for i in self.thread_set.all()])
def journals_count(self):
children = list(self.get_children())
children.append(self)
return Journal.objects.filter(thread__in=children).count()
def get_children(self):
children = self.thread_set.filter(is_staff=False)
if self.is_recurse:
list(children).append(self)
return children
res = [self]
for child in children:
res += child.get_children()
return res
def __str__(self):
return self.key