# Path: apps/core/models.py
# class EnumResult:
#     UNKNOW = 0
#     ABOART = 1
#     WAIT = 2
#     WARN = 3
#     FAIL = 4
#     PASS = 5
#     NEW = 6
#     CANCEL = 7
#     SCHEDULED = 8
#     PANIC = 9
#     FAILINSTALL = 10
#     SKIP = 11
# 
#     def __init__(self):
#         self.enums = dict(RESULT_CHOICES)
# 
#     def _get(self, value):
#         if isinstance(value, int):
#             return self.enums.get(value)
#         elif isinstance(value, str):
#             return [it[0] for it in self.enums.items() if it[1] == value.lower()][0]
# 
#     @staticmethod
#     def choices():
#         er = EnumResult()
#         return er.enums.items()
# 
#     @staticmethod
#     def get(value):
#         er = EnumResult()
#         return er._get(value)
# 
#     @staticmethod
#     def broken_list():
#         return [EnumResult.FAIL, EnumResult.FAILINSTALL, EnumResult.ABOART, EnumResult.PANIC, EnumResult.WARN]
# 
# class Task(models.Model):
#     uid = models.CharField("Task ID", max_length=12, unique=True)
#     recipe = models.ForeignKey(Recipe, related_name="tasks")
#     test = models.ForeignKey(Test)
#     result = models.SmallIntegerField(choices=RESULT_CHOICES,
#                                       default=UNKNOW, db_index=True)
#     status = models.SmallIntegerField(
#         choices=Recipe.STATUS_CHOICES, default=UNKNOW)
#     duration = models.FloatField(default=-1.)
#     datestart = models.DateTimeField(null=True, blank=True)
#     statusbyuser = models.SmallIntegerField(
#         choices=USERSTATUS_CHOICES, default=NONE)
#     alias = models.CharField(max_length=32, blank=True, null=True)
# 
#     def __unicode__(self):
#         return self.uid
# 
#     def logfiles(self):
#         return list(FileLog.objects.filter(task=self).values("path"))
# 
#     def set_result(self, value):
#         try:
#             self.result = [it[0]
#                            for it in RESULT_CHOICES if it[1] == value.lower()][0]
#         except IndexError:
#             logger.error("IndexError: Task result %s %s %s" %
#                          (value, self.result, RESULT_CHOICES))
# 
#     def get_result(self):
#         if self.statusbyuser == WAIVED:
#             return [it[1] for it in USERSTATUS_CHOICES if it[0] == WAIVED][0]
#         return [it[1] for it in RESULT_CHOICES if it[0] == self.result][0]
# 
#     def set_status(self, value):
#         try:
#             self.status = [it[0]
#                            for it in Recipe.STATUS_CHOICES if it[1].lower() == value.lower()][0]
#         except IndexError:
#             logger.error("IndexError: Task status %s %s %s" %
#                          (value, self.status, Recipe.STATUS_CHOICES))
# 
#     def is_completed(self):
#         return (self.status == Recipe.COMPLETED)
# 
#     def set_waived(self):
#         self.statusbyuser = WAIVED
#         self.save()
#         self.recipe.recount_result()
#         self.recipe.save()
# 
#     def get_uid(self):
#         return self.uid.replace("T:", "")


import json
import logging
from django.core.exceptions import MultipleObjectsReturned
from django.db.models import Count
from django.db.models.signals import post_save
from django.dispatch import receiver
from apps.core.models import EnumResult, Task
from models import Score




logger = logging.getLogger("main")

@receiver(post_save, sender=Task)
def recount_test_score(sender, **kwargs):
    task = kwargs["instance"]
    if not task.recipe.job.schedule:
        return

    for tag in task.recipe.job.template.tags.all():

# Path: apps/core/models.py
# class EnumResult:
#     UNKNOW = 0
#     ABOART = 1
#     WAIT = 2
#     WARN = 3
#     FAIL = 4
#     PASS = 5
#     NEW = 6
#     CANCEL = 7
#     SCHEDULED = 8
#     PANIC = 9
#     FAILINSTALL = 10
#     SKIP = 11
# 
#     def __init__(self):
#         self.enums = dict(RESULT_CHOICES)
# 
#     def _get(self, value):
#         if isinstance(value, int):
#             return self.enums.get(value)
#         elif isinstance(value, str):
#             return [it[0] for it in self.enums.items() if it[1] == value.lower()][0]
# 
#     @staticmethod
#     def choices():
#         er = EnumResult()
#         return er.enums.items()
# 
#     @staticmethod
#     def get(value):
#         er = EnumResult()
#         return er._get(value)
# 
#     @staticmethod
#     def broken_list():
#         return [EnumResult.FAIL, EnumResult.FAILINSTALL, EnumResult.ABOART, EnumResult.PANIC, EnumResult.WARN]
# 
# class Task(models.Model):
#     uid = models.CharField("Task ID", max_length=12, unique=True)
#     recipe = models.ForeignKey(Recipe, related_name="tasks")
#     test = models.ForeignKey(Test)
#     result = models.SmallIntegerField(choices=RESULT_CHOICES,
#                                       default=UNKNOW, db_index=True)
#     status = models.SmallIntegerField(
#         choices=Recipe.STATUS_CHOICES, default=UNKNOW)
#     duration = models.FloatField(default=-1.)
#     datestart = models.DateTimeField(null=True, blank=True)
#     statusbyuser = models.SmallIntegerField(
#         choices=USERSTATUS_CHOICES, default=NONE)
#     alias = models.CharField(max_length=32, blank=True, null=True)
# 
#     def __unicode__(self):
#         return self.uid
# 
#     def logfiles(self):
#         return list(FileLog.objects.filter(task=self).values("path"))
# 
#     def set_result(self, value):
#         try:
#             self.result = [it[0]
#                            for it in RESULT_CHOICES if it[1] == value.lower()][0]
#         except IndexError:
#             logger.error("IndexError: Task result %s %s %s" %
#                          (value, self.result, RESULT_CHOICES))
# 
#     def get_result(self):
#         if self.statusbyuser == WAIVED:
#             return [it[1] for it in USERSTATUS_CHOICES if it[0] == WAIVED][0]
#         return [it[1] for it in RESULT_CHOICES if it[0] == self.result][0]
# 
#     def set_status(self, value):
#         try:
#             self.status = [it[0]
#                            for it in Recipe.STATUS_CHOICES if it[1].lower() == value.lower()][0]
#         except IndexError:
#             logger.error("IndexError: Task status %s %s %s" %
#                          (value, self.status, Recipe.STATUS_CHOICES))
# 
#     def is_completed(self):
#         return (self.status == Recipe.COMPLETED)
# 
#     def set_waived(self):
#         self.statusbyuser = WAIVED
#         self.save()
#         self.recipe.recount_result()
#         self.recipe.save()
# 
#     def get_uid(self):
#         return self.uid.replace("T:", "")


# Path: apps/report/receivers.py
import json
import logging
from django.core.exceptions import MultipleObjectsReturned
from django.db.models import Count
from django.db.models.signals import post_save
from django.dispatch import receiver
from apps.core.models import EnumResult, Task
from models import Score





logger = logging.getLogger("main")

@receiver(post_save, sender=Task)
def recount_test_score(sender, **kwargs):
    task = kwargs["instance"]
    if not task.recipe.job.schedule:
        return

    for tag in task.recipe.job.template.tags.all():

        results = Task.objects.filter(test=task.test,