def create(cls, user): user_email = normalize_email(user.email()) new_website_subscription = cls(user_email=user_email, ) new_website_subscription.put() return new_website_subscription
def create(cls, title, content, author_email): new_topic = cls( title=escape_html(title, allow_links=False), content=escape_html(content), author_email=normalize_email(author_email), ) new_topic.put() return new_topic
def create(cls, user, topic): user_email = normalize_email(user.email()) new_topic_subscription = cls( user_email=user_email, topic_id=topic.key.id(), ) new_topic_subscription.put() return new_topic_subscription
def post(self, topic_id): logged_user = users.get_current_user() topic = Topic.get_by_id(int(topic_id)) is_same_author = (topic.author_email == normalize_email( logged_user.email())) is_admin = users.is_current_user_admin() if is_same_author or is_admin: Topic.delete(topic_id) else: return self.write( "Error\nSorry, you're not allowed to delete this topic.") return self.redirect_to('home-page')
def is_user_subscribed(cls, user, topic): '''Class method to check if user is subscribed to a topic. :param user: :type User :param topic: Topic to check if User is subscribed to it. :type Topic :return: Boolean ''' user_email = normalize_email(user.email()) topic_subscriptions = cls.query(cls.topic_id == topic.key.id()) subscriptions = topic_subscriptions.filter( cls.user_email == user_email) subscription_length = subscriptions.count() return subscription_length > 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
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)
def post(self, topic_id): logged_user = users.get_current_user() user_email = normalize_email(logged_user.email()) 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 == 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())