from django.db import models
from django.db.models import Q
from django.core.exceptions import ValidationError

from ..utils.models import BaseModel
from .constants import PERMISSION_CHOICES


class Board(BaseModel):
    name = models.CharField(max_length=255)

    account = models.ForeignKey('accounts.Account')
    created_by = models.ForeignKey('users.User')

    is_shared = models.BooleanField(default=False)

    thumbnail_sm_path = models.TextField(blank=True)
    thumbnail_md_path = models.TextField(blank=True)
    thumbnail_lg_path = models.TextField(blank=True)

    def __str__(self):
        return self.name

    def get_thumbnail_url(self, size='sm'):
        """
        Returns full thumbnail url.
        BUCKET_URL + thumbnail path
        """
        pass

    def is_user_collaborator(self, user, permission=None):
        """
        Returns `True` if a user is a collaborator on this
        Board, `False` otherwise. Optionally checks if a user
        is a collaborator with a specific permission.
        """
        collaborators = BoardCollaborator.objects.filter(board=self, user=user)

        if permission == 'read':
            collaborators = collaborators.filter(
                Q(permission='write') | Q(permission='read'))
        elif permission == 'write':
            collaborators = collaborators.filter(permission='write')

        return collaborators.exists()


class BoardCollaborator(BaseModel):
    board = models.ForeignKey('boards.Board')
    user = models.ForeignKey('users.User', blank=True, null=True)
    invited_user = models.ForeignKey('invitations.Inviteduser',
                                     blank=True, null=True)

    permission = models.CharField(max_length=5, choices=PERMISSION_CHOICES)

    def __str__(self):
        return str(self.user) if self.user else str(self.invited_user)

    def save(self, force_insert=False, force_update=False, **kwargs):
        """
        Performs all steps involved in validating  whenever
        a model object is saved, but not forced.
        """
        if not (force_insert or force_update):
            self.full_clean()

        return super(BoardCollaborator, self).save(
            force_insert, force_update, **kwargs)

    def clean(self):
        """
        Validates that either a user or an invited_user is set.
        """
        if self.user and self.invited_user:
            raise ValidationError(
                'Both user and invited_user cannot be set together.')
        elif not self.user and not self.invited_user:
            raise ValidationError('Either user or invited_user must be set.')

@@ -103,16 +103,100 @@
 ionError
+%0Afrom django.dispatch import receiver%0Afrom django.db.models.signals import post_save
 %0A%0Afrom .
@@ -267,16 +267,51 @@
 _CHOICES
+, READ_PERMISSION, WRITE_PERMISSION
 %0A%0A%0Aclass
@@ -1255,22 +1255,31 @@
 sion == 
-'read'
+READ_PERMISSION
 :%0A      
@@ -1351,23 +1351,32 @@
 mission=
-'write'
+WRITE_PERMISSION
 ) %7C Q(pe
@@ -1388,14 +1388,23 @@
 ion=
-'read'
+READ_PERMISSION
 ))%0A 
@@ -1429,23 +1429,32 @@
 sion == 
-'write'
+WRITE_PERMISSION
 :%0A      
@@ -1511,15 +1511,24 @@
 ion=
-'write'
+WRITE_PERMISSION
 )%0A%0A 
@@ -1566,16 +1566,342 @@
 sts()%0A%0A%0A
+@receiver(%5Bpost_save%5D, sender=Board)%0Adef create_owner_collaborator(instance, created=False, **kwargs):%0A    if created:%0A        account_owner = instance.account.owner%0A%0A        BoardCollaborator.objects.create(%0A            board=instance,%0A            user=account_owner.user,%0A            permission=WRITE_PERMISSION%0A        )%0A%0A%0A
 class Bo

Create a BoardCollaborator for account owner