# Path: velo/core/models.py
# class Log(models.Model):
#     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, blank=True)
#     object_id = models.PositiveIntegerField(null=True, blank=True)
#     content_object = GenericForeignKey('content_type', 'object_id')
# 
#     user = models.ForeignKey(User, null=True, blank=True)
#     action = models.CharField(max_length=50, blank=True)
#     message = models.CharField(max_length=255, blank=True)
#     params = JSONField(blank=True, null=True)
# 
#     created = models.DateTimeField(auto_now_add=True)
#     modified = models.DateTimeField(auto_now=True)
# 
#     def set_message(self, message, commit=True):
#         self.message = message
#         if commit:
#             self.save()
# 
#     def set_object(self, obj, commit=True):
#         self.content_object = obj
#         if commit:
#             self.save()
# 
# Path: velo/velo/utils.py
# def load_class(full_class_string):
#     """
#     dynamically load a class from a string
#     """
# 
#     class_data = full_class_string.split(".")
#     module_path = ".".join(class_data[:-1])
#     class_str = class_data[-1]
# 
#     module = importlib.import_module(module_path)
#     # Finally, we retrieve the Class
#     return getattr(module, class_str)


from django.db import models, transaction
from django.db.models import signals
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django_celery_beat.models import PeriodicTask, PeriodicTasks, CrontabSchedule
from easy_thumbnails.fields import ThumbnailerImageField
from slugify import slugify
from velo.core.models import Log
from velo.core.utils import restart_celerybeat
from velo.results.helper import time_to_seconds
from velo.velo.mixins.models import TimestampMixin
from velo.velo.utils import load_class
import os
import time
import datetime
import uuid

    distance_place_sum = models.IntegerField(blank=True, null=True)

    @property
    def results(self):
        ctype = ContentType.objects.get_for_model(self.__class__)
        return Result.objects.filter(standings_content_type__pk=ctype.id, standings_object_id=self.id)

    @property
    def stages_participated(self):
        stages = [1, 2, 3, 4, 5, 6, 7, 8]
        stages_participated = 0
        for stage in stages:
            if getattr(self, "distance_points%i" % stage, 0) and getattr(self, "distance_points%i" % stage, 0) > 0:
                stages_participated += 1
        return stages_participated

    def set_points(self):
        stages = [1, 2, 3, 4, 5, 6, 7, 8]
        mapping = {obj.id: index for index, obj in enumerate(self.competition.get_children(), start=1)}

        results = self.results

        for result in results:
            setattr(self, "group_points%i" % mapping.get(result.competition_id), result.points_group)
            setattr(self, "distance_points%i" % mapping.get(result.competition_id), result.points_distance)
            setattr(self, "distance_place%i" % mapping.get(result.competition_id), result.result_distance)
            try:
                stages.remove(mapping.get(result.competition_id))
            except:

# Path: velo/core/models.py
# class Log(models.Model):
#     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, blank=True)
#     object_id = models.PositiveIntegerField(null=True, blank=True)
#     content_object = GenericForeignKey('content_type', 'object_id')
# 
#     user = models.ForeignKey(User, null=True, blank=True)
#     action = models.CharField(max_length=50, blank=True)
#     message = models.CharField(max_length=255, blank=True)
#     params = JSONField(blank=True, null=True)
# 
#     created = models.DateTimeField(auto_now_add=True)
#     modified = models.DateTimeField(auto_now=True)
# 
#     def set_message(self, message, commit=True):
#         self.message = message
#         if commit:
#             self.save()
# 
#     def set_object(self, obj, commit=True):
#         self.content_object = obj
#         if commit:
#             self.save()
# 
# Path: velo/velo/utils.py
# def load_class(full_class_string):
#     """
#     dynamically load a class from a string
#     """
# 
#     class_data = full_class_string.split(".")
#     module_path = ".".join(class_data[:-1])
#     class_str = class_data[-1]
# 
#     module = importlib.import_module(module_path)
#     # Finally, we retrieve the Class
#     return getattr(module, class_str)


# Path: velo/results/models.py
from django.db import models, transaction
from django.db.models import signals
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django_celery_beat.models import PeriodicTask, PeriodicTasks, CrontabSchedule
from easy_thumbnails.fields import ThumbnailerImageField
from slugify import slugify
from velo.core.models import Log
from velo.core.utils import restart_celerybeat
from velo.results.helper import time_to_seconds
from velo.velo.mixins.models import TimestampMixin
from velo.velo.utils import load_class
import os
import time
import datetime
import uuid


    distance_place_sum = models.IntegerField(blank=True, null=True)

    @property
    def results(self):
        ctype = ContentType.objects.get_for_model(self.__class__)
        return Result.objects.filter(standings_content_type__pk=ctype.id, standings_object_id=self.id)

    @property
    def stages_participated(self):
        stages = [1, 2, 3, 4, 5, 6, 7, 8]
        stages_participated = 0
        for stage in stages:
            if getattr(self, "distance_points%i" % stage, 0) and getattr(self, "distance_points%i" % stage, 0) > 0:
                stages_participated += 1
        return stages_participated

    def set_points(self):
        stages = [1, 2, 3, 4, 5, 6, 7, 8]
        mapping = {obj.id: index for index, obj in enumerate(self.competition.get_children(), start=1)}

        results = self.results

        for result in results:
            setattr(self, "group_points%i" % mapping.get(result.competition_id), result.points_group)
            setattr(self, "distance_points%i" % mapping.get(result.competition_id), result.points_distance)
            setattr(self, "distance_place%i" % mapping.get(result.competition_id), result.result_distance)
            try:
                stages.remove(mapping.get(result.competition_id))
            except:

                Log.objects.create(content_object=self, message="Multiple results in stage %s" % result.competition)