# 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
        return '[%s] %s' % (self.id, self.body)

    def __str__(self):
        return '[%s] %s' % (self.id, self.body)

    class Meta:
        verbose_name = 'ответ'
        verbose_name_plural = 'Ответы на вопросы'


class UserExamination(models.Model):
    examination = models.ForeignKey(Examination, related_name='user_examinations', verbose_name='Тестирование')
    user = models.ForeignKey(User, related_name='user_examinations', verbose_name='Пользователь')

    points = models.PositiveSmallIntegerField(default=0, verbose_name='Баллы')

    available_from = models.DateTimeField(verbose_name='Тест доступен для прохождения от')
    complete_until = models.DateTimeField(db_index=True, verbose_name='Надо выполнить до')

    scheduler = models.ForeignKey('Scheduler', null=True, blank=True, on_delete=DO_NOTHING, related_name='user_examinaions')
    created_by = models.ForeignKey('User', null=True, blank=True, on_delete=DO_NOTHING, related_name='user_examinaions')

    started_at = models.DateTimeField(null=True, blank=True, verbose_name='Начат')
    must_finished_at = models.DateTimeField(null=True, blank=True, verbose_name='Обязан закончить до')
    finished_at = models.DateTimeField(null=True, blank=True, db_index=True, verbose_name='Закончен')

    created_at = models.DateTimeField(auto_now_add=True)
    deleted_at = models.DateTimeField(null=True, blank=True, db_index=True)

    objects = ExcludeDeletedManager()

# 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

        return '[%s] %s' % (self.id, self.body)

    def __str__(self):
        return '[%s] %s' % (self.id, self.body)

    class Meta:
        verbose_name = 'ответ'
        verbose_name_plural = 'Ответы на вопросы'


class UserExamination(models.Model):
    examination = models.ForeignKey(Examination, related_name='user_examinations', verbose_name='Тестирование')
    user = models.ForeignKey(User, related_name='user_examinations', verbose_name='Пользователь')

    points = models.PositiveSmallIntegerField(default=0, verbose_name='Баллы')

    available_from = models.DateTimeField(verbose_name='Тест доступен для прохождения от')
    complete_until = models.DateTimeField(db_index=True, verbose_name='Надо выполнить до')

    scheduler = models.ForeignKey('Scheduler', null=True, blank=True, on_delete=DO_NOTHING, related_name='user_examinaions')
    created_by = models.ForeignKey('User', null=True, blank=True, on_delete=DO_NOTHING, related_name='user_examinaions')

    started_at = models.DateTimeField(null=True, blank=True, verbose_name='Начат')
    must_finished_at = models.DateTimeField(null=True, blank=True, verbose_name='Обязан закончить до')
    finished_at = models.DateTimeField(null=True, blank=True, db_index=True, verbose_name='Закончен')

    created_at = models.DateTimeField(auto_now_add=True)
    deleted_at = models.DateTimeField(null=True, blank=True, db_index=True)

    objects = ExcludeDeletedManager()

    default_objects = DefaultManager()