Title: Is there an obvious way to override PageManager? · Issue #687 · feincms/feincms · GitHub
Open Graph Title: Is there an obvious way to override PageManager? · Issue #687 · feincms/feincms
X Title: Is there an obvious way to override PageManager? · Issue #687 · feincms/feincms
Description: I would like to override a few methods in PageManager - what would be the most efficient way to achieve that? Here's some context to explain why I want to do that. I wanted to use Django's Sites framework, and configure FeinCMS in a way ...
Open Graph Description: I would like to override a few methods in PageManager - what would be the most efficient way to achieve that? Here's some context to explain why I want to do that. I wanted to use Django's Sites fr...
X Description: I would like to override a few methods in PageManager - what would be the most efficient way to achieve that? Here's some context to explain why I want to do that. I wanted to use Django's ...
Opengraph URL: https://github.com/feincms/feincms/issues/687
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Is there an obvious way to override PageManager?","articleBody":"I would like to override a few methods in PageManager - what would be the most efficient way to achieve that?\r\n\r\n* * *\r\n\r\nHere's some context to explain why I want to do that.\r\n\r\nI wanted to use Django's Sites framework, and configure FeinCMS in a way that would allow me to control on which sites any particular page can be shown.\r\n\r\nYes, FeinCMS already has an extension for that:\r\nhttps://github.com/feincms/feincms/blob/e923d0466b74937d36eb45e2c46ea18cfef83d56/feincms/module/page/extensions/sites.py\r\n\r\nHowever:\r\n\r\n1. It creates 'site' ForeignKey (I want to have 'sites' ManyToMany instead, but I easily fixed that by writing a custom extension)\r\n2. It relies on Site.objects.get_current()\r\n\r\nSite.objects.get_current() works only if you have SITE_ID defined in your settings, otherwise it raises an error and asks to provide request in order to deduce the current Site. I omitted SITE_ID from my settings on purpose because I want to achieve behavior documented in Django docs:\r\nhttps://docs.djangoproject.com/en/dev/ref/contrib/sites/\r\n\r\n\u003eIf the setting is omitted, the get_current_site() function will try to get the current site by comparing the domain with the host name from the request.get_host() method.\r\n\r\nThere are a few problems, though.\r\nI noticed that feincms sites extension modifies which pages are treated as active by using the following approach:\r\n\r\n```\r\ndef current_site(queryset):\r\n return queryset.filter(site=Site.objects.get_current())\r\n\r\nPageManager.add_to_active_filters(current_site, key=\"current_site\")\r\n```\r\n\r\nHowever, without SITE_ID specified in settings, the only way to filter by current site is to somehow pass request to the filtering logic, and from my understanding, with add_to_active_filter there is no way to do so.\r\n\r\nWhich brought me to conclusion that I need to research how Handler view works, because views have access to request.\r\n\r\nI've tried many different approaches, but the only one that worked was to copy FeinCMS Handler code and then manually redefine PageManager methods before Handler class is defined.\r\nhttps://github.com/feincms/feincms/blob/e923d0466b74937d36eb45e2c46ea18cfef83d56/feincms/views/__init__.py#L29\r\n\r\n```\r\nfrom feincms.module.page.models import PageManager\r\nfrom .managers import for_request, page_for_path, best_match_for_path\r\n\r\nPageManager.for_request = for_request\r\nPageManager.page_for_path = page_for_path\r\nPageManager.best_match_for_path = best_match_for_path\r\n\r\nlogger = logging.getLogger(__name__)\r\n\r\nclass Handler(ContentView):\r\n # ...\r\n```\r\ncustom for_request simply passes request to custom page_for_path and best_match_for_path\r\n\r\npage_for_path and best_match_for_path are overridden in order to accept ``request`` as an argument and to additionally filter pages with .filter(sites__in=[request.site])\r\n\r\nAlthough it's very cumbersome, it actually works.\r\n\r\nI tried other, seemingly more elegant approaches, but they didn't work:\r\n\r\n1. If I create a custom manager and try to redefine ``objects`` in Page, then ``AttributeError: 'NoneType' object has no attribute '_mptt_meta'`` is thrown in django-mptt. It's probably because of all the MetaClass magic. I assume there must be some way to fix that?\r\n2. I can override FEINCMS_DEFAULT_PAGE_MODEL, but it creates many unwanted side-effects (i.e. I will have to manually recreate Page admin to import custom Page model, because current admin.py in FeinCMS does not use FENCMS_DEFAULT_PAGE_MODEL). Even when I did that, I had to rename db_table in the custom Page.Meta class, after which django was still complaining about two different models named Page. I ended up ditching this approach because too many things started falling apart.\r\n3. If I use ``add_to_class('objects', PageSitesManager())`` in my custom extension, where PageSitesManager is the custom Manager will all the important methods overridden, it simply does nothing.\r\n\r\nIs there a more elegant/obvious way to override PageManager without causing errors in django-mptt?","author":{"url":"https://github.com/paramono","@type":"Person","name":"paramono"},"datePublished":"2020-05-31T23:18:43.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/687/feincms/issues/687"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e0539bb7-eb7f-5ebf-409f-a4e88c96afa2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D8DE:C3485:24A3B2E:2F1479E:6975F197 |
| html-safe-nonce | 1b718ab1928663d4b1185de391a444f9519c468f70c365b33a99258f0a205acd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOERFOkMzNDg1OjI0QTNCMkU6MkYxNDc5RTo2OTc1RjE5NyIsInZpc2l0b3JfaWQiOiI0MDkzOTk0NzMxMzMzNjExOTI3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0f57fde0803c2b99f461531f2ac7000221def558b2a2f6223e7e0d42483ab3b1 |
| hovercard-subject-tag | issue:628065418 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/feincms/feincms/687/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6f4d6edecc2ada3a8628f8e375e6f8fd5deede7f04356c294cf6525491fa0114/feincms/feincms/issues/687 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6f4d6edecc2ada3a8628f8e375e6f8fd5deede7f04356c294cf6525491fa0114/feincms/feincms/issues/687 |
| og:image:alt | I would like to override a few methods in PageManager - what would be the most efficient way to achieve that? Here's some context to explain why I want to do that. I wanted to use Django's Sites fr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | paramono |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2bce766e7450b03e00b2fc5badd417927ce33a860e78cda3e4ecb9bbd1374cc6 |
| turbo-cache-control | no-preview |
| go-import | github.com/feincms/feincms git https://github.com/feincms/feincms.git |
| octolytics-dimension-user_id | 935594 |
| octolytics-dimension-user_login | feincms |
| octolytics-dimension-repository_id | 116021 |
| octolytics-dimension-repository_nwo | feincms/feincms |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 116021 |
| octolytics-dimension-repository_network_root_nwo | feincms/feincms |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | fcca2b8ef702b5f7f91427a6e920fa44446fe312 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width