# 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
    #     r = Result.objects.filter(competition=self.competition, number__distance=self.number.distance).order_by('time')
    #     if r:
    #         zero_time = datetime.datetime.combine(datetime.date.today(), datetime.time(0, 0, 0, 0))
    #         delta = datetime.datetime.combine(datetime.date.today(), r[0].time) - zero_time
    #         self.loses_distance = datetime.datetime.combine(datetime.date.today(), self.time) - delta
    #     else:
    #         self.loses_distance = '00:00:00'
    #
    # def set_loses_group(self):
    #     r = Result.objects.filter(competition=self.competition, number__distance=self.number.distance, participant__group=self.participant.group).order_by('time')
    #     if r:
    #         zero_time = datetime.datetime.combine(datetime.date.today(), datetime.time(0, 0, 0, 0))
    #         delta = datetime.datetime.combine(datetime.date.today(), r[0].time) - zero_time
    #         self.loses_group = datetime.datetime.combine(datetime.date.today(), self.time) - delta
    #     else:
    #         self.loses_group = '00:00:00'

    def set_avg_speed(self):
        avg_speed = self.avg_speed
        if self.time:
            admin = DistanceAdmin.objects.get(competition=self.competition, distance=self.participant.distance)
            seconds = datetime.timedelta(hours=self.time.hour, minutes=self.time.minute, seconds=self.time.second).seconds
            if admin.distance_actual:
                self.avg_speed = round((float(admin.distance_actual) / float(seconds))*3.6, 1)
            if avg_speed != self.avg_speed:
                return True
        return False

    def get_competition_class(self):
        if not self._competition_class:

# 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

    #     r = Result.objects.filter(competition=self.competition, number__distance=self.number.distance).order_by('time')
    #     if r:
    #         zero_time = datetime.datetime.combine(datetime.date.today(), datetime.time(0, 0, 0, 0))
    #         delta = datetime.datetime.combine(datetime.date.today(), r[0].time) - zero_time
    #         self.loses_distance = datetime.datetime.combine(datetime.date.today(), self.time) - delta
    #     else:
    #         self.loses_distance = '00:00:00'
    #
    # def set_loses_group(self):
    #     r = Result.objects.filter(competition=self.competition, number__distance=self.number.distance, participant__group=self.participant.group).order_by('time')
    #     if r:
    #         zero_time = datetime.datetime.combine(datetime.date.today(), datetime.time(0, 0, 0, 0))
    #         delta = datetime.datetime.combine(datetime.date.today(), r[0].time) - zero_time
    #         self.loses_group = datetime.datetime.combine(datetime.date.today(), self.time) - delta
    #     else:
    #         self.loses_group = '00:00:00'

    def set_avg_speed(self):
        avg_speed = self.avg_speed
        if self.time:
            admin = DistanceAdmin.objects.get(competition=self.competition, distance=self.participant.distance)
            seconds = datetime.timedelta(hours=self.time.hour, minutes=self.time.minute, seconds=self.time.second).seconds
            if admin.distance_actual:
                self.avg_speed = round((float(admin.distance_actual) / float(seconds))*3.6, 1)
            if avg_speed != self.avg_speed:
                return True
        return False

    def get_competition_class(self):
        if not self._competition_class:

            class_ = load_class(self.competition.processing_class)