예제 #1
0
    def get(self, topic_id):
        topic = Topic.get_by_id(int(topic_id))
        comments = Comment.filter_by_topic(int(topic_id)).order(
            Comment.created).fetch()

        logged_user = users.get_current_user()

        is_subscribed = logged_user and topic.author_email == logged_user.email(
        )

        if logged_user and not is_subscribed:
            # check if user asked to be subscribed
            is_subscribed = TopicSubscription.is_user_subscribed(
                logged_user, topic)

        context = {
            "topic":
            topic,
            "comments":
            comments,
            "can_delete":
            users.is_current_user_admin()
            or (logged_user and topic.author_email == logged_user.email()),
            "is_subscribed":
            is_subscribed,
            "user":
            logged_user,
        }

        return self.render_template("topic_details.html",
                                    params=context,
                                    generate_csrf_token=True)
예제 #2
0
    def create(cls, text_value, logged_user, topic):
        new_comment = cls(
            content=text_value,
            author_email=logged_user.email(),
            topic_id=topic.key.id(),
            topic_title=topic.title,
        )
        new_comment.put()

        subscriptions = TopicSubscription.query(TopicSubscription.topic_id == topic.key.id()).fetch()

        subscribers = [topic.author_email, ]
        for subscription in subscriptions:
            if subscription.user_email != logged_user.email():
                subscribers.append(subscription.user_email)

        # send notification to topic author and subscribers

        for topic.author_email in subscribers:
            taskqueue.add(
                url='/task/email-new-comment',
                params={"topic-author-email": topic.author_email,
                        "topic-title": topic.title,
                        "topic-id": topic.key.id(),
                        }
            )

        return new_comment
예제 #3
0
    def post(self, topic_id):
        logged_user = users.get_current_user()

        if not logged_user:
            return self.write(
                "Please login before you're allowed to subscribe to one topic."
            )

        topic = Topic.get_by_id(int(topic_id))

        is_subscribed = topic.author_email == logged_user.email()

        if not is_subscribed:
            # check if user asked to be subscribed
            is_subscribed = TopicSubscription.is_user_subscribed(
                logged_user, topic)

        if is_subscribed:
            return self.write("You are already subscribed")

        TopicSubscription.create(logged_user, topic)

        return self.redirect_to("topic-details", topic_id=topic.key.id())
예제 #4
0
    def create(cls, content, user, topic):
        """Class method to create new comment.

        :param content: Content text
        :type content: str
        :param user: Author user
        :type user: User
        :param topic: Topic where the comment belongs to
        :type topic: Topic
        :return: New comment
        """
        user_email = normalize_email(user.email())

        new_comment = cls(
            content=escape_html(content),
            topic_id=topic.key.id(),
            topic_title=topic.title,
            author_email=user_email,
        )
        new_comment.put()

        subscriptions = TopicSubscription.query(
            TopicSubscription.topic_id == topic.key.id()).fetch()

        subscribers = [
            topic.author_email,
        ]

        for subscription in subscriptions:
            if subscription.user_email != user_email:
                subscribers.append(subscription.user_email)

        # Send notification to topic author and subscribers.
        for email in subscribers:
            params = {
                'topic-author-email': email,
                'topic-title': topic.title,
                'topic-id': topic.key.id(),
            }
            taskqueue.add(url='/task/email-new-comment', params=params)

        return new_comment
예제 #5
0
    def get(self, topic_id):
        is_authorized = False
        is_admin = users.is_current_user_admin()
        logged_user = users.get_current_user()

        if not logged_user:
            return self.write('Error\nYou must login to see this topic.')

        user_email = normalize_email(logged_user.email())

        int_topic_id = int(topic_id)
        topic = Topic.get_by_id(int_topic_id)

        is_same_author = topic.author_email == user_email

        if is_same_author or is_admin:
            is_authorized = True

        is_subscribed = logged_user and is_same_author

        if logged_user and not is_subscribed:
            # check if user asked to be subscribed
            is_subscribed = TopicSubscription.is_user_subscribed(
                logged_user, topic)

        query = Comment.filter_by_topic(topic)
        comments = query.order(Comment.created).fetch()

        context = {
            'topic': topic,
            'comments': comments,
            'can_make_changes': is_authorized,
            'is_subscribed': is_subscribed,
            'flash_message': self.request.get('flash_message'),
            'flash_class': self.request.get('flash_class'),
        }

        return self.render_template_with_csrf('topic_details.html',
                                              params=context)