Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / plugins / groups.py @ 25892058

History | View | Annotate | Download (4.63 KB)

1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4:
3
################################################################################
4
#
5
# Copyright (C) 2007-2013 CS-SI
6
#
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License version 2 as
9
# published by the Free Software Foundation.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
################################################################################
20

    
21
"""
22
Un plugin pour VigiBoard qui ajoute une colonne avec les groupes
23
d'éléments supervisés auxquels appartient l'objet associé
24
à l'événement corrélé.
25
"""
26
import tw.forms as twf
27
from pylons.i18n import lazy_ugettext as l_
28

    
29
from vigiboard.controllers.plugins import VigiboardRequestPlugin, INNER
30
from vigilo.models.session import DBSession
31
from vigilo.models import tables
32
from vigilo.models.tables.group import Group
33
from vigilo.models.tables.grouphierarchy import GroupHierarchy
34
from vigilo.models.tables.secondary_tables import SUPITEM_GROUP_TABLE
35
from sqlalchemy.orm import aliased
36
from sqlalchemy import or_
37

    
38

    
39
class GroupSelector(twf.InputField):
40
    params = ["choose_text", "text_value", "clear_text"]
41
    choose_text = l_('Choose')
42
    clear_text = l_('Clear')
43
    text_value = ''
44

    
45
    template = """
46
<div xmlns="http://www.w3.org/1999/xhtml"
47
   xmlns:py="http://genshi.edgewall.org/" py:strip="">
48
<input type="hidden" name="${name}" class="${css_class}"
49
    id="${id}.value" value="${value}" py:attrs="attrs" />
50
<input type="text" class="${css_class}" id="${id}.ui"
51
    value="${text_value}" readonly="readonly" py:attrs="attrs" />
52
<input type="button" class="${css_class}" id="${id}"
53
    value="${choose_text}" py:attrs="attrs" />
54
<input type="button" class="${css_class}" id="${id}.clear"
55
    value="${clear_text}" py:attrs="attrs" />
56
</div>
57
"""
58

    
59
    def update_params(self, d):
60
        super(GroupSelector, self).update_params(d)
61
        text_value = DBSession.query(Group.name).filter(
62
                        Group.idgroup == d.value).scalar()
63
        if not text_value:
64
            d.value = ''
65
        else:
66
            d.text_value = text_value
67

    
68

    
69
class PluginGroups(VigiboardRequestPlugin):
70
    """
71
    Affiche les groupes d'éléments supervisés auxquels
72
    appartient l'événement corrélé.
73
    """
74
    def get_search_fields(self):
75
        return [
76
            GroupSelector(
77
                'supitemgroup',
78
                label_text=l_('Group'),
79
                validator=twf.validators.Int(if_invalid=None, if_missing=None),
80
            )
81
        ]
82

    
83
    def handle_search_fields(self, query, search, state, subqueries):
84
        if search.get('supitemgroup') is None or state != INNER:
85
            return
86

    
87
        # Il s'agit d'un manager. On applique le filtre
88
        # indépendamment aux 2 sous-requêtes.
89
        if len(subqueries) == 2:
90
            subqueries[0] = subqueries[0].join(
91
                (SUPITEM_GROUP_TABLE,
92
                    or_(
93
                        SUPITEM_GROUP_TABLE.c.idsupitem == \
94
                            tables.LowLevelService.idhost,
95
                        SUPITEM_GROUP_TABLE.c.idsupitem == \
96
                            tables.LowLevelService.idservice,
97
                    )
98
                ),
99
                (GroupHierarchy, GroupHierarchy.idchild ==
100
                    SUPITEM_GROUP_TABLE.c.idgroup)
101
            ).filter(
102
                GroupHierarchy.idparent == search['supitemgroup']
103
            )
104

    
105
            subqueries[1] = subqueries[1].join(
106
                (SUPITEM_GROUP_TABLE,
107
                    SUPITEM_GROUP_TABLE.c.idsupitem == \
108
                        tables.Host.idhost,
109
                ),
110
                (GroupHierarchy, GroupHierarchy.idchild ==
111
                    SUPITEM_GROUP_TABLE.c.idgroup)
112
            ).filter(
113
                GroupHierarchy.idparent == search['supitemgroup']
114
            )
115

    
116
        # Il s'agit d'un utilisateur normal.
117
        else:
118
            GroupHierarchy_aliased = aliased(GroupHierarchy,
119
                name='GroupHierarchy_aliased')
120
            subqueries[0] = subqueries[0].join(
121
                (GroupHierarchy_aliased, GroupHierarchy_aliased.idchild ==
122
                    tables.UserSupItem.idsupitemgroup)
123
            ).filter(
124
                 GroupHierarchy_aliased.idparent == search['supitemgroup']
125
            )
126