Skip to content
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

Prevent developers to add themselves as in_collaboration_with #126

Open
loicteixeira opened this issue Jun 25, 2018 · 2 comments
Open

Prevent developers to add themselves as in_collaboration_with #126

loicteixeira opened this issue Jun 25, 2018 · 2 comments

Comments

@loicteixeira
Copy link
Contributor

loicteixeira commented Jun 25, 2018

#125 adds a new in_cooperation_with field to WagtailSitePages in order to share credit on a given site. However it is possible for a developer to add themselves.

It's a bit tricky as we need to either filter the choices of the Foreign Key and/or add some custom validation based on the parent page which isn't set until the page is saved 😞

@loicteixeira
Copy link
Contributor Author

loicteixeira commented Jun 25, 2018

There is further complication in regard to limiting the choices as you can filter with static values, field values or a method which does not have access to the instance.

I tried to construct the parent's path from the fields values with
limit_choices_to=(~Q(path__startswith=Substr('path', 1, Length('path') - Page.steplen))) but it raises FieldError: Expression contains mixed types. You must set output_field.

Weirdly enough, replacing Length('path') - Page.steplen by Length('path', output_field=CharField()) fixes the error but it's obviously dropping the - Page.steplen.
Trying then to combine and cast the two with Cast(Length('path') - Page.steplen, CharField(max_length=5)) will now raise No function matches the given name and argument types.
Further converting the second argument of Substr from 1 to '1' does not produce any error but does not produce the expected result either.

I'm not even sure why all this casting is necessary in the first place. Postgres documentation state that the length argument should be an integer, but using IntergerField still raise FieldError: Expression contains mixed types. You must set output_field as well.

There must be a better way 😞

@loicteixeira
Copy link
Contributor Author

Actually, extending the base_form_class, we should be able to access self.parent_page. I'll play it with later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant