from collections import defaultdict
from django import template

from ..models import ProblemReport
from ..forms import ProblemReportForm

register = template.Library()


@register.inclusion_tag('problem/message_toolbar_item.html')
def render_problem_button(message):
    if not hasattr(message, 'problemreports'):
        # Get all problem reports for all messages
        request = message.request
        reports = ProblemReport.objects.filter(message__in=request.messages)
        message_reports = defaultdict(list)
        for report in reports:
            message_reports[report.message_id].append(report)
        for message in request.messages:
            message.problemreports = message_reports[message.id]
            message.problemreports_count = len(message.problemreports)
            message.problemreports_unresolved_count = len([
                r for r in message.problemreports if not r.resolved
            ])
            message.problemreports_form = ProblemReportForm(message=message)

    return {
        'message': message
    }

from collections import defaultdict
from django import template

from ..models import ProblemReport
from ..forms import ProblemReportForm

register = template.Library()


@register.inclusion_tag('problem/message_toolbar_item.html')
def render_problem_button(message):
    if not hasattr(message, 'problemreports'):
        # Get all problem reports for all messages
        request = message.request
        reports = ProblemReport.objects.filter(message__in=request.messages)
        message_reports = defaultdict(list)
        for report in reports:
            message_reports[report.message_id].append(report)
        for mes in request.messages:
            mes.problemreports = message_reports[mes.id]
            mes.problemreports_count = len(mes.problemreports)
            mes.problemreports_unresolved_count = len([
                r for r in mes.problemreports if not r.resolved
            ])
            mes.problemreports_form = ProblemReportForm(message=mes)

    return {
        'message': message
    }

Fix overriding variable in problem report tag
Fix overriding variable in problem report tag
  from collections import defaultdict
  from django import template
  
  from ..models import ProblemReport
  from ..forms import ProblemReportForm
  
  register = template.Library()
  
  
  @register.inclusion_tag('problem/message_toolbar_item.html')
  def render_problem_button(message):
      if not hasattr(message, 'problemreports'):
          # Get all problem reports for all messages
          request = message.request
          reports = ProblemReport.objects.filter(message__in=request.messages)
          message_reports = defaultdict(list)
          for report in reports:
              message_reports[report.message_id].append(report)
-         for message in request.messages:
+         for mes in request.messages:
-             message.problemreports = message_reports[message.id]
+             mes.problemreports = message_reports[mes.id]
-             message.problemreports_count = len(message.problemreports)
+             mes.problemreports_count = len(mes.problemreports)
-             message.problemreports_unresolved_count = len([
+             mes.problemreports_unresolved_count = len([
-                 r for r in message.problemreports if not r.resolved
+                 r for r in mes.problemreports if not r.resolved
              ])
-             message.problemreports_form = ProblemReportForm(message=message)
+             mes.problemreports_form = ProblemReportForm(message=mes)
  
      return {
          'message': message
      }
  
Fix overriding variable in problem report tag
## Code Before:
from collections import defaultdict
from django import template

from ..models import ProblemReport
from ..forms import ProblemReportForm

register = template.Library()


@register.inclusion_tag('problem/message_toolbar_item.html')
def render_problem_button(message):
    if not hasattr(message, 'problemreports'):
        # Get all problem reports for all messages
        request = message.request
        reports = ProblemReport.objects.filter(message__in=request.messages)
        message_reports = defaultdict(list)
        for report in reports:
            message_reports[report.message_id].append(report)
        for message in request.messages:
            message.problemreports = message_reports[message.id]
            message.problemreports_count = len(message.problemreports)
            message.problemreports_unresolved_count = len([
                r for r in message.problemreports if not r.resolved
            ])
            message.problemreports_form = ProblemReportForm(message=message)

    return {
        'message': message
    }

## Instruction:
Fix overriding variable in problem report tag
## Code After:
from collections import defaultdict
from django import template

from ..models import ProblemReport
from ..forms import ProblemReportForm

register = template.Library()


@register.inclusion_tag('problem/message_toolbar_item.html')
def render_problem_button(message):
    if not hasattr(message, 'problemreports'):
        # Get all problem reports for all messages
        request = message.request
        reports = ProblemReport.objects.filter(message__in=request.messages)
        message_reports = defaultdict(list)
        for report in reports:
            message_reports[report.message_id].append(report)
        for mes in request.messages:
            mes.problemreports = message_reports[mes.id]
            mes.problemreports_count = len(mes.problemreports)
            mes.problemreports_unresolved_count = len([
                r for r in mes.problemreports if not r.resolved
            ])
            mes.problemreports_form = ProblemReportForm(message=mes)

    return {
        'message': message
    }
