本文整理汇总了Python中models.media.Media.get_by_id方法的典型用法代码示例。如果您正苦于以下问题:Python Media.get_by_id方法的具体用法?Python Media.get_by_id怎么用?Python Media.get_by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.media.Media
的用法示例。
在下文中一共展示了Media.get_by_id方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createTeamMediaSuggestion
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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'
示例2: get_media_and_team_ref
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
def get_media_and_team_ref(self, 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))
return media, team_ref
示例3: create_target_model
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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)
示例4: create_target_model
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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)
示例5: createEventMediaSuggestion
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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
示例6: post
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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'))
示例7: _process_accepted
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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()
示例8: createTeamMediaSuggestion
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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
示例9: suggest_team_media
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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")
示例10: createTeamMediaSuggestion
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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
示例11: createTeamMediaSuggestion
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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'
示例12: post
# 需要导入模块: from models.media import Media [as 别名]
# 或者: from models.media.Media import get_by_id [as 别名]
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))