Skip to content

Commit

Permalink
Replaced mixer with model bakery and updated migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kaufhold committed May 6, 2020
1 parent ec9eea7 commit 6eb9ea2
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 83 deletions.
20 changes: 10 additions & 10 deletions document_library/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('position', models.PositiveIntegerField(null=True, verbose_name='Position', blank=True)),
('object_id', models.PositiveIntegerField()),
('content_type', models.ForeignKey(to='contenttypes.ContentType')),
('content_type', models.ForeignKey(to='contenttypes.ContentType', on_delete=models.CASCADE,)),
],
options={
'ordering': ['position'],
Expand Down Expand Up @@ -66,7 +66,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('title', models.CharField(max_length=256, verbose_name='Title')),
('language_code', models.CharField(max_length=15, db_index=True)),
('master', models.ForeignKey(related_name='translations', editable=False, to='document_library.DocumentCategory', null=True)),
('master', models.ForeignKey(related_name='translations', editable=False, to='document_library.DocumentCategory', null=True, on_delete=models.SET_NULL,)),
],
options={
'managed': True,
Expand All @@ -86,9 +86,9 @@ class Migration(migrations.Migration):
('is_published', models.BooleanField(default=False, verbose_name='Is published')),
('meta_description', models.TextField(max_length=512, verbose_name='Meta description', blank=True)),
('language_code', models.CharField(max_length=15, db_index=True)),
('filer_file', filer.fields.file.FilerFileField(related_name='document_files', verbose_name='File', blank=True, to='filer.File', null=True)),
('master', models.ForeignKey(related_name='translations', editable=False, to='document_library.Document', null=True)),
('thumbnail', filer.fields.file.FilerFileField(related_name='document_thumbnails', verbose_name='Thumbnail', blank=True, to='filer.File', null=True)),
('filer_file', filer.fields.file.FilerFileField(related_name='document_files', verbose_name='File', blank=True, to='filer.File', null=True, on_delete=models.CASCADE)),
('master', models.ForeignKey(related_name='translations', editable=False, to='document_library.Document', null=True, on_delete=models.CASCADE)),
('thumbnail', filer.fields.file.FilerFileField(related_name='document_thumbnails', verbose_name='Thumbnail', blank=True, to='filer.File', null=True, on_delete=models.CASCADE)),
],
options={
'managed': True,
Expand All @@ -101,7 +101,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='document',
name='category',
field=models.ForeignKey(verbose_name='Category', blank=True, to='document_library.DocumentCategory', null=True),
field=models.ForeignKey(verbose_name='Category', blank=True, to='document_library.DocumentCategory', null=True, on_delete=models.SET_NULL),
),
migrations.AddField(
model_name='document',
Expand All @@ -111,22 +111,22 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='document',
name='folder',
field=filer.fields.folder.FilerFolderField(related_name='document_folders', verbose_name='Folder', blank=True, to='filer.Folder', null=True),
field=filer.fields.folder.FilerFolderField(related_name='document_folders', verbose_name='Folder', blank=True, to='filer.Folder', null=True, on_delete=models.CASCADE),
),
migrations.AddField(
model_name='document',
name='image',
field=filer.fields.image.FilerImageField(related_name='document_images', verbose_name='Image', blank=True, to='filer.Image', null=True),
field=filer.fields.image.FilerImageField(related_name='document_images', verbose_name='Image', blank=True, to='filer.Image', null=True, on_delete=models.CASCADE),
),
migrations.AddField(
model_name='document',
name='user',
field=models.ForeignKey(verbose_name='User', blank=True, to=settings.AUTH_USER_MODEL, null=True),
field=models.ForeignKey(verbose_name='User', blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE),
),
migrations.AddField(
model_name='attachment',
name='document',
field=models.ForeignKey(verbose_name='Document', to='document_library.Document'),
field=models.ForeignKey(verbose_name='Document', to='document_library.Document', on_delete=models.CASCADE),
),
migrations.AlterUniqueTogether(
name='documenttranslation',
Expand Down
11 changes: 8 additions & 3 deletions document_library/migrations/0002_documentplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
('cms', '0013_urlconfrevision'),
('document_library', '0001_initial'),
Expand All @@ -15,8 +14,14 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='DocumentPlugin',
fields=[
('cmsplugin_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='cms.CMSPlugin')),
('document', models.ForeignKey(verbose_name='Document', to='document_library.Document')),
('cmsplugin_ptr',
models.OneToOneField(parent_link=True, auto_created=True,
primary_key=True, serialize=False,
to='cms.CMSPlugin',
on_delete=models.CASCADE, )),
('document', models.ForeignKey(verbose_name='Document',
to='document_library.Document',
on_delete=models.CASCADE, )),
],
options={
'abstract': False,
Expand Down
7 changes: 4 additions & 3 deletions document_library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def get_title(self):

class DocumentManager(TranslationManager):
"""Custom manager for the ``Document`` model."""

def published(self, request=None):
"""
Returns the published documents in the current language.
Expand All @@ -109,8 +110,8 @@ def published(self, request=None):
)
# either it has no category or the one it has is published
qs = qs.filter(
models.Q(category__isnull=True) |
models.Q(category__is_published=True))
models.Q(category__isnull=True) | models.Q(
category__is_published=True))
return qs


Expand Down Expand Up @@ -262,7 +263,7 @@ class Document(TranslatableModel):
objects = DocumentManager()

class Meta:
ordering = ('position', '-document_date', )
ordering = ('position', '-document_date',)

def __str__(self):
return self.get_title()
Expand Down
1 change: 0 additions & 1 deletion document_library/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Default settings for the document_library app."""
from django.conf import settings


LOGIN_REQUIRED = getattr(settings, 'DOCUMENT_LIBRARY_LOGIN_REQUIRED', False)
PAGINATION_AMOUNT = getattr(settings, 'DOCUMENT_LIBRARY_PAGINATION_AMOUNT', 1)
PAGINATE_BY_CATEGORIES = getattr(
Expand Down
6 changes: 3 additions & 3 deletions document_library/templatetags/document_library_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
register = template.Library()


@register.assignment_tag
@register.simple_tag
def get_files_for_document(document):
"""
Returns the available files for all languages.
Expand All @@ -25,15 +25,15 @@ def get_files_for_document(document):
return files


@register.assignment_tag(takes_context=True)
@register.simple_tag(takes_context=True)
def get_frontpage_documents(context):
"""Returns the library favs that should be shown on the front page."""
req = context.get('request')
qs = Document.objects.published(req).filter(is_on_front_page=True)
return qs


@register.assignment_tag(takes_context=True)
@register.simple_tag(takes_context=True)
def get_latest_documents(context, count=5):
"""
Returns the latest documents.
Expand Down
31 changes: 15 additions & 16 deletions document_library/tests/document_library_tags_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.template import RequestContext
from django.test import TestCase, RequestFactory

from mixer.backend.django import mixer
from model_bakery import baker

from ..templatetags import document_library_tags

Expand All @@ -12,8 +12,8 @@ class GetFilesForDocumentTestCase(TestCase):
longMessage = True

def setUp(self):
self.user = mixer.blend('auth.User')
self.doc = mixer.blend('document_library.Document')
self.user = baker.make('auth.User')
self.doc = baker.make('document_library.Document')
self.doc_en = self.doc.translate('en')
self.doc_en.save()
self.doc_de = self.doc.translate('de')
Expand All @@ -32,19 +32,18 @@ class GetFrontpageDocumentsTestCase(TestCase):
def setUp(self):
super(GetFrontpageDocumentsTestCase, self).setUp()
# Two documents that should be on the front page
self.en_doc = mixer.blend(
self.en_doc = baker.make(
'document_library.DocumentTranslation',
language_code='en', is_published=True,
is_on_front_page=True)
self.en_doc.master.is_published = True
language_code='en', is_published=True)
self.en_doc.master.is_on_front_page = True
self.en_doc.master.is_published = True
self.en_doc.master.save()
self.de_doc = self.en_doc.master.translate('de')
self.de_doc.is_published = True
self.de_doc.save()

# And one that should not be on the front page
mixer.blend('document_library.DocumentTranslation')
baker.make('document_library.DocumentTranslation')

def test_tag(self):
req = RequestFactory().get('/')
Expand Down Expand Up @@ -72,14 +71,14 @@ class GetLatestDocumentsTestCase(TestCase):
longMessage = True

def test_tag(self):
mixer.blend('document_library.DocumentTranslation', language_code='en',
is_published=True)
mixer.blend('document_library.DocumentTranslation', language_code='en',
is_published=True)
mixer.blend('document_library.DocumentTranslation', language_code='en',
is_published=True)
mixer.blend('document_library.DocumentTranslation', language_code='en',
is_published=False)
baker.make('document_library.DocumentTranslation', language_code='en',
is_published=True)
baker.make('document_library.DocumentTranslation', language_code='en',
is_published=True)
baker.make('document_library.DocumentTranslation', language_code='en',
is_published=True)
baker.make('document_library.DocumentTranslation', language_code='en',
is_published=False)

req = RequestFactory().get('/')
req.LANGUAGE_CODE = 'en'
Expand Down
67 changes: 34 additions & 33 deletions document_library/tests/models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from django.test import TestCase

from mixer.backend.django import mixer
from model_bakery import baker

from .. import models

Expand All @@ -16,7 +16,7 @@ class AttachmentTestCase(TestCase):
longMessage = True

def test_model(self):
obj = mixer.blend('document_library.Attachment')
obj = baker.make('document_library.Attachment')
self.assertTrue(obj.pk, msg=(
'Should be able to instantiate and save the model.'))

Expand All @@ -26,18 +26,20 @@ class DocumentTestCase(TestCase):
longMessage = True

def test_model(self):
instance = mixer.blend('document_library.DocumentTranslation')
instance = baker.make('document_library.DocumentTranslation')
self.assertTrue(instance.pk, msg=(
'Should be able to instantiate and save the object.'))

def test_get_filetype(self):
instance = mixer.blend('document_library.DocumentTranslation',
language_code='en').master
self.assertEqual(instance.get_filetype(), '', msg=(
'Should return the filetype.'))
self.de_doc = baker.make('document_library.DocumentTranslation')
instance = self.de_doc.master.translate('de')

self.assertEqual(
instance.get_filetype(), '', msg='Should return the filetype.')

instance.filer_file = mixer.blend('filer.File')
self.assertEqual(instance.get_filetype(), '', msg=(
instance.filer_file = baker.make('filer.File')
instance.filer_file.file.name = 'image.jpg'
self.assertEqual(instance.get_filetype(), 'JPG', msg=(
'Should return the filetype.'))


Expand All @@ -46,13 +48,13 @@ class DocumentCategoryTestCase(TestCase):
longMessage = True

def test_model(self):
instance = mixer.blend(
instance = baker.make(
'document_library.DocumentCategoryTranslation').master
self.assertTrue(instance.pk, msg=(
'Should be able to instantiate and save the object.'))

def test_get_title(self):
instance = mixer.blend('document_library.DocumentCategoryTranslation')
instance = baker.make('document_library.DocumentCategoryTranslation')
result = instance.master.get_title()
self.assertTrue(result, msg=('Should return the translated title.'))

Expand All @@ -62,31 +64,30 @@ class DocumentManagerTestCase(TestCase):
longMessage = True

def setUp(self):
self.en_doc = mixer.blend(
'document_library.DocumentTranslation',
category=mixer.blend(
'document_library.DocumentCategoryTranslation',
is_published=True),
language_code='en', is_published=False)
self.de_doc = mixer.blend(
'document_library.DocumentTranslation',
category=mixer.blend(
'document_library.DocumentCategoryTranslation',
is_published=True),
language_code='de', is_published=False)
self.de_doc_no_public_cat = mixer.blend(
'document_library.DocumentTranslation',
category=mixer.blend(
'document_library.DocumentCategoryTranslation',
is_published=False),
language_code='de', is_published=False)
new_doc = self.de_doc.master.translate('en')
de_cat = baker.make('document_library.DocumentCategory',
is_published=True)
self.de_doc = baker.make('document_library.DocumentTranslation')
new_doc = self.de_doc.master.translate('de')
new_doc.category = de_cat
new_doc.is_published = True
new_doc.save()
new_doc = self.en_doc.master.translate('de')

en_cat = baker.make('document_library.DocumentCategory',
is_published=True)
self.en_doc = baker.make('document_library.DocumentTranslation')
new_doc = self.en_doc.master.translate('en')
new_doc.category = en_cat
new_doc.is_published = True
new_doc.save()
new_doc = self.de_doc_no_public_cat.master.translate('en')

nonpub_cat = baker.make(
'document_library.DocumentCategory',
is_published=False
)
self.de_doc_no_public_cat = baker.make(
'document_library.DocumentTranslation')
new_doc = self.de_doc_no_public_cat.master.translate('de')
new_doc.category = nonpub_cat
new_doc.is_published = False
new_doc.save()

Expand Down Expand Up @@ -114,7 +115,7 @@ class DocumentPluginTestCase(TestCase):
longMessage = True

def test_model(self):
instance = models.DocumentPlugin(document=mixer.blend(
instance = models.DocumentPlugin(document=baker.make(
'document_library.DocumentTranslation').master)
instance.save()
self.assertTrue(instance.pk, msg=(
Expand Down
2 changes: 1 addition & 1 deletion document_library/tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flake8: noqa
"""
These settings are used by the ``manage.py`` command.
With normal tests we want to use the fastest possible way which is an
Expand All @@ -12,7 +13,6 @@

from .test_settings import * # NOQA


DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
Expand Down
3 changes: 2 additions & 1 deletion document_library/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
os.path.join(os.path.dirname(__file__), 'test_static'),
)

MIDDLEWARE_CLASSES = (
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
Expand All @@ -63,6 +63,7 @@
'OPTIONS': {
'context_processors': (
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
'django.template.context_processors.request',
'django.template.context_processors.media',
Expand Down
1 change: 0 additions & 1 deletion document_library/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
urlpatterns += i18n_patterns(
url(r'^admin/', include(admin.site.urls)),
url(r'^docs/', include('document_library.urls')),
url(r'^', include('cms.urls')),
)
Loading

0 comments on commit 6eb9ea2

Please sign in to comment.