Exemple #1
0
    def post(self, request, category, forum, *args, **kwargs):
        # Form new topic
        form = forms.FormAddTopic(request.POST, request.FILES)

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

            now = timezone.now()
            User = get_user_model()
            user = User.objects.get(id=request.user.id)
            # Get forum
            forum = get_object_or_404(models.Forum,
                                      category__name=category,
                                      name=forum)
            title = conditional_escape(request.POST['title'])

            obj.date = now
            obj.user = user
            obj.forum = forum
            obj.title = title
            obj.slug = defaultfilters.slugify(request.POST['title'])

            # If has attachment
            if 'attachment' in request.FILES:
                id_attachment = get_random_string(length=32)
                obj.id_attachment = id_attachment

                file_name = request.FILES['attachment']
                obj.attachment = file_name

            # If the forum is moderate send email
            obj = utils.check_moderate_topic_email(request, forum, obj)
            # Save topic
            obj.save()

            # Get moderators forum and send notification
            list_us = utils.get_moderators_and_send_notification_topic(
                request, forum, obj)

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

            # Data necessary for realtime
            data = realtime.data_base_realtime(obj, photo, forum.name,
                                               username)

            # Add to real time new notification
            realtime.new_notification(data, list_us)

            # Message for user. Form success
            messages.success(
                request,
                _("The topic '%(topic)s' was successfully created") %
                {'topic': obj.title})
            return self.form_valid(form, **kwargs)
        else:
            messages.error(request, _("Invalid form"))
            return self.form_invalid(form, **kwargs)
Exemple #2
0
    def perform_create(self, serializer):
        request = self.request
        is_my_user = int(request.data['user']) == request.user.id
        # If is my user or is superuser can create
        if is_my_user or request.user.is_superuser:
            # Save the record comment
            if serializer.is_valid():
                comment = serializer.save()
            else:
                return Response(
                    serializer.errors,
                    status=status.HTTP_400_BAD_REQUEST
                )

            topic_id = request.data['topic']
            topic = get_object_or_404(models.Topic, pk=topic_id)

            # Parameters for notification comments
            photo = utils.get_photo_profile(request.user.id)
            username = request.user.username
            forum = topic.forum.name

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

            # Get url for email
            url = reverse_lazy('topic', kwargs={
                'category': topic.forum.category, 'forum': forum,
                'slug': topic.slug, 'idtopic': str(topic.idtopic)
            })

            # Send e    mail
            utils.send_mail_comment(str(url), list_email)

            # Data necessary for realtime
            data = realtime.data_base_realtime(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
            comment_description = request.data['description']
            realtime.new_comment(data, comment_description)

            return Response(
                serializer.data, status=status.HTTP_201_CREATED
            )
        else:
            raise PermissionDenied({
                    "message": "Not your user"
                })
Exemple #3
0
    def perform_create(self, serializer):
        request = self.request
        is_my_user = int(request.data['user']) == request.user.id

        # If is my user or is superuser can create
        if is_my_user or request.user.is_superuser:
            forum_id = request.data['forum']
            forum = get_object_or_404(models.Forum, pk=forum_id)
            category = forum.category.name
            # If has permissions
            if utils.user_can_create_topic(category, forum, request.user):
                # Save the record topic
                if serializer.is_valid():
                    # If the forum is moderate send email
                    serializer = utils.check_moderate_topic_email(
                        request, forum, serializer
                    )
                    # Save record
                    topic = serializer.save()
                else:
                    return Response(
                        serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST
                    )

                # Parameters for realtime
                photo = utils.get_photo_profile(request.user.id)
                username = request.user.username
                forum_name = forum.name

                # Get moderators forum and send notification
                list_us = utils.get_moderators_and_send_notification_topic(
                    request, forum, topic
                )

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

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

                return Response(
                    serializer.data, status=status.HTTP_201_CREATED
                )
            else:
                raise PermissionDenied({
                    "message": "You don't have permission to access"
                })
        else:
            raise PermissionDenied({
                    "message": "Not your user"
                })
    def perform_create(self, serializer):
        request = self.request
        is_my_user = int(request.data['user']) == request.user.id
        # If is my user or is superuser can create
        if is_my_user or request.user.is_superuser:
            # Save the record comment
            if serializer.is_valid():
                comment = serializer.save()
            else:
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)

            topic_id = request.data['topic']
            topic = get_object_or_404(models.Topic, pk=topic_id)

            # Parameters for notification comments
            photo = utils.get_photo_profile(request.user.id)
            username = request.user.username
            forum = topic.forum.name

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

            # Get url for email
            url = reverse_lazy('topic',
                               kwargs={
                                   'category': topic.forum.category,
                                   'forum': forum,
                                   'slug': topic.slug,
                                   'idtopic': str(topic.idtopic)
                               })

            # Send e    mail
            utils.send_mail_comment(str(url), list_email)

            # Data necessary for realtime
            data = realtime.data_base_realtime(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
            comment_description = request.data['description']
            realtime.new_comment(data, comment_description)

            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            raise PermissionDenied({"message": "Not your user"})
    def perform_create(self, serializer):
        request = self.request
        is_my_user = int(request.data['user']) == request.user.id

        # If is my user or is superuser can create
        if is_my_user or request.user.is_superuser:
            forum_id = request.data['forum']
            forum = get_object_or_404(models.Forum, pk=forum_id)
            category = forum.category.name
            # If has permissions
            if utils.user_can_create_topic(category, forum, request.user):
                # Save the record topic
                if serializer.is_valid():
                    # If the forum is moderate send email
                    serializer = utils.check_moderate_topic_email(
                        request, forum, serializer)
                    # Save record
                    topic = serializer.save()
                else:
                    return Response(serializer.errors,
                                    status=status.HTTP_400_BAD_REQUEST)

                # Parameters for realtime
                photo = utils.get_photo_profile(request.user.id)
                username = request.user.username
                forum_name = forum.name

                # Get moderators forum and send notification
                list_us = utils.get_moderators_and_send_notification_topic(
                    request, forum, topic)

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

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

                return Response(serializer.data,
                                status=status.HTTP_201_CREATED)
            else:
                raise PermissionDenied(
                    {"message": "You don't have permission to access"})
        else:
            raise PermissionDenied({"message": "Not your user"})
Exemple #6
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)
Exemple #7
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)
Exemple #8
0
    def post(self, request, category, forum, *args, **kwargs):
        # Form new topic
        form = forms.FormAddTopic(request.POST, request.FILES)

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

            now = timezone.now()
            User = get_user_model()
            user = User.objects.get(id=request.user.id)
            # Get forum
            forum = get_object_or_404(
                models.Forum, category__name=category, name=forum
            )
            title = conditional_escape(request.POST['title'])

            obj.date = now
            obj.user = user
            obj.forum = forum
            obj.title = title
            obj.slug = defaultfilters.slugify(request.POST['title'])
            obj.moderate = utils.check_topic_moderate(user, forum)

            # If has attachment
            if 'attachment' in request.FILES:
                id_attachment = get_random_string(length=32)
                obj.id_attachment = id_attachment

                file_name = request.FILES['attachment']
                obj.attachment = file_name

            # If the forum is moderate send email
            obj = utils.check_moderate_topic_email(request, forum, obj)
            # Save topic
            obj.save()

            # Get moderators forum and send notification
            list_us = utils.get_moderators_and_send_notification_topic(
                request, forum, obj
            )

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

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

            # Add to real time new notification
            realtime.new_notification(data, list_us)

            # Message for user. Form success
            messages.success(
                request, _("The topic '%(topic)s' was successfully created")
                % {'topic': obj.title}
            )
            return self.form_valid(form, **kwargs)
        else:
            messages.error(request, _("Invalid form"))
            return self.form_invalid(form, **kwargs)