Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / plugins / date.py @ a2744508

History | View | Annotate | Download (3.22 KB)

1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4:
3
################################################################################
4
#
5
# Copyright (C) 2007-2012 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 la date à laquelle
23
est survenu un événement et la durée depuis laquelle l'événement est actif.
24
"""
25
import tw.forms as twf
26
from pylons.i18n import ugettext as _, lazy_ugettext as l_
27
from tg.i18n import get_lang
28
import tg
29

    
30
from vigilo.models import tables
31

    
32
from vigiboard.controllers.plugins import VigiboardRequestPlugin, ITEMS
33
from vigiboard.lib.dateformat import DateFormatConverter
34

    
35
def get_calendar_lang():
36
    # TODO: Utiliser le champ "language" du modèle pour cet utilisateur ?
37
    # On récupère la langue du navigateur de l'utilisateur
38
    lang = get_lang()
39
    if not lang:
40
        lang = tg.config['lang']
41
    else:
42
        lang = lang[0]
43

    
44
    # TODO: Il faudrait gérer les cas où tout nous intéresse dans "lang".
45
    # Si l'identifiant de langage est composé (ex: "fr_FR"),
46
    # on ne récupère que la 1ère partie.
47
    lang = lang.replace('_', '-')
48
    lang = lang.split('-')[0]
49
    return lang
50

    
51
def get_date_format():
52
    # @HACK: nécessaire car l_() retourne un object LazyString
53
    # qui n'est pas sérialisable en JSON.
54
    return _('%Y-%m-%d %I:%M:%S %p').encode('utf-8')
55

    
56
class PluginDate(VigiboardRequestPlugin):
57
    """Plugin pour l'ajout d'une colonne Date."""
58
    def get_search_fields(self):
59
        return [
60
            twf.CalendarDateTimePicker(
61
                'from_date',
62
                label_text=l_('From'),
63
                button_text=l_("Choose"),
64
                not_empty=False,
65
                validator=DateFormatConverter(if_missing=None),
66
                date_format=get_date_format,
67
                calendar_lang=get_calendar_lang,
68
            ),
69
            twf.CalendarDateTimePicker(
70
                'to_date',
71
                label_text=l_('To'),
72
                button_text=l_("Choose"),
73
                not_empty=False,
74
                validator=DateFormatConverter(if_missing=None),
75
                date_format=get_date_format,
76
                calendar_lang=get_calendar_lang,
77
            ),
78
        ]
79

    
80
    def handle_search_fields(self, query, search, state, subqueries):
81
        if state != ITEMS:
82
            return
83

    
84
        if search.get('from_date'):
85
            query.add_filter(tables.CorrEvent.timestamp_active >=
86
                search['from_date'])
87
        if search.get('to_date'):
88
            query.add_filter(tables.CorrEvent.timestamp_active <=
89
                search['to_date'])