aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas "Cakeisalie5" Touhey <thomas@touhey.fr>2018-10-29 23:59:25 +0100
committerThomas "Cakeisalie5" Touhey <thomas@touhey.fr>2018-10-29 23:59:25 +0100
commit56b7f45fc1804cfa8eb6b419a90f13d1aec063b5 (patch)
tree7b3c2c7d1fb34839c5aedc3b9be8c2fb02061df0
parenta1926f6b195ccaee7c994848aaf4f70d2fdec976 (diff)
Corrected a few things, yet there is plenty to be done.
-rwxr-xr-xsgdfi/_decode.py83
-rwxr-xr-xsgdfi/_intranet.py48
-rwxr-xr-xsgdfi/_util.py6
3 files changed, 105 insertions, 32 deletions
diff --git a/sgdfi/_decode.py b/sgdfi/_decode.py
index 05071eb..ea65cf5 100755
--- a/sgdfi/_decode.py
+++ b/sgdfi/_decode.py
@@ -6,7 +6,7 @@
""" Main object to decode files and dumps from SGDF's intranet. """
from itertools import count as _count
-from datetime import datetime as _datetime
+from datetime import datetime as _datetime, date as _date
from re import finditer as _rfindter, sub as _rreplace, split as _rsplit
from io import IOBase as _IOBase, StringIO as _StringIO, BytesIO as _BytesIO, \
TextIOWrapper as _TextIOWrapper
@@ -229,9 +229,6 @@ class Decoder:
resp.append(_AjaxField(code, name, attrib, text))
- if hint == 'sic':
- return resp
-
# The answer's payload usually is in the second entry.
# The code may allow to determinate what it corresponds to,
# but a lot of codes can be used depending on the actions
@@ -241,6 +238,31 @@ class Decoder:
field = resp[1]
+ # Check if it is an error.
+
+ if field.name == "pageRedirect":
+ loc = _unquote(field.text)
+
+ try:
+ p = _urlparse(loc)
+ path = p.path.casefold()
+
+ if path in ('/erreurs/erreur.aspx',
+ '/specialisation/sgdf/erreurs/erreur.aspx'):
+ path = _parse_qs(p.query)['aspxerrorpath'][0]
+ raise _BadRequestError() from None
+ elif path in ('/erreurs/404.aspx',
+ '/specialisation/sgdf/erreurs/404.aspx'):
+ path = _parse_qs(p.query)['aspxerrorpath'][0]
+ raise _NotFoundError() from None
+ elif path in ('/erreurs/interdit.aspx',
+ '/specialisation/sgdf/erreurs/interdit.aspx'):
+ raise _ForbiddenError() from None
+ except (AssertionError, KeyError, IndexError):
+ pass
+
+ raise _RedirectError(loc)
+
# If we need a hint, let's find out the hint.
if hint is None:
@@ -262,7 +284,14 @@ class Decoder:
msg = "Ajax hint could not be determined"
raise ValueError(msg) from None
- return func(field.text)
+ # Let's decode the content using BeautifulSoup then send it
+ # to the function!
+
+ tree = _BeautifulSoup(field.text, 'lxml')
+ if hint == 'sic':
+ return tree
+
+ return func(tree)
elif type == 'text/csv':
# The input might be of different type, we want a text stream.
@@ -888,15 +917,15 @@ class Decoder:
# HTML (Ajax) fragments decoding.
# ---
- _decode_ajax_intranet_personlist_codes = (9192, 9431, 9449, 15819, 17464,
+ _decode_ajax_intranet_adherentlist_codes = (9192, 9431, 9449, 15819, 17464,
17721, 20723, 22607, 22864)
- _decode_ajax_intranet_person_summary_codes = (21686,)
- _decode_ajax_intranet_person_family_codes = (7190,)
+ _decode_ajax_intranet_adherent_summary_codes = (21686,)
+ _decode_ajax_intranet_adherent_family_codes = (7190,)
_decode_ajax_intranet_calendar_month_codes = (23896, 24180, 24954, 26883,
26931, 27407, 27933, 28026, 29232, 31576, 33283, 33288, 33438, 38663,
39664, 40051, 47167)
- def _decode_html_intranet_personlist_fragment(self, tree):
+ def _decode_html_intranet_adherentlist_fragment(self, tree):
""" Decode the HTML operations from a BeautifulSoup decoded
content. """
@@ -917,7 +946,7 @@ class Decoder:
except:
continue
- p = Person()
+ p = _Adherent()
# Nom et ID interne.
@@ -980,7 +1009,7 @@ class Decoder:
return elts
- def _decode_html_intranet_person_summary_fragment(self, tree):
+ def _decode_html_intranet_adherent_summary_fragment(self, tree):
""" Decode the HTML summary page fragment for a person. """
parent = tree.find(id = 'ctl00_ctl00_MainContent_TabsContent_' \
@@ -999,7 +1028,7 @@ class Decoder:
# IID (from the print button).
iid = tree.find(id = '_btnPrint')
- iid = iid['onclick']
+ iid = iid.attrs['onclick']
iid = iid[iid.find('ResumeAdherent.aspx?id=') + 23:]
iid = iid[:iid.find('&')]
p.iid = iid
@@ -1110,7 +1139,7 @@ class Decoder:
bd = bd.text
mday, mon, year = map(int, bd.split('/'))
- p.birth_date = Date(year, mon, mday)
+ p.birth_date = _date(year, mon, mday)
# Onglet "Personne" : commune de naissance.
@@ -1136,22 +1165,18 @@ class Decoder:
nal = parent.find(id = f'{rpre}modeleIndividu__lblNumeroAllocataire')
nal = nal.text
- p.allocations.number = (None, nal)[nal != '.']
-
- caf = parent.find(id = f'{rpre}modeleIndividu__cbRegimeCAF')
+ caf = parent.find(id = f'{rpre}modeleIndividu__cbRegimeGeneral')
msa = parent.find(id = f'{rpre}modeleIndividu__cbRegimeMSA')
- mar = parent.find(id = f'{rpre}modeleIndividu__cbRegimeMaritime')
- aut = parent.find(id = f'{rpre}modeleIndividu__cbRegimeAutre')
- aun = parent.find(id = f'{rpre}modeleIndividu__lblRegimeAllocataire')
- aun = aun.text
-
- if checked(caf) or checked(mar):
- p.allocations.regime = AL_GEN
- elif checked(msa):
- p.allocations.regime = AL_MSA
- else:
- p.allocations.regime = AL_OTH
- p.allocations.regime_name = ("", aun)[aun != '.']
+ aut = parent.find(id = f'{rpre}modeleIndividu__cbRegimeEtranger')
+
+ #p.allocations.number = (None, nal)[nal != '.']
+ #if checked(caf) or checked(mar):
+ # p.allocations.regime = AL_GEN
+ #elif checked(msa):
+ # p.allocations.regime = AL_MSA
+ #else:
+ # p.allocations.regime = AL_OTH
+ # p.allocations.regime_name = ("", aun)[aun != '.']
# Onglet "Personne" : autorise les interventions chirurgicales.
@@ -1181,7 +1206,7 @@ class Decoder:
return p
- def _decode_html_intranet_person_family_fragment(self, tree):
+ def _decode_html_intranet_adherent_family_fragment(self, tree):
""" Decode the HTML family page fragment for an adherent. """
parent = tree.find(id = 'ctl00_ctl00_MainContent_TabsContent_' \
diff --git a/sgdfi/_intranet.py b/sgdfi/_intranet.py
index 6866b56..9d410a2 100755
--- a/sgdfi/_intranet.py
+++ b/sgdfi/_intranet.py
@@ -26,7 +26,8 @@ from ._util import InvalidCredentialsError as _InvalidCredentialsError, \
InvalidPasswordError as _InvalidPasswordError, \
UnauthorizedAccountError as _UnauthorizedAccountError, \
RedirectError as _RedirectError, BadRequestError as _BadRequestError, \
- NotFoundError as _NotFoundError, Pagination as _Pagination
+ NotFoundError as _NotFoundError, ForbiddenError as _ForbiddenError, \
+ Pagination as _Pagination
from ._dbs import StructureType as _StructureType, \
StructureTypeData as _StructureTypeData, \
StructureStatus as _StructureStatus, \
@@ -192,6 +193,9 @@ class AnonymousIntranetSession:
'/specialisation/sgdf/erreurs/404.aspx'):
path = _parse_qs(p.query)['aspxerrorpath'][0]
raise _NotFoundError() from None
+ elif path in ('/erreurs/interdit.aspx',
+ '/specialisation/sgdf/erreurs/interdit.aspx'):
+ raise _ForbiddenError() from None
except (AssertionError, KeyError, IndexError):
pass
@@ -755,6 +759,48 @@ class IntranetSession(AnonymousIntranetSession):
return st
# ---
+ # Interact with adherents.
+ # ---
+
+ def get_adherents(self):
+ """ Get the adherents to which we can access. """
+
+ raise NotImplementedError
+
+ def get_adherent(self, ad_id):
+ """ Get an adherent. """
+
+ iid = _IID(ad_id)
+ path = '/Specialisation/Sgdf/adherents/ResumeAdherent.aspx' \
+ f'?id={iid.urlsafe()}'
+ stname = 'ctl00_ctl00_MainContent_TabsContent_' \
+ 'TabContainerResumeAdherent_ClientState'
+ state = {
+ 'ActiveTabIndex': 0,
+ 'TabState': [True] * 11}
+ args = {
+ stname: _jsondumps(state),
+ 'ctl00': {
+ 'ctl00': {
+ 'ScriptManager1': 'ctl00$ctl00$_upMainContent' \
+ '|ctl00$ctl00$MainContent$TabsContent$' \
+ 'TabContainerResumeAdherent'}}}
+
+ # Récupération du premier onglet, "Personne".
+
+ state['ActiveTabIndex'] = 0
+ args[stname] = _jsondumps(state)
+ result = self.get_page(path, args, method = self.METHOD_AJAX,
+ hint = 'intranet_adherent_summary')[0]
+
+ #state['ActiveTabIndex'] = 1
+ #args[stname] = _jsondumps(state)
+ #result = self.get_page(path, args, method = self.METHOD_AJAX,
+ # hint = 'person_family')
+
+ return result
+
+ # ---
# Interact with events.
# ---
diff --git a/sgdfi/_util.py b/sgdfi/_util.py
index a639c4b..dc07414 100755
--- a/sgdfi/_util.py
+++ b/sgdfi/_util.py
@@ -8,9 +8,9 @@
from inspect import getargspec as _getargspec
from copy import deepcopy as _deepcopy
-from urllib.parse import quote as _quote
+from urllib.parse import quote as _quote, unquote as _unquote
from enum import Enum
-from datetime import datetime as _datetime
+from datetime import datetime as _datetime, date as _date
from base64 import b64decode as _b64decode, b64encode as _b64encode
import regex as _re
@@ -224,6 +224,8 @@ class DateProperty(Property):
if isinstance(value, _datetime):
pass # use the value as is.
+ elif isinstance(value, _date):
+ value = _datetime.fromordinal(value.toordinal())
elif type(value) == str:
m = _datere.match(value)
if m is None: