Esempio n. 1
0
    def get(self):
        df = DatafeedResourceLibrary()

        teams = df.getHallOfFameTeams()
        if teams:
            media_to_update = []
            for team in teams:
                team_reference = Media.create_reference(
                    'team', team['team_id'])

                video_foreign_key = team['video']
                if video_foreign_key:
                    media_to_update.append(
                        Media(id=Media.render_key_name(MediaType.YOUTUBE_VIDEO,
                                                       video_foreign_key),
                              media_type_enum=MediaType.YOUTUBE_VIDEO,
                              media_tag_enum=[MediaTag.CHAIRMANS_VIDEO],
                              references=[team_reference],
                              year=team['year'],
                              foreign_key=video_foreign_key))

                presentation_foreign_key = team['presentation']
                if presentation_foreign_key:
                    media_to_update.append(
                        Media(id=Media.render_key_name(
                            MediaType.YOUTUBE_VIDEO, presentation_foreign_key),
                              media_type_enum=MediaType.YOUTUBE_VIDEO,
                              media_tag_enum=[MediaTag.CHAIRMANS_PRESENTATION],
                              references=[team_reference],
                              year=team['year'],
                              foreign_key=presentation_foreign_key))

                essay_foreign_key = team['essay']
                if essay_foreign_key:
                    media_to_update.append(
                        Media(id=Media.render_key_name(MediaType.EXTERNAL_LINK,
                                                       essay_foreign_key),
                              media_type_enum=MediaType.EXTERNAL_LINK,
                              media_tag_enum=[MediaTag.CHAIRMANS_ESSAY],
                              references=[team_reference],
                              year=team['year'],
                              foreign_key=essay_foreign_key))

            MediaManipulator.createOrUpdate(media_to_update)
        else:
            logging.info("No Hall of Fame teams found")
            teams = []

        template_values = {
            'teams': teams,
        }

        if 'X-Appengine-Taskname' not in self.request.headers:  # Only write out if not in taskqueue
            path = os.path.join(
                os.path.dirname(__file__),
                '../templates/datafeeds/hall_of_fame_teams_get.html')
            self.response.out.write(template.render(path, template_values))
    def get(self):
        df = DatafeedResourceLibrary()

        teams = df.getHallOfFameTeams()
        if teams:
            media_to_update = []
            for team in teams:
                team_reference = Media.create_reference('team', team['team_id'])

                video_foreign_key = team['video']
                if video_foreign_key:
                    media_to_update.append(Media(id=Media.render_key_name(MediaType.YOUTUBE_VIDEO, video_foreign_key),
                                                 media_type_enum=MediaType.YOUTUBE_VIDEO,
                                                 media_tag_enum=[MediaTag.CHAIRMANS_VIDEO],
                                                 references=[team_reference],
                                                 year=team['year'],
                                                 foreign_key=video_foreign_key))

                presentation_foreign_key = team['presentation']
                if presentation_foreign_key:
                    media_to_update.append(Media(id=Media.render_key_name(MediaType.YOUTUBE_VIDEO, presentation_foreign_key),
                                                 media_type_enum=MediaType.YOUTUBE_VIDEO,
                                                 media_tag_enum=[MediaTag.CHAIRMANS_PRESENTATION],
                                                 references=[team_reference],
                                                 year=team['year'],
                                                 foreign_key=presentation_foreign_key))

                essay_foreign_key = team['essay']
                if essay_foreign_key:
                    media_to_update.append(Media(id=Media.render_key_name(MediaType.EXTERNAL_LINK, essay_foreign_key),
                                                 media_type_enum=MediaType.EXTERNAL_LINK,
                                                 media_tag_enum=[MediaTag.CHAIRMANS_ESSAY],
                                                 references=[team_reference],
                                                 year=team['year'],
                                                 foreign_key=essay_foreign_key))

            MediaManipulator.createOrUpdate(media_to_update)
        else:
            logging.info("No Hall of Fame teams found")
            teams = []

        template_values = {
            'teams': teams,
        }

        if 'X-Appengine-Taskname' not in self.request.headers:  # Only write out if not in taskqueue
            path = os.path.join(os.path.dirname(__file__), '../templates/datafeeds/hall_of_fame_teams_get.html')
            self.response.out.write(template.render(path, template_values))
    def _process_request(self, request, event_key):
        try:
            video_list = json.loads(request.body)
        except Exception:
            self._errors = json.dumps({"Error": "Invalid JSON. Please check input."})
            self.abort(400)
            return

        if not isinstance(video_list, list) or not video_list:
            self._errors = json.dumps({"Error": "Invalid JSON. Please check input."})
            self.abort(400)
            return

        media_to_put = []
        event_reference = Media.create_reference('event', self.event.key_name)
        for youtube_id in video_list:
            media = Media(
                id=Media.render_key_name(MediaType.YOUTUBE_VIDEO, youtube_id),
                foreign_key=youtube_id,
                media_type_enum=MediaType.YOUTUBE_VIDEO,
                details_json=None,
                private_details_json=None,
                year=self.event.year,
                references=[event_reference],
                preferred_references=[],
            )
            media_to_put.append(media)

        MediaManipulator.createOrUpdate(media_to_put)
    def createEventMediaSuggestion(cls, author_account_key, media_url, event_key, private_details_json=None):
        """Create an Event Media Suggestion. Returns status (success, suggestion_exists, media_exists, bad_url)"""

        media_dict = MediaParser.partial_media_dict_from_url(media_url)
        if media_dict is not None:
            if media_dict['media_type_enum'] != MediaType.YOUTUBE_VIDEO:
                return 'bad_url', None

            existing_media = Media.get_by_id(Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']))
            if existing_media is None or event_key not in [reference.id() for reference in existing_media.references]:
                foreign_type = Media.SLUG_NAMES[media_dict['media_type_enum']]
                suggestion_id = Suggestion.render_media_key_name(event_key[:4], 'event', event_key, foreign_type, media_dict['foreign_key'])
                suggestion = Suggestion.get_by_id(suggestion_id)
                if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                    media_dict['year'] = event_key[:4]
                    media_dict['reference_type'] = 'event'
                    media_dict['reference_key'] = event_key
                    target_model = 'event_media'
                    if private_details_json is not None:
                        media_dict['private_details_json'] = private_details_json

                    suggestion = Suggestion(
                        id=suggestion_id,
                        author=author_account_key,
                        target_model=target_model,
                        )
                    suggestion.contents = media_dict
                    suggestion.put()
                    return 'success', suggestion
                else:
                    return 'suggestion_exists', None
            else:
                return 'media_exists', None
        else:
            return 'bad_url', None
    def post(self):
        self._require_admin()

        media_dict = MediaParser.partial_media_dict_from_url(
            self.request.get('media_url').strip())
        if media_dict is not None:
            year_str = self.request.get('year')
            if year_str == '':
                year = None
            else:
                year = int(year_str.strip())

            media = Media(
                id=Media.render_key_name(media_dict['media_type_enum'],
                                         media_dict['foreign_key']),
                foreign_key=media_dict['foreign_key'],
                media_type_enum=media_dict['media_type_enum'],
                details_json=media_dict.get('details_json', None),
                year=year,
                references=[
                    Media.create_reference(self.request.get('reference_type'),
                                           self.request.get('reference_key'))
                ],
            )
            MediaManipulator.createOrUpdate(media)

        self.redirect(self.request.get('originating_url'))
    def post(self):
        self._require_login()

        team_key = self.request.get("team_key")
        year_str = self.request.get("year")

        success_code = 0
        media_dict = MediaParser.partial_media_dict_from_url(
            self.request.get('media_url').strip())
        if media_dict is not None:
            existing_media = Media.get_by_id(
                Media.render_key_name(media_dict['media_type_enum'],
                                      media_dict['foreign_key']))
            if existing_media is None or team_key not in [
                    reference.id() for reference in existing_media.references
            ]:
                media_dict['year'] = int(year_str)
                media_dict['reference_type'] = 'team'
                media_dict['reference_key'] = team_key

                suggestion = Suggestion(
                    author=self.user_bundle.account.key,
                    target_model="media",
                )
                suggestion.contents = media_dict
                suggestion.put()
            success_code = 1

        self.redirect('/suggest/team/media?team_key=%s&year=%s&success=%s' %
                      (team_key, year_str, success_code))
    def createEventMediaSuggestion(cls, author_account_key, media_url, event_key, private_details_json=None):
        """Create an Event Media Suggestion. Returns status (success, suggestion_exists, media_exists, bad_url)"""

        media_dict = MediaParser.partial_media_dict_from_url(media_url)
        if media_dict is not None:
            if media_dict['media_type_enum'] != MediaType.YOUTUBE_VIDEO:
                return 'bad_url', None

            existing_media = Media.get_by_id(Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']))
            if existing_media is None or event_key not in [reference.id() for reference in existing_media.references]:
                foreign_type = Media.SLUG_NAMES[media_dict['media_type_enum']]
                suggestion_id = Suggestion.render_media_key_name(event_key[:4], 'event', event_key, foreign_type, media_dict['foreign_key'])
                suggestion = Suggestion.get_by_id(suggestion_id)
                if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                    media_dict['year'] = event_key[:4]
                    media_dict['reference_type'] = 'event'
                    media_dict['reference_key'] = event_key
                    target_model = 'event_media'
                    if private_details_json is not None:
                        media_dict['private_details_json'] = private_details_json

                    suggestion = Suggestion(
                        id=suggestion_id,
                        author=author_account_key,
                        target_model=target_model,
                        )
                    suggestion.contents = media_dict
                    suggestion.put()
                    return 'success', suggestion
                else:
                    return 'suggestion_exists', None
            else:
                return 'media_exists', None
        else:
            return 'bad_url', None
    def parse(self, response):
        """
        Parse team avatar from FMSAPI
        """

        # Get team avatar
        current_page = response['pageCurrent']
        total_pages = response['pageTotal']
        teams = response['teams']
        avatars = []
        keys_to_delete = set()

        for teamData in teams:
            team_number = teamData['teamNumber']
            foreign_key = "avatar_{}_frc{}".format(self.year, team_number)
            media_key = ndb.Key(
                'Media', Media.render_key_name(MediaType.AVATAR, foreign_key))

            if not teamData['encodedAvatar']:
                keys_to_delete.add(media_key)
                continue
            else:
                encoded_avatar = teamData['encodedAvatar']

            avatar = Media(
                key=media_key,
                details_json=json.dumps({'base64Image': encoded_avatar}),
                foreign_key=foreign_key,
                media_type_enum=MediaType.AVATAR,
                references=[ndb.Key('Team', "frc{}".format(team_number))],
                year=self.year)

            avatars.append(avatar)

        return (avatars, keys_to_delete, (current_page < total_pages))
    def parse(self, response):
        """
        Parse team avatar from FMSAPI
        """

        # Get team avatar
        current_page = response['pageCurrent']
        total_pages = response['pageTotal']
        teams = response['teams']
        avatars = []
        keys_to_delete = set()

        for teamData in teams:
            team_number = teamData['teamNumber']
            foreign_key = "avatar_{}_frc{}".format(self.year, team_number)
            media_key = ndb.Key('Media', Media.render_key_name(MediaType.AVATAR, foreign_key))

            if not teamData['encodedAvatar']:
                keys_to_delete.add(media_key)
                continue
            else:
                encoded_avatar = teamData['encodedAvatar']

            avatar = Media(
                key=media_key,
                details_json=json.dumps({'base64Image': encoded_avatar}),
                foreign_key=foreign_key,
                media_type_enum=MediaType.AVATAR,
                references=[ndb.Key('Team', "frc{}".format(team_number))],
                year=self.year
            )

            avatars.append(avatar)

        return (avatars, keys_to_delete, (current_page < total_pages))
    def createTeamMediaSuggestion(cls, author_account_key, media_url, team_key, year_str):
        """Create a Team Media Suggestion. Returns status (success, exists, bad_url)"""

        media_dict = MediaParser.partial_media_dict_from_url(media_url)
        if media_dict is not None:
            existing_media = Media.get_by_id(Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']))
            if existing_media is None or team_key not in [reference.id() for reference in existing_media.references]:
                foreign_type = Media.SLUG_NAMES[media_dict['media_type_enum']]
                suggestion_id = Suggestion.render_key_name(year_str, 'team', team_key, foreign_type, media_dict['foreign_key'])
                suggestion = Suggestion.get_by_id(suggestion_id)
                if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                    media_dict['year'] = int(year_str)
                    media_dict['reference_type'] = 'team'
                    media_dict['reference_key'] = team_key

                    suggestion = Suggestion(
                        id=suggestion_id,
                        author=author_account_key,
                        target_model="media",
                        )
                    suggestion.contents = media_dict
                    suggestion.put()
                    return 'success'
                else:
                    return 'suggestion_exists'
            else:
                return 'media_exists'
        else:
            return 'bad_url'
Esempio n. 11
0
    def createTeamMediaSuggestion(cls,
                                  author_account_key,
                                  media_url,
                                  team_key,
                                  year_str,
                                  private_details_json=None,
                                  is_social=False):
        """Create a Team Media Suggestion. Returns status (success, suggestion_exists, media_exists, bad_url)"""

        media_dict = MediaParser.partial_media_dict_from_url(media_url)
        if media_dict is not None:
            if media_dict.get("is_social", False) != is_social:
                return 'bad_url', None

            existing_media = Media.get_by_id(
                Media.render_key_name(media_dict['media_type_enum'],
                                      media_dict['foreign_key']))
            if existing_media is None or team_key not in [
                    reference.id() for reference in existing_media.references
            ]:
                foreign_type = Media.SLUG_NAMES[media_dict['media_type_enum']]
                suggestion_id = Suggestion.render_media_key_name(
                    year_str, 'team', team_key, foreign_type,
                    media_dict['foreign_key'])
                suggestion = Suggestion.get_by_id(suggestion_id)
                if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                    media_dict['year'] = int(year_str) if year_str else None
                    media_dict['reference_type'] = 'team'
                    media_dict['reference_key'] = team_key
                    if private_details_json is not None:
                        media_dict[
                            'private_details_json'] = private_details_json

                    target_model = "media"
                    if media_dict.get("is_social", False):
                        target_model = "social-media"

                    if media_dict.get('media_type',
                                      '') in MediaType.robot_types:
                        target_model = "robot"

                    if Event.validate_key_name(team_key):
                        target_model = 'event_media'
                        media_dict['reference_type'] = 'event'

                    suggestion = Suggestion(
                        id=suggestion_id,
                        author=author_account_key,
                        target_model=target_model,
                    )
                    suggestion.contents = media_dict
                    suggestion.put()
                    return 'success', suggestion
                else:
                    return 'suggestion_exists', None
            else:
                return 'media_exists', None
        else:
            return 'bad_url', None
Esempio n. 12
0
 def testMediaExists(self):
     media_id = Media.render_key_name(MediaType.IMGUR, 'ruRAxDm')
     Media.get_or_insert(media_id,
                         media_type_enum=MediaType.IMGUR,
                         foreign_key='ruRAxDm',
                         references=[ndb.Key(Team, 'frc1124')]).put()
     status, _ = SuggestionCreator.createTeamMediaSuggestion(
         self.account.key, "http://imgur.com/ruRAxDm", "frc1124", "2016")
     self.assertEqual(status, 'media_exists')
Esempio n. 13
0
 def testMediaExists(self):
     media_id = Media.render_key_name(MediaType.YOUTUBE_VIDEO,
                                      'H-54KMwMKY0')
     Media.get_or_insert(media_id,
                         media_type_enum=MediaType.YOUTUBE_VIDEO,
                         foreign_key='H-54KMwMKY0',
                         references=[ndb.Key(Event, '2016nyny')]).put()
     status, _ = SuggestionCreator.createEventMediaSuggestion(
         self.account.key, "https://www.youtube.com/watch?v=H-54KMwMKY0",
         "2016nyny")
     self.assertEqual(status, 'media_exists')
 def test_media_exists(self):
     media_id = Media.render_key_name(MediaType.YOUTUBE_VIDEO, 'H-54KMwMKY0')
     Media.get_or_insert(
         media_id,
         media_type_enum=MediaType.YOUTUBE_VIDEO,
         foreign_key='H-54KMwMKY0',
         references=[ndb.Key(Event, '2016nyny')]).put()
     status, _ = SuggestionCreator.createEventMediaSuggestion(
         self.account.key,
         "https://www.youtube.com/watch?v=H-54KMwMKY0",
         "2016nyny")
     self.assertEqual(status, 'media_exists')
 def create_media_model(cls, suggestion, team_reference, preferred_references=[]):
     media_type_enum = suggestion.contents['media_type_enum']
     return Media(
         id=Media.render_key_name(media_type_enum, suggestion.contents['foreign_key']),
         foreign_key=suggestion.contents['foreign_key'],
         media_type_enum=media_type_enum,
         details_json=suggestion.contents.get('details_json', None),
         private_details_json=suggestion.contents.get('private_details_json', None),
         year=int(suggestion.contents['year']) if not suggestion.contents.get('is_social', False) else None,
         references=[team_reference],
         preferred_references=preferred_references,
     )
 def create_media(cls, suggestion, team_reference, preferred_references=[]):
     media_type_enum = suggestion.contents['media_type_enum']
     return Media(
         id=Media.render_key_name(media_type_enum, suggestion.contents['foreign_key']),
         foreign_key=suggestion.contents['foreign_key'],
         media_type_enum=media_type_enum,
         details_json=suggestion.contents.get('details_json', None),
         private_details_json=suggestion.contents.get('private_details_json', None),
         year=int(suggestion.contents['year']) if not suggestion.contents.get('is_social', False) else None,
         references=[team_reference],
         preferred_references=preferred_references,
     )
 def testMediaExists(self):
     media_id = Media.render_key_name(MediaType.IMGUR, 'ruRAxDm')
     Media.get_or_insert(
         media_id,
         media_type_enum=MediaType.IMGUR,
         foreign_key='ruRAxDm',
         references=[ndb.Key(Team, 'frc1124')]).put()
     status = SuggestionCreator.createTeamMediaSuggestion(
         self.account.key,
         "http://imgur.com/ruRAxDm",
         "frc1124",
         "2016")
     self.assertEqual(status, 'media_exists')
Esempio n. 18
0
    def suggest_team_media(self, request):
        current_user = endpoints.get_current_user()
        if current_user is None:
            return BaseResponse(code=401,
                                message="Unauthorized to make suggestions")
        user_id = PushHelper.user_email_to_id(current_user.email())

        # For now, only allow team media suggestions
        if request.reference_type != "team":
            # Trying to suggest a media for an invalid model type
            return BaseResponse(code=400, message="Bad model type")

        media_dict = MediaParser.partial_media_dict_from_url(
            request.media_url.strip())
        if media_dict is not None:
            existing_media = Media.get_by_id(
                Media.render_key_name(media_dict['media_type_enum'],
                                      media_dict['foreign_key']))
            if existing_media is None \
                    or request.reference_key not in [reference.id() for reference in existing_media.references]:
                media_dict['year'] = request.year
                media_dict['reference_type'] = request.reference_type
                media_dict['reference_key'] = request.reference_key

                # Need to split deletehash out into its own private dict. Don't want that to be exposed via API...
                if request.details_json:
                    incoming_details = json.loads(request.details_json)
                    private_details = None
                    if 'deletehash' in incoming_details:
                        private_details = {
                            'deletehash': incoming_details.pop('deletehash')
                        }

                    media_dict['private_details_json'] = json.dumps(
                        private_details) if private_details else None
                    media_dict['details_json'] = json.dumps(incoming_details)

                suggestion = Suggestion(author=ndb.Key(Account, user_id),
                                        target_model="media")
                suggestion.contents = media_dict
                suggestion.put()

                return BaseResponse(code=200, message="Suggestion added")
            else:
                return BaseResponse(code=304,
                                    message="Suggestion already exists")
        else:
            return BaseResponse(code=400, message="Bad suggestion url")
    def post(self):
        self._require_admin()

        accept_keys = map(int, self.request.POST.getall("accept_keys[]"))
        reject_keys = map(int, self.request.POST.getall("reject_keys[]"))

        accepted_suggestion_futures = [
            Suggestion.get_by_id_async(key) for key in accept_keys
        ]
        rejected_suggestion_futures = [
            Suggestion.get_by_id_async(key) for key in reject_keys
        ]
        accepted_suggestions = map(lambda a: a.get_result(),
                                   accepted_suggestion_futures)
        rejected_suggestions = map(lambda a: a.get_result(),
                                   rejected_suggestion_futures)

        for suggestion in accepted_suggestions:
            media = Media(
                id=Media.render_key_name(
                    suggestion.contents['media_type_enum'],
                    suggestion.contents['foreign_key']),
                foreign_key=suggestion.contents['foreign_key'],
                media_type_enum=suggestion.contents['media_type_enum'],
                details_json=suggestion.contents.get('details_json', None),
                year=int(suggestion.contents['year']),
                references=[
                    Media.create_reference(
                        suggestion.contents['reference_type'],
                        suggestion.contents['reference_key'])
                ],
            )
            MediaManipulator.createOrUpdate(media)

        all_suggestions = accepted_suggestions
        all_suggestions.extend(rejected_suggestions)

        for suggestion in all_suggestions:
            if suggestion.key.id() in accept_keys:
                suggestion.review_state = Suggestion.REVIEW_ACCEPTED
            if suggestion.key.id() in reject_keys:
                suggestion.review_state = Suggestion.REVIEW_REJECTED
            suggestion.reviewer = self.user_bundle.account.key
            suggestion.reviewer_at = datetime.datetime.now()

        ndb.put_multi(all_suggestions)

        self.redirect("/admin/suggestions/media/review")
    def _process_accepted(self, accept_key):
        """
        Performs all actions for an accepted Suggestion in a Transaction.
        Suggestions are processed one at a time (instead of in batch) in a
        Transaction to prevent possible race conditions.

        Actions include:
        - Creating and saving a new Media for the Suggestion
        - Removing a reference from another Media's preferred_references
        - Marking the Suggestion as accepted and saving it
        """
        # Async get
        suggestion_future = Suggestion.get_by_id_async(accept_key)

        # Setup
        to_replace_id = self.request.POST.get('replace-preferred-{}'.format(accept_key), None)

        # Resolve async Futures
        suggestion = suggestion_future.get_result()

        # Make sure Suggestion hasn't been processed (by another thread)
        if suggestion.review_state != Suggestion.REVIEW_PENDING:
            return

        team_reference = Media.create_reference(
            suggestion.contents['reference_type'],
            suggestion.contents['reference_key'])
        media_type_enum = suggestion.contents['media_type_enum']

        media = Media(
            id=Media.render_key_name(suggestion.contents['media_type_enum'], suggestion.contents['foreign_key']),
            foreign_key=suggestion.contents['foreign_key'],
            media_type_enum=media_type_enum,
            details_json=suggestion.contents.get('details_json', None),
            private_details_json=suggestion.contents.get('private_details_json', None),
            year=int(suggestion.contents['year']) if not suggestion.contents.get('is_social', False) else None,
            references=[team_reference],
        )

        # Mark Suggestion as accepted
        suggestion.review_state = Suggestion.REVIEW_ACCEPTED
        suggestion.reviewer = self.user_bundle.account.key
        suggestion.reviewed_at = datetime.datetime.now()

        # Do all DB writes
        MediaManipulator.createOrUpdate(media)
        suggestion.put()
    def createTeamMediaSuggestion(cls, author_account_key, media_url, team_key, year_str, private_details_json=None, is_social=False, default_preferred=False):
        """Create a Team Media Suggestion. Returns status (success, suggestion_exists, media_exists, bad_url)"""

        media_dict = MediaParser.partial_media_dict_from_url(media_url)
        if media_dict is not None:
            if media_dict.get("is_social", False) != is_social:
                return 'bad_url', None

            existing_media = Media.get_by_id(Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']))
            if existing_media is None or team_key not in [reference.id() for reference in existing_media.references]:
                foreign_type = Media.SLUG_NAMES[media_dict['media_type_enum']]
                suggestion_id = Suggestion.render_media_key_name(year_str, 'team', team_key, foreign_type, media_dict['foreign_key'])
                suggestion = Suggestion.get_by_id(suggestion_id)
                if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                    media_dict['year'] = int(year_str) if year_str else None
                    media_dict['reference_type'] = 'team'
                    media_dict['reference_key'] = team_key
                    media_dict['default_preferred'] = default_preferred
                    if private_details_json is not None:
                        media_dict['private_details_json'] = private_details_json

                    target_model = "media"
                    if media_dict.get("is_social", False):
                        target_model = "social-media"

                    if media_dict.get('media_type', '') in MediaType.robot_types:
                        target_model = "robot"

                    if Event.validate_key_name(team_key):
                        target_model = 'event_media'
                        media_dict['reference_type'] = 'event'

                    suggestion = Suggestion(
                        id=suggestion_id,
                        author=author_account_key,
                        target_model=target_model,
                        target_key=team_key,
                        )
                    suggestion.contents = media_dict
                    suggestion.put()
                    return 'success', suggestion
                else:
                    return 'suggestion_exists', None
            else:
                return 'media_exists', None
        else:
            return 'bad_url', None
    def createTeamMediaSuggestion(cls, author_account_key, media_url, team_key, year_str, private_details_json=None, is_social=False):
        """Create a Team Media Suggestion. Returns status (success, suggestion_exists, media_exists, bad_url)"""

        # Sanitize input url
        media_url = media_url.strip()
        parsed = urlparse(media_url)
        media_url = "{}://{}{}".format(parsed.scheme, parsed.netloc, parsed.path)

        media_dict = MediaParser.partial_media_dict_from_url(media_url)
        if media_dict is not None:
            if media_dict.get("is_social", False) != is_social:
                return 'bad_url'

            existing_media = Media.get_by_id(Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']))
            if existing_media is None or team_key not in [reference.id() for reference in existing_media.references]:
                foreign_type = Media.SLUG_NAMES[media_dict['media_type_enum']]
                suggestion_id = Suggestion.render_media_key_name(year_str, 'team', team_key, foreign_type, media_dict['foreign_key'])
                suggestion = Suggestion.get_by_id(suggestion_id)
                if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                    media_dict['year'] = int(year_str) if year_str else None
                    media_dict['reference_type'] = 'team'
                    media_dict['reference_key'] = team_key
                    if private_details_json is not None:
                        media_dict['private_details_json'] = private_details_json

                    target_model = "media"
                    if media_dict.get("is_social", False):
                        target_model = "social-media"

                    suggestion = Suggestion(
                        id=suggestion_id,
                        author=author_account_key,
                        target_model=target_model,
                        )
                    suggestion.contents = media_dict
                    suggestion.put()
                    return 'success'
                else:
                    return 'suggestion_exists'
            else:
                return 'media_exists'
        else:
            return 'bad_url'
Esempio n. 23
0
    def suggest_team_media(self, request):
        current_user = endpoints.get_current_user()
        if current_user is None:
            return BaseResponse(code=401, message="Unauthorized to make suggestions")
        user_id = PushHelper.user_email_to_id(current_user.email())

        # For now, only allow team media suggestions
        if request.reference_type != "team":
            # Trying to suggest a media for an invalid model type
            return BaseResponse(code=400, message="Bad model type")

        media_dict = MediaParser.partial_media_dict_from_url(request.media_url.strip())
        if media_dict is not None:
            existing_media = Media.get_by_id(
                    Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']))
            if existing_media is None \
                    or request.reference_key not in [reference.id() for reference in existing_media.references]:
                media_dict['year'] = request.year
                media_dict['reference_type'] = request.reference_type
                media_dict['reference_key'] = request.reference_key

                # Need to split deletehash out into its own private dict. Don't want that to be exposed via API...
                if request.details_json:
                    incoming_details = json.loads(request.details_json)
                    private_details = None
                    if 'deletehash' in incoming_details:
                        private_details = {'deletehash': incoming_details.pop('deletehash')}

                    media_dict['private_details_json'] = json.dumps(private_details) if private_details else None
                    media_dict['details_json'] = json.dumps(incoming_details)

                suggestion = Suggestion(
                    author=ndb.Key(Account, user_id),
                    target_model="media"
                )
                suggestion.contents = media_dict
                suggestion.put()

                return BaseResponse(code=200, message="Suggestion added")
            else:
                return BaseResponse(code=304, message="Suggestion already exists")
        else:
            return BaseResponse(code=400, message="Bad suggestion url")
Esempio n. 24
0
    def createTeamMediaSuggestion(cls,
                                  author_account_key,
                                  media_url,
                                  team_key,
                                  year_str,
                                  private_details_json=None):
        """Create a Team Media Suggestion. Returns status (success, suggestion_exists, media_exists, bad_url)"""

        media_dict = MediaParser.partial_media_dict_from_url(media_url.strip())
        if media_dict is not None:
            existing_media = Media.get_by_id(
                Media.render_key_name(media_dict['media_type_enum'],
                                      media_dict['foreign_key']))
            if existing_media is None or team_key not in [
                    reference.id() for reference in existing_media.references
            ]:
                foreign_type = Media.SLUG_NAMES[media_dict['media_type_enum']]
                suggestion_id = Suggestion.render_media_key_name(
                    year_str, 'team', team_key, foreign_type,
                    media_dict['foreign_key'])
                suggestion = Suggestion.get_by_id(suggestion_id)
                if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                    media_dict['year'] = int(year_str)
                    media_dict['reference_type'] = 'team'
                    media_dict['reference_key'] = team_key
                    if private_details_json is not None:
                        media_dict[
                            'private_details_json'] = private_details_json

                    suggestion = Suggestion(
                        id=suggestion_id,
                        author=author_account_key,
                        target_model="media",
                    )
                    suggestion.contents = media_dict
                    suggestion.put()
                    return 'success'
                else:
                    return 'suggestion_exists'
            else:
                return 'media_exists'
        else:
            return 'bad_url'
    def createTeamMediaSuggestion(cls, author_account_key, media_url, team_key, year_str):
        """Create a Team Media Suggestion. Returns True on success and False on failure"""

        media_dict = MediaParser.partial_media_dict_from_url(media_url)
        if media_dict is not None:
            existing_media = Media.get_by_id(Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']))
            if existing_media is None or team_key not in [reference.id() for reference in existing_media.references]:
                media_dict['year'] = int(year_str)
                media_dict['reference_type'] = 'team'
                media_dict['reference_key'] = team_key

                suggestion = Suggestion(
                    author=author_account_key,
                    target_model="media",
                    )
                suggestion.contents = media_dict
                suggestion.put()
                return True
        return False
    def post(self):
        self._require_admin()

        accept_keys = map(int, self.request.POST.getall("accept_keys[]"))
        reject_keys = map(int, self.request.POST.getall("reject_keys[]"))

        accepted_suggestion_futures = [Suggestion.get_by_id_async(key) for key in accept_keys]
        rejected_suggestion_futures = [Suggestion.get_by_id_async(key) for key in reject_keys]
        accepted_suggestions = map(lambda a: a.get_result(), accepted_suggestion_futures)
        rejected_suggestions = map(lambda a: a.get_result(), rejected_suggestion_futures)

        for suggestion in accepted_suggestions:
            media = Media(
                id=Media.render_key_name(suggestion.contents['media_type_enum'], suggestion.contents['foreign_key']),
                foreign_key=suggestion.contents['foreign_key'],
                media_type_enum=suggestion.contents['media_type_enum'],
                details_json=suggestion.contents.get('details_json', None),
                year=int(suggestion.contents['year']),
                references=[Media.create_reference(
                    suggestion.contents['reference_type'],
                    suggestion.contents['reference_key'])],
            )
            MediaManipulator.createOrUpdate(media)

        all_suggestions = accepted_suggestions
        all_suggestions.extend(rejected_suggestions)

        for suggestion in all_suggestions:
            if suggestion.key.id() in accept_keys:
                suggestion.review_state = Suggestion.REVIEW_ACCEPTED
            if suggestion.key.id() in reject_keys:
                suggestion.review_state = Suggestion.REVIEW_REJECTED
            suggestion.reviewer = self.user_bundle.account.key
            suggestion.reviewer_at = datetime.datetime.now()

        ndb.put_multi(all_suggestions)

        self.redirect("/admin/suggestions/media/review")
Esempio n. 27
0
    def createTeamMediaSuggestion(cls, author_account_key, media_url, team_key,
                                  year_str):
        """Create a Team Media Suggestion. Returns True on success and False on failure"""

        media_dict = MediaParser.partial_media_dict_from_url(media_url)
        if media_dict is not None:
            existing_media = Media.get_by_id(
                Media.render_key_name(media_dict['media_type_enum'],
                                      media_dict['foreign_key']))
            if existing_media is None or team_key not in [
                    reference.id() for reference in existing_media.references
            ]:
                media_dict['year'] = int(year_str)
                media_dict['reference_type'] = 'team'
                media_dict['reference_key'] = team_key

                suggestion = Suggestion(
                    author=author_account_key,
                    target_model="media",
                )
                suggestion.contents = media_dict
                suggestion.put()
                return True
        return False
    def post(self):
        self._require_login()

        team_key = self.request.get("team_key")
        year_str = self.request.get("year")

        success_code = 0
        media_dict = MediaParser.partial_media_dict_from_url(self.request.get('media_url').strip())
        if media_dict is not None:
            existing_media = Media.get_by_id(Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']))
            if existing_media is None or team_key not in [reference.id() for reference in existing_media.references]:
                media_dict['year'] = int(year_str)
                media_dict['reference_type'] = 'team'
                media_dict['reference_key'] = team_key

                suggestion = Suggestion(
                    author=self.user_bundle.account.key,
                    target_model="media",
                    )
                suggestion.contents = media_dict
                suggestion.put()
            success_code = 1

        self.redirect('/suggest/team/media?team_key=%s&year=%s&success=%s' % (team_key, year_str, success_code))
    def post(self):
        self._require_admin()

        media_dict = MediaParser.partial_media_dict_from_url(self.request.get('media_url').strip())
        if media_dict is not None:
            year_str = self.request.get('year')
            if year_str == '':
                year = None
            else:
                year = int(year_str.strip())

            media = Media(
                id=Media.render_key_name(media_dict['media_type_enum'], media_dict['foreign_key']),
                foreign_key=media_dict['foreign_key'],
                media_type_enum=media_dict['media_type_enum'],
                details_json=media_dict.get('details_json', None),
                year=year,
                references=[Media.create_reference(
                    self.request.get('reference_type'),
                    self.request.get('reference_key'))],
            )
            MediaManipulator.createOrUpdate(media)

        self.redirect(self.request.get('originating_url'))
    def _process_accepted(self, accept_key, preferred_keys):
        """
        Performs all actions for an accepted Suggestion in a Transaction.
        Suggestions are processed one at a time (instead of in batch) in a
        Transaction to prevent possible race conditions.

        Actions include:
        - Creating and saving a new Media for the Suggestion
        - Removing a reference from another Media's preferred_references
        - Marking the Suggestion as accepted and saving it
        """
        # Async get
        suggestion_future = Suggestion.get_by_id_async(accept_key)

        # Setup
        to_replace_id = self.request.POST.get('replace-preferred-{}'.format(accept_key), None)

        # Resolve async Futures
        suggestion = suggestion_future.get_result()

        # Make sure Suggestion hasn't been processed (by another thread)
        if suggestion.review_state != Suggestion.REVIEW_PENDING:
            return

        # Remove preferred reference from another Media if specified
        team_reference = Media.create_reference(
            suggestion.contents['reference_type'],
            suggestion.contents['reference_key'])
        if to_replace_id:
            to_replace = Media.get_by_id(to_replace_id)
            if team_reference not in to_replace.preferred_references:
                return  # Preferred reference must have been edited earlier. Skip this Suggestion for now.
            to_replace.preferred_references.remove(team_reference)

        # Add preferred reference to current Media (images only) if explicitly listed in preferred_keys or if to_replace_id exists
        media_type_enum = suggestion.contents['media_type_enum']
        preferred_references = []
        if media_type_enum in MediaType.image_types and ('preferred::{}'.format(suggestion.key.id()) in preferred_keys or to_replace_id):
            preferred_references = [team_reference]

        media = Media(
            id=Media.render_key_name(suggestion.contents['media_type_enum'], suggestion.contents['foreign_key']),
            foreign_key=suggestion.contents['foreign_key'],
            media_type_enum=media_type_enum,
            details_json=suggestion.contents.get('details_json', None),
            private_details_json=suggestion.contents.get('private_details_json', None),
            year=int(suggestion.contents['year']),
            references=[team_reference],
            preferred_references=preferred_references,
        )

        # Mark Suggestion as accepted
        suggestion.review_state = Suggestion.REVIEW_ACCEPTED
        suggestion.reviewer = self.user_bundle.account.key
        suggestion.reviewed_at = datetime.datetime.now()

        # Do all DB writes
        if to_replace_id:
            MediaManipulator.createOrUpdate(to_replace, auto_union=False)
        MediaManipulator.createOrUpdate(media)
        suggestion.put()