# MIT License
# Copyright (c) 2017 MassChallenge, Inc.

from django.db.models import Q
from impact.v1.helpers.criterion_helper import CriterionHelper


class MatchingCriterionHelper(CriterionHelper):
    def __init__(self, subject):
        super().__init__(subject)
        self._app_ids_to_targets = {}
        self._target_counts = {}

    def app_count(self, apps, option_name):
        return self.target_counts(apps).get(option_name, 0)

    def find_app_ids(self, feedbacks, apps, target):
        if not feedbacks:
            return []
        result = []
        app_map = self.app_ids_to_targets(apps)
        return [app_id for app_id in
                feedbacks.values_list("application_id", flat=True)
                if app_id in app_map and app_map[app_id] == target.id]

    def app_ids_to_targets(self, apps):
        self._check_cache(apps)
        return self._app_ids_to_targets

    def target_counts(self, apps):
        self._check_cache(apps)
        return self._target_counts

    def _check_cache(self, apps):
        if not self._app_ids_to_targets:
            self.calc_app_ids_to_targets(apps)

@@ -53,39 +53,8 @@
 c.%0A%0A
-from django.db.models import Q%0A
 from
@@ -511,28 +511,8 @@
  %5B%5D%0A
-        result = %5B%5D%0A
     

Fix pycodestyle issues