# 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
        response = self.client.get(download_url)
        self.assertEqual(response.status_code, 200)
        print(dir(response))
        self.assertEqual(response['Content-Language'], 'en')

    def test_form_widget_shows_proper_filename(self):
        # Create book
        save_url = reverse('model_files:book.add')
        index_file = open(get_file_path('inferno_index.txt'))
        form_data = {'name': 'Inferno',
                     'index': index_file}
        self.client.post(save_url, form_data, follow=True)
        index_file.close()
        book = Book.objects.get(name='Inferno')

        # Check widget download text
        form = BookForm(instance=book)
        self.assertIn('>inferno_index.txt</a>', form['index'].as_widget())

    def test_admin_form_widget_shows_proper_filename(self):
        # Create book
        save_url = reverse('model_files:book.add')
        index_file = open(get_file_path('inferno_index.txt'))
        form_data = {'name': 'Inferno',
                     'index': index_file}
        self.client.post(save_url, form_data, follow=True)
        index_file.close()
        book = Book.objects.get(name='Inferno')

        # Check widget download text

# 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

        response = self.client.get(download_url)
        self.assertEqual(response.status_code, 200)
        print(dir(response))
        self.assertEqual(response['Content-Language'], 'en')

    def test_form_widget_shows_proper_filename(self):
        # Create book
        save_url = reverse('model_files:book.add')
        index_file = open(get_file_path('inferno_index.txt'))
        form_data = {'name': 'Inferno',
                     'index': index_file}
        self.client.post(save_url, form_data, follow=True)
        index_file.close()
        book = Book.objects.get(name='Inferno')

        # Check widget download text
        form = BookForm(instance=book)
        self.assertIn('>inferno_index.txt</a>', form['index'].as_widget())

    def test_admin_form_widget_shows_proper_filename(self):
        # Create book
        save_url = reverse('model_files:book.add')
        index_file = open(get_file_path('inferno_index.txt'))
        form_data = {'name': 'Inferno',
                     'index': index_file}
        self.client.post(save_url, form_data, follow=True)
        index_file.close()
        book = Book.objects.get(name='Inferno')

        # Check widget download text

        form = BookAdminForm(instance=book)