How to override a view class?

Hi,

class LogoutView(BrowserView):

def __call__(self):
    mt = getToolByName(self.context, 'portal_membership')
    mt.logoutUser(self.request)
    transaction_note('Logged out')
    # Handle external logout requests from other portals
    next_ = self.request.get('next', None)
    portal_url = getToolByName(self.context, 'portal_url')
    if next_ is not None and portal_url.isURLInPortal(next_):
        target_url = next_
    else:
        target_url = self.request.URL1 + '/logged-out'

    registry = queryUtility(IRegistry)
    external_logout_url = registry['plone.external_logout_url']
    if external_logout_url:
        target_url = external_logout_url
    self.request.response.redirect(target_url)

In this class from Products.CMFPlone i wan to change " '/logged-out' " inside the else condition to '/my_custom ' page from my custom addon. How can i do that?.

The standard approach is to use "monkey patching" for Python code.

This might be helpful (not sure if it works with most decent Plone versions):

Hi.

You can register the view using your package layer interface, and override the desired method:


  <browser:page
      name="logout"
      for="plone.app.layout.navigation.interfaces.INavigationRoot"
      permission="zope.Public"
      class=".logout.LogoutView"
      layer="plugin.autenticacion.interfaces.IPluginAutenticacionLayer"
      />
1 Like

I think i tried this on early.let me try once again

Whohoo!!.Its Worked tnx @rber474

1 Like

Is there any example code for understanding the monkey patches

You could also just override the browser page logged-out to just do what your addon wants to do. The best approach to override a browser page would be to register your own on your custom browserlayer, like @rber474 described.

Thank you sir.

Shahkhan Sh via Plone Community wrote at 2024-1-15 09:57 +0000:

...
In this class from Products.CMFPlone i wan to change " '/logged-out' " inside the else condition to '/my_custom ' page from my custom addon. How can i do that?.

In the ZMI, you can override some views via the object
"portal_view_customizations".

2 Likes

Hi, I'm again
i created a welcome page and through the welcome page that redirects to login page.after successfully login the welcome page appears again. what was the problem and its solution to remove that?

I think, there has been common agreement for more than a decade or more that portal_view_customizations is a misconception and should no longer be used.

1 Like

Shahkhan Sh via Plone Community wrote at 2024-1-15 12:08 +0000:

i created a welcome page and through the welcome page that redirects to login page.after successfully login the welcome page appears again. what was the problem and its solution to remove that?

The standard redirect logic for login:
If a view requires authentication, it redirects to the login page
and tells this via the parameter "came_from" where the login page
should redirect after a successful login.
If "came_from" is present and points somewhere into the portal,
the login page will redirect there after a successful login, otherwise
it will redirect to the portal.

adapter = queryMultiAdapter(
(self.context, self.request),
IRedirectAfterLogin
)
if adapter:
came_from = adapter(came_from, is_initial_login)
if not came_from:
came_from = self.context.absolute_url()

    self.request.response.redirect(came_from)

I think the adapter is this.

is like the concept of method overriding?