from django import template
from django.template import Context, loader
from stream_django.exceptions import MissingDataException
import logging


logger = logging.getLogger(__name__)
register = template.Library()

LOG = 'warn'
IGNORE = 'ignore'
FAIL = 'fail'

missing_data_policies = [LOG, IGNORE, FAIL]


def handle_not_enriched_data(activity, policy):
    message = 'could not enrich field(s) %r for activity #%s' % (activity.not_enriched_data, activity.get('id'))
    if policy == IGNORE:
        pass
    elif policy == FAIL:
        raise MissingDataException(message)
    elif policy == LOG:
        logger.warn(message)
    else:
        raise TypeError('%s is not a valid missing_data_policy' % policy)


def render_activity(context, activity, template_prefix='', missing_data_policy=LOG):
    if hasattr(activity, 'enriched') and not activity.enriched:
        handle_not_enriched_data(activity, missing_data_policy)
        return ''

    if template_prefix != '':
        template_prefix = '%s_' % template_prefix

    if 'activities' in activity:
        template_name = "activity/aggregated/%s%s.html" % (template_prefix, activity['verb'])
    else:
        template_name = "activity/%s%s.html" % (template_prefix, activity['verb'])

    tmpl = loader.get_template(template_name)
    context['activity'] = activity
    context = Context(context)
    return tmpl.render(context)


register.simple_tag(takes_context=True)(render_activity)

from django import template
from django.template import loader
from stream_django.exceptions import MissingDataException
import logging


logger = logging.getLogger(__name__)
register = template.Library()

LOG = 'warn'
IGNORE = 'ignore'
FAIL = 'fail'

missing_data_policies = [LOG, IGNORE, FAIL]


def handle_not_enriched_data(activity, policy):
    message = 'could not enrich field(s) %r for activity #%s' % (activity.not_enriched_data, activity.get('id'))
    if policy == IGNORE:
        pass
    elif policy == FAIL:
        raise MissingDataException(message)
    elif policy == LOG:
        logger.warn(message)
    else:
        raise TypeError('%s is not a valid missing_data_policy' % policy)


def render_activity(context, activity, template_prefix='', missing_data_policy=LOG):
    if hasattr(activity, 'enriched') and not activity.enriched:
        handle_not_enriched_data(activity, missing_data_policy)
        return ''

    if template_prefix != '':
        template_prefix = '%s_' % template_prefix

    if 'activities' in activity:
        template_name = "activity/aggregated/%s%s.html" % (template_prefix, activity['verb'])
    else:
        template_name = "activity/%s%s.html" % (template_prefix, activity['verb'])

    tmpl = loader.get_template(template_name)
    context['activity'] = activity
    return tmpl.render(context)


register.simple_tag(takes_context=True)(render_activity)

Use dict as a context object for Django 1.11 compatibility
Use dict as a context object for Django 1.11 compatibility

Django’s template rendering in 1.11 needs a dictionary as the context
instead of the object Context, otherwise the following error is raised:
context must be a dict rather than Context.

  from django import template
- from django.template import Context, loader
+ from django.template import loader
  from stream_django.exceptions import MissingDataException
  import logging
  
  
  logger = logging.getLogger(__name__)
  register = template.Library()
  
  LOG = 'warn'
  IGNORE = 'ignore'
  FAIL = 'fail'
  
  missing_data_policies = [LOG, IGNORE, FAIL]
  
  
  def handle_not_enriched_data(activity, policy):
      message = 'could not enrich field(s) %r for activity #%s' % (activity.not_enriched_data, activity.get('id'))
      if policy == IGNORE:
          pass
      elif policy == FAIL:
          raise MissingDataException(message)
      elif policy == LOG:
          logger.warn(message)
      else:
          raise TypeError('%s is not a valid missing_data_policy' % policy)
  
  
  def render_activity(context, activity, template_prefix='', missing_data_policy=LOG):
      if hasattr(activity, 'enriched') and not activity.enriched:
          handle_not_enriched_data(activity, missing_data_policy)
          return ''
  
      if template_prefix != '':
          template_prefix = '%s_' % template_prefix
  
      if 'activities' in activity:
          template_name = "activity/aggregated/%s%s.html" % (template_prefix, activity['verb'])
      else:
          template_name = "activity/%s%s.html" % (template_prefix, activity['verb'])
  
      tmpl = loader.get_template(template_name)
      context['activity'] = activity
-     context = Context(context)
      return tmpl.render(context)
  
  
  register.simple_tag(takes_context=True)(render_activity)
  
Use dict as a context object for Django 1.11 compatibility
## Code Before:
from django import template
from django.template import Context, loader
from stream_django.exceptions import MissingDataException
import logging


logger = logging.getLogger(__name__)
register = template.Library()

LOG = 'warn'
IGNORE = 'ignore'
FAIL = 'fail'

missing_data_policies = [LOG, IGNORE, FAIL]


def handle_not_enriched_data(activity, policy):
    message = 'could not enrich field(s) %r for activity #%s' % (activity.not_enriched_data, activity.get('id'))
    if policy == IGNORE:
        pass
    elif policy == FAIL:
        raise MissingDataException(message)
    elif policy == LOG:
        logger.warn(message)
    else:
        raise TypeError('%s is not a valid missing_data_policy' % policy)


def render_activity(context, activity, template_prefix='', missing_data_policy=LOG):
    if hasattr(activity, 'enriched') and not activity.enriched:
        handle_not_enriched_data(activity, missing_data_policy)
        return ''

    if template_prefix != '':
        template_prefix = '%s_' % template_prefix

    if 'activities' in activity:
        template_name = "activity/aggregated/%s%s.html" % (template_prefix, activity['verb'])
    else:
        template_name = "activity/%s%s.html" % (template_prefix, activity['verb'])

    tmpl = loader.get_template(template_name)
    context['activity'] = activity
    context = Context(context)
    return tmpl.render(context)


register.simple_tag(takes_context=True)(render_activity)

## Instruction:
Use dict as a context object for Django 1.11 compatibility
## Code After:
from django import template
from django.template import loader
from stream_django.exceptions import MissingDataException
import logging


logger = logging.getLogger(__name__)
register = template.Library()

LOG = 'warn'
IGNORE = 'ignore'
FAIL = 'fail'

missing_data_policies = [LOG, IGNORE, FAIL]


def handle_not_enriched_data(activity, policy):
    message = 'could not enrich field(s) %r for activity #%s' % (activity.not_enriched_data, activity.get('id'))
    if policy == IGNORE:
        pass
    elif policy == FAIL:
        raise MissingDataException(message)
    elif policy == LOG:
        logger.warn(message)
    else:
        raise TypeError('%s is not a valid missing_data_policy' % policy)


def render_activity(context, activity, template_prefix='', missing_data_policy=LOG):
    if hasattr(activity, 'enriched') and not activity.enriched:
        handle_not_enriched_data(activity, missing_data_policy)
        return ''

    if template_prefix != '':
        template_prefix = '%s_' % template_prefix

    if 'activities' in activity:
        template_name = "activity/aggregated/%s%s.html" % (template_prefix, activity['verb'])
    else:
        template_name = "activity/%s%s.html" % (template_prefix, activity['verb'])

    tmpl = loader.get_template(template_name)
    context['activity'] = activity
    return tmpl.render(context)


register.simple_tag(takes_context=True)(render_activity)
