예제 #1
0
    def create_post(self, request):
        self.log_action = "send"
        self.log_resource = "mural"
        self.log_context = {}

        if "file" in request.data:
            file = request.FILES["file"]

            data = json.loads(request.data["data"])
        else:
            file = None

            data = (
                request.data
                if request.data
                else json.loads(request.body.decode("utf-8"))
            )

        username = data["email"]
        message = data["message"]
        space = data["subject"]
        action = data["action"]

        info = {}

        subject = Subject.objects.get(slug=space)
        user = User.objects.get(email=username)

        post = SubjectPost()
        post.action = action
        post.space = subject
        post.post = message
        post.user = user

        if not file is None:
            post.image = file

        post.save()

        if not post.pk is None:
            users = getSpaceUsers(user.id, post)

            entries = []

            paths = [
                reverse("mural:manage_subject"),
                reverse("mural:subject_view", args=(), kwargs={"slug": subject.slug}),
            ]

            simple_notify = _("%s has made a post in %s") % (
                str(post.user),
                str(post.space),
            )

            notification = {
                "type": "mural",
                "subtype": "post",
                "paths": paths,
                "user_icon": user.image_url,
                "simple_notify": simple_notify,
                "complete": render_to_string(
                    "mural/_view.html", {"post": post}, request
                ),
                "container": "#" + subject.slug,
                "accordion": True,
                "post_type": "subjects",
            }

            notification = json.dumps(notification)

            for user in users:
                entries.append(MuralVisualizations(viewed=False, user=user, post=post))
                sendMuralPushNotification(user, post.user, simple_notify)
                Group("user-%s" % user.id).send({"text": notification})

            MuralVisualizations.objects.bulk_create(entries)

            self.log_context["subject_id"] = post.space.id
            self.log_context["subject_name"] = post.space.name
            self.log_context["subject_slug"] = post.space.slug

            serializer = MuralSerializer(
                post, context={"request_user": user, "subject": space}
            )

            json_r = json.dumps(serializer.data)
            json_r = json.loads(json_r)

            info["data"] = {}
            info["data"]["new_post"] = json_r

            info["message"] = _("Post created successfully!")
            info["success"] = True
            info["number"] = 1

            super(MuralViewset, self).createLog(
                user,
                self.log_component,
                self.log_action,
                self.log_resource,
                self.log_context,
            )
        else:
            info["message"] = _("Error while creating post!")
            info["success"] = False
            info["number"] = 0

        response = json.dumps(info)

        return HttpResponse(response)
예제 #2
0
    def create_comment(self, request):
        self.log_action = "send"
        self.log_resource = "post_comment"
        self.log_context = {}

        if "file" in request.data:
            file = request.FILES["file"]

            data = json.loads(request.data["data"])
        else:
            file = None

            data = (
                request.data
                if request.data
                else json.loads(request.body.decode("utf-8"))
            )

        username = data["email"]
        message = data["message"]
        post = data["post_id"]

        info = {}

        mural = SubjectPost.objects.get(id=post)
        user = User.objects.get(email=username)

        comment = Comment()
        comment.comment = message
        comment.post = mural
        comment.user = user

        if not file is None:
            comment.image = file

        comment.save()

        if not comment.pk is None:
            users = getSpaceUsers(user.id, mural)

            entries = []

            paths = [
                reverse("mural:manage_general"),
                reverse("mural:manage_category"),
                reverse("mural:manage_subject"),
                reverse(
                    "mural:subject_view",
                    args=(),
                    kwargs={"slug": mural.get_space_slug()},
                ),
            ]

            simple_notify = _("%s has commented in a post") % (str(comment.user))

            notification = {
                "type": "mural",
                "subtype": "post",
                "paths": paths,
                "user_icon": user.image_url,
                "simple_notify": simple_notify,
                "complete": render_to_string(
                    "mural/_view_comment.html", {"comment": comment}, request
                ),
                "container": "#post-" + str(mural.get_id()),
                "post_type": mural._my_subclass,
                "type_slug": mural.get_space_slug(),
            }

            notification = json.dumps(notification)

            for user in users:
                entries.append(
                    MuralVisualizations(viewed=False, user=user, comment=comment)
                )
                sendMuralPushNotification(user, comment.user, simple_notify)
                Group("user-%s" % user.id).send({"text": notification})

            MuralVisualizations.objects.bulk_create(entries)

            self.log_context["post_id"] = mural.id
            self.log_context["subject_id"] = mural.space.id
            self.log_context["subject_name"] = mural.space.name
            self.log_context["subject_slug"] = mural.space.slug

            serializer = CommentsSerializer(
                comment, context={"request_user": user, "subject": mural.space.slug}
            )

            json_r = json.dumps(serializer.data)
            json_r = json.loads(json_r)

            info["data"] = {}
            info["data"]["new_comment"] = json_r

            info["message"] = _("Comment created successfully!")
            info["success"] = True
            info["number"] = 1

            super(MuralViewset, self).createLog(
                user,
                self.log_component,
                self.log_action,
                self.log_resource,
                self.log_context,
            )
        else:
            info["message"] = _("Error while creating comment!")
            info["success"] = False
            info["number"] = 0

        response = json.dumps(info)

        return HttpResponse(response)