def post(self, team_number):
        self._require_login()
        self._require_registration()

        current_user_id = self.user_bundle.account.key.id()
        team_key = 'frc{}'.format(team_number)

        if self.request.get('favorite'):
            favorite = Favorite(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.TEAM,
                model_key=team_key
            )
            MyTBAHelper.add_favorite(favorite)
        else:
            MyTBAHelper.remove_favorite(current_user_id, team_key, ModelType.TEAM)

        subs = self.request.get_all('notification_types')
        if subs:
            subscription = Subscription(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.TEAM,
                model_key=team_key,
                notification_types=[int(s) for s in subs]
            )
            MyTBAHelper.add_subscription(subscription)
        else:
            MyTBAHelper.remove_subscription(current_user_id, team_key, ModelType.TEAM)

        self.redirect('/account/mytba?status=team_updated#my-teams')
    def post(self, event_key):
        self._require_login()
        self._require_registration()

        current_user_id = self.user_bundle.account.key.id()

        event = Event.get_by_id(event_key)
        subscribed_matches = set(self.request.get_all('subscribed_matches'))

        for match in event.matches:
            if not match.has_been_played:
                match_key = match.key.id()
                if match.key.id() in subscribed_matches:
                    sub = Subscription(
                        parent=ndb.Key(Account, current_user_id),
                        user_id=current_user_id,
                        model_type=ModelType.MATCH,
                        model_key=match_key,
                        notification_types=[NotificationType.UPCOMING_MATCH]
                    )
                    MyTBAHelper.add_subscription(sub)
                else:
                    MyTBAHelper.remove_subscription(current_user_id, match_key, ModelType.MATCH)

        self.redirect('/account/mytba?status=match_updated#my-matches'.format(event_key))
    def post(self, match_key):
        self._require_login()
        self._require_registration()

        current_user_id = self.user_bundle.account.key.id()
        match = Match.get_by_id(match_key)

        if self.request.get('favorite'):
            favorite = Favorite(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.MATCH,
                model_key=match_key
            )
            MyTBAHelper.add_favorite(favorite)
        else:
            MyTBAHelper.remove_favorite(current_user_id, match_key, ModelType.MATCH)

        subs = self.request.get_all('notification_types')
        if subs:
            subscription = Subscription(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.MATCH,
                model_key=match_key,
                notification_types=[int(s) for s in subs]
            )
            MyTBAHelper.add_subscription(subscription)
        else:
            MyTBAHelper.remove_subscription(current_user_id, match_key, ModelType.MATCH)

        self.redirect('/account/mytba?status=match_updated#my-matches')
Beispiel #4
0
    def post(self, match_key):
        self._require_registration()

        current_user_id = self.user_bundle.account.key.id()
        match = Match.get_by_id(match_key)

        if self.request.get('favorite'):
            favorite = Favorite(parent=ndb.Key(Account, current_user_id),
                                user_id=current_user_id,
                                model_type=ModelType.MATCH,
                                model_key=match_key)
            MyTBAHelper.add_favorite(favorite)
        else:
            MyTBAHelper.remove_favorite(current_user_id, match_key,
                                        ModelType.MATCH)

        subs = self.request.get_all('notification_types')
        if subs:
            subscription = Subscription(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.MATCH,
                model_key=match_key,
                notification_types=[int(s) for s in subs])
            MyTBAHelper.add_subscription(subscription)
        else:
            MyTBAHelper.remove_subscription(current_user_id, match_key,
                                            ModelType.MATCH)

        self.redirect('/account/mytba?status=match_updated#my-matches')
Beispiel #5
0
    def post(self, team_number):
        self._require_registration()

        current_user_id = self.user_bundle.account.key.id()
        team_key = 'frc{}'.format(team_number)

        if self.request.get('favorite'):
            favorite = Favorite(parent=ndb.Key(Account, current_user_id),
                                user_id=current_user_id,
                                model_type=ModelType.TEAM,
                                model_key=team_key)
            MyTBAHelper.add_favorite(favorite)
        else:
            MyTBAHelper.remove_favorite(current_user_id, team_key,
                                        ModelType.TEAM)

        subs = self.request.get_all('notification_types')
        if subs:
            subscription = Subscription(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.TEAM,
                model_key=team_key,
                notification_types=[int(s) for s in subs])
            MyTBAHelper.add_subscription(subscription)
        else:
            MyTBAHelper.remove_subscription(current_user_id, team_key,
                                            ModelType.TEAM)

        self.redirect('/account/mytba?status=team_updated#my-teams')
    def post(self, event_key):
        self._require_login('/account/register')
        self._require_registration('/account/register')

        current_user_id = self.user_bundle.account.key.id()

        if self.request.get('favorite'):
            favorite = Favorite(parent=ndb.Key(Account, current_user_id),
                                user_id=current_user_id,
                                model_type=ModelType.EVENT,
                                model_key=event_key)
            MyTBAHelper.add_favorite(favorite)
        else:
            MyTBAHelper.remove_favorite(current_user_id, event_key)

        subs = self.request.get_all('notification_types')
        if subs:
            subscription = Subscription(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.EVENT,
                model_key=event_key,
                notification_types=[int(s) for s in subs])
            MyTBAHelper.add_subscription(subscription)
        else:
            MyTBAHelper.remove_subscription(current_user_id, event_key)

        self.redirect('/account/mytba?status=event_updated#my-events')
Beispiel #7
0
    def post(self, event_key):
        self._require_registration()

        current_user_id = self.user_bundle.account.key.id()

        event = Event.get_by_id(event_key)
        subscribed_matches = set(self.request.get_all('subscribed_matches'))

        for match in event.matches:
            if not match.has_been_played:
                match_key = match.key.id()
                if match.key.id() in subscribed_matches:
                    sub = Subscription(
                        parent=ndb.Key(Account, current_user_id),
                        user_id=current_user_id,
                        model_type=ModelType.MATCH,
                        model_key=match_key,
                        notification_types=[NotificationType.UPCOMING_MATCH])
                    MyTBAHelper.add_subscription(sub)
                else:
                    MyTBAHelper.remove_subscription(current_user_id, match_key,
                                                    ModelType.MATCH)

        self.redirect(
            '/account/mytba?status=match_updated#my-matches'.format(event_key))
    def post(self, event_key):
        self._require_registration()

        current_user_id = self.user_bundle.account.key.id()

        if self.request.get("favorite"):
            favorite = Favorite(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.EVENT,
                model_key=event_key,
            )
            MyTBAHelper.add_favorite(favorite)
        else:
            MyTBAHelper.remove_favorite(current_user_id, event_key, ModelType.EVENT)

        subs = self.request.get_all("notification_types")
        if subs:
            subscription = Subscription(
                parent=ndb.Key(Account, current_user_id),
                user_id=current_user_id,
                model_type=ModelType.EVENT,
                model_key=event_key,
                notification_types=[int(s) for s in subs],
            )
            MyTBAHelper.add_subscription(subscription)
        else:
            MyTBAHelper.remove_subscription(current_user_id, event_key, ModelType.EVENT)

        self.redirect("/account/mytba?status=event_updated#my-events")
Beispiel #9
0
 def test_add_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 does not already exist - return a 200 and put
     with patch.object(NotificationHelper, 'send_subscription_update') as mock_send_subscription_update, \
          patch.object(subscription, 'put') as mock_put:
             self.assertEqual(MyTBAHelper.add_subscription(subscription, 'sending_device_key'), 200)
             mock_put.assert_called_once()
             mock_send_subscription_update.assert_called_once_with('user_id', 'sending_device_key')
Beispiel #10
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')
Beispiel #11
0
    def update_model_preferences(self, request):
        user_id = get_current_user_id(self.headers)
        if user_id is None:
            return BaseResponse(
                code=401, message="Unauthorized to update model preferences")
        model_key = request.model_key
        model_type = request.model_type
        output = {}
        code = 0

        if request.favorite:
            fav = Favorite(parent=ndb.Key(Account, user_id),
                           user_id=user_id,
                           model_key=model_key,
                           model_type=model_type)
            result = MyTBAHelper.add_favorite(fav, request.device_key)
            if result == 200:
                output['favorite'] = {"code": 200, "message": "Favorite added"}
                code += 100
            elif result == 304:
                output['favorite'] = {
                    "code": 304,
                    "message": "Favorite already exists"
                }
                code += 304
            else:
                output['favorite'] = {
                    "code": 500,
                    "message": "Unknown error adding favorite"
                }
                code += 500
        else:
            result = MyTBAHelper.remove_favorite(user_id, model_key,
                                                 model_type,
                                                 request.device_key)
            if result == 200:
                output['favorite'] = {
                    "code": 200,
                    "message": "Favorite deleted"
                }
                code += 100
            elif result == 404:
                output['favorite'] = {
                    "code": 404,
                    "message": "Favorite not found"
                }
                code += 404
            else:
                output['favorite'] = {
                    "code": 500,
                    "message": "Unknown error removing favorite"
                }
                code += 500

        if request.notifications:
            sub = Subscription(
                parent=ndb.Key(Account, user_id),
                user_id=user_id,
                model_key=model_key,
                model_type=request.model_type,
                notification_types=PushHelper.notification_enums_from_string(
                    request.notifications))
            result = MyTBAHelper.add_subscription(sub, request.device_key)
            if result == 200:
                output['subscription'] = {
                    "code": 200,
                    "message": "Subscription updated"
                }
                code += 100
            elif result == 304:
                output['subscription'] = {
                    "code": 304,
                    "message": "Subscription already exists"
                }
                code += 304
            else:
                output['subscription'] = {
                    "code": 500,
                    "message": "Unknown error adding favorite"
                }
                code += 500
        else:
            result = MyTBAHelper.remove_subscription(user_id, model_key,
                                                     model_type,
                                                     request.device_key)
            if result == 200:
                output['subscription'] = {
                    "code": 200,
                    "message": "Subscription removed"
                }
                code += 100
            elif result == 404:
                output['subscription'] = {
                    "code": 404,
                    "message": "Subscription not found"
                }
                code += 404
            else:
                output['subscription'] = {
                    "code": 500,
                    "message": "Unknown error removing subscription"
                }
                code += 500

        return BaseResponse(code=code, message=json.dumps(output))
    def update_model_preferences(self, request):
        current_user = endpoints.get_current_user()
        if current_user is None:
            return BaseResponse(code=401, message="Unauthorized to update model preferences")
        user_id = PushHelper.user_email_to_id(current_user.email())
        model_key = request.model_key
        output = {}
        code = 0

        if request.favorite:
            fav = Favorite(
                parent=ndb.Key(Account, user_id),
                user_id=user_id,
                model_key=model_key,
                model_type=request.model_type
            )
            result = MyTBAHelper.add_favorite(fav, request.device_key)
            if result == 200:
                output['favorite'] = {"code":    200,
                                      "message": "Favorite added"}
                code += 100
            elif result == 304:
                output['favorite'] = {"code":    304,
                                      "message": "Favorite already exists"}
                code += 304
            else:
                output['favorite'] = {"code":    500,
                                      "message": "Unknown error adding favorite"}
                code += 500
        else:
            result = MyTBAHelper.remove_favorite(user_id, model_key, request.device_key)
            if result == 200:
                output['favorite'] = {"code":    200,
                                      "message": "Favorite deleted"}
                code += 100
            elif result == 404:
                output['favorite'] = {"code":    404,
                                      "message": "Favorite not found"}
                code += 404
            else:
                output['favorite'] = {"code":    500,
                                      "message": "Unknown error removing favorite"}
                code += 500

        if request.notifications:
            sub = Subscription(
                parent=ndb.Key(Account, user_id),
                user_id=user_id,
                model_key=model_key,
                model_type=request.model_type,
                notification_types=PushHelper.notification_enums_from_string(request.notifications)
            )
            result = MyTBAHelper.add_subscription(sub, request.device_key)
            if result == 200:
                output['subscription'] = {"code":    200,
                                          "message": "Subscription updated"}
                code += 100
            elif result == 304:
                output['subscription'] = {"code":    304,
                                          "message": "Subscription already exists"}
                code += 304
            else:
                output['subscription'] = {"code":    500,
                                          "message": "Unknown error adding favorite"}
                code += 500
        else:
            result = MyTBAHelper.remove_subscription(user_id, model_key, request.device_key)
            if result == 200:
                output['subscription'] = {"code":    200,
                                          "message": "Subscription removed"}
                code += 100
            elif result == 404:
                output['subscription'] = {"code":    404,
                                          "message": "Subscription not found"}
                code += 404
            else:
                output['subscription'] = {"code":    500,
                                          "message": "Unknown error removing subscription"}
                code += 500

        return BaseResponse(code=code, message=json.dumps(output))
Beispiel #13
0
    def post(self):
        self._require_login('/account/register')
        self._require_registration('/account/register')

        current_user_id = self.user_bundle.account.key.id()
        target_account_id = self.request.get('account_id')
        if current_user_id == target_account_id:
            action = self.request.get('action')
            #         if action == "favorite_add":
            #             model = self.request.get('model_key')
            #             if not ValidationHelper.is_valid_model_key(model):
            #                 self.redirect('/account/mytba?error=invalid_model')
            #                 return
            #             favorite = Favorite(parent = ndb.Key(Account, current_user_id), model_key =  model, user_id = current_user_id)
            #            MyTBAHelper.add_favorite(favorite)
            #            self.redirect('/account/mytba')
            #            return
            if action == "favorite_delete":
                model_key = self.request.get('model_key')
                result = MyTBAHelper.remove_favorite(current_user_id,
                                                     model_key)
                if result == 404:
                    self.redirect('/account/mytba?error=fav_not_found')
                    return
                self.redirect('/account/mytba')
                return

    #         elif action == "subscription_add":
    #             model = self.request.get('model_key')
    #             if not ValidationHelper.is_valid_model_key(model):
    #                 self.redirect('/account/mytba?error=invalid_model')
    #                 return
    #             subs = self.request.get_all('notification_types')
    #             if not subs:
    #                 # No notification types specified. Don't add
    #                 self.redirect('/account/mytba?error=no_sub_types')
    #                 return
    #             subscription = Subscription(parent = ndb.Key(Account, current_user_id), user_id = current_user_id, model_key = model, notification_types = [int(s) for s in subs])
    #             MyTBAHelper.add_subscription(subscription)
    #             self.redirect('/account/mytba')
    #             return
            elif action == "subscription_year_add":
                now = datetime.datetime.now()
                year = self.request.get('year')
                if not "{}".format(now.year) == year:
                    # Can only subscribe to the current year's firehose
                    self.redirect('/account/mytba?error=invalid_year')
                    return
                key = "{}*".format(year)
                subs = self.request.get_all('notification_types')
                if not subs:
                    # No notification types specified. Don't add
                    self.redirect('/account/mytba?error=no_sub_types')
                    return
                subscription = Subscription(
                    parent=ndb.Key(Account, current_user_id),
                    user_id=current_user_id,
                    model_key=key,
                    model_type=ModelType.EVENT,
                    notification_types=[int(s) for s in subs])

                logging.info("{}".format(self.request.get('webhooks_only')))
                MyTBAHelper.add_subscription(subscription)
                self.redirect('/account/mytba')
                return
            elif action == "subscription_delete":
                model_key = self.request.get('model_key')
                result = MyTBAHelper.remove_subscription(
                    current_user_id, model_key)
                if result == 404:
                    self.redirect('/account/mytba?error=sub_not_found')
                    return
                self.redirect('/account/mytba')
                return
        self.redirect('/account/mytba?error=invalid_account')
    def post(self):
        self._require_login('/account/register')
        self._require_registration('/account/register')

        current_user_id = self.user_bundle.account.key.id()
        target_account_id = self.request.get('account_id')
        if current_user_id == target_account_id:
            action = self.request.get('action')
    #         if action == "favorite_add":
    #             model = self.request.get('model_key')
    #             if not ValidationHelper.is_valid_model_key(model):
    #                 self.redirect('/account/mytba?error=invalid_model')
    #                 return
    #             favorite = Favorite(parent = ndb.Key(Account, current_user_id), model_key =  model, user_id = current_user_id)
    #            MyTBAHelper.add_favorite(favorite)
    #            self.redirect('/account/mytba')
    #            return
            if action == "favorite_delete":
                model_key = self.request.get('model_key')
                result = MyTBAHelper.remove_favorite(current_user_id, model_key)
                if result == 404:
                    self.redirect('/account/mytba?error=fav_not_found')
                    return
                self.redirect('/account/mytba')
                return
    #         elif action == "subscription_add":
    #             model = self.request.get('model_key')
    #             if not ValidationHelper.is_valid_model_key(model):
    #                 self.redirect('/account/mytba?error=invalid_model')
    #                 return
    #             subs = self.request.get_all('notification_types')
    #             if not subs:
    #                 # No notification types specified. Don't add
    #                 self.redirect('/account/mytba?error=no_sub_types')
    #                 return
    #             subscription = Subscription(parent = ndb.Key(Account, current_user_id), user_id = current_user_id, model_key = model, notification_types = [int(s) for s in subs])
    #             MyTBAHelper.add_subscription(subscription)
    #             self.redirect('/account/mytba')
    #             return
            elif action == "subscription_year_add":
                now = datetime.datetime.now()
                year = self.request.get('year')
                if not "{}".format(now.year) == year:
                    # Can only subscribe to the current year's firehose
                    self.redirect('/account/mytba?error=invalid_year')
                    return
                key = "{}*".format(year)
                subs = self.request.get_all('notification_types')
                if not subs:
                    # No notification types specified. Don't add
                    self.redirect('/account/mytba?error=no_sub_types')
                    return
                subscription = Subscription(parent = ndb.Key(Account, current_user_id), user_id = current_user_id, model_key = key, model_type = ModelType.EVENT, notification_types = [int(s) for s in subs])

                logging.info("{}".format(self.request.get('webhooks_only')))
                MyTBAHelper.add_subscription(subscription)
                self.redirect('/account/mytba')
                return
            elif action == "subscription_delete":
                model_key = self.request.get('model_key')
                result = MyTBAHelper.remove_subscription(current_user_id, model_key)
                if result == 404:
                    self.redirect('/account/mytba?error=sub_not_found')
                    return
                self.redirect('/account/mytba')
                return
        self.redirect('/account/mytba?error=invalid_account')