# Generated by Django 3.2.5 on 2021-08-02 04:53

import datetime

import ckeditor.fields
import django.contrib.postgres.fields.citext
import django.core.validators
import django.db.models.deletion
import imagekit.models.fields
from django.conf import settings
from django.contrib.auth.management import create_permissions
from django.db import migrations, models

import config.settings.common.paths
from apps.users.constants import (
    DEFAULT_USER_PERMISSIONS,
    DEFAULT_STUDENT_PERMISSIONS,
    DEFAULT_MENTOR_PERMISSIONS
)


def insert_default_groups(apps, schema_editor):
    """Add default groups and their permissions."""
    default_groups = (
        DEFAULT_USER_PERMISSIONS,
        DEFAULT_STUDENT_PERMISSIONS,
        DEFAULT_MENTOR_PERMISSIONS,
    )
    group_class = apps.get_model('auth', 'Group')
    permission_class = apps.get_model('auth', 'Permission')

    for group in default_groups:
        new_group = group_class.objects.create(name=group["name"])

        for app_config in apps.get_app_configs():
            app_config.models_module = True
            create_permissions(app_config, apps=apps, verbosity=0)
            app_config.models_module = None

        perms = permission_class.objects.filter(
            codename__in=group["permissions"]
        )
        new_group.permissions.add(*perms)
        new_group.save()


def revert_default_groups(apps, schema_editor):
    """Rollback `insert_default_groups` changes in migration."""
    default_groups = (
        DEFAULT_USER_PERMISSIONS,
        DEFAULT_STUDENT_PERMISSIONS,
        DEFAULT_MENTOR_PERMISSIONS,
    )
    group_class = apps.get_model('auth', 'Group')
    for group in default_groups:
        group_class.objects.delete(name=group["name"])


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('contenttypes', '0002_remove_content_type_name'),
    ]

    operations = [
        migrations.CreateModel(
            name='Chapter',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('order', models.PositiveIntegerField(db_index=True, editable=False, verbose_name='order')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
                ('modified_at', models.DateTimeField(auto_now=True, verbose_name='modified at')),
                ('title', models.CharField(max_length=100, verbose_name='Title')),
                ('description', ckeditor.fields.RichTextField(blank=True, max_length=400, null=True, verbose_name='Description')),
                ('is_hidden', models.BooleanField(default=False, verbose_name='Is hidden')),
                ('start_date', models.DateField(validators=[django.core.validators.MinValueValidator(datetime.date.today)], verbose_name='Start date')),
                ('finish_date', models.DateField(validators=[django.core.validators.MinValueValidator(datetime.date.today)], verbose_name='Finish date')),
            ],
            options={
                'verbose_name': 'Chapter',
                'verbose_name_plural': 'Chapters',
                'ordering': ('order',),
            },
        ),
        migrations.CreateModel(
            name='Tag',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', django.contrib.postgres.fields.citext.CICharField(max_length=35, unique=True, verbose_name='Name')),
            ],
        ),
        migrations.CreateModel(
            name='Topic',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('order', models.PositiveIntegerField(db_index=True, editable=False, verbose_name='order')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
                ('modified_at', models.DateTimeField(auto_now=True, verbose_name='modified at')),
                ('title', models.CharField(max_length=100, verbose_name='Title')),
                ('description', ckeditor.fields.RichTextField(blank=True, max_length=400, null=True, verbose_name='Description')),
                ('chapter', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='topics', to='courses.chapter', verbose_name='Chapter')),
                ('speaker', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='topics', to=settings.AUTH_USER_MODEL, verbose_name='Speaker')),
                ('tags', models.ManyToManyField(blank=True, related_name='topics', to='courses.Tag', verbose_name='Tags')),
            ],
            options={
                'verbose_name': 'Topic',
                'verbose_name_plural': 'Topics',
            },
        ),
        migrations.CreateModel(
            name='Task',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('order', models.PositiveIntegerField(db_index=True, editable=False, verbose_name='order')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
                ('modified_at', models.DateTimeField(auto_now=True, verbose_name='modified at')),
                ('title', models.CharField(max_length=100, verbose_name='Title')),
                ('description', ckeditor.fields.RichTextField(max_length=16000, verbose_name='Description')),
                ('is_hidden', models.BooleanField(default=False, verbose_name='Is hidden')),
                ('topic', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tasks', to='courses.topic', verbose_name='Topic')),
            ],
            options={
                'verbose_name': 'Task',
                'verbose_name_plural': 'Tasks',
                'permissions': (('can_rate_tasks', 'Can rate students tasks.'),),
            },
        ),
        migrations.CreateModel(
            name='Course',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
                ('modified_at', models.DateTimeField(auto_now=True, verbose_name='modified at')),
                ('title', models.CharField(max_length=100, verbose_name='Title')),
                ('description', ckeditor.fields.RichTextField(max_length=400, verbose_name='Description')),
                ('image', imagekit.models.fields.ProcessedImageField(default='course_default.jpg', upload_to=config.settings.common.paths._default_media_path, verbose_name='Image')),
                ('is_hidden', models.BooleanField(default=True, verbose_name='Is hidden')),
                ('start_date', models.DateField(validators=[django.core.validators.MinValueValidator(datetime.date.today)], verbose_name='Start date')),
                ('finish_date', models.DateField(validators=[django.core.validators.MinValueValidator(datetime.date.today)], verbose_name='Finish date')),
                ('users', models.ManyToManyField(blank=True, related_name='courses', to=settings.AUTH_USER_MODEL, verbose_name='Users')),
            ],
            options={
                'verbose_name': 'Course',
                'verbose_name_plural': 'Courses',
            },
        ),
        migrations.CreateModel(
            name='Comment',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
                ('modified_at', models.DateTimeField(auto_now=True, verbose_name='modified at')),
                ('object_id', models.PositiveIntegerField()),
                ('text', ckeditor.fields.RichTextField(max_length=6800, verbose_name='Text')),
                ('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')),
                ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='sub_comments', to='courses.comment', verbose_name='Parent')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='users.User')),
            ],
            options={
                'verbose_name': 'Comment',
                'verbose_name_plural': 'Comments',
                'permissions': (('can_change_others', 'Can change other users attachments.'), ('can_delete_others', 'Can delete other users attachments.')),
            },
        ),
        migrations.AddField(
            model_name='chapter',
            name='course',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='chapters', to='courses.course', verbose_name='Course'),
        ),
        migrations.CreateModel(
            name='Attachment',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
                ('modified_at', models.DateTimeField(auto_now=True, verbose_name='modified at')),
                ('object_id', models.PositiveIntegerField()),
                ('title', models.CharField(blank=True, max_length=100, verbose_name='Title')),
                ('file', models.FileField(upload_to='attachments/files/', verbose_name='File')),
                ('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')),
            ],
            options={
                'verbose_name': 'Attachment',
                'verbose_name_plural': 'Attachments',
                'permissions': (('can_change_others', 'Can change other users attachments.'), ('can_delete_others', 'Can delete other users attachments.')),
            },
        ),
        migrations.CreateModel(
            name='Rating',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
                ('modified_at', models.DateTimeField(auto_now=True, verbose_name='modified at')),
                ('object_id', models.PositiveIntegerField()),
                ('mark', models.PositiveSmallIntegerField(validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(10)], verbose_name='Mark')),
                ('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='User')),
            ],
            options={
                'verbose_name': 'Rating',
                'verbose_name_plural': 'Ratings',
                'unique_together': {('content_type', 'object_id', 'user')},
            },
        ),
        migrations.RunPython(insert_default_groups, revert_default_groups)
    ]
