Project

General

Profile

Revision 4b573169

ID4b573169205951fae4671ab9ce8bc895e3832241
Parent f37d1e09
Child 741ff4aa

Added by Francois POIROTTE over 13 years ago

[#160] Utilisation de webhelpers.paginate() pour la pagination dans VigiBoard.

En plus de gérer automatiquement les cas aux limites (ex: page demandée > pages disponibles),
le module paginate centralise aussi les informations concernant la pagination dans un seul
objet, ce qui facilite la lecture du code.
La pagination peut se faire selon un modèle permettant de spécifier les liens qui doivent
apparaître, offrant une plus grande flexibilité pour l'affichage (ex: première page, page
précédente, pages aux alentours, page suivante, dernière page).

git-svn-id: https://vigilo-dev.si.c-s.fr/svn@6448 b22e2e97-25c9-44ff-b637-2e5ceca36478

View differences:

vigiboard/controllers/root.py
25 25
import math
26 26

  
27 27
from tg.exceptions import HTTPNotFound, HTTPInternalServerError
28
from tg import expose, validate, require, flash, \
28
from tg import expose, validate, require, flash, url, \
29 29
    tmpl_context, request, config, session, redirect
30
from webhelpers import paginate
30 31
from tw.forms import validators
31 32
from pylons.i18n import ugettext as _, lazy_ugettext as l_
32 33
from sqlalchemy import asc
......
57 58
from vigiboard.controllers.vigiboardrequest import VigiboardRequest
58 59
from vigiboard.controllers.vigiboard_controller import VigiboardRootController
59 60

  
60
from vigiboard.widgets.edit_event import edit_event_status_options
61
from vigiboard.widgets.edit_event import edit_event_status_options, \
62
                                            EditEventForm
61 63
from vigiboard.widgets.search_form import create_search_form, get_calendar_lang
62 64

  
63 65
__all__ = ('RootController', 'get_last_modification_timestamp',
......
212 214
            else:
213 215
                aggregates.add_filter(CorrEvent.timestamp_active <= to_date)
214 216

  
215
        # Calcul des éléments à afficher et du nombre de pages possibles
216
        total_rows = aggregates.num_rows()
217
        # Pagination des résultats
218
        aggregates.generate_request()
217 219
        items_per_page = int(config['vigiboard_items_per_page'])
218

  
219
        id_first_row = items_per_page * (page-1)
220
        id_last_row = min(id_first_row + items_per_page, total_rows)
221

  
222
        # Si le numéro de page dépasse le nombre de pages existantes,
223
        # on redirige automatiquement vers la 1ère page.
224
        if total_rows and id_first_row >= total_rows:
225
            redirect('/', page=total_rows / items_per_page, **search)
226

  
227
        aggregates.format_events(id_first_row, id_last_row)
228
        aggregates.generate_tmpl_context()
229

  
230
        nb_pages = int(math.ceil(total_rows / (items_per_page + 0.0)))
231
        if not total_rows:
232
            id_first_row = 0
233
        else:
234
            id_first_row += 1
220
        page = paginate.Page(aggregates.req, page=page, items_per_page=items_per_page)
235 221

  
236 222
        # Récupération des données des plugins
237 223
        plugins_data = {}
238 224
        plugins = dict(config['columns_plugins'])
225

  
226
        ids_events = [event[0].idcause for event in page.items]
227
        ids_correvents = [event[0].idcorrevent for event in page.items]
239 228
        for plugin in plugins:
240
            plugin_data = plugins[plugin].get_bulk_data(
241
                [event[0].idcorrevent for event in aggregates.events]
242
            )
243
            if plugin_data :
229
            plugin_data = plugins[plugin].get_bulk_data(ids_correvents)
230
            if plugin_data:
244 231
                plugins_data[plugin] = plugin_data
245 232

  
233
        # Ajout des formulaires et préparation
234
        # des données pour ces formulaires.
235
        tmpl_context.last_modification = \
236
            mktime(get_last_modification_timestamp(ids_events).timetuple())
237

  
238
        tmpl_context.edit_event_form = EditEventForm("edit_event_form",
239
            submit_text=_('Apply'), action=url('/update'))
240

  
246 241
        return dict(
247 242
            hostname = None,
248 243
            servicename = None,
249
            events = aggregates.events,
250 244
            plugins_data = plugins_data,
251
            rows_info = {
252
                'id_first_row': id_first_row,
253
                'id_last_row': id_last_row,
254
                'total_rows': total_rows,
255
            },
256
            nb_pages = nb_pages,
257 245
            page = page,
258 246
            event_edit_status_options = edit_event_status_options,
259 247
            search_form = create_search_form,
......
319 307
        elif isinstance(cause_supitem, Host):
320 308
            hostname = cause_supitem.name
321 309

  
310
        # Pagination des résultats
311
        events.generate_request()
312
        items_per_page = int(config['vigiboard_items_per_page'])
313
        page = paginate.Page(events.req, page=page, items_per_page=items_per_page)
314

  
322 315
        # Vérification que l'événement existe
323
        total_rows = events.num_rows()
324
        if total_rows < 1:
316
        if not page.item_count:
325 317
            flash(_('No masked event or access denied'), 'error')
326 318
            redirect('/')
327 319

  
328
         # Calcul des éléments à afficher et du nombre de pages possibles
329
        total_rows = events.num_rows()
330
        items_per_page = int(config['vigiboard_items_per_page'])
331

  
332
        id_first_row = items_per_page * (page-1)
333
        id_last_row = min(id_first_row + items_per_page, total_rows)
334

  
335
        events.format_events(id_first_row, id_last_row)
336
        events.generate_tmpl_context()
337

  
338
        nb_pages = int(math.ceil(total_rows / (items_per_page + 0.0)))
339
        if not total_rows:
340
            id_first_row = 0
341
        else:
342
            id_first_row += 1
343

  
344 320
        return dict(
345 321
            idcorrevent = idcorrevent,
346 322
            hostname = hostname,
347 323
            servicename = servicename,
348
            events = events.events,
349 324
            plugins_data = {},
350
            rows_info = {
351
                'id_first_row': id_first_row,
352
                'id_last_row': id_last_row,
353
                'total_rows': total_rows,
354
            },
355
            nb_pages = nb_pages,
356 325
            page = page,
357 326
            search_form = create_search_form,
358 327
            search = {},
......
406 375
        events.generate_tmpl_context()
407 376
        history = events.format_history()
408 377

  
409
        total_rows = history.count()
378
        # Pagination des résultats
410 379
        items_per_page = int(config['vigiboard_items_per_page'])
411

  
412
        id_first_row = items_per_page * (page-1)
413
        id_last_row = min(id_first_row + items_per_page, total_rows)
414

  
415
        history_entries = history[id_first_row : id_last_row]
416

  
417
        nb_pages = int(math.ceil(total_rows / (items_per_page + 0.0)))
418
        if not total_rows:
419
            id_first_row = 0
420
        else:
421
            id_first_row += 1
422

  
380
        page = paginate.Page(history, page=page, items_per_page=items_per_page)
423 381
        event = events.req[0]
424 382

  
425 383
        return dict(
......
427 385
            hostname = event.hostname,
428 386
            servicename = event.servicename,
429 387
            plugins_data = {},
430
            rows_info = {
431
                'id_first_row': id_first_row,
432
                'id_last_row': id_last_row,
433
                'total_rows': total_rows,
434
            },
435
            nb_pages = nb_pages,
436 388
            page = page,
437
            history = history_entries,
438 389
            search_form = create_search_form,
439 390
            search = {},
440 391
            get_calendar_lang = get_calendar_lang,
......
482 433
            Event.idsupitem == aggregates.items.c.idsupitem))
483 434
        aggregates.add_filter(aggregates.items.c.idsupitem == idsupitem)
484 435

  
436
        # Pagination des résultats
437
        aggregates.generate_request()
438
        items_per_page = int(config['vigiboard_items_per_page'])
439
        page = paginate.Page(aggregates.req, page=page, items_per_page=items_per_page)
440

  
485 441
        # Vérification qu'il y a au moins 1 événement qui correspond
486
        total_rows = aggregates.num_rows()
487
        if not total_rows:
442
        if not page.item_count:
488 443
            flash(_('No access to this host/service or no event yet'), 'error')
489 444
            redirect('/')
490 445

  
491
        items_per_page = int(config['vigiboard_items_per_page'])
446
        # Ajout des formulaires et préparation
447
        # des données pour ces formulaires.
448
        ids_events = [event[0].idcause for event in page.items]
449
        tmpl_context.last_modification = \
450
            mktime(get_last_modification_timestamp(ids_events).timetuple())
492 451

  
493
        id_first_row = items_per_page * (page-1)
494
        id_last_row = min(id_first_row + items_per_page, total_rows)
495

  
496
        aggregates.format_events(id_first_row, id_last_row)
497
        aggregates.generate_tmpl_context()
498

  
499
        nb_pages = int(math.ceil(total_rows / (items_per_page + 0.0)))
500
        if not total_rows:
501
            id_first_row = 0
502
        else:
503
            id_first_row += 1
452
        tmpl_context.edit_event_form = EditEventForm("edit_event_form",
453
            submit_text=_('Apply'), action=url('/update'))
504 454

  
505 455
        return dict(
506 456
            hostname = host,
507 457
            servicename = service,
508
            events = aggregates.events,
509 458
            plugins_data = {},
510
            rows_info = {
511
                'id_first_row': id_first_row,
512
                'id_last_row': id_last_row,
513
                'total_rows': total_rows,
514
            },
515
            nb_pages = nb_pages,
516 459
            page = page,
517 460
            event_edit_status_options = edit_event_status_options,
518 461
            search_form = create_search_form,

Also available in: Unified diff