def _render(self, primary, *fallbacks): """Pick a render strategy for this combination of players. This method is intended to allow you to implement a client-side fallback strategy, for when multiple players can be used. If this method returns None, then :meth:`render` will simply render the primary player and ignore all the rest. :param \*players: Instantiated player objects. :returns: Markup or None if no suitable strategy applies. """ fallback = fallbacks and fallbacks[0] or None # Render our HTML5+Flash player if it's available vars = None if isinstance(primary, AbstractHTML5Player) \ and (isinstance(fallback, AbstractFlashPlayer) or fallback is None): vars = {'html5': primary, 'flash': fallback, 'prefer_flash': False} elif isinstance(primary, AbstractFlashPlayer) \ and isinstance(fallback, AbstractHTML5Player): vars = {'flash': primary, 'html5': fallback, 'prefer_flash': True} if vars: return render('players/html5_or_flash.html', vars) # Alternately, try to render a plain Flash player if isinstance(primary, (AbstractFlashPlayer, AbstractFlashEmbedPlayer)): return render('players/flash_swiff.html', {'flash': primary}) return None
def media_player(media, is_widescreen=False, show_like=True, show_dislike=True, show_download=False, show_embed=False, show_playerbar=True, show_popout=True, show_resize=False, show_share=True, js_init=None, **kwargs): """Instantiate and render the preferred player that can play this media. We make no effort to pick the "best" player here, we simply return the first player that *can* play any of the URIs associated with the given media object. It's up to the user to declare their own preferences wisely. Player preferences are fetched from the database and the :attr:`mediacore.model.players.c.data` dict is passed as kwargs to :meth:`AbstractPlayer.__init__`. :type media: :class:`mediacore.model.media.Media` :param media: A media instance to play. :param js_init: Optional function to call after the javascript player controller has been instantiated. Example of a function literal: ``function(controller){ controller.setFillScreen(true); }``. Any function reference can be used as long as it is defined in all pages and accepts the JS player controller as its first and only argument. :param \*\*kwargs: Extra kwargs for :meth:`AbstractPlayer.__init__`. :rtype: `str` or `None` :returns: A rendered player. """ uris = media.get_uris() # Find the first player that can play any uris for player_cls, player_data in fetch_enabled_players(): can_play = player_cls.can_play(uris) if any(can_play): break else: return None # Grab just the uris that the chosen player can play playable_uris = [uri for uri, plays in izip(uris, can_play) if plays] kwargs['data'] = player_data player = player_cls(media, playable_uris, **kwargs) return render('players/html5_or_flash.html', { 'player': player, 'media': media, 'uris': uris, 'is_widescreen': is_widescreen, 'js_init': js_init, 'show_like': show_like, 'show_dislike': show_dislike, 'show_download': show_download, 'show_embed': show_embed, 'show_playerbar': show_playerbar, 'show_popout': show_popout, 'show_resize': show_resize and player.supports_resizing, 'show_share': show_share, })
def wrapped_f(*args, **kwargs): result = f(*args, **kwargs) tmpl = template if hasattr(request, 'override_template'): tmpl = request.override_template if tmpl == 'json': if isinstance(result, (list, tuple)): msg = ("JSON responses with Array envelopes are susceptible " "to cross-site data leak attacks, see " "http://wiki.pylonshq.com/display/pylonsfaq/Warnings") warnings.warn(msg, Warning, 2) log.warning(msg) response.headers['Content-Type'] = 'application/json' return simplejson.dumps(result) if request.environ.get('paste.testing', False): # Make the vars passed from action to template accessible to tests request.environ['paste.testing_variables']['tmpl_vars'] = result # Serve application/xhtml+xml instead of text/html during testing. # This allows us to query the response xhtml as ElementTree XML # instead of BeautifulSoup HTML. # NOTE: We do not serve true xhtml to all clients that support it # because of a bug in Mootools Swiff as of v1.2.4: # https://mootools.lighthouseapp.com/projects/2706/tickets/758 if response.content_type == 'text/html': response.content_type = 'application/xhtml+xml' return render(tmpl, tmpl_vars=result, method='auto')
def media_player(media, is_widescreen=False, show_like=True, show_dislike=True, show_download=False, show_embed=False, show_playerbar=True, show_popout=True, show_resize=False, show_share=True, js_init=None, **kwargs): """Instantiate and render the preferred player that can play this media. We make no effort to pick the "best" player here, we simply return the first player that *can* play any of the URIs associated with the given media object. It's up to the user to declare their own preferences wisely. Player preferences are fetched from the database and the :attr:`mediacore.model.players.c.data` dict is passed as kwargs to :meth:`AbstractPlayer.__init__`. :type media: :class:`mediacore.model.media.Media` :param media: A media instance to play. :param js_init: Optional function to call after the javascript player controller has been instantiated. Example of a function literal: ``function(controller){ controller.setFillScreen(true); }``. Any function reference can be used as long as it is defined in all pages and accepts the JS player controller as its first and only argument. :param \*\*kwargs: Extra kwargs for :meth:`AbstractPlayer.__init__`. :rtype: `str` or `None` :returns: A rendered player. """ player = preferred_player_for_media(media, **kwargs) return render( 'players/html5_or_flash.html', { 'player': player, 'media': media, 'uris': media.get_uris(), 'is_widescreen': is_widescreen, 'js_init': js_init, 'show_like': show_like, 'show_dislike': show_dislike, 'show_download': show_download, 'show_embed': show_embed, 'show_playerbar': show_playerbar, 'show_popout': show_popout, 'show_resize': show_resize and (player and player.supports_resizing), 'show_share': show_share, })
def result(success, message=None, comment=None): if request.is_xhr: result = dict(success=success, message=message) if comment: result["comment"] = render("comments/_list.html", {"comment_to_render": comment}, method="xhtml") return result elif success: return redirect(action="view") else: return self.view(slug, name=name, email=email, body=body, **kwargs)
def result(success, message=None, comment=None): if request.is_xhr: result = dict(success=success, message=message) if comment: result['comment'] = render('comments/_list.html', {'comment_to_render': comment}, method='xhtml') return result elif success: return redirect(action='view') else: return self.view(slug, name=name, email=email, body=body, **kwargs)
def wrapped_f(*args, **kwargs): if request_method and request_method != request.method: raise HTTPMethodNotAllowed().exception result = f(*args, **kwargs) tmpl = template if hasattr(request, 'override_template'): tmpl = request.override_template if tmpl == 'string': return result if tmpl == 'json': if isinstance(result, (list, tuple)): msg = ("JSON responses with Array envelopes are susceptible " "to cross-site data leak attacks, see " "http://wiki.pylonshq.com/display/pylonsfaq/Warnings") if config['debug']: raise TypeError(msg) warnings.warn(msg, Warning, 2) log.warning(msg) response.headers['Content-Type'] = 'application/json' return simplejson.dumps(result) if request.environ.get('paste.testing', False): # Make the vars passed from action to template accessible to tests request.environ['paste.testing_variables']['tmpl_vars'] = result # Serve application/xhtml+xml instead of text/html during testing. # This allows us to query the response xhtml as ElementTree XML # instead of BeautifulSoup HTML. # NOTE: We do not serve true xhtml to all clients that support it # because of a bug in Mootools Swiff as of v1.2.4: # https://mootools.lighthouseapp.com/projects/2706/tickets/758 if response.content_type == 'text/html': response.content_type = 'application/xhtml+xml' return render(tmpl, tmpl_vars=result, method='auto')
def render_rows(media): rows = {} for m in media: stream = render('admin/media/index-table.html', {'media': [m]}) rows[m.id] = unicode(stream.select('table/tbody/tr')) return rows
def render_rows(media): rows = {} for m in media: stream = render("admin/media/index-table.html", {"media": [m]}) rows[m.id] = unicode(stream.select("table/tbody/tr")) return rows
def media_player(media, is_widescreen=False, show_like=True, show_dislike=True, show_download=False, show_embed=False, show_playerbar=True, show_popout=True, show_resize=False, show_share=True, js_init=None, **kwargs): """Instantiate and render the preferred player that can play this media. We make no effort to pick the "best" player here, we simply return the first player that *can* play any of the URIs associated with the given media object. It's up to the user to declare their own preferences wisely. Player preferences are fetched from the database and the :attr:`mediacore.model.players.c.data` dict is passed as kwargs to :meth:`AbstractPlayer.__init__`. :type media: :class:`mediacore.model.media.Media` :param media: A media instance to play. :param js_init: Optional function to call after the javascript player controller has been instantiated. Example of a function literal: ``function(controller){ controller.setFillScreen(true); }``. Any function reference can be used as long as it is defined in all pages and accepts the JS player controller as its first and only argument. :param \*\*kwargs: Extra kwargs for :meth:`AbstractPlayer.__init__`. :rtype: `str` or `None` :returns: A rendered player. """ # we have to check if current user is anonymous or authenticated userid = check_user_authentication(request) # check if current user has already voted this media object votes = Vote.query.get_votes(media_id=media.id, user_name=userid) if (not userid) or votes.count(): show_like = False show_dislike = False uris = media.get_uris() # Find the first player that can play any uris for player_cls, player_data in fetch_enabled_players(): can_play = player_cls.can_play(uris) if any(can_play): break else: return None # Grab just the uris that the chosen player can play playable_uris = [uri for uri, plays in izip(uris, can_play) if plays] kwargs['data'] = player_data player = player_cls(media, playable_uris, **kwargs) return render( 'players/html5_or_flash.html', { 'player': player, 'media': media, 'uris': uris, 'is_widescreen': is_widescreen, 'js_init': js_init, 'show_like': show_like, 'show_dislike': show_dislike, 'show_download': show_download, 'show_embed': show_embed, 'show_playerbar': show_playerbar, 'show_popout': show_popout, 'show_resize': show_resize and player.supports_resizing, 'show_share': show_share, })