# 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
class Department(models.Model):
    """ Отдел """
    name = models.CharField(max_length=255, unique=True, verbose_name='Название')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children',
                               verbose_name='Родительский отдел')
    responsible = models.ManyToManyField(User, related_name='departments_owner', blank=True,
                                         verbose_name='Ответственные')
    employees = models.ManyToManyField(User, related_name='departments', blank=True, verbose_name='Сотрудники отдела')
    deleted_at = models.DateTimeField(null=True, blank=True, db_index=True)

    objects = ExcludeDeletedManager()
    default_objects = DefaultManager()

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'подразделение'
        verbose_name_plural = 'Подразделения'


class Examination(models.Model):
    name = models.CharField(max_length=255, verbose_name='Название')
    minutes_to_pass = models.PositiveSmallIntegerField(default=30, verbose_name='Сколько минут дано на тест')
    department = models.ForeignKey(Department, related_name='examinations', verbose_name='Отдел')
    deleted_at = models.DateTimeField(null=True, blank=True, db_index=True)


# 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

class Department(models.Model):
    """ Отдел """
    name = models.CharField(max_length=255, unique=True, verbose_name='Название')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children',
                               verbose_name='Родительский отдел')
    responsible = models.ManyToManyField(User, related_name='departments_owner', blank=True,
                                         verbose_name='Ответственные')
    employees = models.ManyToManyField(User, related_name='departments', blank=True, verbose_name='Сотрудники отдела')
    deleted_at = models.DateTimeField(null=True, blank=True, db_index=True)

    objects = ExcludeDeletedManager()
    default_objects = DefaultManager()

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'подразделение'
        verbose_name_plural = 'Подразделения'


class Examination(models.Model):
    name = models.CharField(max_length=255, verbose_name='Название')
    minutes_to_pass = models.PositiveSmallIntegerField(default=30, verbose_name='Сколько минут дано на тест')
    department = models.ForeignKey(Department, related_name='examinations', verbose_name='Отдел')
    deleted_at = models.DateTimeField(null=True, blank=True, db_index=True)


    objects = ExcludeDeletedManager()