filters.py 1.36 KB
Newer Older
erdnaxe's avatar
erdnaxe committed
1
import logging
erdnaxe's avatar
erdnaxe committed
2
import re
erdnaxe's avatar
erdnaxe committed
3
from datetime import date, timedelta
erdnaxe's avatar
erdnaxe committed
4 5 6

from icalendar_tools import get_event_start_date

erdnaxe's avatar
erdnaxe committed
7 8
logger = logging.getLogger('Filters')

erdnaxe's avatar
erdnaxe committed
9 10

def filter_date(event) -> bool:
erdnaxe's avatar
erdnaxe committed
11
    """Return true if event day is after the beginning of last week"""
erdnaxe's avatar
erdnaxe committed
12 13 14
    event_date = get_event_start_date(event)

    if event_date:
erdnaxe's avatar
erdnaxe committed
15 16
        from_date = date.today() - timedelta(weeks=1)
        return event_date >= from_date
erdnaxe's avatar
erdnaxe committed
17 18
    else:
        # If there is a bug in data then do not filter
erdnaxe's avatar
erdnaxe committed
19 20
        summary = event.get('summary')
        logger.warn('There was an issue with the date of {}'.format(summary))
erdnaxe's avatar
erdnaxe committed
21 22 23 24 25 26 27 28 29
        return True


def filter_group(event, selected_groups) -> bool:
    """Filter group according to the group selected"""
    summary = event.get('summary')
    summary_search = re.search('([0-9]{3}) - ([A-Z]{2}) \(grpe : ([^\)]*)\)', summary, re.IGNORECASE)

    if summary_search:
erdnaxe's avatar
erdnaxe committed
30 31 32 33
        ue, group = 'ue_' + summary_search.group(1), summary_search.group(3)
        if ue in selected_groups:
            # UE is configured
            if group in selected_groups[ue]:
erdnaxe's avatar
erdnaxe committed
34 35 36 37
                # Group was selected
                return True
            else:
                # Group is not the one selected
erdnaxe's avatar
erdnaxe committed
38
                logger.info('Removed {} because it had not been chosen'.format(summary))
erdnaxe's avatar
erdnaxe committed
39 40 41 42
                return False

    # If there is not a group specified do not filter
    return True