Look up a Page via its apphook?

26 views
Skip to first unread message

tated

unread,
Feb 11, 2016, 9:13:01 PM2/11/16
to django CMS users
I am looking for a way for an object to reference the page that is apphooked to its parent app. For example:

given this apphook:


class NewsHook(CMSApp):
    name = _("News")
    urls = ["apps.news.urls"]

apphook_pool.register(NewsHook)


and this model inside apps.news.models:


class Article(models.Model):
    title = models.CharField(max_length=255)
    ...


Is it possible to reference the page(s) associated with NewsHook in, say, a method on Article?


From the model side I've gotten as far as article._meta.app_label or article._meta.app_config.verbose_name, but that only yields 'news' and 'News', respectively. And I know from https://github.com/divio/django-cms/blob/7888ab8421bb836c8f7a1127d9a2bf4d4bbdf23e/cms/models/pagemodel.py#L82 that a page's apphook is accessible with page.application_urls, which gives me 'u'NewsHook'But I'm missing a link.


I suppose I could filter Pages by the application_urls field and look for a match with my article._meta.app_config.verbose_name, but that would be neither neither failsafe nor pretty.


Any ideas for a better way?

Iacopo Spalletti

unread,
Feb 12, 2016, 8:06:13 AM2/12/16
to djang...@googlegroups.com
Il 11/02/2016 22:13, tated ha scritto:
> I am looking for a way for an object to reference the page that is
> apphooked to its parent app. For example:
>
> given this apphook:
>
>
> |classNewsHook(CMSApp):name =_("News")urls
> =["apps.news.urls"]apphook_pool.register(NewsHook)|
>
>
> and this model inside |apps.news.models|:
>
>
> |classArticle(models.Model):title =models.CharField(max_length=255)...|
>
>
> Is it possible to reference the page(s) associated with NewsHook in,
> say, a method on Article?
>
>
> From the model side I've gotten as far
> as |article._meta.app_label| or |article._meta.app_config.verbose_name|,
> but that only yields |'news'| and |'News'|, respectively. And I know
> from https://github.com/divio/django-cms/blob/7888ab8421bb836c8f7a1127d9a2bf4d4bbdf23e/cms/models/pagemodel.py#L82 that
> a page's apphook is accessible with |page.application_urls|, which gives
> me |'u'NewsHook'|. But I'm missing a link.
>
>
> I suppose I could filter Pages by the application_urls field and look
> for a match with my |article._meta.app_config.verbose_name|, but that
> would be neither neither failsafe nor pretty.
>
>
> Any ideas for a better way?
>
What's exactly your use case?

``request.current_page`` provides the page for the current apphook, in
case you need it.
There is no definite link between model and the apphook (apphook it's
just a wrapper for an urlconf); you might be able to link them by using
the urlconf, but it depends on the structure of your application.


--
Saluti

Iacopo Spalletti

tated

unread,
Feb 16, 2016, 1:18:07 PM2/16/16
to django CMS users
Here's my use case:

* I've added a page extension called Reviewer, which lets you select an admin user as the reviewer for a Page and its children

* On the Django admin change form for a content type, like an Article, I want to either add a custom 'Save and Notify Reviewer' button (or maybe alternatively a post_save signal on the default 'Save' button) for users with certain limited permissions -- let's call them Interns. The action of that button should be to email the corresponding Reviewer for that section of the page tree than an intern has readied some content for them to review.

I would rather not manage Reviewer as a foreign key relationship to Article if I can avoid it, because I still need Reviewer to be associated with Page for other actions and I don't want admins to have to manage Reviewer in two separate (possibly redundant) places. Also, in the future I may need to use namespaced apphooks to attach the same app to multiple locations in the page tree. In that case, the reviewer will likely need to be different.

I can use request.current_page for the Page-related (incl. placeholders, plugins) notifications because they'll be editing from the front end. But not for admin-edited models, I don't think.

Thanks a bunch for your insight.
Reply all
Reply to author
Forward
0 new messages