Exemplo n.º 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)
Exemplo n.º 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"
                })
Exemplo n.º 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"
                })
Exemplo n.º 4
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"})
Exemplo n.º 5
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)
Exemplo n.º 6
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"})
Exemplo n.º 7
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)
Exemplo n.º 8
0
    def get(self, request, username, *args, **kwargs):
        template_name = "musette/profile.html"

        # Get user param
        User = get_user_model()
        user = get_object_or_404(User, username=username)
        iduser = user.id

        # Get model extend Profile
        ModelProfile = utils.get_main_model_profile()

        # Get name app of the extend model Profile
        app = utils.get_app_model(ModelProfile)

        # Check if the model profile is extended
        count_fields_model = utils.get_count_fields_model(ModelProfile)
        count_fields_abstract = utils.get_count_fields_model(
            models.AbstractProfile)
        if count_fields_model > count_fields_abstract:
            model_profile_is_extend = True
        else:
            model_profile_is_extend = False
        profile = get_object_or_404(ModelProfile, iduser=iduser)

        photo = utils.get_photo_profile(iduser)

        # Get last topic of the profile
        topics = models.Topic.objects.filter(user=user)[:5]

        data = {
            'profile': profile,
            'photo': photo,
            'user': request.user,
            'topics': topics,
            'app': app,
            'model_profile_is_extend': model_profile_is_extend
        }

        return render(request, template_name, data)
Exemplo n.º 9
0
    def get(self, request, username, *args, **kwargs):
        template_name = "musette/profile.html"

        # Get user param
        User = get_user_model()
        user = get_object_or_404(User, username=username)
        iduser = user.id

        profile = get_object_or_404(models.Profile, iduser=iduser)

        photo = utils.get_photo_profile(iduser)

        # Get last topic of the profile
        topics = models.Topic.objects.filter(user=user)[:5]

        data = {
            'profile': profile,
            'photo': photo,
            'user': request.user,
            'topics': topics
        }

        return render(request, template_name, data)
Exemplo n.º 10
0
    def get(self, request, username, *args, **kwargs):
        template_name = "musette/profile.html"

        # Get user param
        User = get_user_model()
        user = get_object_or_404(User, username=username)
        iduser = user.id

        profile = get_object_or_404(models.Profile, iduser=iduser)

        photo = utils.get_photo_profile(iduser)

        # Get last topic of the profile
        topics = models.Topic.objects.filter(user=user)[:5]

        data = {
            'profile': profile,
            'photo': photo,
            'user': request.user,
            'topics': topics
        }

        return render(request, template_name, data)
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo n.º 13
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)