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 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 _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 # 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 _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 test_remove_media_reference(self): self.loginUser() self.giveTeamAdminAccess() team_reference = Media.create_reference('team', 'frc1124') suggestion_id = self.createMediaSuggestion() suggestion = Suggestion.get_by_id(suggestion_id) media = MediaCreator.create_media_model(suggestion, team_reference) media_id = media.put() self.assertTrue(ndb.Key(Team, 'frc1124') in media.references) form = self.getMediaAdminForm('remove_media', media_id.id()) response = form.submit().follow() self.assertEqual(response.status_int, 301) media = media_id.get() self.assertFalse(team_reference in media.references)
def test_manage_media_expired_auth(self): self.loginUser() access_key = self.giveTeamAdminAccess() team_reference = Media.create_reference('team', 'frc1124') suggestion_id = self.createSocialMediaSuggestion() suggestion = Suggestion.get_by_id(suggestion_id) media = MediaCreator.create_media_model(suggestion, team_reference) media_id = media.put() self.assertTrue(ndb.Key(Team, 'frc1124') in media.references) form = self.getMediaAdminForm('remove_media', media_id.id()) access = access_key.get() access.expiration += datetime.timedelta(days=-7) access.put() response = form.submit(status='403', expect_errors=True) self.assertEqual(response.status_int, 403)
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 create_target_model(self, suggestion): # Create a basic Media from this suggestion return MediaCreator.from_suggestion(suggestion)
def candidate_media(self): team_reference = Media.create_reference( self.contents['reference_type'], self.contents['reference_key']) return MediaCreator.create_media_model(self, team_reference)