# 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
            }
            self.client.post(url, form_data)

        book = Book.objects.get(name='A Random Book')
        self.assertTrue(book.pages.name.endswith('/file_without_extension'))

    def test_same_file_with_no_extension_for_different_rows(self):
        url = reverse('model_files:book.add')

        index_file = open(get_file_path('file_without_extension'))
        form_data = {'name': 'Book ABC',
                     'index': index_file}
        self.client.post(url, form_data)
        index_file.seek(0)
        form_data = {'name': 'Book XYZ',
                     'index': index_file}
        self.client.post(url, form_data)
        index_file.close()

        self.assertEqual(BookIndex.objects.count(), 2)

        abc = Book.objects.get(name='Book ABC')
        xyz = Book.objects.get(name='Book XYZ')
        self.assertNotEqual(abc.index.name, xyz.index.name)

    def test_save_file_without_using_form(self):
        file_name = 'manual.txt'
        file_content_string = b'Test file content'
        content_file = ContentFile(file_content_string)


# 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

            }
            self.client.post(url, form_data)

        book = Book.objects.get(name='A Random Book')
        self.assertTrue(book.pages.name.endswith('/file_without_extension'))

    def test_same_file_with_no_extension_for_different_rows(self):
        url = reverse('model_files:book.add')

        index_file = open(get_file_path('file_without_extension'))
        form_data = {'name': 'Book ABC',
                     'index': index_file}
        self.client.post(url, form_data)
        index_file.seek(0)
        form_data = {'name': 'Book XYZ',
                     'index': index_file}
        self.client.post(url, form_data)
        index_file.close()

        self.assertEqual(BookIndex.objects.count(), 2)

        abc = Book.objects.get(name='Book ABC')
        xyz = Book.objects.get(name='Book XYZ')
        self.assertNotEqual(abc.index.name, xyz.index.name)

    def test_save_file_without_using_form(self):
        file_name = 'manual.txt'
        file_content_string = b'Test file content'
        content_file = ContentFile(file_content_string)


        device = SoundDevice(name='BXPM 778')