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.
100 lines
4.2 KiB
100 lines
4.2 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 django.db import connection
|
|
|
|
from courses.models import Achievements, Course, CourseMap, Diploma
|
|
from finance.models import Bill
|
|
from storage.models import Storage
|
|
|
|
ACTION_CHOICES = (
|
|
('try', 'попытался сдать'),
|
|
('yes', 'одобрил'),
|
|
('no', 'отклонил'),
|
|
('favorite', 'добавил в избранное'),
|
|
('watch', 'просмотрел'),
|
|
('like', 'лайкнул'),
|
|
('dislike', 'дизлайкнул'),
|
|
('comment', 'оставил комментарий'),
|
|
('start', 'начал прохождение'),
|
|
('end', 'закончил прохождение'),
|
|
('create', 'создал'),
|
|
('update', 'обновил'),
|
|
('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.CharField(max_length=31, 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, unique=True, editable=False)
|
|
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 check_status(self):
|
|
# Определяет статус треда если такой есть.
|
|
# Возможно, костыль.
|
|
res = None
|
|
for i in self.journal_set.all():
|
|
if i.action_type in ['try', 'yes', 'no']:
|
|
res = i.action_type
|
|
return res
|
|
|
|
def check_perm(self, user):
|
|
res = user in self.subscribers.all()
|
|
for i in self.groups.all():
|
|
res = res or i in user.groups
|
|
return res or sum([int(i.check_perm(user)) for i in self.parent.all()])
|
|
|
|
def child_thread_count(self):
|
|
# cursor = connection.cursor()
|
|
# if self.is_recurse:
|
|
# cursor.execute("""
|
|
# WITH RECURSIVE temp1 (to_thread_id, from_thread_id) AS (
|
|
# SELECT T1.to_thread_id, T1.from_thread_id
|
|
# FROM journals_thread_parent T1
|
|
# WHERE to_thread_id = %s
|
|
# UNION
|
|
# SELECT T2.to_thread_id, T2.from_thread_id
|
|
# FROM journals_thread_parent T2
|
|
# INNER JOIN temp1 ON(T2.from_thread_id = temp1.to_thread_id)
|
|
# )
|
|
# SELECT COUNT(*) FROM temp1
|
|
# """, [self.id])
|
|
# count = cursor.fetchone()
|
|
# return int(count[0])
|
|
|
|
return self.thread_set.count()
|
|
|
|
def journals_count(self):
|
|
return self.journal_set.count()
|
|
|
|
def __str__(self):
|
|
return self.key
|
|
|