-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when loading in wagtailtrans own hooks #153
base: master
Are you sure you want to change the base?
Changes from all commits
68b02ae
0bc5ff5
1d62740
4dfeda8
cd968d7
d993ec7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Generated by Django 2.1.5 on 2019-02-12 08:23 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import wagtail.core.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('wagtailcore', '0040_page_draft_title'), | ||
('pages', '0002_auto_20161214_0842'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Article', | ||
fields=[ | ||
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), | ||
('date', models.DateField()), | ||
('intro_text', wagtail.core.fields.RichTextField()), | ||
('language', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='wagtailtrans.Language')), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
bases=('wagtailcore.page',), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import datetime | ||
|
||
import factory | ||
from factory.fuzzy import FuzzyDate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. factory.fuzzy is deprecated according to the docs: https://factoryboy.readthedocs.io/en/latest/fuzzy.html |
||
from wagtail.core.models import Page | ||
from wagtail.images.tests.utils import ( | ||
get_image_model, get_test_image_file) | ||
|
||
from wagtailtrans import models | ||
|
||
from tests._sandbox.pages.models import HomePage | ||
from tests._sandbox.pages.models import HomePage, Article | ||
from tests.factories import language | ||
|
||
|
||
|
@@ -74,3 +77,35 @@ class WagtailPageFactory(factory.DjangoModelFactory): | |
|
||
class Meta: | ||
model = Page | ||
|
||
@classmethod | ||
def _create(cls, *args, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think we need this since we're mostly using a pytest fixture when creating a site tree. Or is there any particular reason why you implemented this? |
||
model = args[0] | ||
parent = kwargs.pop( | ||
'parent', | ||
Page.objects.get(path='0001') | ||
) | ||
|
||
try: | ||
return model.objects.get(title=kwargs['title']) | ||
except model.DoesNotExist: | ||
kwargs['depth'] = parent.depth + 1 | ||
|
||
if kwargs['depth'] == 0: | ||
return model.add_root(**kwargs) | ||
else: | ||
return parent.add_child( | ||
instance=model( | ||
**kwargs | ||
) | ||
) | ||
|
||
|
||
class ArticleFactory(WagtailPageFactory): | ||
title = 'Article 1' | ||
intro_text = 'This is an article' | ||
date = FuzzyDate(datetime.date(2019, 1, 1)) | ||
language = factory.SubFactory(language.LanguageFactory) | ||
|
||
class Meta: | ||
model = Article |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
|
||
from wagtail.admin import widgets | ||
from wagtail.core import hooks | ||
|
||
from tests.factories.pages import ArticleFactory | ||
from wagtailtrans.models import TranslatablePage | ||
from wagtailtrans.wagtail_hooks import edit_in_language_button | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestWagtailHooks: | ||
def setup(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need the 2 pages created in the setup for each test, so lets just create the page we need in the test itself |
||
self.article = ArticleFactory() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The purpose of this |
||
self.transpage = TranslatablePage() | ||
|
||
@hooks.register('register_page_listing_buttons') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you defining this here? I think we can just import it right, just like the |
||
def page_translations_menu(self, page, page_perms, is_parent=False): | ||
# To test this hook i need to add it here, if we don't do this | ||
# we can't import the hook like the edit_in_language_button | ||
if not issubclass(page.__class__, TranslatablePage): | ||
return | ||
|
||
if hasattr(page, 'canonical_page') and page.canonical_page: | ||
return | ||
|
||
yield widgets.ButtonWithDropdownFromHook( | ||
'Translate into', | ||
hook_name='wagtailtrans_dropdown_hook', | ||
page=page, | ||
page_perms=page_perms, | ||
is_parent=is_parent, | ||
priority=10 | ||
) | ||
|
||
def test_edit_in_language_wagtail_hook_with_regular_page(self): | ||
assert list(edit_in_language_button(self.article, page_perms=[])) == [] | ||
|
||
def test_edit_in_language_wagtail_hook_translateable_page(self): | ||
result = list(edit_in_language_button(self.transpage, page_perms=[])) | ||
assert len(result) == 1 | ||
assert result[0].label == 'Edit in' | ||
|
||
def test_page_translations_menu_with_regular_page(self): | ||
assert list(self.page_translations_menu(self.article, page_perms=[])) == [] | ||
|
||
def test_page_translations_menu_translateable_page(self): | ||
result = list(self.page_translations_menu(self.transpage, page_perms=[])) | ||
assert len(result) == 1 | ||
assert result[0].label == 'Translate into' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, my last comment was misleading. I meant
isinstance
, as you had changed it just before, not sure why I saidissubclass
!