From ce4a2010bb39f8c5013ddade7d5a4fbd7fae0ffa Mon Sep 17 00:00:00 2001 From: Zambom Date: Sun, 2 Apr 2017 22:52:16 -0300 Subject: [PATCH] Refactoring chat app to remove the division in conversations between general, category and subject --- amadeus/static/js/socket.js | 24 ------------------------ categories/migrations/0015_auto_20170402_2207.py | 20 ++++++++++++++++++++ chat/migrations/0002_auto_20170402_2207.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ chat/models.py | 16 ++-------------- chat/templates/chat/_participants.html | 23 ----------------------- chat/templates/chat/_talks_list.html | 23 ----------------------- chat/templates/chat/list.html | 12 ++---------- chat/templates/chat/list_category.html | 110 -------------------------------------------------------------------------------------------------------------- chat/templates/chat/list_participants.html | 12 ++---------- chat/templates/chat/list_subject.html | 111 --------------------------------------------------------------------------------------------------------------- chat/templatetags/chat_tags.py | 10 ++-------- chat/urls.py | 5 ----- chat/views.py | 210 ++++++++++++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- subjects/templatetags/subject_counter.py | 2 +- 14 files changed, 117 insertions(+), 527 deletions(-) create mode 100644 categories/migrations/0015_auto_20170402_2207.py create mode 100644 chat/migrations/0002_auto_20170402_2207.py delete mode 100644 chat/templates/chat/_participants.html delete mode 100644 chat/templates/chat/_talks_list.html delete mode 100644 chat/templates/chat/list_category.html delete mode 100644 chat/templates/chat/list_subject.html diff --git a/amadeus/static/js/socket.js b/amadeus/static/js/socket.js index 3bb5389..5320872 100644 --- a/amadeus/static/js/socket.js +++ b/amadeus/static/js/socket.js @@ -267,30 +267,6 @@ function messageReceived(content) { $(this).show(); }); - $('.chat-tabs').find('li').each(function () { - var identity = $(this).data('chat'); - - if (identity == content.subtype) { - var span = $(this).find('span'), - actual = span.text(); - - actual = parseInt(actual, 10) + 1; - - span.text(actual); - } - }); - - var item = $("#" + content.space); - - if (typeof(item) != "undefined") { - var span = item.parent().find('span.item_badge'), - actual = span.text(); - - actual = parseInt(actual, 10) + 1; - - span.text(actual); - } - if (content.subtype == "subject") { var subject_cbadge = $("#subject_" + content.space).find('.chat_notify'), actual = subject_cbadge.text(); diff --git a/categories/migrations/0015_auto_20170402_2207.py b/categories/migrations/0015_auto_20170402_2207.py new file mode 100644 index 0000000..3490a7b --- /dev/null +++ b/categories/migrations/0015_auto_20170402_2207.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.4 on 2017-04-03 01:07 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('categories', '0014_auto_20170224_0023'), + ] + + operations = [ + migrations.AlterField( + model_name='category', + name='description', + field=models.CharField(blank=True, max_length=300, null=True, verbose_name='description'), + ), + ] diff --git a/chat/migrations/0002_auto_20170402_2207.py b/chat/migrations/0002_auto_20170402_2207.py new file mode 100644 index 0000000..9c47f1e --- /dev/null +++ b/chat/migrations/0002_auto_20170402_2207.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.4 on 2017-04-03 01:07 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('subjects', '0014_auto_20170130_1828'), + ('chat', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='categorytalk', + name='conversation_ptr', + ), + migrations.RemoveField( + model_name='categorytalk', + name='space', + ), + migrations.RemoveField( + model_name='generaltalk', + name='conversation_ptr', + ), + migrations.RemoveField( + model_name='subjecttalk', + name='conversation_ptr', + ), + migrations.RemoveField( + model_name='subjecttalk', + name='space', + ), + migrations.RemoveField( + model_name='conversation', + name='_my_subclass', + ), + migrations.AddField( + model_name='talkmessages', + name='subject', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='message_subject', to='subjects.Subject', verbose_name='Subject'), + ), + migrations.AlterField( + model_name='talkmessages', + name='talk', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='message_talk', to='chat.Conversation', verbose_name='Conversation'), + ), + migrations.AlterField( + model_name='talkmessages', + name='user', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='message_user', to=settings.AUTH_USER_MODEL, verbose_name='User'), + ), + migrations.DeleteModel( + name='CategoryTalk', + ), + migrations.DeleteModel( + name='GeneralTalk', + ), + migrations.DeleteModel( + name='SubjectTalk', + ), + ] diff --git a/chat/models.py b/chat/models.py index 9eca8af..5e92986 100644 --- a/chat/models.py +++ b/chat/models.py @@ -5,11 +5,7 @@ from django.core import validators from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ -from topics.decorators import always_as_child - -from categories.models import Category from subjects.models import Subject -from topics.models import KnowsChild from users.models import User def validate_img_extension(value): @@ -25,24 +21,16 @@ def upload_filename(instance, filename): return os.path.join(path, filename) -class Conversation(KnowsChild): +class Conversation(models.Model): user_one = models.ForeignKey(User, verbose_name = _('User One'), related_name = 'talk_user_start') user_two = models.ForeignKey(User, verbose_name = _('User Two'), related_name = 'talk_user_end') -class GeneralTalk(Conversation): - space = models.IntegerField(_('Space'), default = 0, blank = True) - -class CategoryTalk(Conversation): - space = models.ForeignKey(Category, verbose_name = ('Category'), related_name = 'talk_category', null = True) - -class SubjectTalk(Conversation): - space = models.ForeignKey(Subject, verbose_name = _('Subject'), related_name = 'talk_subject', null = True) - class TalkMessages(models.Model): text = models.TextField(_('Comment'), blank = True) image = models.ImageField(verbose_name = _('Image'), null = True, blank = True, upload_to = upload_filename, validators = [validate_img_extension]) talk = models.ForeignKey(Conversation, verbose_name = _('Conversation'), related_name = 'message_talk', null = True) user = models.ForeignKey(User, verbose_name = _('User'), related_name = 'message_user', null = True) + subject = models.ForeignKey(Subject, verbose_name = _('Subject'), related_name = 'message_subject', null = True) create_date = models.DateTimeField(_('Create Date'), auto_now_add = True) class ChatVisualizations(models.Model): diff --git a/chat/templates/chat/_participants.html b/chat/templates/chat/_participants.html deleted file mode 100644 index 0fbdf61..0000000 --- a/chat/templates/chat/_participants.html +++ /dev/null @@ -1,23 +0,0 @@ -{% load static i18n chat_tags %} - -{% if participants.count > 0 %} -
-

{% trans 'Participants' %}

- -
- {% for participant in participants %} - {% include 'chat/_view_participant.html' with space=space space_type=space_type %} - {% endfor %} -
-
-
-
-
-
-
-{% else %} -
- -

{% trans 'There is no other participants in this space yet.' %}

-
-{% endif %} \ No newline at end of file diff --git a/chat/templates/chat/_talks_list.html b/chat/templates/chat/_talks_list.html deleted file mode 100644 index 7d35147..0000000 --- a/chat/templates/chat/_talks_list.html +++ /dev/null @@ -1,23 +0,0 @@ -{% load static i18n chat_tags %} - -{% if conversations.count > 0 %} -
-

{% trans 'Conversations' %}

- -
- {% for chat in conversations %} - {% include 'chat/_view.html' with space=space space_type=space_type %} - {% endfor %} -
-
-
-
-
-
-
-{% else %} -
- -

{% trans 'You do not posses messages in this space yet.' %}

-
-{% endif %} \ No newline at end of file diff --git a/chat/templates/chat/list.html b/chat/templates/chat/list.html index 76410f2..4f20b06 100644 --- a/chat/templates/chat/list.html +++ b/chat/templates/chat/list.html @@ -6,21 +6,13 @@ {% block breadcrumbs %} {{ block.super }} - {% trans 'Messages General' as general %} + {% trans 'Messages' as general %} {% breadcrumb general 'chat:manage_general' %} {% endblock %} {% block content %} -
- -
- -
+
diff --git a/chat/templates/chat/list_category.html b/chat/templates/chat/list_category.html deleted file mode 100644 index 6ea61b2..0000000 --- a/chat/templates/chat/list_category.html +++ /dev/null @@ -1,110 +0,0 @@ -{% extends 'base.html' %} - -{% load static i18n pagination permissions_tags chat_tags %} -{% load django_bootstrap_breadcrumbs %} - -{% block breadcrumbs %} - {{ block.super }} - - {% trans 'Messages per Category' as category %} - - {% breadcrumb category 'chat:manage_category' %} -{% endblock %} - -{% block content %} - - - - -
- {% if categories.count > 0 %} -
- {% for category in categories %} - {% category_permissions request.user category as has_category_permissions %} - - {% if category.visible or has_category_permissions %} - {% if category.visible %} -
-
- {% elif has_category_permissions %} -
-
- {% endif %} - -
-
-

- - {{ category.name }} ({{ category|notifies_category:request.user }}) - - -

- -
- {% if has_category_permissions %} - - - {% endif %} -
-
-
-
-
-
-
-
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- {% endif %} - {% endfor %} - - {% pagination request paginator page_obj %} -
- {% endif %} -
- - - - - - - - - -{% endblock %} \ No newline at end of file diff --git a/chat/templates/chat/list_participants.html b/chat/templates/chat/list_participants.html index eaa8483..52c240f 100644 --- a/chat/templates/chat/list_participants.html +++ b/chat/templates/chat/list_participants.html @@ -6,7 +6,7 @@ {% block breadcrumbs %} {{ block.super }} - {% trans 'Messages General' as general %} + {% trans 'Messages' as general %} {% trans 'Participants' as participants_bread %} {% breadcrumb general 'chat:manage_general' %} @@ -14,15 +14,7 @@ {% endblock %} {% block content %} - - -
+
diff --git a/chat/templates/chat/list_subject.html b/chat/templates/chat/list_subject.html deleted file mode 100644 index 72813ac..0000000 --- a/chat/templates/chat/list_subject.html +++ /dev/null @@ -1,111 +0,0 @@ -{% extends 'base.html' %} - -{% load static i18n pagination permissions_tags chat_tags %} -{% load django_bootstrap_breadcrumbs %} - -{% block breadcrumbs %} - {{ block.super }} - - {% trans 'Messages per Subject' as subject %} - - {% breadcrumb subject 'chat:manage_category' %} -{% endblock %} - -{% block content %} - - - - -
- {% if subjects.count > 0 %} -
- {% for subject in subjects %} - {% subject_permissions request.user subject as has_subject_permissions %} - - {% if subject.visible or has_subject_permissions %} - {% if subject.visible %} -
-
- {% elif has_subject_permissions %} -
-
- {% endif %} -
-
-

- - {{ subject }} - - ({{ subject|notifies_subject:request.user }}) - -

- -
- {% if has_subject_permissions %} - - - {% endif %} -
-
-
-
-
-
-
-
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- {% endif %} - {% endfor %} - - {% pagination request paginator page_obj %} -
- {% endif %} -
- - - - - - - - - - -{% endblock %} \ No newline at end of file diff --git a/chat/templatetags/chat_tags.py b/chat/templatetags/chat_tags.py index 5177a18..3baa4e8 100644 --- a/chat/templatetags/chat_tags.py +++ b/chat/templatetags/chat_tags.py @@ -1,7 +1,7 @@ from django import template from django.conf import settings from django.utils import timezone -from django.db.models import Count, F +from django.db.models import Count, F, Q from django.utils.translation import ugettext_lazy as _ from django.contrib.sessions.models import Session @@ -78,14 +78,8 @@ def fav_class(message, user): return "btn_fav" -@register.filter(name = 'notifies_category') -def notifies_category(category, user): - total = ChatVisualizations.objects.filter(message__talk__categorytalk__space = category, user = user, viewed = False).count() - - return total - @register.filter(name = 'notifies_subject') def notifies_subject(subject, user): - total = ChatVisualizations.objects.filter(message__talk__subjecttalk__space = subject, user = user, viewed = False).count() + total = ChatVisualizations.objects.filter(Q(message__subject = subject, user = user, viewed = False) & (Q(user__is_staff = True) | Q(message__subject__students = user) | Q(message__subject__professor = user) | Q(message__subject__category__coordinators = user))).distinct().count() return total \ No newline at end of file diff --git a/chat/urls.py b/chat/urls.py index 459d896..0b86392 100644 --- a/chat/urls.py +++ b/chat/urls.py @@ -3,13 +3,8 @@ from . import views urlpatterns = [ url(r'^$', views.GeneralIndex.as_view(), name='manage_general'), - url(r'^categories/$', views.CategoryIndex.as_view(), name='manage_category'), - url(r'^subjects/$', views.SubjectIndex.as_view(), name='manage_subject'), url(r'^subject/(?P[\w_-]+)/$', views.SubjectView.as_view(), name='subject_view'), url(r'^participants/$', views.GeneralParticipants.as_view(), name='participants_general'), - url(r'^category/talks/(?P[\w_-]+)/$', views.CategoryTalks.as_view(), name='category_talks'), - url(r'^category/participants/(?P[\w_-]+)/$', views.CategoryParticipants.as_view(), name='participants_category'), - url(r'^subject/talks/(?P[\w_-]+)/$', views.SubjectTalks.as_view(), name='subject_talks'), url(r'^subject/participants/(?P[\w_-]+)/$', views.SubjectParticipants.as_view(), name='participants_subject'), url(r'^render_message/([\w_-]+)/([\w_-]+)/([\w_-]+)/([\w_-]+)/([\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', views.render_message, name='render_message'), url(r'^favorite/([\w_-]+)/$', views.favorite, name='favorite'), diff --git a/chat/views.py b/chat/views.py index c51ff2f..eb56796 100644 --- a/chat/views.py +++ b/chat/views.py @@ -24,7 +24,7 @@ from categories.models import Category from subjects.models import Subject from users.models import User -from .models import Conversation, GeneralTalk, CategoryTalk, SubjectTalk, TalkMessages, ChatVisualizations, ChatFavorites +from .models import Conversation, TalkMessages, ChatVisualizations, ChatFavorites from .forms import ChatMessageForm class GeneralIndex(LoginRequiredMixin, generic.ListView): @@ -40,12 +40,8 @@ class GeneralIndex(LoginRequiredMixin, generic.ListView): def get_queryset(self): user = self.request.user - conversations = Conversation.objects.filter((Q(user_one = user) | Q(user_two = user)) & Q(categorytalk__isnull = True) & Q(subjecttalk__isnull = True)) - - self.totals['general'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__generaltalk__isnull = False).count() - self.totals['category'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__categorytalk__isnull = False).count() - self.totals['subject'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__subjecttalk__isnull = False).count() - + conversations = Conversation.objects.filter((Q(user_one = user) | Q(user_two = user))) + return conversations def get_context_data(self, **kwargs): @@ -72,11 +68,7 @@ class GeneralParticipants(LoginRequiredMixin, generic.ListView): search = self.request.GET.get('search', '') users = User.objects.filter(Q(username__icontains = search) | Q(last_name__icontains = search) | Q(social_name__icontains = search) | Q(email__icontains = search)).distinct().order_by('social_name','username').exclude(email = user.email) - - self.totals['general'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__generaltalk__isnull = False).count() - self.totals['category'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__categorytalk__isnull = False).count() - self.totals['subject'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__subjecttalk__isnull = False).count() - + return users def get_context_data(self, **kwargs): @@ -89,151 +81,13 @@ class GeneralParticipants(LoginRequiredMixin, generic.ListView): return context -class CategoryIndex(LoginRequiredMixin, generic.ListView): - login_url = reverse_lazy("users:login") - redirect_field_name = 'next' - - template_name = 'chat/list_category.html' - context_object_name = "categories" - paginate_by = 10 - - totals = {} - - def get_queryset(self): - user = self.request.user - page = self.request.GET.get('page', False) - - if user.is_staff: - categories = Category.objects.all() - else: - categories = Category.objects.filter(Q(coordinators__pk = user.pk) | Q(subject_category__professor__pk = user.pk) | Q(subject_category__students__pk = user.pk, visible = True)).distinct() - - self.totals['general'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__generaltalk__isnull = False).count() - self.totals['category'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__categorytalk__isnull = False).count() - self.totals['subject'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__subjecttalk__isnull = False).count() - - return categories - - def get_context_data(self, **kwargs): - context = super(CategoryIndex, self).get_context_data(**kwargs) - - context['title'] = _('Messages per Category') - context['totals'] = self.totals - context['chat_menu_active'] = 'subjects_menu_active' - - return context - -class CategoryTalks(LoginRequiredMixin, generic.ListView): - login_url = reverse_lazy("users:login") - redirect_field_name = 'next' - - template_name = 'chat/_talks_list.html' - context_object_name = "conversations" - - def get_queryset(self): - user = self.request.user - cat = self.kwargs.get('category', 0) - - conversations = CategoryTalk.objects.filter((Q(user_one = user) | Q(user_two = user)) & Q(space__id = cat)) - - return conversations - - def get_context_data(self, **kwargs): - context = super(CategoryTalks, self).get_context_data(**kwargs) - - context['space'] = self.kwargs.get('category', 0) - context['space_type'] = 'category' - - return context - -class CategoryParticipants(LoginRequiredMixin, generic.ListView): - login_url = reverse_lazy("users:login") - redirect_field_name = 'next' - - template_name = 'chat/_participants.html' - context_object_name = "participants" - - def get_queryset(self): - user = self.request.user - cat = self.kwargs.get('category', 0) - search = self.request.GET.get('search', '') - - users = User.objects.filter((Q(username__icontains = search) | Q(last_name__icontains = search) | Q(social_name__icontains = search) | Q(email__icontains = search)) & (Q(is_staff = True) | Q(subject_student__category__id = cat) | Q(professors__category__id = cat) | Q(coordinators__id = cat))).distinct().order_by('social_name','username').exclude(email = user.email) - - return users - - def get_context_data(self, **kwargs): - context = super(CategoryParticipants, self).get_context_data(**kwargs) - - context['space'] = self.kwargs.get('category', 0) - context['space_type'] = 'category' - - return context - -class SubjectIndex(LoginRequiredMixin, generic.ListView): - login_url = reverse_lazy("users:login") - redirect_field_name = 'next' - - template_name = 'chat/list_subject.html' - context_object_name = "subjects" - paginate_by = 10 - - totals = {} - - def get_queryset(self): - user = self.request.user - page = self.request.GET.get('page', False) - - if user.is_staff: - subjects = Subject.objects.all() - else: - subjects = Subject.objects.filter(Q(professor__pk = user.pk) | Q(students__pk = user.pk, visible = True) | Q(category__coordinators__pk = user.pk)).distinct() - - self.totals['general'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__generaltalk__isnull = False).count() - self.totals['category'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__categorytalk__isnull = False).count() - self.totals['subject'] = ChatVisualizations.objects.filter(user = user, viewed = False, message__talk__subjecttalk__isnull = False).count() - - return subjects - - def get_context_data(self, **kwargs): - context = super(SubjectIndex, self).get_context_data(**kwargs) - - context['title'] = _('Messages per Subject') - context['totals'] = self.totals - context['chat_menu_active'] = 'subjects_menu_active' - - return context - -class SubjectTalks(LoginRequiredMixin, generic.ListView): - login_url = reverse_lazy("users:login") - redirect_field_name = 'next' - - template_name = 'chat/_talks_list.html' - context_object_name = "conversations" - - def get_queryset(self): - user = self.request.user - sub = self.kwargs.get('subject', 0) - - conversations = SubjectTalk.objects.filter((Q(user_one = user) | Q(user_two = user)) & Q(space__id = sub)) - - return conversations - - def get_context_data(self, **kwargs): - context = super(SubjectTalks, self).get_context_data(**kwargs) - - context['space'] = self.kwargs.get('subject', 0) - context['space_type'] = 'subject' - - return context - class SubjectParticipants(LoginRequiredMixin, generic.ListView): login_url = reverse_lazy("users:login") redirect_field_name = 'next' - template_name = 'chat/_participants.html' + template_name = 'chat/subject_view_participants.html' context_object_name = "participants" - paginate_by = None + paginate_by = 10 def dispatch(self, request, *args,**kwargs): typep = self.request.GET.get('type', '') @@ -253,27 +107,18 @@ class SubjectParticipants(LoginRequiredMixin, generic.ListView): users = User.objects.filter((Q(username__icontains = search) | Q(last_name__icontains = search) | Q(social_name__icontains = search) | Q(email__icontains = search)) & (Q(is_staff = True) | Q(subject_student__id = sub) | Q(professors__id = sub) | Q(coordinators__subject_category__id = sub))).distinct().order_by('social_name','username').exclude(email = user.email) - typep = self.request.GET.get('type', '') - - if not typep == 'modal': - self.paginate_by = 10 - return users def get_context_data(self, **kwargs): context = super(SubjectParticipants, self).get_context_data(**kwargs) - typep = self.request.GET.get('type', '') - - if not typep == 'modal': - sub = self.kwargs.get('subject', 0) - subject = get_object_or_404(Subject, id = sub) - - context['subject'] = subject - context['search'] = self.request.GET.get('search', '') - context['title'] = _('%s - Participants')%(str(subject)) - self.template_name = 'chat/subject_view_participants.html' + sub = self.kwargs.get('subject', 0) + subject = get_object_or_404(Subject, id = sub) + context['subject'] = subject + context['search'] = self.request.GET.get('search', '') + context['title'] = _('%s - Participants')%(str(subject)) + context['space'] = self.kwargs.get('subject', 0) context['space_type'] = 'subject' @@ -300,7 +145,10 @@ class SubjectView(LoginRequiredMixin, generic.ListView): slug = self.kwargs.get('slug') subject = get_object_or_404(Subject, slug = slug) - conversations = SubjectTalk.objects.filter((Q(user_one = user) | Q(user_two = user)) & Q(space = subject)) + conversations = Conversation.objects.filter((Q(user_one = user) & (Q(user_two__is_staff = True) | + Q(user_two__subject_student = subject) | Q(user_two__professors = subject) | Q(user_two__coordinators__subject_category = subject))) | + (Q(user_two = user) & (Q(user_one__is_staff = True) | Q(user_one__subject_student = subject) | + Q(user_one__professors = subject) | Q(user_one__coordinators__subject_category = subject)))).distinct() return conversations @@ -399,37 +247,23 @@ class SendMessage(LoginRequiredMixin, generic.edit.CreateView): space = self.kwargs.get('space', 0) if talk_id == "-1": - if space_type == 'general': - talk = GeneralTalk.objects.create(user_one = self.request.user, user_two = user, space = 0) - elif space_type == 'category': - cat = get_object_or_404(Category, id = space) - talk = CategoryTalk.objects.create(user_one = self.request.user, user_two = user, space = cat) - else: - sub = get_object_or_404(Subject, id = space) - talk = SubjectTalk.objects.create(user_one = self.request.user, user_two = user, space = sub) + talk = Conversation.objects.create(user_one = self.request.user, user_two = user) else: talk = get_object_or_404(Conversation, id = talk_id) self.object.talk = talk + if space_type == 'subject': + self.object.subject = get_object_or_404(Subject, id = space) + space = self.object.subject.slug + self.object.save() simple_notify = textwrap.shorten(strip_tags(self.object.text), width = 30, placeholder = "...") if self.object.image: simple_notify += " ".join(_("[Photo]")) - - subclass = self.object.talk._my_subclass - - if subclass == "generaltalk": - space_type = "general" - elif subclass == "categorytalk": - space_type = "category" - space = self.object.talk.categorytalk.space.slug - else: - space_type = "subject" - space = self.object.talk.subjecttalk.space.slug - + notification = { "type": "chat", "subtype": space_type, diff --git a/subjects/templatetags/subject_counter.py b/subjects/templatetags/subject_counter.py index 9a4f90f..4459710 100644 --- a/subjects/templatetags/subject_counter.py +++ b/subjects/templatetags/subject_counter.py @@ -43,7 +43,7 @@ def mural_number(subject, user): def chat_number(subject, user): context = {} - context['number'] = ChatVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & Q(message__talk__subjecttalk__space = subject)).count() + context['number'] = ChatVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & Q(message__subject = subject) & (Q(user__is_staff = True) | Q(message__subject__students = user) | Q(message__subject__professor = user) | Q(message__subject__category__coordinators = user))).distinct().count() context['custom_class'] = 'chat_notify' return context -- libgit2 0.21.2