Passed
Push — 2.x ( 2a1b20...d626d0 )
by Jordi
07:27
created

senaite.core.browser.clients.client.contacts.myorganization   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 26
dl 0
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MyOrganizationView.available() 0 8 3
A MyOrganizationView.__call__() 0 20 4
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2025 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
from bika.lims.browser import BrowserView
23
from senaite.core.config.registry import CLIENT_LANDING_PAGE
24
from senaite.core.registry import get_registry_record
25
26
27
class MyOrganizationView(BrowserView):
28
    """Redirects to the current user's organization view, if any. Otherwise,
29
    falls-back to portal view
30
    """
31
32
    def __call__(self):
33
34
        client = api.get_current_client()
35
        if client:
36
            # User belongs to a client, redirect to client's default view
37
            view = get_registry_record(CLIENT_LANDING_PAGE)
38
            url = "{}/{}".format(api.get_url(client), view)
39
            return self.request.response.redirect(url)
40
41
        current_user = api.get_current_user()
42
        contact = api.get_user_contact(current_user)
43
        if contact and not contact.isGlobal():
44
            # Redirect to the contact's container
45
            parent = api.get_parent(contact)
46
            url = api.get_url(parent)
47
            return self.request.response.redirect(url)
48
49
        # Not a contact, redirect to portal
50
        url = api.get_url(api.get_portal())
51
        return self.request.response.redirect(url)
52
53
    def available(self):
54
        """Available expression for the menu action
55
        """
56
        if not api.is_client_contact(self.context):
57
            return False
58
        if self.context.isGlobal():
59
            return False
60
        return True
61