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 createApiWriteSuggestion(cls, author_account_key, event_key,
                                 affiliation, auth_types):
        """
        Create a suggestion for auth keys request.
        Returns status (success, no_affiliation, bad_event)
        """
        if not affiliation:
            return 'no_affiliation'

        if event_key is not None:
            event = Event.get_by_id(event_key)
            if event and event.event_type_enum not in EventType.SEASON_EVENT_TYPES:
                suggestion = Suggestion(author=author_account_key,
                                        target_model="api_auth_access")
                auth_types = [int(type) for type in auth_types]
                clean_auth_types = filter(
                    lambda a: a in AuthType.type_names.keys(), auth_types)
                suggestion.contents = {
                    'event_key': event_key,
                    'affiliation': affiliation,
                    'auth_types': clean_auth_types,
                }
                suggestion.put()
                return 'success'
            else:
                return 'bad_event'
        else:
            return 'bad_event'
    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))
Exemplo n.º 4
0
    def createApiWriteSuggestion(cls, author_account_key, event_key, affiliation, auth_types):
        """
        Create a suggestion for auth keys request.
        Returns status (success, no_affiliation, bad_event)
        """
        if not affiliation:
            return 'no_affiliation'

        if event_key:
            event = Event.get_by_id(event_key)
            if event:
                suggestion = Suggestion(
                    author=author_account_key,
                    target_model="api_auth_access",
                    target_key=event_key,
                )
                auth_types = [int(type) for type in auth_types]
                clean_auth_types = filter(lambda a: a in AuthType.write_type_names.keys(), auth_types)

                # If we're requesting keys for an official event, filter out everything but videos
                # Admin can still override this at review time, but it's unlikely
                if event.event_type_enum in EventType.SEASON_EVENT_TYPES:
                    clean_auth_types = filter(lambda a: a == AuthType.MATCH_VIDEO, clean_auth_types)

                suggestion.contents = {
                    'event_key': event_key,
                    'affiliation': affiliation,
                    'auth_types': clean_auth_types,
                }
                suggestion.put()
                return 'success'
            else:
                return 'bad_event'
        else:
            return 'bad_event'
    def post(self):
        self._require_login()

        match_key = self.request.get("match_key")
        youtube_url = self.request.get("youtube_url")

        youtube_id = None
        regex1 = re.match(r".*youtu\.be\/(.*)", youtube_url)
        if regex1 is not None:
            youtube_id = regex1.group(1)
        else:
            regex2 = re.match(r".*v=([a-zA-Z0-9_-]*)", youtube_url)
            if regex2 is not None:
                youtube_id = regex2.group(1)

        if youtube_id is not None:
            suggestion = Suggestion(
                author=self.user_bundle.account.key,
                target_key=match_key,
                target_model="match",
            )
            suggestion.contents = {"youtube_videos": [youtube_id]}
            suggestion.put()

        self.redirect('/suggest/match/video?match_key=%s&success=1' %
                      match_key)
 def createMatchVideoYouTubeSuggestion(cls, author_account_key, youtube_id,
                                       match_key):
     """Create a YouTube Match Video. Returns status (success, suggestion_exists, video_exists, bad_url)"""
     if youtube_id:
         match = Match.get_by_id(match_key)
         if not match:
             return 'bad_match'
         if youtube_id not in match.youtube_videos:
             year = match_key[:4]
             suggestion_id = Suggestion.render_media_key_name(
                 year, 'match', match_key, 'youtube', youtube_id)
             suggestion = Suggestion.get_by_id(suggestion_id)
             if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                 suggestion = Suggestion(
                     id=suggestion_id,
                     author=author_account_key,
                     target_key=match_key,
                     target_model="match",
                 )
                 suggestion.contents = {"youtube_videos": [youtube_id]}
                 suggestion.put()
                 return 'success'
             else:
                 return 'suggestion_exists'
         else:
             return 'video_exists'
     else:
         return 'bad_url'
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
 def createMatchVideoYouTubeSuggestion(cls, author_account_key, youtube_id, match_key):
     """Create a YouTube Match Video. Returns status (success, suggestion_exists, video_exists, bad_url)"""
     if youtube_id:
         match = Match.get_by_id(match_key)
         if not match:
             return 'bad_match'
         if youtube_id not in match.youtube_videos:
             year = match_key[:4]
             suggestion_id = Suggestion.render_media_key_name(year, 'match', match_key, 'youtube', youtube_id)
             suggestion = Suggestion.get_by_id(suggestion_id)
             if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                 suggestion = Suggestion(
                     id=suggestion_id,
                     author=author_account_key,
                     target_key=match_key,
                     target_model="match",
                     )
                 suggestion.contents = {"youtube_videos": [youtube_id]}
                 suggestion.put()
                 return 'success'
             else:
                 return 'suggestion_exists'
         else:
             return 'video_exists'
     else:
         return 'bad_url'
 def createMatchVideoInternetArchiveSuggestion(cls, author_account_key,
                                               archive_id, match_key):
     if archive_id:
         match = Match.get_by_id(match_key)
         if not match:
             return 'bad_match'
         if archive_id not in match.internet_archive_videos:
             year = match_key[:4]
             suggestion_id = Suggestion.render_media_key_name(
                 year, 'match', match_key, 'internet_archive', archive_id)
             suggestion = Suggestion.get_by_id(suggestion_id)
             if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                 suggestion = Suggestion(
                     id=suggestion_id,
                     author=author_account_key,
                     target_key=match_key,
                     target_model="match",
                 )
                 suggestion.contents = {
                     "internet_archive_videos": [archive_id]
                 }
                 suggestion.put()
                 return 'success'
             else:
                 return 'suggestion_exists'
         else:
             return 'video_exists'
     else:
         return 'bad_url'
    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'
    def post(self):
        self._require_login()

        match_key = self.request.get("match_key")
        youtube_url = self.request.get("youtube_url")

        youtube_id = None
        regex1 = re.match(r".*youtu\.be\/(.*)", youtube_url)
        if regex1 is not None:
            youtube_id = regex1.group(1)
        else:
            regex2 = re.match(r".*v=([a-zA-Z0-9_-]*)", youtube_url)
            if regex2 is not None:
                youtube_id = regex2.group(1)

        if youtube_id is not None:
            suggestion = Suggestion(
                author=self.user_bundle.account.key,
                target_key=match_key,
                target_model="match",
                )
            suggestion.contents = {"youtube_videos": [youtube_id]}
            suggestion.put()

        self.redirect('/suggest/match/video?match_key=%s&success=1' % match_key)
    def createApiWriteSuggestion(cls, author_account_key, event_key, affiliation, auth_types):
        """
        Create a suggestion for auth keys request.
        Returns status (success, no_affiliation, bad_event)
        """
        if not affiliation:
            return 'no_affiliation'

        if event_key:
            event = Event.get_by_id(event_key)
            if event:
                suggestion = Suggestion(
                    author=author_account_key,
                    target_model="api_auth_access",
                    target_key=event_key,
                )
                auth_types = [int(type) for type in auth_types]
                clean_auth_types = filter(lambda a: a in AuthType.write_type_names.keys(), auth_types)

                # If we're requesting keys for an official event, filter out everything but videos
                # Admin can still override this at review time, but it's unlikely
                if event.event_type_enum in EventType.SEASON_EVENT_TYPES:
                    clean_auth_types = filter(lambda a: a == AuthType.MATCH_VIDEO, clean_auth_types)

                suggestion.contents = {
                    'event_key': event_key,
                    'affiliation': affiliation,
                    'auth_types': clean_auth_types,
                }
                suggestion.put()
                return 'success'
            else:
                return 'bad_event'
        else:
            return 'bad_event'
Exemplo n.º 13
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
    def createMatchVideoSuggestion(self):
        user_bundle = UserBundle()
        match = Match.query().fetch(1)[0]  #probably a cleaner way to do this

        suggestion = Suggestion(author=user_bundle.account.key,
                                target_key=match.key_name,
                                target_model="match")
        suggestion.contents = {"youtube_videos": [self.YOUTUBE_ID]}
        suggestion.put()
Exemplo n.º 15
0
    def createOffseasonEventSuggestion(cls, author_account_key, name,
                                       start_date, end_date, website, address):
        """
        Create a suggestion for offseason event. Returns (status, failures):
        ('success', None)
        ('validation_failure', failures)
        """
        failures = {}
        if not name:
            failures['name'] = "Missing event name"
        if not start_date:
            failures['start_date'] = "Missing start date"
        if not end_date:
            failures['end_date'] = "Missing end date"
        if not website:
            failures['website'] = "Missing website"
        if not address:
            failures['venue_address'] = "Missing address"

        start_datetime = None
        end_datetime = None
        if start_date:
            try:
                start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
            except ValueError:
                failures[
                    'start_date'] = "Invalid start date format (year-month-date)"

        if end_date:
            try:
                end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
            except ValueError:
                failures[
                    'end_date'] = "Invalid end date format (year-month-date)"

        if start_datetime and end_datetime and end_datetime < start_datetime:
            failures['end_date'] = "End date must not be before the start date"

        if failures:
            return 'validation_failure', failures

        # Note that we don't specify an explicit key for event suggestions
        # We don't trust users to input correct event keys (that's for the moderator to do)
        suggestion = Suggestion(
            author=author_account_key,
            target_model="offseason-event",
        )
        suggestion.contents = {
            'name': name,
            'start_date': start_date,
            'end_date': end_date,
            'website': website,
            'address': address
        }
        suggestion.put()
        return 'success', None
    def createMatchVideoSuggestion(self):
        user_bundle = UserBundle()
        match = Match.query().fetch(1)[0] #probably a cleaner way to do this

        suggestion = Suggestion(
            author=user_bundle.account.key,
            target_key=match.key_name,
            target_model="match")
        suggestion.contents = {"youtube_videos": [self.YOUTUBE_ID]}
        suggestion.put()
class TestMatchSuggestionAccepter(unittest2.TestCase):
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        ndb.get_context().clear_cache(
        )  # Prevent data from leaking between tests

        self.testbed.init_taskqueue_stub(root_path=".")

        self.account = Account(email="*****@*****.**", )
        self.account.put()

        self.suggestion = Suggestion(
            author=self.account.key,
            contents_json="{\"youtube_videos\":[\"123456\"]}",
            target_key="2012ct_qm1",
            target_model="match")
        self.suggestion.put()

        self.event = Event(
            id="2012ct",
            event_short="ct",
            year=2012,
            event_type_enum=EventType.REGIONAL,
        )
        self.event.put()

        self.match = Match(
            id="2012ct_qm1",
            alliances_json=
            """{"blue": {"score": -1, "teams": ["frc3464", "frc20", "frc1073"]}, "red": {"score": -1, "teams": ["frc69", "frc571", "frc176"]}}""",
            comp_level="qm",
            event=self.event.key,
            year=2012,
            set_number=1,
            match_number=1,
            team_key_names=[
                u'frc69', u'frc571', u'frc176', u'frc3464', u'frc20',
                u'frc1073'
            ],
            youtube_videos=["abcdef"])
        self.match.put()

    def tearDown(self):
        self.testbed.deactivate()

    def test_accept_suggestions(self):
        MatchSuggestionAccepter.accept_suggestions([self.suggestion])

        match = Match.get_by_id("2012ct_qm1")
        self.assertTrue("abcdef" in match.youtube_videos)
        self.assertTrue("123456" in match.youtube_videos)
    def createEventWebcastSuggestion(self):
        user_bundle = UserBundle()
        event = Event.query().fetch(1)[0]

        suggestion = Suggestion(
            author=user_bundle.account.key,
            target_key=event.key_name,
            target_model="event",
            )
        suggestion.contents = {"webcast_url": self.YOUTUBE_URL}
        suggestion.put()
    def createEventWebcastSuggestion(self):
        user_bundle = UserBundle()
        event = Event.query().fetch(1)[0]

        suggestion = Suggestion(
            author=user_bundle.account.key,
            target_key=event.key_name,
            target_model="event",
        )
        suggestion.contents = {"webcast_url": self.YOUTUBE_URL}
        suggestion.put()
class TestMatchSuggestionAccepter(unittest2.TestCase):
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        ndb.get_context().clear_cache()  # Prevent data from leaking between tests

        self.testbed.init_taskqueue_stub(root_path=".")

        self.account = Account(
            email="*****@*****.**",
        )
        self.account.put()

        self.suggestion = Suggestion(
            author=self.account.key,
            contents_json="{\"youtube_videos\":[\"123456\"]}",
            target_key="2012ct_qm1",
            target_model="match"
        )
        self.suggestion.put()

        self.event = Event(
          id="2012ct",
          event_short="ct",
          year=2012,
          event_type_enum=EventType.REGIONAL,
        )
        self.event.put()

        self.match = Match(
            id="2012ct_qm1",
            alliances_json="""{"blue": {"score": -1, "teams": ["frc3464", "frc20", "frc1073"]}, "red": {"score": -1, "teams": ["frc69", "frc571", "frc176"]}}""",
            comp_level="qm",
            event=self.event.key,
            year=2012,
            set_number=1,
            match_number=1,
            team_key_names=[u'frc69', u'frc571', u'frc176', u'frc3464', u'frc20', u'frc1073'],
            youtube_videos=["abcdef"]
        )
        self.match.put()

    def tearDown(self):
        self.testbed.deactivate()

    def test_accept_suggestions(self):
        MatchSuggestionAccepter.accept_suggestion(self.match, self.suggestion)

        match = Match.get_by_id("2012ct_qm1")
        self.assertTrue("abcdef" in match.youtube_videos)
        self.assertTrue("123456" in match.youtube_videos)
    def createOffseasonEventSuggestion(cls, author_account_key, name, start_date, end_date, website, address):
        """
        Create a suggestion for offseason event. Returns (status, failures):
        ('success', None)
        ('validation_failure', failures)
        """
        failures = {}
        if not name:
            failures['name'] = "Missing event name"
        if not start_date:
            failures['start_date'] = "Missing start date"
        if not end_date:
            failures['end_date'] = "Missing end date"
        if not website:
            failures['website'] = "Missing website"
        if not address:
            failures['venue_address'] = "Missing address"

        start_datetime = None
        end_datetime = None
        if start_date:
            try:
                start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
            except ValueError:
                failures['start_date'] = "Invalid start date format (year-month-date)"

        if end_date:
            try:
                end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
            except ValueError:
                failures['end_date'] = "Invalid end date format (year-month-date)"

        if start_datetime and end_datetime and end_datetime < start_datetime:
            failures['end_date'] = "End date must not be before the start date"

        if failures:
            return 'validation_failure', failures

        # Note that we don't specify an explicit key for event suggestions
        # We don't trust users to input correct event keys (that's for the moderator to do)
        suggestion = Suggestion(
            author=author_account_key,
            target_model="offseason-event",
        )
        suggestion.contents = {
            'name': name,
            'start_date': start_date,
            'end_date': end_date,
            'website': website,
            'address': address}
        suggestion.put()
        return 'success', None
Exemplo n.º 22
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")
Exemplo n.º 23
0
    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 post(self):
        self._require_login()

        event_key = self.request.get("event_key")
        webcast_url = self.request.get("webcast_url")

        if not webcast_url:
            self.redirect('/suggest/event/webcast?event_key=%s&result=blank_webcast' % event_key, abort=True)

        suggestion = Suggestion(
            author=self.user_bundle.account.key,
            target_key=event_key,
            target_model="event",
            )
        suggestion.contents = {"webcast_url": webcast_url}
        suggestion.put()

        self.redirect('/suggest/event/webcast?event_key=%s&result=success' % event_key)
    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
Exemplo n.º 26
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)"""

        # 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'
Exemplo n.º 27
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'
Exemplo n.º 28
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_login()

        match_key = self.request.get("match_key")
        match_future = Match.get_by_id_async(self.request.get("match_key"))
        youtube_url = self.request.get("youtube_url")

        youtube_id = None
        regex1 = re.match(r".*youtu\.be\/(.*)", youtube_url)
        if regex1 is not None:
            youtube_id = regex1.group(1)
        else:
            regex2 = re.match(r".*v=([a-zA-Z0-9_-]*)", youtube_url)
            if regex2 is not None:
                youtube_id = regex2.group(1)

        if youtube_id is not None:
            if youtube_id not in match_future.get_result().youtube_videos:
                year = match_key[:4]
                suggestion_id = Suggestion.render_media_key_name(year, 'match', match_key, 'youtube', youtube_id)
                suggestion = Suggestion.get_by_id(suggestion_id)
                if not suggestion or suggestion.review_state != Suggestion.REVIEW_PENDING:
                    suggestion = Suggestion(
                        id=suggestion_id,
                        author=self.user_bundle.account.key,
                        target_key=match_key,
                        target_model="match",
                        )
                    suggestion.contents = {"youtube_videos": [youtube_id]}
                    suggestion.put()
                    status = 'success'
                else:
                    status = 'suggestion_exists'
            else:
                status = 'video_exists'
        else:
            status = 'bad_url'

        self.redirect('/suggest/match/video?match_key={}&status={}'.format(match_key, status))
    def post(self):
        self._require_login()

        event_key = self.request.get("event_key")
        webcast_url = self.request.get("webcast_url")

        if not webcast_url:
            self.redirect(
                '/suggest/event/webcast?event_key=%s&result=blank_webcast' %
                event_key,
                abort=True)

        suggestion = Suggestion(
            author=self.user_bundle.account.key,
            target_key=event_key,
            target_model="event",
        )
        suggestion.contents = {"webcast_url": webcast_url}
        suggestion.put()

        self.redirect('/suggest/event/webcast?event_key=%s&result=success' %
                      event_key)
    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))
Exemplo n.º 32
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
Exemplo n.º 33
0
    def createOffseasonEventSuggestion(cls,
                                       author_account_key,
                                       name,
                                       start_date,
                                       end_date,
                                       website,
                                       venue_name,
                                       address,
                                       city,
                                       state,
                                       country,
                                       first_code=None,
                                       suggestion_id=None):
        """
        Create a suggestion for offseason event. Returns (status, failures):
        ('success', None)
        ('validation_failure', failures)
        """
        failures = {}
        if not name:
            failures['name'] = "Missing event name"
        if not start_date:
            failures['start_date'] = "Missing start date"
        if not end_date:
            failures['end_date'] = "Missing end date"
        if not website:
            failures['website'] = "Missing website"
        if not address:
            failures['venue_address'] = "Missing address"
        if not venue_name:
            failures['venue_name'] = "Missing venue name"
        if not city:
            failures['venue_city'] = "Missing city"
        if not state:
            failures['venue_state'] = "Missing state"
        if not country:
            failures['venue_country'] = "Missing country"

        start_datetime = None
        end_datetime = None
        if start_date:
            try:
                start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
            except ValueError:
                failures[
                    'start_date'] = "Invalid start date format (year-month-date)"

        if end_date:
            try:
                end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
            except ValueError:
                failures[
                    'end_date'] = "Invalid end date format (year-month-date)"

        if start_datetime and end_datetime and end_datetime < start_datetime:
            failures['end_date'] = "End date must not be before the start date"

        if failures and not suggestion_id:
            # Be more lenient with auto-added suggestions
            return 'validation_failure', failures

        # Note that we don't typically specify an explicit key for event suggestions
        # We don't trust users to input correct event keys (that's for the moderator to do)
        suggestion = Suggestion(
            id=suggestion_id) if suggestion_id else Suggestion()
        suggestion.author = author_account_key
        suggestion.target_model = "offseason-event"
        suggestion.contents = {
            'name': name,
            'start_date': start_date,
            'end_date': end_date,
            'website': website,
            'venue_name': venue_name,
            'address': address,
            'city': city,
            'state': state,
            'country': country,
            'first_code': first_code,
        }
        suggestion.put()
        return 'success', None
Exemplo n.º 34
0
    def createOffseasonEventSuggestion(cls, author_account_key, name, start_date, end_date, website, venue_name, address, city, state, country, first_code=None, suggestion_id=None):
        """
        Create a suggestion for offseason event. Returns (status, failures):
        ('success', None)
        ('validation_failure', failures)
        """
        failures = {}
        if not name:
            failures['name'] = "Missing event name"
        if not start_date:
            failures['start_date'] = "Missing start date"
        if not end_date:
            failures['end_date'] = "Missing end date"
        if not website:
            failures['website'] = "Missing website"
        if not address:
            failures['venue_address'] = "Missing address"
        if not venue_name:
            failures['venue_name'] = "Missing venue name"
        if not city:
            failures['venue_city'] = "Missing city"
        if not state:
            failures['venue_state'] = "Missing state"
        if not country:
            failures['venue_country'] = "Missing country"

        start_datetime = None
        end_datetime = None
        if start_date:
            try:
                start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
            except ValueError:
                failures['start_date'] = "Invalid start date format (year-month-date)"

        if end_date:
            try:
                end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
            except ValueError:
                failures['end_date'] = "Invalid end date format (year-month-date)"

        if start_datetime and end_datetime and end_datetime < start_datetime:
            failures['end_date'] = "End date must not be before the start date"

        if failures and not suggestion_id:
            # Be more lenient with auto-added suggestions
            return 'validation_failure', failures

        # Note that we don't typically specify an explicit key for event suggestions
        # We don't trust users to input correct event keys (that's for the moderator to do)
        suggestion = Suggestion(id=suggestion_id) if suggestion_id else Suggestion()
        suggestion.author=author_account_key
        suggestion.target_model="offseason-event"
        suggestion.contents = {
            'name': name,
            'start_date': start_date,
            'end_date': end_date,
            'website': website,
            'venue_name': venue_name,
            'address': address,
            'city': city,
            'state': state,
            'country': country,
            'first_code': first_code,
        }
        suggestion.put()
        return 'success', None