本文整理汇总了Python中models.media.Media类的典型用法代码示例。如果您正苦于以下问题:Python Media类的具体用法?Python Media怎么用?Python Media使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Media类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_target_model
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)
示例2: createTeamMediaSuggestion
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'
示例3: get_media_and_team_ref
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
示例4: createEventMediaSuggestion
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
示例5: _process_request
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)
示例6: TestTeamMediaApiController
class TestTeamMediaApiController(unittest2.TestCase):
def setUp(self):
app = webapp2.WSGIApplication([webapp2.Route(r'/<team_key:>/<year:>', ApiTeamMediaController, methods=['GET'])], debug=True)
self.testapp = webtest.TestApp(app)
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_urlfetch_stub()
self.testbed.init_memcache_stub()
self.testbed.init_taskqueue_stub(root_path=".")
self.team = Team(
id="frc254",
name="very long name",
team_number=254,
nickname="Teh Chezy Pofs",
address="Greenville, SC, USA"
)
self.team.put()
self.cdmedia = Media(
key=ndb.Key('Media', 'cdphotothread_39894'),
details_json=u'{"image_partial": "fe3/fe38d320428adf4f51ac969efb3db32c_l.jpg"}',
foreign_key=u'39894',
media_type_enum=1,
references=[ndb.Key('Team', 'frc254')],
year=2014)
self.cdmedia.put()
self.cddetails = dict()
self.cddetails["image_partial"] = "fe3/fe38d320428adf4f51ac969efb3db32c_l.jpg"
self.ytmedia = Media(
key=ndb.Key('Media', 'youtube_aFZy8iibMD0'),
details_json=None,
foreign_key=u'aFZy8iibMD0',
media_type_enum=0,
references=[ndb.Key('Team', 'frc254')],
year=2014)
self.ytmedia.put()
def tearDown(self):
self.testbed.deactivate()
def testTeamMediaApi(self):
response = self.testapp.get('/frc254/2014', headers={"X-TBA-App-Id": "tba-tests:team_media-controller-test:v01"})
media = json.loads(response.body)
self.assertEqual(len(media), 2)
cd = media[0]
self.assertEqual(cd["type"], "cdphotothread")
self.assertEqual(cd["foreign_key"], "39894")
self.assertEqual(cd["details"], self.cddetails)
yt = media[1]
self.assertEqual(yt["type"], "youtube")
self.assertEqual(yt["foreign_key"], "aFZy8iibMD0")
self.assertEqual(yt["details"], {})
示例7: create_target_model
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)
示例8: get
def get(self):
suggestions = (
Suggestion.query()
.filter(Suggestion.review_state == Suggestion.REVIEW_PENDING)
.filter(Suggestion.target_model == "media")
.fetch(limit=50)
)
# Quick and dirty way to group images together
suggestions = sorted(
suggestions, key=lambda x: 0 if x.contents["media_type_enum"] in MediaType.image_types else 1
)
reference_keys = []
existing_preferred_keys_futures = []
for suggestion in suggestions:
reference_key = suggestion.contents["reference_key"]
reference = Media.create_reference(suggestion.contents["reference_type"], reference_key)
reference_keys.append(reference)
if "details_json" in suggestion.contents:
suggestion.details = json.loads(suggestion.contents["details_json"])
if "image_partial" in suggestion.details:
suggestion.details["thumbnail"] = suggestion.details["image_partial"].replace("_l", "_m")
# Find existing preferred images
existing_preferred_keys_futures.append(
Media.query(
Media.media_type_enum.IN(MediaType.image_types),
Media.references == reference,
Media.preferred_references == reference,
Media.year == suggestion.contents["year"],
).fetch_async(keys_only=True)
)
reference_futures = ndb.get_multi_async(reference_keys)
existing_preferred_futures = map(lambda x: ndb.get_multi_async(x.get_result()), existing_preferred_keys_futures)
references = map(lambda r: r.get_result(), reference_futures)
existing_preferred = map(lambda l: map(lambda x: x.get_result(), l), existing_preferred_futures)
suggestions_and_references_and_preferred = zip(suggestions, references, existing_preferred)
self.template_values.update(
{
"suggestions_and_references_and_preferred": suggestions_and_references_and_preferred,
"max_preferred": Media.MAX_PREFERRED,
}
)
self.response.out.write(
jinja2_engine.render("suggestions/suggest_team_media_review_list.html", self.template_values)
)
示例9: test_media_exists
def test_media_exists(self):
media_id = Media.render_key_name(MediaType.YOUTUBE_VIDEO, 'H-54KMwMKY0')
Media.get_or_insert(
media_id,
media_type_enum=MediaType.YOUTUBE_VIDEO,
foreign_key='H-54KMwMKY0',
references=[ndb.Key(Event, '2016nyny')]).put()
status, _ = SuggestionCreator.createEventMediaSuggestion(
self.account.key,
"https://www.youtube.com/watch?v=H-54KMwMKY0",
"2016nyny")
self.assertEqual(status, 'media_exists')
示例10: _process_accepted
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()
示例11: testMediaExists
def testMediaExists(self):
media_id = Media.render_key_name(MediaType.IMGUR, 'ruRAxDm')
Media.get_or_insert(
media_id,
media_type_enum=MediaType.IMGUR,
foreign_key='ruRAxDm',
references=[ndb.Key(Team, 'frc1124')]).put()
status = SuggestionCreator.createTeamMediaSuggestion(
self.account.key,
"http://imgur.com/ruRAxDm",
"frc1124",
"2016")
self.assertEqual(status, 'media_exists')
示例12: setUp
def setUp(self):
app = webapp2.WSGIApplication(
[webapp2.Route(r"/<team_key:>/<year:>", ApiTeamMediaController, methods=["GET"])], debug=True
)
self.testapp = webtest.TestApp(app)
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_urlfetch_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.team = Team(
id="frc254",
name="very long name",
team_number=254,
nickname="Teh Chezy Pofs",
city="Greenville",
state_prov="SC",
country="USA",
)
self.team.put()
self.cdmedia = Media(
key=ndb.Key("Media", "cdphotothread_39894"),
details_json=u'{"image_partial": "fe3/fe38d320428adf4f51ac969efb3db32c_l.jpg"}',
foreign_key=u"39894",
media_type_enum=1,
references=[ndb.Key("Team", "frc254")],
year=2014,
)
self.cdmedia.put()
self.cddetails = dict()
self.cddetails["image_partial"] = "fe3/fe38d320428adf4f51ac969efb3db32c_l.jpg"
self.ytmedia = Media(
key=ndb.Key("Media", "youtube_aFZy8iibMD0"),
details_json=None,
foreign_key=u"aFZy8iibMD0",
media_type_enum=0,
references=[ndb.Key("Team", "frc254")],
year=2014,
)
self.ytmedia.put()
示例13: get
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))
示例14: parse
def parse(self, response):
"""
Parse team avatar from FMSAPI
"""
# Get team avatar
current_page = response['pageCurrent']
total_pages = response['pageTotal']
teams = response['teams']
avatars = []
keys_to_delete = set()
for teamData in teams:
team_number = teamData['teamNumber']
foreign_key = "avatar_{}_frc{}".format(self.year, team_number)
media_key = ndb.Key('Media', Media.render_key_name(MediaType.AVATAR, foreign_key))
if not teamData['encodedAvatar']:
keys_to_delete.add(media_key)
continue
else:
encoded_avatar = teamData['encodedAvatar']
avatar = Media(
key=media_key,
details_json=json.dumps({'base64Image': encoded_avatar}),
foreign_key=foreign_key,
media_type_enum=MediaType.AVATAR,
references=[ndb.Key('Team', "frc{}".format(team_number))],
year=self.year
)
avatars.append(avatar)
return (avatars, keys_to_delete, (current_page < total_pages))
示例15: _query_async
def _query_async(self):
team_key = self._query_args[0]
year = self._query_args[1]
medias = yield Media.query(
Media.references == ndb.Key(Team, team_key),
Media.year == year).fetch_async()
raise ndb.Return(medias)