# Path: demo_and_tests/model_filefields_example/forms.py
# class BookForm(forms.ModelForm):
#     class Meta(object):
#         model = Book
#         exclude = []
#         widgets = {
#             'index': DBClearableFileInput,
#             'pages': DBClearableFileInput,
#             'cover': DBClearableFileInput,
#         }
# 
# class BookAdminForm(adminforms.ModelForm):
#     class Meta(object):
#         model = Book
#         exclude = []
#         widgets = {
#             'index': DBAdminClearableFileInput,
#             'pages': DBAdminClearableFileInput,
#             'cover': DBAdminClearableFileInput,
#         }
# 
# Path: demo_and_tests/model_filefields_example/models.py
# class Book(models.Model):
#     book_pk = models.AutoField(primary_key=True)
#     name = models.CharField(max_length=100, unique=True)
#     index = models.FileField(
#         upload_to='model_filefields_example.BookIndex/bytes/filename/mimetype',
#         blank=True, null=True
#     )
#     pages = models.FileField(
#         upload_to='model_filefields_example.BookPages/bytes/filename/mimetype',
#         blank=True, null=True
#     )
#     cover = models.ImageField(
#         upload_to='model_filefields_example.BookCover/bytes/filename/mimetype',
#         blank=True, null=True
#     )
# 
#     def get_absolute_url(self):
#         return reverse('model_files:book.edit', kwargs={'pk': self.pk})
# 
#     def save(self, *args, **kwargs):
#         delete_file_if_needed(self, 'index')
#         delete_file_if_needed(self, 'pages')
#         super(Book, self).save(*args, **kwargs)
# 
#     def delete(self, *args, **kwargs):
#         super(Book, self).delete(*args, **kwargs)
#         delete_file(self, 'index')
#         delete_file(self, 'pages')
# 
#     def __str__(self):
#         return self.name
# 
# class BookIndex(models.Model):
#     book_index_pk = models.AutoField(primary_key=True)
#     bytes = models.TextField()
#     filename = models.CharField(max_length=255)
#     mimetype = models.CharField(max_length=50)
# 
# class BookPages(models.Model):
#     book_pages_pk = models.AutoField(primary_key=True)
#     bytes = models.TextField()
#     filename = models.CharField(max_length=255)
#     mimetype = models.CharField(max_length=50)
# 
# class SoundDevice(models.Model):
#     name = models.CharField(max_length=100)
#     instruction_manual = models.FileField(
#         upload_to='model_filefields_example.SoundDeviceInstructionManual'
#                   '/bytes/filename/mimetype',
#         blank=True,
#         null=True
#     )
# 
# Path: db_file_storage/compat.py



import mimetypes
import os
import sys
from django.core.files.storage import default_storage
from django.conf import settings
from django.core.files.base import ContentFile
from django.test import TestCase
from django.utils.http import urlencode
from .forms import BookForm, BookAdminForm
from .models import Book, BookIndex, BookPages, SoundDevice
from db_file_storage.compat import reverse
        admin_form['index'].as_widget()

    def verify_file(self, download_url, file_name):
        response = self.client.get(download_url)
        with open(get_file_path(file_name), 'rb') as the_file:
            # Assert that the contents of the saved file are correct
            self.assertEqual(the_file.read(), response.content)
        # Assert that the mimetype of the saved file is correct
        self.assertEqual(
            mimetypes.guess_type(file_name)[0],
            response['Content-Type']
        )

    def test_files_operations(self):
        save_new_url = reverse('model_files:book.add')
        download_url = reverse('db_file_storage.download_file')

        #
        # Add "Inferno" book without index or pages.
        #
        form_data = {'name': 'Inferno'}
        self.client.post(save_new_url, form_data, follow=True)
        inferno = Book.objects.get(name='Inferno')
        edit_inferno_url = reverse('model_files:book.edit',
                                   kwargs={'pk': inferno.pk})
        # Assert book file fields are empty
        self.assertEqual(inferno.index.name, '')
        self.assertEqual(inferno.pages.name, '')
        # Assert no BookIndex and no BookPages were created
        self.assertEqual(BookIndex.objects.count(), 0)

# Path: demo_and_tests/model_filefields_example/forms.py
# class BookForm(forms.ModelForm):
#     class Meta(object):
#         model = Book
#         exclude = []
#         widgets = {
#             'index': DBClearableFileInput,
#             'pages': DBClearableFileInput,
#             'cover': DBClearableFileInput,
#         }
# 
# class BookAdminForm(adminforms.ModelForm):
#     class Meta(object):
#         model = Book
#         exclude = []
#         widgets = {
#             'index': DBAdminClearableFileInput,
#             'pages': DBAdminClearableFileInput,
#             'cover': DBAdminClearableFileInput,
#         }
# 
# Path: demo_and_tests/model_filefields_example/models.py
# class Book(models.Model):
#     book_pk = models.AutoField(primary_key=True)
#     name = models.CharField(max_length=100, unique=True)
#     index = models.FileField(
#         upload_to='model_filefields_example.BookIndex/bytes/filename/mimetype',
#         blank=True, null=True
#     )
#     pages = models.FileField(
#         upload_to='model_filefields_example.BookPages/bytes/filename/mimetype',
#         blank=True, null=True
#     )
#     cover = models.ImageField(
#         upload_to='model_filefields_example.BookCover/bytes/filename/mimetype',
#         blank=True, null=True
#     )
# 
#     def get_absolute_url(self):
#         return reverse('model_files:book.edit', kwargs={'pk': self.pk})
# 
#     def save(self, *args, **kwargs):
#         delete_file_if_needed(self, 'index')
#         delete_file_if_needed(self, 'pages')
#         super(Book, self).save(*args, **kwargs)
# 
#     def delete(self, *args, **kwargs):
#         super(Book, self).delete(*args, **kwargs)
#         delete_file(self, 'index')
#         delete_file(self, 'pages')
# 
#     def __str__(self):
#         return self.name
# 
# class BookIndex(models.Model):
#     book_index_pk = models.AutoField(primary_key=True)
#     bytes = models.TextField()
#     filename = models.CharField(max_length=255)
#     mimetype = models.CharField(max_length=50)
# 
# class BookPages(models.Model):
#     book_pages_pk = models.AutoField(primary_key=True)
#     bytes = models.TextField()
#     filename = models.CharField(max_length=255)
#     mimetype = models.CharField(max_length=50)
# 
# class SoundDevice(models.Model):
#     name = models.CharField(max_length=100)
#     instruction_manual = models.FileField(
#         upload_to='model_filefields_example.SoundDeviceInstructionManual'
#                   '/bytes/filename/mimetype',
#         blank=True,
#         null=True
#     )
# 
# Path: db_file_storage/compat.py



# Path: demo_and_tests/model_filefields_example/tests.py
import mimetypes
import os
import sys
from django.core.files.storage import default_storage
from django.conf import settings
from django.core.files.base import ContentFile
from django.test import TestCase
from django.utils.http import urlencode
from .forms import BookForm, BookAdminForm
from .models import Book, BookIndex, BookPages, SoundDevice
from db_file_storage.compat import reverse

        admin_form['index'].as_widget()

    def verify_file(self, download_url, file_name):
        response = self.client.get(download_url)
        with open(get_file_path(file_name), 'rb') as the_file:
            # Assert that the contents of the saved file are correct
            self.assertEqual(the_file.read(), response.content)
        # Assert that the mimetype of the saved file is correct
        self.assertEqual(
            mimetypes.guess_type(file_name)[0],
            response['Content-Type']
        )

    def test_files_operations(self):
        save_new_url = reverse('model_files:book.add')
        download_url = reverse('db_file_storage.download_file')

        #
        # Add "Inferno" book without index or pages.
        #
        form_data = {'name': 'Inferno'}
        self.client.post(save_new_url, form_data, follow=True)
        inferno = Book.objects.get(name='Inferno')
        edit_inferno_url = reverse('model_files:book.edit',
                                   kwargs={'pk': inferno.pk})
        # Assert book file fields are empty
        self.assertEqual(inferno.index.name, '')
        self.assertEqual(inferno.pages.name, '')
        # Assert no BookIndex and no BookPages were created
        self.assertEqual(BookIndex.objects.count(), 0)

        self.assertEqual(BookPages.objects.count(), 0)