<commit_before>from django.conf import settings
from django.db.models.query import QuerySet
from django.template import TemplateSyntaxError, VariableDoesNotExist, Node
from django.template import Library, Variable, loader, Context

# ID of current flowplayer being rendered (global to ensure unique)
FLOWPLAYER_ITERATOR = 0

register = Library()

class FlowPlayerNode(Node):
    "Renderer class for the flowplayer template tag."
    
    def __init__(self, media, player_class):
        """
        Constructor.

        Parameters:

            media
                Media file url OR an array of urls
            player_class
                Type of player to show (changes css class and config source)
        """
        self.player_class = player_class
        self.media = Variable(media)

        if settings.FLOWPLAYER_URL:
            self.player_url = settings.FLOWPLAYER_URL
        else:
            self.player_url = "%sflowplayer/FlowPlayerLight.swf" % (settings.MEDIA_URL)

        # Import the configuration settings to set on the player output
        # Configuration is defined in the settings (multiple types of player)
        if 'default' in settings.FLOWPLAYER_CONFIG:
            self.player_config = settings.FLOWPLAYER_CONFIG['default']
        else:
            self.player_config = dict()

        if player_class in settings.FLOWPLAYER_CONFIG:
            self.player_config.update(settings.FLOWPLAYER_CONFIG[player_class])

    def render(self, context):

        if 'flowplayer_iterator' in context:
            context['flowplayer_iterator'] += 1
        else:
            context['flowplayer_iterator'] = 0

        try:           
            # Try resolve this variable in the template context
            self.media = self.media.resolve(context) 
        except:
            # Cannot resolve, therefore treat as url string
            pass
        
        # Have we got an array or a string?
        if isinstance(self.media, list):
            # Can resolve, push first url into the url variable
            self.media_url = self.media[0]['url']
            self.media_playlist = self.media
        if isinstance(self.media, QuerySet):
            # Can resolve, push first url into the url variable
            self.media_url = self.media[0].url
            self.media_playlist = self.media
        else:
            self.media_url = self.media
            self.media_playlist = False

        t = loader.get_template('flowplayer/flowplayer.html')
        code_context = Context(
                            {"player_url": self.player_url,
                             "player_id": context['flowplayer_iterator'],
                             "player_class": self.player_class,
                             "player_config": self.player_config,
                             "media_url": self.media_url,
                             "media_playlist":self.media_playlist
                            }, autoescape=context.autoescape)
        return t.render(code_context)

def do_flowplayer(parser, token):
    """
    This will insert an flash-based flv videoplayer (flowplayer) in form of an <object>
    code block.

    Usage::

        {% flowplayer media_url %}

    Example::
    
        {% flowplayer video.flv %}        
    
    By default, 'flowplayer' tag will use FlowPlayerLight.swf found at  
    ``{{ MEDIA_URL }}flowplayer/FlowPlayerLight.swf``.
    
    To change this add FLOWPLAYER_URL to your settings.py file

    Pass a dict of urls to the player to get a playlisted player instance
    
    """    
   
    args = token.split_contents()
    
    if len(args) < 2:
        raise TemplateSyntaxError, "'flowplayer' tag requires at least one argument."            
    
    if len(args) == 3:
        player_class = args[2]
    else:
        player_class = None

    media = args[1]

    return FlowPlayerNode(media, player_class)


# register the tag 
register.tag('flowplayer', do_flowplayer)
<commit_msg>Fixed multiple flowplayer tags in for loop and multiple flowplayers in the same page with id<commit_after>from django.conf import settings
from django.db.models.query import QuerySet
from django.template import TemplateSyntaxError, VariableDoesNotExist, Node
from django.template import Library, Variable, loader, Context

# ID of current flowplayer being rendered (global to ensure unique)
FLOWPLAYER_ITERATOR = 0

register = Library()

class FlowPlayerNode(Node):
    "Renderer class for the flowplayer template tag."
    
    def __init__(self, media, player_class, player_id=None):
        """
        Constructor.

        Parameters:

            media
                Media file url OR an array of urls
            player_class
                Type of player to show (changes css class and config source)
        """
        self.player_class = player_class
        self.media = Variable(media)
        if player_id != None:
            self.player_id = Variable(player_id)

        if settings.FLOWPLAYER_URL:
            self.player_url = settings.FLOWPLAYER_URL
        else:
            self.player_url = "%sflowplayer/FlowPlayerLight.swf" % (settings.MEDIA_URL)

        # Import the configuration settings to set on the player output
        # Configuration is defined in the settings (multiple types of player)
        if 'default' in settings.FLOWPLAYER_CONFIG:
            self.player_config = settings.FLOWPLAYER_CONFIG['default']
        else:
            self.player_config = dict()

        if player_class in settings.FLOWPLAYER_CONFIG:
            self.player_config.update(settings.FLOWPLAYER_CONFIG[player_class])

    def render(self, context):

        if 'flowplayer_iterator' in context:
            context['flowplayer_iterator'] += 1
        else:
            context['flowplayer_iterator'] = 0

        try:           
            # Try resolve this variable in the template context
            self.media_element = self.media.resolve(context) 
        except:
            # Cannot resolve, therefore treat as url string
            self.media_element = self.media
        
        try:
            self.extra_id = self.player_id.resolve(context)
        except:
            self.extra_id = ''

        # Have we got an array or a string?
        if isinstance(self.media_element, list):
            # Can resolve, push first url into the url variable
            self.media_url = self.media_element[0]['url']
            self.media_playlist = self.media_element
        if isinstance(self.media_element, QuerySet):
            # Can resolve, push first url into the url variable
            self.media_url = self.media_element[0].url
            self.media_playlist = self.media_element
        else:
            self.media_url = self.media_element
            self.media_playlist = False
        
        t = loader.get_template('flowplayer/flowplayer.html')
        code_context = Context(
                            {"player_url": self.player_url,
                             "player_id": '%s%s' % (context['flowplayer_iterator'],self.extra_id),
                             "player_class": self.player_class,
                             "player_config": self.player_config,
                             "media_url": self.media_url,
                             "media_playlist":self.media_playlist
                            }, autoescape=context.autoescape)
        return t.render(code_context)

def do_flowplayer(parser, token):
    """
    This will insert an flash-based flv videoplayer (flowplayer) in form of an <object>
    code block.

    Usage::

        {% flowplayer media_url %}

    Example::
    
        {% flowplayer video.flv %}        
    
    By default, 'flowplayer' tag will use FlowPlayerLight.swf found at  
    ``{{ MEDIA_URL }}flowplayer/FlowPlayerLight.swf``.
    
    To change this add FLOWPLAYER_URL to your settings.py file

    Pass a dict of urls to the player to get a playlisted player instance
    
    """    
   
    args = token.split_contents()
    
    if len(args) < 2:
        raise TemplateSyntaxError, "'flowplayer' tag requires at least one argument."            
    
    if len(args) == 3:
        player_class = args[2]
    else:
        player_class = None
        
    if len(args) == 4:
        player_id = args[3]
    else:
        player_id = None

    media = args[1]

    return FlowPlayerNode(media, player_class, player_id)


# register the tag 
register.tag('flowplayer', do_flowplayer)

{'fraction_non_alphanumeric': 0.07620399952668323, 'fraction_numerical': 0.0023665838362323987, 'mean_word_length': 2.0402877697841726, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708227', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2210, 'n_words': 643}