Beispiel #1
0
    def get(self, request, category, forum, slug, idtopic, *args, **kwargs):

        template_name = "musette/topic_index.html"
        page_template = "musette/topic.html"

        # Get topic
        forum = get_object_or_404(models.Forum,
                                  category__name=category,
                                  name=forum,
                                  hidden=False)
        topic = get_object_or_404(models.Topic, idtopic=idtopic, slug=slug)

        # Check if the topis is enable
        moderators = forum.moderators.all()
        if not (request.user in moderators):
            if (not topic.moderate):
                raise Http404

        # Form for comments
        form_comment = forms.FormAddComment()

        # Get comments of the topic
        comments = models.Comment.objects.filter(topic_id=idtopic)

        # Get photo of created user topic
        photo = utils.get_photo_profile(topic.user.id)

        # Get suggest topic
        words = topic.title.split(" ")
        condition = Q()
        for word in words:
            condition |= Q(title__icontains=word)

        suggest = models.Topic.objects.filter(condition).exclude(
            idtopic=topic.idtopic)[:10]

        data = {
            'topic': topic,
            'form_comment': form_comment,
            'comments': comments,
            'photo': photo,
            'suggest': suggest
        }

        if request.is_ajax():
            template_name = page_template
        return render(request, template_name, data)
Beispiel #2
0
    def post(self, request, category, forum, slug, idtopic, *args, **kwargs):
        # Form new comment
        form = forms.FormAddComment(request.POST)

        url = reverse_lazy('topic',
                           kwargs={
                               'category': category,
                               'forum': forum,
                               'slug': slug,
                               'idtopic': str(idtopic)
                           })

        if form.is_valid():
            obj = form.save(commit=False)

            # Save new comment
            now = timezone.now()
            User = get_user_model()
            user = User.objects.get(id=request.user.id)
            topic = get_object_or_404(models.Topic, idtopic=idtopic)
            obj.date = now
            obj.user = user
            obj.topic_id = topic.idtopic
            obj.save()

            # Update last activity TopicSearch
            models.Topic.objects.filter(idtopic=idtopic).update(
                last_activity=now)

            # Data for notification real time
            idcomment = obj.idcomment
            comment = models.Comment.objects.get(idcomment=idcomment)
            username = request.user.username

            # Get photo profile
            photo = utils.get_photo_profile(request.user.id)

            # Send notifications comment
            params = utils.get_users_and_send_notification_comment(
                request, topic, comment)
            list_us = params['list_us']
            list_email = params['list_email']

            # Send email
            form.send_mail_comment(str(url), list_email)

            # Data necessary for realtime
            data = realtime.data_base_realtime(comment.topic, photo, forum,
                                               username)
            data['is_topic'] = False
            data['is_comment'] = True

            # Send new notification realtime
            realtime.new_notification(data, list_us)

            # Send new comment in realtime
            realtime.new_comment(data, comment.description)

            messages.success(request, _("Added new comment"))
            return HttpResponseRedirect(url)
        else:
            messages.error(request, _("Field required"))
            return HttpResponseRedirect(url)