def render(self, context):
     slug = self.slug.resolve(context)
     request = context.get('request', None)
     if request is None:  # pragma: no cover
         if settings.DEBUG:
             msg = '"django.template.context_processors.request" is required to use django-scribbler'
             raise ImproperlyConfigured(msg)
         else:
             return ''
     if self.url:
         url = self.url.resolve(context)
     else:
         url = request.path
     key = CACHE_KEY_FUNCTION(slug=slug, url=url)
     scribble = cache.get(key, None)
     if scribble is None:
         try:
             scribble = Scribble.objects.get(slug=slug, url=url)
         except Scribble.DoesNotExist:
             scribble = Scribble(slug=slug, url=url, content=self.raw)
         if CACHE_TIMEOUT:
             cache.set(key, scribble, CACHE_TIMEOUT)
     if scribble.pk:
         if hasattr(template, 'engines'):
             scribble_template = template.engines['django'].from_string(scribble.content)
         else:
             scribble_template = template.Template(scribble.content)
     else:
         scribble.content = self.raw
         if hasattr(template, 'engines'):
             scribble_template = template.engines['django'].from_string(self.raw)
         else:
             scribble_template = template.Template(self.raw)
     scribble_context = build_scribble_context(scribble)
     content = scribble_template.render(scribble_context, request)
     wrapper_template = template.loader.get_template('scribbler/scribble-wrapper.html')
     context['scribble'] = scribble
     context['rendered_scribble'] = content
     user = context.get('user', None)
     show_controls = False
     can_edit = False
     can_add = False
     can_delete = False
     if user:
         can_edit = scribble.pk and user.has_perm('scribbler.change_scribble')
         can_add = (not scribble.pk) and user.has_perm('scribbler.add_scribble')
         can_delete = scribble.pk and user.has_perm('scribbler.delete_scribble')
     show_controls = can_edit or can_add or can_delete
     if can_edit or can_add:
         context['scribble_form'] = ScribbleForm(instance=scribble, prefix=slug)
     context['show_controls'] = show_controls
     context['can_add_scribble'] = can_add
     context['can_edit_scribble'] = can_edit
     context['can_delete_scribble'] = can_delete
     context['raw_content'] = self.raw
     # render() takes a dict, so we have to extract the context dict from the object
     context_data = context.dicts[-1]
     return wrapper_template.render(context_data, request)
 def render(self, context):
     slug = self.slug.resolve(context)
     request = context.get('request', None)
     if request is None:  # pragma: no cover
         if settings.DEBUG:
             msg = '"django.core.context_processors.request" is required to use django-scribbler'
             raise ImproperlyConfigured(msg)
         else:
             return ''
     if self.url:
         url = self.url.resolve(context)
     else:
         url = request.path
     key = CACHE_KEY_FUNCTION(slug=slug, url=url)
     scribble = cache.get(key, None)
     if scribble is None:
         try:
             scribble = Scribble.objects.get(slug=slug, url=url)
         except Scribble.DoesNotExist:
             scribble = Scribble(slug=slug, url=url, content=self.raw)
         if CACHE_TIMEOUT:
             cache.set(key, scribble, CACHE_TIMEOUT)
     if scribble.pk:
         scribble_template = template.Template(scribble.content)
     else:
         scribble.content = self.raw
         scribble_template = self.nodelist_default
     scribble_context = build_scribble_context(scribble, request)
     content = scribble_template.render(scribble_context)
     wrapper_template = template.loader.get_template(
         'scribbler/scribble-wrapper.html')
     context['scribble'] = scribble
     context['rendered_scribble'] = content
     user = context.get('user', None)
     show_controls = False
     can_edit = False
     can_add = False
     can_delete = False
     if user:
         can_edit = scribble.pk and user.has_perm(
             'scribbler.change_scribble')
         can_add = (
             not scribble.pk) and user.has_perm('scribbler.add_scribble')
         can_delete = scribble.pk and user.has_perm(
             'scribbler.delete_scribble')
     show_controls = can_edit or can_add or can_delete
     if can_edit or can_add:
         context['scribble_form'] = ScribbleForm(instance=scribble,
                                                 prefix=slug)
     context['show_controls'] = show_controls
     context['can_add_scribble'] = can_add
     context['can_edit_scribble'] = can_edit
     context['can_delete_scribble'] = can_delete
     context['raw_content'] = self.raw
     return wrapper_template.render(context)
def scribble_field(context, model_instance, field_name):
    """
    Renders a scribble-able field from a model instance.

    Usage:
    {% scribble_field model_instance 'field_name' %}
    """

    # TODO: This should maybe be a utility funciton
    request = context.get('request', None)
    if request is None:  # pragma: no cover
        if settings.DEBUG:
            msg = '"django.core.context_processors.request" is required to use django-scribbler'
            raise ImproperlyConfigured(msg)
        else:
            return ''

    model_content_type = ContentType.objects.get_for_model(model_instance)
    field_value = getattr(model_instance, field_name)
    if hasattr(template, 'engines'):
        scribble_template = template.engines['django'].from_string(field_value)
    else:
        scribble_template = template.Template(field_value)
    scribble_context = build_scribble_context(None)
    rendered_content = scribble_template.render(scribble_context, request)
    context['rendered_scribble'] = rendered_content

    user = context.get('user', None)
    can_edit = False
    if user:
        perm_name = '{0}.change_{1}'.format(
            model_content_type.app_label,
            model_content_type.model,
        )
        can_edit = user.has_perm(perm_name)
    if can_edit:
        context['scribble_form'] = FieldScribbleForm(
            model_content_type,
            model_instance.pk,
            field_name,
            field_value=field_value)
    context['show_controls'] = can_edit
    context['can_add_scribble'] = False
    context['can_edit_scribble'] = can_edit
    context['can_delete_scribble'] = False
    context['raw_content'] = field_value
    # render() takes a dict, so we have to extract the context dict from the object
    context_data = context.dicts[-1]
    wrapper_template = template.loader.get_template(
        'scribbler/scribble-wrapper.html')
    return wrapper_template.render(context_data, request)
Beispiel #4
0
def scribble_field(context, model_instance, field_name):
    """
    Renders a scribble-able field from a model instance.

    Usage:
    {% scribble_field model_instance 'field_name' %}
    """

    # TODO: This should maybe be a utility funciton
    request = context.get('request', None)
    if request is None:  # pragma: no cover
        if settings.DEBUG:
            msg = '"django.template.context_processors.request" is required to use django-scribbler'
            raise ImproperlyConfigured(msg)
        else:
            return ''

    model_content_type = ContentType.objects.get_for_model(model_instance)
    field_value = getattr(model_instance, field_name)
    if hasattr(template, 'engines'):
        scribble_template = template.engines['django'].from_string(field_value)
    else:
        scribble_template = template.Template(field_value)
    scribble_context = build_scribble_context(None)
    rendered_content = scribble_template.render(scribble_context, request)
    context['rendered_scribble'] = rendered_content

    user = context.get('user', None)
    can_edit = False
    if user:
        perm_name = '{0}.change_{1}'.format(
            model_content_type.app_label,
            model_content_type.model,
        )
        can_edit = user.has_perm(perm_name)
    if can_edit:
        context['scribble_form'] = FieldScribbleForm(model_content_type,
                                                     model_instance.pk,
                                                     field_name,
                                                     field_value=field_value)
    context['show_controls'] = can_edit
    context['can_add_scribble'] = False
    context['can_edit_scribble'] = can_edit
    context['can_delete_scribble'] = False
    context['raw_content'] = field_value
    # render() takes a dict, so we have to extract the context dict from the object
    context_data = context.dicts[-1]
    wrapper_template = template.loader.get_template(
        'scribbler/scribble-wrapper.html')
    return wrapper_template.render(context_data, request)
 def render(self, context):
     slug = self.slug.resolve(context)
     request = context.get('request', None)
     if request is None: # pragma: no cover
         if settings.DEBUG:
             msg = '"django.core.context_processors.request" is required to use django-scribbler'
             raise ImproperlyConfigured(msg)
         else:
             return ''
     url = request.path
     key = CACHE_KEY_FUNCTION(slug=slug, url=url)
     scribble = cache.get(key, None)
     if scribble is None:
         try:
             scribble = Scribble.objects.get(slug=slug, url=url)
         except Scribble.DoesNotExist:
             scribble = Scribble(slug=slug, url=url, content=self.raw)
         if CACHE_TIMEOUT:
             cache.set(key, scribble, CACHE_TIMEOUT)
     if scribble.pk:
         scribble_template = template.Template(scribble.content)
     else:
         scribble.content = self.raw
         scribble_template = self.nodelist_default
     scribble_context = build_scribble_context(scribble, request)
     content = scribble_template.render(scribble_context)
     wrapper_template = template.loader.get_template('scribbler/scribble-wrapper.html')
     context['scribble'] = scribble
     context['rendered_scribble'] = content
     user = context.get('user', None)
     show_controls = False
     can_edit = False
     can_add = False
     can_delete = False
     if user:
         can_edit = scribble.pk and user.has_perm('scribbler.change_scribble')
         can_add = (not scribble.pk) and user.has_perm('scribbler.add_scribble')
         can_delete = scribble.pk and user.has_perm('scribbler.delete_scribble')
     show_controls = can_edit or can_add or can_delete
     if can_edit or can_add:
         context['scribble_form'] = ScribbleForm(instance=scribble, prefix=slug)
     context['show_controls'] = show_controls
     context['can_add_scribble'] = can_add
     context['can_edit_scribble'] = can_edit
     context['can_delete_scribble'] = can_delete
     context['raw_content'] = self.raw
     return wrapper_template.render(context)
Beispiel #6
0
 def render(self, context):
     slug = self.slug.resolve(context)
     request = context.get('request', None)
     if request is None:  # pragma: no cover
         if settings.DEBUG:
             msg = '"django.template.context_processors.request" is required to use django-scribbler'
             raise ImproperlyConfigured(msg)
         else:
             return ''
     if self.url:
         url = self.url.resolve(context)
     else:
         url = request.path
     key = CACHE_KEY_FUNCTION(slug=slug, url=url)
     scribble = cache.get(key, None)
     if scribble is None:
         try:
             scribble = Scribble.objects.get(slug=slug, url=url)
         except Scribble.DoesNotExist:
             scribble = Scribble(slug=slug, url=url, content=self.raw)
         if CACHE_TIMEOUT:
             cache.set(key, scribble, CACHE_TIMEOUT)
     if scribble.pk:
         if hasattr(template, 'engines'):
             scribble_template = template.engines['django'].from_string(
                 scribble.content)
         else:
             scribble_template = template.Template(scribble.content)
     else:
         scribble.content = self.raw
         if hasattr(template, 'engines'):
             scribble_template = template.engines['django'].from_string(
                 self.raw)
         else:
             scribble_template = template.Template(self.raw)
     scribble_context = build_scribble_context(scribble)
     content = scribble_template.render(scribble_context, request)
     if self.readonly:
         return content
     wrapper_template = template.loader.get_template(
         'scribbler/scribble-wrapper.html')
     context['scribble'] = scribble
     context['rendered_scribble'] = content
     user = context.get('user', None)
     show_controls = False
     can_edit = False
     can_add = False
     can_delete = False
     if user:
         can_edit = scribble.pk and user.has_perm(
             'scribbler.change_scribble')
         can_add = (
             not scribble.pk) and user.has_perm('scribbler.add_scribble')
         can_delete = scribble.pk and user.has_perm(
             'scribbler.delete_scribble')
     show_controls = can_edit or can_add or can_delete
     if can_edit or can_add:
         context['scribble_form'] = ScribbleForm(instance=scribble,
                                                 prefix=slug)
     context['show_controls'] = show_controls
     context['can_add_scribble'] = can_add
     context['can_edit_scribble'] = can_edit
     context['can_delete_scribble'] = can_delete
     context['raw_content'] = self.raw
     # render() takes a dict, so we have to extract the context dict from the object
     context_data = context.dicts[-1]
     return wrapper_template.render(context_data, request)