Exemple #1
0
    def post(self, topic_id):
        user = users.get_current_user()
        topic = Topic.get_by_id(int(topic_id))

        new_subscription = Subscription(email=user.email(),
                                        topic_id=topic.key.id())
        # subscriptions = Subscription.query().fetch()
        new_subscription.put()

        return self.redirect_to("topic-details", topic_id=topic.key.id())
Exemple #2
0
    def post(self):

        user = CustomUser.get_current_user(self)

        if not user or not user.is_active:
            return self.write(
                "Please login before you're allowed to post a topic.")

        title = cgi.escape(self.request.get("title").strip())
        text = cgi.escape(self.request.get("text").strip())

        if title == "" or text == "":
            return self.write("Please fill out all the fields.")

        if not CSRF.validate_token(self.request.get("csrf_token")):
            return self.write("CSRF fail")

        # Validate recaptcha
        payload = {
            'secret': os.environ.get("RECAPTCHA_KEY"),
            'response': self.request.get("g-recaptcha-response"),
        }

        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        result = urlfetch.fetch(
            url="https://www.google.com/recaptcha/api/siteverify",
            headers=headers,
            payload=urlencode(payload),
            method=urlfetch.POST)

        recaptcha_response = json.loads(result.content)

        if not recaptcha_response['success']:
            return self.write("Please verify you are not a robot")

        new_topic = Topic(title=title, content=text, user_email=user.email())
        new_topic.put()

        topic_id = new_topic.key.id()

        # Subscribe the author to the topic
        new_subscription = Subscription(user_id=user.id,
                                        user_email=user.email(),
                                        topic_id=topic_id)
        new_subscription.put()

        return self.redirect("/topic/view/" + str(topic_id))
Exemple #3
0
    def get(self, topic_id):

        user = CustomUser.get_current_user(self)

        if not user:
            return self.redircet("/")

        if user.is_subscribed(topic_id):
            user.delete_subscription(topic_id)
            return self.write(
                "You have successfuly unsubscribed from the topic")
        else:

            new_subscription = Subscription(user_id=user.id,
                                            user_email=user.email(),
                                            topic_id=int(topic_id))
            new_subscription.put()

            return self.write("You have successfuly subscribed to the topic")
Exemple #4
0
    def get(self):

        user = CustomUser.get_current_user(self)

        if not user:
            return self.redirect("/")

        existing_subscription = Subscription.query(
            Subscription.user_id == user.id,
            Subscription.topic_id == 1).fetch()
        if existing_subscription:
            existing_subscription[0].key.delete()
            return self.write("You have been unsubscribed.")

        new_subscription = Subscription(user_id=user.id,
                                        topic_id=1,
                                        user_email=user.email())
        new_subscription.put()

        return self.write("You have successfuly subscribed.")
Exemple #5
0
 def test_remove_subscription_200_sending_device_key(self):
     subscription = Subscription(
         parent=ndb.Key(Account, 'user_id'),
         user_id='user_id',
         model_key='frc7332',
         model_type=ModelType.TEAM)
     subscription_key = subscription.put()
     # Subscription does not exist - 404
     with patch.object(NotificationHelper, 'send_subscription_update') as mock_send_subscription_update, \
          patch.object(ndb, 'delete_multi') as mock_delete:
             self.assertEqual(MyTBAHelper.remove_subscription(subscription.user_id, subscription.model_key, subscription.model_type, 'sending_device_key'), 200)
             mock_delete.assert_called_once_with([subscription_key])
             mock_send_subscription_update.assert_called_once_with('user_id', 'sending_device_key')
Exemple #6
0
    def test_add_subscription_200_update_sending_device_key(self):
        subscription = Subscription(
            parent=ndb.Key(Account, 'user_id'),
            user_id='user_id',
            model_key='frc7332',
            model_type=ModelType.TEAM)
        subscription.put()

        new_subscription = Subscription(
            parent=ndb.Key(Account, 'user_id'),
            user_id='user_id',
            model_key='frc7332',
            model_type=ModelType.TEAM,
            notification_types=[NotificationType.UPCOMING_MATCH])

        # Subscription exists but with different notification types - update and return 200
        with patch.object(NotificationHelper, 'send_subscription_update') as mock_send_subscription_update, \
             patch.object(subscription, 'put') as mock_put:
                self.assertEqual(MyTBAHelper.add_subscription(new_subscription, 'sending_device_key'), 200)
                self.assertEqual(new_subscription.notification_types, subscription.notification_types)
                mock_put.assert_called_once()
                mock_send_subscription_update.assert_called_once_with('user_id', 'sending_device_key')