# Path: test/models.py
# class Author(models.Model):
#     name = models.CharField("Name", max_length=128)
#     age = models.SmallIntegerField("Age", null=True, blank=True)
# 
#     class Meta:
#         ordering = ('name',)
#         verbose_name = "Author"
#         verbose_name_plural = "Authors"
# 
#     def __str__(self):
#         return self.name
# 
#     def double_age(self):
#         return self.age*2
#     double_age.short_description = "Double Age"
# 
# class Book(models.Model):
#     title = models.CharField("Title", max_length=128)
#     author = models.ForeignKey(Author, on_delete=models.CASCADE)
#     uuid = models.UUIDField(default=uuid.uuid4)
# 
#     class Meta:
#         ordering = ('title',)
#         verbose_name = "Book"
#         verbose_name_plural = "Books"
# 
#     def __str__(self):
#         return self.title
# 
# Path: test/views.py
# class AuthorCrudViewset(PopupCrudViewSet):
#     model = Author
#     fields = ('name', 'age')
#     list_display = ('name', 'age', 'half_age', 'double_age')
#     list_url = reverse_lazy("authors")
#     new_url = reverse_lazy("new-author")
#     page_title = "Author List"
# 
#     """
#     form_class = AuthorForm
#     list_permission_required = ('tests.add_author',)
#     create_permission_required = ('tests.add_author',)
#     update_permission_required = ('tests.change_author',)
#     delete_permission_required = ('tests.delete_author',)
#     """
# 
#     def half_age(self, author):
#         return int(author.age/2)
#     half_age.short_description = "Half Age"
#     half_age.order_field = 'age'
# 
#     def get_detail_url(self, obj):
#         return reverse("author-detail", kwargs={'pk': obj.pk})
# 
#     def get_edit_url(self, obj):
#         return reverse("edit-author", kwargs={'pk': obj.pk})
# 
#     def get_delete_url(self, obj):
#         if obj.age < 18:
#             return None
#         return reverse("delete-author", kwargs={'pk': obj.pk})
# 
# class BookCrudViewset(PopupCrudViewSet):
#     model = Book
#     form_class = BookForm
#     list_display = ('title', 'author')
#     list_url = reverse_lazy("books:list")
#     new_url = reverse_lazy("books:create")
#     related_object_popups = {
#         'author': reverse_lazy("new-author")
#     }
#     legacy_crud = True
#     item_actions = [
#         ('Up', 'glyphicon glyphicon-ok', 'up_vote'),
#         ('Down', 'glyphicon glyphicon-remove', 'down_vote'),
#     ]
# 
#     @staticmethod
#     def get_edit_url(obj):
#         return reverse_lazy("books:update", kwargs={'pk': obj.pk})
# 
#     @staticmethod
#     def get_delete_url(obj):
#         return reverse_lazy("books:delete", kwargs={'pk': obj.pk})
# 
#     @staticmethod
#     def get_detail_url(obj):
#         return reverse_lazy("books:detail", kwargs={'pk': obj.pk})
# 
#     def up_vote(self, request, book):
#         return True, "Up vote successful"
# 
#     def down_vote(self, request, book):
#         return True, "Down vote successful"
# 
# class BookUUIDCrudViewSet(PopupCrudViewSet):
#     '''CRUD views using slug field as url kwarg instead of the default pk'''
# 
#     model = Book
#     form_class = BookForm
#     list_display = ('title', 'author')
#     list_url = reverse_lazy("uuidbooks:list")
#     new_url = reverse_lazy("uuidbooks:create")
#     pk_url_kwarg = None
#     slug_field = 'uuid'
#     slug_url_kwarg = 'uuid'
# 
#     @staticmethod
#     def get_edit_url(obj):
#         return reverse("uuidbooks:update", kwargs={'uuid': obj.uuid.hex})
# 
#     @staticmethod
#     def get_delete_url(obj):
#         return reverse("uuidbooks:delete", kwargs={'uuid': obj.uuid.hex})
# 
#     @staticmethod
#     def get_detail_url(obj):
#         return reverse("uuidbooks:detail", kwargs={'uuid': obj.uuid.hex})


import re
import json
import six
from django.test import TestCase
from django.http import JsonResponse
    from django.urls import reverse
    from django.core.urlresolvers import reverse
from .models import Author, Book
from .views import AuthorCrudViewset, BookCrudViewset, BookUUIDCrudViewSet
        from popupcrud.views import POPUPCRUD
    def test_page_title(self):
        author = Author.objects.create(name="John", age=26)
        response = self.client.get(reverse("authors"))
        self.assertEquals(response.context['page_title'], "Author List")

    def test_empty_data(self):
        response = self.client.get(reverse("authors"))
        self.assertNotContains(response, "<table class='table")
        self.assertNotContains(response, "<th>Name</th>")
        self.assertNotContains(response, "<th>Age</th>")
        self.assertContains(response, "No records found")

    def test_object_access_through_pk_urls(self):
        '''Test CRUDViewset view access using object.pk based urls'''
        for _ in range(0, 10):
            Author.objects.create(name="John", age=25)
        response = self.client.get(reverse("authors"))
        self.assertContains(response, "New Author")
        self.assertContains(response, AuthorCrudViewset.new_url)
        for obj in Author.objects.all():
            self.assertContains(response, AuthorCrudViewset().get_edit_url(obj))
            self.assertContains(response, AuthorCrudViewset().get_delete_url(obj))
            self.assertContains(response, AuthorCrudViewset().get_detail_url(obj))

    def test_object_access_through_slug_urls(self):
        '''Test CRUDViewset view access using slug_field based urls'''
        john = Author.objects.create(name='Peter', age=25)
        for index in range(1, 10):
            Book.objects.create(title='Title %d' % index, author=john)
        response = self.client.get(reverse("uuidbooks:list"))

# Path: test/models.py
# class Author(models.Model):
#     name = models.CharField("Name", max_length=128)
#     age = models.SmallIntegerField("Age", null=True, blank=True)
# 
#     class Meta:
#         ordering = ('name',)
#         verbose_name = "Author"
#         verbose_name_plural = "Authors"
# 
#     def __str__(self):
#         return self.name
# 
#     def double_age(self):
#         return self.age*2
#     double_age.short_description = "Double Age"
# 
# class Book(models.Model):
#     title = models.CharField("Title", max_length=128)
#     author = models.ForeignKey(Author, on_delete=models.CASCADE)
#     uuid = models.UUIDField(default=uuid.uuid4)
# 
#     class Meta:
#         ordering = ('title',)
#         verbose_name = "Book"
#         verbose_name_plural = "Books"
# 
#     def __str__(self):
#         return self.title
# 
# Path: test/views.py
# class AuthorCrudViewset(PopupCrudViewSet):
#     model = Author
#     fields = ('name', 'age')
#     list_display = ('name', 'age', 'half_age', 'double_age')
#     list_url = reverse_lazy("authors")
#     new_url = reverse_lazy("new-author")
#     page_title = "Author List"
# 
#     """
#     form_class = AuthorForm
#     list_permission_required = ('tests.add_author',)
#     create_permission_required = ('tests.add_author',)
#     update_permission_required = ('tests.change_author',)
#     delete_permission_required = ('tests.delete_author',)
#     """
# 
#     def half_age(self, author):
#         return int(author.age/2)
#     half_age.short_description = "Half Age"
#     half_age.order_field = 'age'
# 
#     def get_detail_url(self, obj):
#         return reverse("author-detail", kwargs={'pk': obj.pk})
# 
#     def get_edit_url(self, obj):
#         return reverse("edit-author", kwargs={'pk': obj.pk})
# 
#     def get_delete_url(self, obj):
#         if obj.age < 18:
#             return None
#         return reverse("delete-author", kwargs={'pk': obj.pk})
# 
# class BookCrudViewset(PopupCrudViewSet):
#     model = Book
#     form_class = BookForm
#     list_display = ('title', 'author')
#     list_url = reverse_lazy("books:list")
#     new_url = reverse_lazy("books:create")
#     related_object_popups = {
#         'author': reverse_lazy("new-author")
#     }
#     legacy_crud = True
#     item_actions = [
#         ('Up', 'glyphicon glyphicon-ok', 'up_vote'),
#         ('Down', 'glyphicon glyphicon-remove', 'down_vote'),
#     ]
# 
#     @staticmethod
#     def get_edit_url(obj):
#         return reverse_lazy("books:update", kwargs={'pk': obj.pk})
# 
#     @staticmethod
#     def get_delete_url(obj):
#         return reverse_lazy("books:delete", kwargs={'pk': obj.pk})
# 
#     @staticmethod
#     def get_detail_url(obj):
#         return reverse_lazy("books:detail", kwargs={'pk': obj.pk})
# 
#     def up_vote(self, request, book):
#         return True, "Up vote successful"
# 
#     def down_vote(self, request, book):
#         return True, "Down vote successful"
# 
# class BookUUIDCrudViewSet(PopupCrudViewSet):
#     '''CRUD views using slug field as url kwarg instead of the default pk'''
# 
#     model = Book
#     form_class = BookForm
#     list_display = ('title', 'author')
#     list_url = reverse_lazy("uuidbooks:list")
#     new_url = reverse_lazy("uuidbooks:create")
#     pk_url_kwarg = None
#     slug_field = 'uuid'
#     slug_url_kwarg = 'uuid'
# 
#     @staticmethod
#     def get_edit_url(obj):
#         return reverse("uuidbooks:update", kwargs={'uuid': obj.uuid.hex})
# 
#     @staticmethod
#     def get_delete_url(obj):
#         return reverse("uuidbooks:delete", kwargs={'uuid': obj.uuid.hex})
# 
#     @staticmethod
#     def get_detail_url(obj):
#         return reverse("uuidbooks:detail", kwargs={'uuid': obj.uuid.hex})


# Path: test/tests.py
import re
import json
import six
from django.test import TestCase
from django.http import JsonResponse
    from django.urls import reverse
    from django.core.urlresolvers import reverse
from .models import Author, Book
from .views import AuthorCrudViewset, BookCrudViewset, BookUUIDCrudViewSet
        from popupcrud.views import POPUPCRUD

    def test_page_title(self):
        author = Author.objects.create(name="John", age=26)
        response = self.client.get(reverse("authors"))
        self.assertEquals(response.context['page_title'], "Author List")

    def test_empty_data(self):
        response = self.client.get(reverse("authors"))
        self.assertNotContains(response, "<table class='table")
        self.assertNotContains(response, "<th>Name</th>")
        self.assertNotContains(response, "<th>Age</th>")
        self.assertContains(response, "No records found")

    def test_object_access_through_pk_urls(self):
        '''Test CRUDViewset view access using object.pk based urls'''
        for _ in range(0, 10):
            Author.objects.create(name="John", age=25)
        response = self.client.get(reverse("authors"))
        self.assertContains(response, "New Author")
        self.assertContains(response, AuthorCrudViewset.new_url)
        for obj in Author.objects.all():
            self.assertContains(response, AuthorCrudViewset().get_edit_url(obj))
            self.assertContains(response, AuthorCrudViewset().get_delete_url(obj))
            self.assertContains(response, AuthorCrudViewset().get_detail_url(obj))

    def test_object_access_through_slug_urls(self):
        '''Test CRUDViewset view access using slug_field based urls'''
        john = Author.objects.create(name='Peter', age=25)
        for index in range(1, 10):
            Book.objects.create(title='Title %d' % index, author=john)
        response = self.client.get(reverse("uuidbooks:list"))

        self.assertContains(response, BookUUIDCrudViewSet.new_url)