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 post(self):
        team_number = self.request.get("team_number")
        if not team_number:
            self.abort(400)
        team_number = int(team_number)
        self._require_team_admin_access(team_number)

        media_key_name = self.request.get("media_key_name")
        media = Media.get_by_id(media_key_name)
        if not media:
            self.abort(400)
        team_ref = Media.create_reference('team', 'frc{}'.format(team_number))
        action = self.request.get('action')
        if action == "remove_media_reference":
            if team_ref in media.references:
                media.references.remove(team_ref)
            if team_ref in media.preferred_references:
                media.preferred_references.remove(team_ref)
        elif action == "remove_media_preferred":
            if team_ref in media.preferred_references:
                media.preferred_references.remove(team_ref)
        elif action == "add_media_preferred":
            if team_ref not in media.preferred_references:
                media.preferred_references.append(team_ref)
        else:
            self.abort(400)

        MediaManipulator.createOrUpdate(media, auto_union=False)
        self.redirect('/mod/')
    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)

        # 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 = MediaCreator.create_media(suggestion, 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 create_target_model(self, suggestion):
        # Setup
        to_replace = None
        to_replace_id = self.request.POST.get("replace-preferred-{}".format(suggestion.key.id()), None)

        # 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 self.preferred_keys or to_replace_id
        ):
            preferred_references = [team_reference]

        media = MediaCreator.create_media_model(suggestion, team_reference, 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:
            MediaManipulator.createOrUpdate(to_replace, auto_union=False)
        MediaManipulator.createOrUpdate(media)
Exemplo n.º 5
0
    def create_target_model(self, suggestion):
        # Setup
        to_replace = None
        to_replace_id = self.request.POST.get('replace-preferred-{}'.format(suggestion.key.id()), None)
        year = int(self.request.POST.get('year-{}'.format(suggestion.key.id())))

        # Override year if necessary
        suggestion.contents['year'] = year
        suggestion.contents_json = json.dumps(suggestion.contents)
        suggestion._contents = None

        # 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:
                # Preferred reference must have been edited earlier. Skip this Suggestion for now.
                return
            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 self.preferred_keys or to_replace_id):
            preferred_references = [team_reference]

        media = MediaCreator.create_media_model(suggestion, team_reference, preferred_references)

        # Do all DB writes
        if to_replace:
            MediaManipulator.createOrUpdate(to_replace, auto_union=False)
        return MediaManipulator.createOrUpdate(media)
    def create_target_model(self, suggestion):
        # Setup
        to_replace = None
        to_replace_id = self.request.POST.get('replace-preferred-{}'.format(suggestion.key.id()), None)
        year = int(self.request.POST.get('year-{}'.format(suggestion.key.id())))

        # Override year if necessary
        suggestion.contents['year'] = year
        suggestion.contents_json = json.dumps(suggestion.contents)
        suggestion._contents = None

        # 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:
                # Preferred reference must have been edited earlier. Skip this Suggestion for now.
                return
            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 self.preferred_keys or to_replace_id):
            preferred_references = [team_reference]

        media = MediaCreator.create_media_model(suggestion, team_reference, preferred_references)

        # Do all DB writes
        if to_replace:
            MediaManipulator.createOrUpdate(to_replace, auto_union=False)
        return MediaManipulator.createOrUpdate(media)
    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'))
Exemplo n.º 8
0
    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)

        # 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 = MediaCreator.create_media(suggestion, 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()
Exemplo n.º 9
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 post(self, media_key_name):
        self._require_admin()

        media = Media.get_by_id(media_key_name)

        media.preferred_references.remove(
            media.create_reference(self.request.get("reference_type"),
                                   self.request.get("reference_key_name")))

        MediaManipulator.createOrUpdate(media, auto_union=False)

        self.redirect(self.request.get('originating_url'))
    def post(self, media_key_name):
        self._require_admin()

        media = Media.get_by_id(media_key_name)

        media.preferred_references.remove(media.create_reference(
            self.request.get("reference_type"),
            self.request.get("reference_key_name")))

        MediaManipulator.createOrUpdate(media, auto_union=False)

        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 = MediaCreator.create_media(suggestion, team_reference, 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()
    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")
Exemplo n.º 14
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 from_suggestion(cls, suggestion):
        team_reference = Media.create_reference(
            suggestion.contents['reference_type'],
            suggestion.contents['reference_key'])

        media = MediaCreator.create_media_model(suggestion, team_reference)
        return MediaManipulator.createOrUpdate(media)
Exemplo n.º 16
0
    def from_suggestion(cls, suggestion):
        team_reference = Media.create_reference(
            suggestion.contents['reference_type'],
            suggestion.contents['reference_key'])

        media = MediaCreator.create_media_model(suggestion, team_reference)
        return MediaManipulator.createOrUpdate(media)
    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()
Exemplo n.º 18
0
    def get(self, key_name):
        fms_df = DatafeedFMSAPI('v2.0')
        year = datetime.date.today().year
        team = Team.get_by_id(key_name)

        avatar, keys_to_delete = fms_df.getTeamAvatar(year, key_name)

        if avatar:
            MediaManipulator.createOrUpdate(avatar)

        MediaManipulator.delete_keys(keys_to_delete)

        template_values = {
            'key_name': key_name,
            'team': team,
            'success': avatar is not None,
        }

        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/usfirst_team_avatar_get.html')
            self.response.out.write(template.render(path, template_values))
Exemplo n.º 19
0
    def create_target_model(self, suggestion):
        # Setup

        # Remove preferred reference from another Media if specified
        event_reference = Media.create_reference(
            suggestion.contents['reference_type'],
            suggestion.contents['reference_key'])

        media = MediaCreator.create_media_model(suggestion, event_reference, [])

        # Do all DB writes
        return MediaManipulator.createOrUpdate(media)
    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")
Exemplo n.º 21
0
    def get(self, key_name):
        fms_df = DatafeedFMSAPI('v2.0')
        year = datetime.date.today().year
        team = Team.get_by_id(key_name)

        avatar, keys_to_delete = fms_df.getTeamAvatar(year, key_name)

        if avatar:
            MediaManipulator.createOrUpdate(avatar)

        MediaManipulator.delete_keys(keys_to_delete)

        template_values = {
            'key_name': key_name,
            'team': team,
            'success': avatar is not None,
        }

        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/usfirst_team_avatar_get.html')
            self.response.out.write(template.render(path, template_values))
    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'))
Exemplo n.º 23
0
    def create_target_model(self, suggestion):
        # Setup
        to_replace = None
        to_replace_id = self.request.POST.get(
            'replace-preferred-{}'.format(suggestion.key.id()), None)

        # 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 self.preferred_keys or to_replace_id):
            preferred_references = [team_reference]

        media = MediaCreator.create_media_model(suggestion, team_reference,
                                                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:
            MediaManipulator.createOrUpdate(to_replace, auto_union=False)
        MediaManipulator.createOrUpdate(media)
    def post(self):
        team_number = self.request.get("team_number")
        if not team_number:
            self.abort(400)
        team_number = int(team_number)
        team = Team.get_by_id("frc{}".format(team_number))
        if not team:
            self.abort(400)
        self._require_team_admin_access(team_number)

        action = self.request.get('action')
        if action == "remove_media_reference":
            media, team_ref = self.get_media_and_team_ref(team_number)
            if team_ref in media.references:
                media.references.remove(team_ref)
            if team_ref in media.preferred_references:
                media.preferred_references.remove(team_ref)
            MediaManipulator.createOrUpdate(media, auto_union=False)
        elif action == "remove_media_preferred":
            media, team_ref = self.get_media_and_team_ref(team_number)
            if team_ref in media.preferred_references:
                media.preferred_references.remove(team_ref)
            MediaManipulator.createOrUpdate(media, auto_union=False)
        elif action == "add_media_preferred":
            media, team_ref = self.get_media_and_team_ref(team_number)
            if team_ref not in media.preferred_references:
                media.preferred_references.append(team_ref)
            MediaManipulator.createOrUpdate(media, auto_union=False)
        elif action == "set_team_info":
            robot_name = self.request.get("robot_name").strip()
            current_year = datetime.datetime.now().year
            robot_key = Robot.renderKeyName(team.key_name, current_year)
            if robot_name:
                robot = Robot(
                    id=robot_key,
                    team=team.key,
                    year=current_year,
                    robot_name=robot_name,
                )
                RobotManipulator.createOrUpdate(robot)
            else:
                RobotManipulator.delete_keys([ndb.Key(Robot, robot_key)])
        else:
            self.abort(400)

        self.redirect('/mod/')
Exemplo n.º 25
0
    def post(self):
        team_number = self.request.get("team_number")
        if not team_number:
            self.abort(400)
        team_number = int(team_number)
        team = Team.get_by_id("frc{}".format(team_number))
        if not team:
            self.abort(400)
        self._require_team_admin_access(team_number)

        action = self.request.get('action')
        if action == "remove_media_reference":
            media, team_ref = self.get_media_and_team_ref(team_number)
            if team_ref in media.references:
                media.references.remove(team_ref)
            if team_ref in media.preferred_references:
                media.preferred_references.remove(team_ref)
            MediaManipulator.createOrUpdate(media, auto_union=False)
        elif action == "remove_media_preferred":
            media, team_ref = self.get_media_and_team_ref(team_number)
            if team_ref in media.preferred_references:
                media.preferred_references.remove(team_ref)
            MediaManipulator.createOrUpdate(media, auto_union=False)
        elif action == "add_media_preferred":
            media, team_ref = self.get_media_and_team_ref(team_number)
            if team_ref not in media.preferred_references:
                media.preferred_references.append(team_ref)
            MediaManipulator.createOrUpdate(media, auto_union=False)
        elif action == "set_team_info":
            robot_name = self.request.get("robot_name").strip()
            current_year = datetime.datetime.now().year
            robot_key = Robot.renderKeyName(team.key_name, current_year)
            if robot_name:
                robot = Robot(
                    id=robot_key,
                    team=team.key,
                    year=current_year,
                    robot_name=robot_name,
                )
                RobotManipulator.createOrUpdate(robot)
            else:
                RobotManipulator.delete_keys([ndb.Key(Robot, robot_key)])
        else:
            self.abort(400)

        self.redirect('/mod/')
Exemplo n.º 26
0
    def get(self, event_key):
        df = DatafeedFMSAPI('v2.0')
        df2 = DatafeedFIRSTElasticSearch()

        event = Event.get_by_id(event_key)

        # Update event
        fmsapi_events, fmsapi_districts = df.getEventDetails(event_key)
        elasticsearch_events = df2.getEventDetails(event)
        updated_event = EventManipulator.mergeModels(fmsapi_events,
                                                     elasticsearch_events)
        if updated_event:
            event = EventManipulator.createOrUpdate(updated_event)
        DistrictManipulator.createOrUpdate(fmsapi_districts)

        models = df.getEventTeams(event_key)
        teams = []
        district_teams = []
        robots = []
        for group in models:
            # models is a list of tuples (team, districtTeam, robot)
            if isinstance(group[0], Team):
                teams.append(group[0])
            if isinstance(group[1], DistrictTeam):
                district_teams.append(group[1])
            if isinstance(group[2], Robot):
                robots.append(group[2])

        # Merge teams
        teams = TeamManipulator.mergeModels(teams, df2.getEventTeams(event))

        # Write new models
        if teams and event.year == tba_config.MAX_YEAR:  # Only update from latest year
            teams = TeamManipulator.createOrUpdate(teams)
        district_teams = DistrictTeamManipulator.createOrUpdate(district_teams)
        robots = RobotManipulator.createOrUpdate(robots)

        if not teams:
            # No teams found registered for this event
            teams = []
        if type(teams) is not list:
            teams = [teams]

        # Build EventTeams
        cmp_hack_sitevar = Sitevar.get_or_insert('cmp_registration_hacks')
        events_without_eventteams = cmp_hack_sitevar.contents.get('skip_eventteams', []) \
            if cmp_hack_sitevar else []
        skip_eventteams = event_key in events_without_eventteams
        event_teams = [
            EventTeam(id=event.key_name + "_" + team.key_name,
                      event=event.key,
                      team=team.key,
                      year=event.year) for team in teams
        ] if not skip_eventteams else []

        # Delete eventteams of teams that are no longer registered
        if event_teams and not skip_eventteams:
            existing_event_team_keys = set(
                EventTeam.query(EventTeam.event == event.key).fetch(
                    1000, keys_only=True))
            event_team_keys = set([et.key for et in event_teams])
            et_keys_to_delete = existing_event_team_keys.difference(
                event_team_keys)
            EventTeamManipulator.delete_keys(et_keys_to_delete)

            event_teams = EventTeamManipulator.createOrUpdate(event_teams)
        if type(event_teams) is not list:
            event_teams = [event_teams]

        if event.year in {2018, 2019, 2020}:
            avatars, keys_to_delete = df.getEventTeamAvatars(event.key_name)
            if avatars:
                MediaManipulator.createOrUpdate(avatars)
            MediaManipulator.delete_keys(keys_to_delete)

        template_values = {
            'event': event,
            'event_teams': event_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/usfirst_event_details_get.html')
            self.response.out.write(template.render(path, template_values))
Exemplo n.º 27
0
    def get(self, event_key):
        df = DatafeedFMSAPI('v2.0')
        df2 = DatafeedFIRSTElasticSearch()

        event = Event.get_by_id(event_key)

        # Update event
        fmsapi_events, fmsapi_districts = df.getEventDetails(event_key)
        elasticsearch_events = df2.getEventDetails(event)
        updated_event = EventManipulator.mergeModels(
            fmsapi_events,
            elasticsearch_events)
        if updated_event:
            event = EventManipulator.createOrUpdate(updated_event)
        DistrictManipulator.createOrUpdate(fmsapi_districts)

        models = df.getEventTeams(event_key)
        teams = []
        district_teams = []
        robots = []
        for group in models:
            # models is a list of tuples (team, districtTeam, robot)
            if isinstance(group[0], Team):
                teams.append(group[0])
            if isinstance(group[1], DistrictTeam):
                district_teams.append(group[1])
            if isinstance(group[2], Robot):
                robots.append(group[2])

        # Merge teams
        teams = TeamManipulator.mergeModels(teams, df2.getEventTeams(event))

        # Write new models
        if teams:
            teams = TeamManipulator.createOrUpdate(teams)
        district_teams = DistrictTeamManipulator.createOrUpdate(district_teams)
        robots = RobotManipulator.createOrUpdate(robots)

        if not teams:
            # No teams found registered for this event
            teams = []
        if type(teams) is not list:
            teams = [teams]

        # Build EventTeams
        cmp_hack_sitevar = Sitevar.get_or_insert('cmp_registration_hacks')
        events_without_eventteams = cmp_hack_sitevar.contents.get('skip_eventteams', []) \
            if cmp_hack_sitevar else []
        skip_eventteams = event_key in events_without_eventteams
        event_teams = [EventTeam(
            id=event.key_name + "_" + team.key_name,
            event=event.key,
            team=team.key,
            year=event.year)
            for team in teams] if not skip_eventteams else []

        # Delete eventteams of teams that are no longer registered
        if event_teams and not skip_eventteams:
            existing_event_team_keys = set(EventTeam.query(EventTeam.event == event.key).fetch(1000, keys_only=True))
            event_team_keys = set([et.key for et in event_teams])
            et_keys_to_delete = existing_event_team_keys.difference(event_team_keys)
            EventTeamManipulator.delete_keys(et_keys_to_delete)

            event_teams = EventTeamManipulator.createOrUpdate(event_teams)
        if type(event_teams) is not list:
            event_teams = [event_teams]

        if event.year == 2018:
            avatars, keys_to_delete = df.getEventTeamAvatars(event.key_name)
            if avatars:
                MediaManipulator.createOrUpdate(avatars)
            MediaManipulator.delete_keys(keys_to_delete)

        template_values = {
            'event': event,
            'event_teams': event_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/usfirst_event_details_get.html')
            self.response.out.write(template.render(path, template_values))