"""Models for imager_profile."""

from django.db import models
from django.contrib.auth.models import User, Group
from django.utils.encoding import python_2_unicode_compatible
from django.db.models.signals import post_save
from django.dispatch import receiver

# Create your models here.


class ActiveProfileManager(models.Manager):  # pragma: no cover
    """Create Model Manager for Active Profiles."""

    def get_queryset(self):
        """Return active users."""
        qs = super(ActiveProfileManager, self).get_queryset()
        return qs.filter(user__is_active__exact=True)


@python_2_unicode_compatible
class ImagerProfile(models.Model):
    """The imager user and all their attributes."""

    objects = models.Manager()
    active = ActiveProfileManager()

    user = models.OneToOneField(
        User,
        related_name="profile",
        on_delete=models.CASCADE
    )
    CAMERA_CHOICES = [
        ('Nikon', 'Nikon'),
        ('iPhone', 'iPhone'),
        ('Canon', 'Canon')
    ]
    camera_type = models.CharField(
        max_length=10,
        choices=CAMERA_CHOICES,
        default=""
    )
    address = models.CharField(max_length=70, default="")
    bio = models.TextField(default="")
    personal_website = models.URLField(default="")
    for_hire = models.BooleanField(default=False)
    travel_distance = models.IntegerField(default=0)
    phone_number = models.CharField(max_length=20, default="")
    STYLE_CHOICES = [
        ('Portrait', 'Portrait'),
        ('Landscape', 'Landscape'),
        ('Black and White', 'Black and White'),
        ('Sport', 'Sport')
    ]
    photography_type = models.CharField(
        max_length=20,
        choices=STYLE_CHOICES,
        default=""
    )

    @property
    def is_active(self):  # pragma: no cover
        """Return True if user associated with this profile is active."""
        return self.user.is_active

    def __str__(self):
        """Display user data as a string."""
        return "User: {}, Camera: {}, Address: {}, Phone number: {}, For Hire? {}, Photography style: {}".format(self.user, self.camera_type, self.address, self.phone_number, self.for_hire, self.photography_type)


@receiver(post_save, sender=User)
def make_profile_for_user(sender, instance, **kwargs):
    """Called when user is made and hooks that user to a profile."""
    if kwargs["created"]:
        # import pdb; pdb.set_trace()
        group = Group.objects.get(name="user")
        instance.groups.add(group)
        new_profile = ImagerProfile(user=instance)
        new_profile.save()

@@ -106,16 +106,87 @@
 r, Group
+, Permission%0Afrom django.contrib.contenttypes.models import ContentType
 %0Afrom dj
@@ -286,16 +286,30 @@
 ost_save
+, post_migrate
 %0Afrom dj
@@ -342,35 +342,98 @@
 ver%0A
-%0A# Create your models here.
+from django.contrib.auth.management import create_permissions%0Afrom django.apps import apps
 %0A%0A%0Ac
@@ -2510,46 +2510,8 @@
 %22%5D:%0A
-        # import pdb; pdb.set_trace()%0A
     
@@ -2553,16 +2553,16 @@
 %22user%22)%0A
+
         
@@ -2639,16 +2639,16 @@
 stance)%0A
-
         
@@ -2666,8 +2666,649 @@
 .save()%0A
+%0A%0A@receiver(post_migrate)%0Adef create_group(**kwargs):%0A    %22%22%22On migration create group if it doesn't exists.%22%22%22%0A    for app_config in apps.get_app_configs():%0A        app_config.models_module = True%0A        create_permissions(app_config, apps=apps, verbosity=0)%0A        app_config.models_module = None%0A    group, created = Group.objects.get_or_create(name='user')%0A    imager_content_type = ContentType.objects.get(app_label='imager_profile')%0A    permissions = Permission.objects.filter(content_type=imager_content_type)%0A    if created:%0A        for permission in permissions:%0A            group.permissions.add(permission)%0A        group.save()%0A

add way to create group user on migration