pages.py 3.74 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-

# Copyright(C) 2018      Vincent A
#
5
# This file is part of a weboob module.
6
#
7
# This weboob module is free software: you can redistribute it and/or modify
Vincent A's avatar
Vincent A committed
8
# it under the terms of the GNU Lesser General Public License as published by
9 10 11
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
12
# This weboob module is distributed in the hope that it will be useful,
13 14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Vincent A's avatar
Vincent A committed
15
# GNU Lesser General Public License for more details.
16
#
Vincent A's avatar
Vincent A committed
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25

from __future__ import unicode_literals

from datetime import timedelta

from weboob.browser.pages import HTMLPage, LoggedPage, JsonPage
from weboob.capabilities.calendar import BaseCalendarEvent, STATUS
26 27 28 29 30
from weboob.capabilities.bill import (
    Subscription, Document, DocumentTypes,
)
from weboob.tools.date import new_date, parse_date
from weboob.tools.compat import urljoin
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101


class LoginPage(HTMLPage):
    pass


class HomePage(LoggedPage, HTMLPage):
    pass


class User(object):
    id = None
    name = None
    start = None
    end = None


class UsersPage(LoggedPage, JsonPage):
    def iter_users(self):
        for dpt in self.doc['data']:
            for d in dpt['users']:
                u = User()
                u.id = d['id']
                u.name = d['displayName']

                v = d['dtContractStart']
                if v:
                    u.start = parse_date(v)
                v = d['dtContractEnd']
                if v:
                    u.end = parse_date(v)

                yield u


class CalendarPage(LoggedPage, JsonPage):
    @staticmethod
    def _offset(start_date, offset):
        return start_date + timedelta(days=offset)

    def iter_events(self, start_date, users):
        start_date = start_date.replace(hour=0, minute=0, second=0, microsecond=0)
        for d in self.doc['data']:
            if not d['ls']: # seems to be validation state
                continue
            assert d['ls'] == 2

            user = users[d['u']]

            ev = BaseCalendarEvent()
            ev.timezone = 'Europe/Paris'
            ev.summary = user.name
            ev.status = STATUS.CONFIRMED
            ev.start_date = self._offset(start_date, d['o'])

            if d['a'] == 2:
                ev.end_date = ev.start_date + timedelta(days=1)

                ev.start_date = ev.start_date.date()
                ev.end_date = ev.end_date.date()
            elif d['a'] == 1:
                ev.start_date = ev.start_date + timedelta(hours=12)
                ev.end_date = ev.start_date + timedelta(hours=12)
            else:
                assert d['a'] == 0
                ev.end_date = ev.start_date + timedelta(hours=12)

            if user.end and new_date(user.end) < new_date(ev.start_date):
                continue

            yield ev
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123


class SubscriptionPage(LoggedPage, JsonPage):
    def get_subscription(self):
        sub = Subscription()
        sub.id = str(self.doc['data']['id'])
        sub.subscriber = sub.label = self.doc['header']['principal']
        return sub


class DocumentsPage(LoggedPage, JsonPage):
    def iter_documents(self, subid):
        for d in self.doc['data']['items']:
            doc = Document()
            doc.id = '%s_%s' % (subid, d['id'])
            doc._docid = d['id']
            doc.label = d['import']['name']
            doc.date = parse_date(d['import']['endDate'])
            doc.url = urljoin(self.url, '/pagga/download/%s' % doc._docid)
            doc.type = DocumentTypes.BILL
            doc.format = 'pdf'
            yield doc