from braces.views import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.shortcuts import (
    get_object_or_404,
    redirect
)

from contacts.models import (
    Book,
    BookOwner,
)


class BookOwnerMixin(LoginRequiredMixin):

    def get_queryset(self):
        if self.kwargs.get('book'):
            try:
                bookowner = BookOwner.objects.get(
                    user=self.request.user,
                    book_id=self.kwargs.get('book'),
                )
                return self.model.objects.for_user(
                    self.request.user, book=bookowner.book,
                )
            except BookOwner.DoesNotExist:
                pass
        return self.model.objects.for_user(self.request.user)

    def get_object(self, queryset=None):
        instance = super(BookOwnerMixin, self).get_object(queryset)

        if not instance.can_be_viewed_by(self.request.user):
            raise PermissionDenied

        return instance

    def form_valid(self, form):
        form.instance.book = BookOwner.objects.get(
            user=self.request.user
        ).book
        response = super(BookOwnerMixin, self).form_valid(form)

        return response

from braces.views import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.shortcuts import (
    get_object_or_404,
    redirect
)

from contacts.models import (
    Book,
    BookOwner,
)


class BookOwnerMixin(LoginRequiredMixin):

    def get_queryset(self):
        if self.kwargs.get('book'):
            try:
                bookowner = BookOwner.objects.get(
                    user=self.request.user,
                    book_id=self.kwargs.get('book'),
                )
                return self.model.objects.for_user(
                    self.request.user, book=bookowner.book,
                )
            except BookOwner.DoesNotExist:
                pass
        return self.model.objects.for_user(self.request.user)

    def get_object(self, queryset=None):
        instance = super(BookOwnerMixin, self).get_object(queryset)

        if not instance.can_be_viewed_by(self.request.user):
            raise PermissionDenied

        return instance

    def form_valid(self, form):
        try:
            form.instance.book = BookOwner.objects.get(
                user=self.request.user
            ).book
        except BookOwner.DoesNotExist:
            form.instance.book = Book.objects.create(
                name="{}'s Contacts".format(self.request.user),
            )
            BookOwner.objects.create(book=form.instance.book, user=self.request.user)
        return super(BookOwnerMixin, self).form_valid(form)

Create book if one doesn't exist during tag creation
Create book if one doesn't exist during tag creation

  from braces.views import LoginRequiredMixin
  from django.core.exceptions import PermissionDenied
  from django.shortcuts import (
      get_object_or_404,
      redirect
  )
  
  from contacts.models import (
      Book,
      BookOwner,
  )
  
  
  class BookOwnerMixin(LoginRequiredMixin):
  
      def get_queryset(self):
          if self.kwargs.get('book'):
              try:
                  bookowner = BookOwner.objects.get(
                      user=self.request.user,
                      book_id=self.kwargs.get('book'),
                  )
                  return self.model.objects.for_user(
                      self.request.user, book=bookowner.book,
                  )
              except BookOwner.DoesNotExist:
                  pass
          return self.model.objects.for_user(self.request.user)
  
      def get_object(self, queryset=None):
          instance = super(BookOwnerMixin, self).get_object(queryset)
  
          if not instance.can_be_viewed_by(self.request.user):
              raise PermissionDenied
  
          return instance
  
      def form_valid(self, form):
+         try:
-         form.instance.book = BookOwner.objects.get(
+             form.instance.book = BookOwner.objects.get(
-             user=self.request.user
+                 user=self.request.user
-         ).book
+             ).book
+         except BookOwner.DoesNotExist:
+             form.instance.book = Book.objects.create(
+                 name="{}'s Contacts".format(self.request.user),
+             )
+             BookOwner.objects.create(book=form.instance.book, user=self.request.user)
-         response = super(BookOwnerMixin, self).form_valid(form)
+         return super(BookOwnerMixin, self).form_valid(form)
  
-         return response
- 
Create book if one doesn't exist during tag creation
## Code Before:
from braces.views import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.shortcuts import (
    get_object_or_404,
    redirect
)

from contacts.models import (
    Book,
    BookOwner,
)


class BookOwnerMixin(LoginRequiredMixin):

    def get_queryset(self):
        if self.kwargs.get('book'):
            try:
                bookowner = BookOwner.objects.get(
                    user=self.request.user,
                    book_id=self.kwargs.get('book'),
                )
                return self.model.objects.for_user(
                    self.request.user, book=bookowner.book,
                )
            except BookOwner.DoesNotExist:
                pass
        return self.model.objects.for_user(self.request.user)

    def get_object(self, queryset=None):
        instance = super(BookOwnerMixin, self).get_object(queryset)

        if not instance.can_be_viewed_by(self.request.user):
            raise PermissionDenied

        return instance

    def form_valid(self, form):
        form.instance.book = BookOwner.objects.get(
            user=self.request.user
        ).book
        response = super(BookOwnerMixin, self).form_valid(form)

        return response

## Instruction:
Create book if one doesn't exist during tag creation
## Code After:
from braces.views import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.shortcuts import (
    get_object_or_404,
    redirect
)

from contacts.models import (
    Book,
    BookOwner,
)


class BookOwnerMixin(LoginRequiredMixin):

    def get_queryset(self):
        if self.kwargs.get('book'):
            try:
                bookowner = BookOwner.objects.get(
                    user=self.request.user,
                    book_id=self.kwargs.get('book'),
                )
                return self.model.objects.for_user(
                    self.request.user, book=bookowner.book,
                )
            except BookOwner.DoesNotExist:
                pass
        return self.model.objects.for_user(self.request.user)

    def get_object(self, queryset=None):
        instance = super(BookOwnerMixin, self).get_object(queryset)

        if not instance.can_be_viewed_by(self.request.user):
            raise PermissionDenied

        return instance

    def form_valid(self, form):
        try:
            form.instance.book = BookOwner.objects.get(
                user=self.request.user
            ).book
        except BookOwner.DoesNotExist:
            form.instance.book = Book.objects.create(
                name="{}'s Contacts".format(self.request.user),
            )
            BookOwner.objects.create(book=form.instance.book, user=self.request.user)
        return super(BookOwnerMixin, self).form_valid(form)
