# Path: core/managers.py
# class UserDefaultManager(UserManager):
#     pass
# 
# class UserExcludeDeletedManager(UserManager):
#     def get_queryset(self):
#         return super(UserExcludeDeletedManager, self).get_queryset().filter(deleted_at__isnull=True).order_by('deleted_at')
# 
# class DefaultManager(models.Manager):
#     pass
# 
# class ExcludeDeletedManager(models.Manager):
#     def get_queryset(self):
#         return super(ExcludeDeletedManager, self).get_queryset().filter(deleted_at__isnull=True).order_by('deleted_at')
# 
# Path: core/fields.py
# class JSONField(models.TextField):
#     def __init__(self, *args, **kwargs):
#         default = kwargs.get('default', '{}')
#         if isinstance(default, (list, dict)):
#             kwargs['default'] = json.dumps(default)
#         models.TextField.__init__(self, *args, **kwargs)
# 
#     def to_python(self, value):
#         """Convert our string value to JSON after we load it from the DB"""
#         if value is None or value == '':
#             return {}
#         return json.loads(value)
# 
#     def get_db_prep_save(self, value, connection, **kwargs):
#         """Convert our JSON object to a string before we save"""
#         if value is None and self.null:
#             return None
#         # default values come in as strings; only non-strings should be
#         # run through `dumps`
#         if not isinstance(value, six.string_types):
#             value = json.dumps(value)
#         return value
# 
#     def deconstruct(self):
#         name, path, args, kwargs = super(JSONField, self).deconstruct()
#         if self.default == '{}':
#             del kwargs['default']
#         return name, path, args, kwargs


import datetime
import json
import string
import random
from dateutil.relativedelta import relativedelta
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.db.models import DO_NOTHING, Q
from core.managers import UserDefaultManager, UserExcludeDeletedManager, DefaultManager, ExcludeDeletedManager
from core.fields import JSONField
from django.forms import model_to_dict

        questions_count = len(question_logs)
        point_for_one_right_answer = float(100) / questions_count

        for question_log in question_logs:

            right_answers_count = question_log.get_right_answers_count()

            right_answers_user_count = 0
            for answer in question_log.user_examination_answer_logs.all():
                right_answers_user_count += answer.is_right

            if right_answers_user_count > 0:
                points += point_for_one_right_answer * (float(right_answers_user_count) / right_answers_count)

        self.points = points

        if commit:
            self.save()

        return self


class UserExaminationQuestionLog(models.Model):
    user_examination = models.ForeignKey(UserExamination, related_name='logs')
    question = models.ForeignKey(Question, on_delete=DO_NOTHING, related_name='logs')

    position = models.PositiveSmallIntegerField()

    question_data = JSONField(default={})

# Path: core/managers.py
# class UserDefaultManager(UserManager):
#     pass
# 
# class UserExcludeDeletedManager(UserManager):
#     def get_queryset(self):
#         return super(UserExcludeDeletedManager, self).get_queryset().filter(deleted_at__isnull=True).order_by('deleted_at')
# 
# class DefaultManager(models.Manager):
#     pass
# 
# class ExcludeDeletedManager(models.Manager):
#     def get_queryset(self):
#         return super(ExcludeDeletedManager, self).get_queryset().filter(deleted_at__isnull=True).order_by('deleted_at')
# 
# Path: core/fields.py
# class JSONField(models.TextField):
#     def __init__(self, *args, **kwargs):
#         default = kwargs.get('default', '{}')
#         if isinstance(default, (list, dict)):
#             kwargs['default'] = json.dumps(default)
#         models.TextField.__init__(self, *args, **kwargs)
# 
#     def to_python(self, value):
#         """Convert our string value to JSON after we load it from the DB"""
#         if value is None or value == '':
#             return {}
#         return json.loads(value)
# 
#     def get_db_prep_save(self, value, connection, **kwargs):
#         """Convert our JSON object to a string before we save"""
#         if value is None and self.null:
#             return None
#         # default values come in as strings; only non-strings should be
#         # run through `dumps`
#         if not isinstance(value, six.string_types):
#             value = json.dumps(value)
#         return value
# 
#     def deconstruct(self):
#         name, path, args, kwargs = super(JSONField, self).deconstruct()
#         if self.default == '{}':
#             del kwargs['default']
#         return name, path, args, kwargs


# Path: core/models.py
import datetime
import json
import string
import random
from dateutil.relativedelta import relativedelta
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.db.models import DO_NOTHING, Q
from core.managers import UserDefaultManager, UserExcludeDeletedManager, DefaultManager, ExcludeDeletedManager
from core.fields import JSONField
from django.forms import model_to_dict


        questions_count = len(question_logs)
        point_for_one_right_answer = float(100) / questions_count

        for question_log in question_logs:

            right_answers_count = question_log.get_right_answers_count()

            right_answers_user_count = 0
            for answer in question_log.user_examination_answer_logs.all():
                right_answers_user_count += answer.is_right

            if right_answers_user_count > 0:
                points += point_for_one_right_answer * (float(right_answers_user_count) / right_answers_count)

        self.points = points

        if commit:
            self.save()

        return self


class UserExaminationQuestionLog(models.Model):
    user_examination = models.ForeignKey(UserExamination, related_name='logs')
    question = models.ForeignKey(Question, on_delete=DO_NOTHING, related_name='logs')

    position = models.PositiveSmallIntegerField()

    question_data = JSONField(default={})

    question_answers_data = JSONField(default=[])