Skip to content

Commit

Permalink
Merge pull request #22 from AugustH/v1-11
Browse files Browse the repository at this point in the history
Version 1.11
  • Loading branch information
AugustH committed Mar 5, 2019
2 parents a8af9c4 + fdbcf53 commit eb5aefe
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 15 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='webuntis',
version='0.1.10',
version='0.1.11',
author='Markus Unterwaditzer',
author_email='markus@unterwaditzer.net',
packages=find_packages(),
Expand Down
127 changes: 125 additions & 2 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ class CustomListResult(self.Result):
self.assert_strict_equal(x, [results[2]])


x = list(r.filter(id=[1, 2]))
self.assert_strict_equal(x, [results[1], results[2]])
x = list(r.filter(id=[2, 1]))
self.assert_strict_equal(x, [results[2], results[1]])


class DepartmentTests(WebUntisTestCase):
def test_basics(self):
x = webuntis.objects.DepartmentObject(
Expand Down Expand Up @@ -443,12 +449,129 @@ def test_combine(self):
],
session=object())

pl2 = webuntis.objects.PeriodList(
data=[
{'id': 1216514, 'date': 20181210, 'startTime': 1135, 'endTime': 1225, 'kl': [{'id': 504}],
'te': [{'id': 129}], 'su': [{'id': 12}], 'ro': [{'id': 10}],
'activityType': 'Unterricht'},
{'id': 1225275, 'date': 20181210, 'startTime': 800, 'endTime': 850, 'kl': [{'id': 504}],
'te': [{'id': 181}], 'su': [{'id': 37}], 'ro': [{'id': 10}],
'activityType': 'Unterricht'},
{'id': 1225276, 'date': 20181210, 'startTime': 850, 'endTime': 940, 'kl': [{'id': 504}],
'te': [{'id': 181}], 'su': [{'id': 37}], 'ro': [{'id': 10}],
'activityType': 'Unterricht'},
{'id': 1249890, 'date': 20181210, 'startTime': 955, 'endTime': 1045, 'kl': [{'id': 504}],
'te': [{'id': 165}], 'su': [{'id': 167}], 'ro': [{'id': 10}],
'activityType': 'Unterricht'},
{'id': 1251794, 'date': 20181210, 'startTime': 1045, 'endTime': 1135, 'kl': [{'id': 504}],
'te': [{'id': 182}], 'su': [{'id': 185}], 'ro': [{'id': 10}],
'activityType': 'Unterricht'},
{'id': 1323913, 'date': 20181210, 'startTime': 1415, 'endTime': 1505, 'kl': [{'id': 504}],
'te': [{'id': 55}, {'id': 86}, {'id': 157}, {'id': 206}], 'su': [{'id': 195}],
'ro': [{'id': 10}, {'id': 23}, {'id': 159}, {'id': 177}], 'activityType': 'Unterricht'},
{'id': 1323914, 'date': 20181210, 'startTime': 1515, 'endTime': 1605, 'kl': [{'id': 504}],
'te': [{'id': 55}, {'id': 86}, {'id': 157}, {'id': 206}], 'su': [{'id': 195}],
'ro': [{'id': 10}, {'id': 23}, {'id': 159}, {'id': 177}], 'activityType': 'Unterricht'},
{'id': 1323915, 'date': 20181210, 'startTime': 1605, 'endTime': 1655, 'kl': [{'id': 504}],
'te': [{'id': 55}, {'id': 86}, {'id': 157}, {'id': 206}], 'su': [{'id': 195}],
'ro': [{'id': 10}, {'id': 23}, {'id': 159}, {'id': 177}], 'activityType': 'Unterricht'},
{'id': 1323916, 'date': 20181210, 'startTime': 1655, 'endTime': 1745, 'kl': [{'id': 504}],
'te': [{'id': 55}, {'id': 86}, {'id': 157}, {'id': 206}], 'su': [{'id': 195}],
'ro': [{'id': 10}, {'id': 23}, {'id': 159}, {'id': 177}], 'activityType': 'Unterricht'}

],
session=object())

pl3 = webuntis.objects.PeriodList(
data=[
{'id': 1284516, 'date': 20181219, 'startTime': 800, 'endTime': 850, 'kl': [{'id': 472}],
'te': [{'id': 30, 'orgid': 221}], 'su': [{'id': 210}], 'ro': [{'id': 69}],
'activityType': 'Unterricht'},
{'id': 1284517, 'date': 20181219, 'startTime': 850, 'endTime': 940, 'kl': [{'id': 472}],
'te': [{'id': 30, 'orgid': 221}], 'su': [{'id': 210}], 'ro': [{'id': 69}],
'activityType': 'Unterricht'},
{'id': 1284518, 'date': 20181219, 'startTime': 955, 'endTime': 1045, 'kl': [{'id': 472}],
'te': [{'id': 30, 'orgid': 221}], 'su': [{'id': 210}], 'ro': [{'id': 69}],
'activityType': 'Unterricht'},
{'id': 1284519, 'date': 20181219, 'startTime': 1045, 'endTime': 1135, 'kl': [{'id': 472}],
'te': [{'id': 30, 'orgid': 221}], 'su': [{'id': 210}], 'ro': [{'id': 69}],
'activityType': 'Unterricht'},
{'id': 1284520, 'date': 20181219, 'startTime': 1135, 'endTime': 1225, 'kl': [{'id': 472}],
'te': [{'id': 30, 'orgid': 221}], 'su': [{'id': 210}], 'ro': [{'id': 69}],
'activityType': 'Unterricht'},
{'id': 1284521, 'date': 20181219, 'startTime': 1325, 'endTime': 1415, 'kl': [{'id': 472}],
'te': [{'id': 30, 'orgid': 221}], 'su': [{'id': 210}], 'ro': [{'id': 69}],
'activityType': 'Unterricht'},
{'id': 1284522, 'date': 20181219, 'startTime': 1415, 'endTime': 1505, 'kl': [{'id': 472}],
'te': [{'id': 30, 'orgid': 221}], 'su': [{'id': 210}], 'ro': [{'id': 69}],
'activityType': 'Unterricht'},
{'id': 1284523, 'date': 20181219, 'startTime': 1515, 'endTime': 1605, 'kl': [{'id': 472}],
'te': [{'id': 30, 'orgid': 221}], 'su': [{'id': 210}], 'ro': [{'id': 69}],
'activityType': 'Unterricht'},
{'id': 1284812, 'date': 20181219, 'startTime': 800, 'endTime': 850, 'kl': [{'id': 472}],
'te': [{'id': 161}], 'su': [{'id': 210}], 'ro': [{'id': 63}],
'activityType': 'Unterricht'},
{'id': 1284813, 'date': 20181219, 'startTime': 850, 'endTime': 940, 'kl': [{'id': 472}],
'te': [{'id': 161}], 'su': [{'id': 210}], 'ro': [{'id': 63}],
'activityType': 'Unterricht'},
{'id': 1284814, 'date': 20181219, 'startTime': 955, 'endTime': 1045, 'kl': [{'id': 472}],
'te': [{'id': 161}], 'su': [{'id': 210}], 'ro': [{'id': 63}],
'activityType': 'Unterricht'},
{'id': 1284815, 'date': 20181219, 'startTime': 1045, 'endTime': 1135, 'kl': [{'id': 472}],
'te': [{'id': 161}], 'su': [{'id': 210}], 'ro': [{'id': 63}],
'activityType': 'Unterricht'},
{'id': 1284816, 'date': 20181219, 'startTime': 1135, 'endTime': 1225, 'kl': [{'id': 472}],
'te': [{'id': 161}], 'su': [{'id': 210}], 'ro': [{'id': 63}],
'activityType': 'Unterricht'},
{'id': 1284817, 'date': 20181219, 'startTime': 1325, 'endTime': 1415, 'kl': [{'id': 472}],
'te': [{'id': 161}], 'su': [{'id': 210}], 'ro': [{'id': 63}],
'activityType': 'Unterricht'},
{'id': 1284818, 'date': 20181219, 'startTime': 1415, 'endTime': 1505, 'kl': [{'id': 472}],
'te': [{'id': 161}], 'su': [{'id': 210}], 'ro': [{'id': 63}],
'activityType': 'Unterricht'},
{'id': 1284819, 'date': 20181219, 'startTime': 1515, 'endTime': 1605, 'kl': [{'id': 472}],
'te': [{'id': 161}], 'su': [{'id': 210}], 'ro': [{'id': 63}],
'activityType': 'Unterricht'},
{'id': 1285108, 'date': 20181219, 'startTime': 800, 'endTime': 850, 'kl': [{'id': 472}],
'te': [{'id': 100}], 'su': [{'id': 210}], 'ro': [{'id': 65}],
'activityType': 'Unterricht'},
{'id': 1285109, 'date': 20181219, 'startTime': 850, 'endTime': 940, 'kl': [{'id': 472}],
'te': [{'id': 100}], 'su': [{'id': 210}], 'ro': [{'id': 65}],
'activityType': 'Unterricht'},
{'id': 1285110, 'date': 20181219, 'startTime': 955, 'endTime': 1045, 'kl': [{'id': 472}],
'te': [{'id': 100}], 'su': [{'id': 210}], 'ro': [{'id': 65}],
'activityType': 'Unterricht'},
{'id': 1285111, 'date': 20181219, 'startTime': 1045, 'endTime': 1135, 'kl': [{'id': 472}],
'te': [{'id': 100}], 'su': [{'id': 210}], 'ro': [{'id': 65}],
'activityType': 'Unterricht'}

],
session=object())

assert len(pl) == 3
combined = pl.combine()
assert len(combined) == 2
c0 = combined._data[0]
assert c0[u'startTime'] == 800

assert len(pl2) == 9
combined = pl2.combine(combine_breaks=False)
assert len(combined) == 6
combined = pl2.combine() # combine_breaks=True
assert len(combined) == 5

assert len(pl3) == 20
combined = pl3.combine(combine_breaks=False)
assert len(combined) == 4
c0 = combined._data[0]
assert c0[u'startTime'] == 800
assert c0[u'endTime'] == 940
combined = pl3.combine() # combine_breaks=True)
assert len(combined) == 1
c0 = combined._data[0]
assert c0[u'startTime'] == 800
assert c0[u'endTime'] == 1605


class StudentTests(WebUntisTestCase):

Expand Down Expand Up @@ -662,7 +785,7 @@ def testAbsencesList(self):
class ClassRegEventsTests(WebUntisTestCase):
def testClassRegEvent(self):
cre = webuntis.objects.ClassRegEvent(
data={'studentid': '42', # <- key!
data={'studentid': '42', # <- key!
'surname': 'Potter',
'forname': 'Harry',
'date': 20180417,
Expand All @@ -672,7 +795,7 @@ def testClassRegEvent(self):
},
session=StubSession()
)
assert type(cre) == webuntis.objects.ClassRegEvent
assert type(cre) == webuntis.objects.ClassRegEvent
assert cre.student.name == u'Potter'
assert cre.sur_name == u'Potter'
assert cre.fore_name == u'Harry'
Expand Down
2 changes: 1 addition & 1 deletion webuntis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
:copyright: (c) 2012 by Markus Unterwaditzer.
:license: BSD, see LICENSE for more details.
"""
__version__ = '0.1.10'
__version__ = '0.1.11'
from webuntis.session import Session

from webuntis import errors
40 changes: 32 additions & 8 deletions webuntis/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ def filter(self, **criterions):
bar = [kl for kl in s.klassen()
if kl.id in {'1A', '2A', '3A', '4A'}]
# Or you can use a list: this keeps the order: the first element
# of the result corresponds to the first element in the filter
# Important after using combine()
bar = s.klassen().filter(name=['1A', '2A', '3A', '4A'])
# --> bar[0].name == '1A'
# Since ``filter`` returns a ListResult itself too, we can chain
# multiple calls together:
bar = s.klassen().filter(id=4, name='7A') # is the same as
Expand Down Expand Up @@ -151,6 +157,14 @@ def meets_criterions(item):

return True

if isinstance(criterions[0][1], list):
return type(self)(
parent=self,
data=[self.filter(**{key: v})[0]
for key, values in criterions
for v in values
])

return type(self)(
parent=self,
data=[item for item in self if meets_criterions(item)]
Expand Down Expand Up @@ -306,7 +320,7 @@ def klassen(self):
this period."""

return self._session.klassen(from_cache=True).filter(
id=set([kl[u'id'] for kl in self._data[u'kl']])
id=[kl[u'id'] for kl in self._data[u'kl']]
)

@lazyproperty
Expand All @@ -315,7 +329,7 @@ def teachers(self):
which are attending this period."""

return self._session.teachers(from_cache=True).filter(
id=set([te[u'id'] for te in self._data[u'te']])
id=[te[u'id'] for te in self._data[u'te']]
)

@lazyproperty
Expand All @@ -326,7 +340,7 @@ def subjects(self):
their own period."""

return self._session.subjects(from_cache=True).filter(
id=set([su[u'id'] for su in self._data[u'su']])
id=[su[u'id'] for su in self._data[u'su']]
)

@lazyproperty
Expand All @@ -336,7 +350,7 @@ def rooms(self):
lesson that is actually occuring at multiple locations (?)."""

return self._session.rooms(from_cache=True).filter(
id=set([ro[u'id'] for ro in self._data[u'ro']])
id=[ro[u'id'] for ro in self._data[u'ro']]
)

@lazyproperty
Expand All @@ -356,7 +370,7 @@ def code(self):
def original_teachers(self):
""" Support for original teachers """
try:
return self._session.teachers(from_cache=True).filter(id=set([te[u'orgid'] for te in self._data[u'te']]))
return self._session.teachers(from_cache=True).filter(id=[te[u'orgid'] for te in self._data[u'te']])
except KeyError:
pass
return []
Expand All @@ -365,7 +379,7 @@ def original_teachers(self):
def original_rooms(self):
""" Support for original rooms """
try:
return self._session.rooms(from_cache=True).filter(id=set([ro[u'orgid'] for ro in self._data[u'ro']]))
return self._session.rooms(from_cache=True).filter(id=[ro[u'orgid'] for ro in self._data[u'ro']])
except KeyError:
pass
return []
Expand Down Expand Up @@ -686,7 +700,10 @@ def combine(self, combine_breaks=True):
return timetable_utils.combine(self,
{'date', 'type', 'kl', 'su'},
combine_breaks,
lambda p: (p[u'te'][0][u'name'], p[u'date'], p[u'startTime']))
lambda p: (p['type'],
str(p[u'te'] and p[u'te'][0][u'name']),
p[u'date'],
p[u'startTime']))


class TimeUnitObject(Result):
Expand Down Expand Up @@ -874,7 +891,7 @@ def subject(self):
def teachers(self):
"""@TODO: untested - always empty"""
try:
tes = set(int(te) for te in self._data[u'teacherIds'] if te)
tes = list(int(te) for te in self._data[u'teacherIds'] if te)
except ValueError:
return []

Expand Down Expand Up @@ -928,6 +945,13 @@ def time(self):
except KeyError:
return 0

@lazyproperty
def status(self):
try:
return self._data[u'excuseStatus']
except KeyError:
return ''


class AbsencesList(ListResult):
"""A list of absences."""
Expand Down
4 changes: 4 additions & 0 deletions webuntis/objects.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ class AbsenceObject(Result):
def reason(self) -> str:
...

@property
def status(self) -> str:
...

@property
def time(self) -> int:
...
Expand Down
52 changes: 52 additions & 0 deletions webuntis/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,58 @@ def _create_date_param(end, start, **kwargs):
}, **kwargs)
return parameters

def get_student(self, surname, fore_name, dob=0):
"""
Search for a student by name
:param surname: family name
:type surname: str
:param fore_name: fore name
:type fore_name: str
:param dob: date of birth, use 0 if unknown -- unknown Unit!
:type dob: int
:return: a dummy StudentObject with just the id filled
:raises: :exc:`KeyError`
"""
s = self._search(surname=surname, fore_name=fore_name, dob=dob, what=5)
id = s._data
if not id:
raise KeyError("Student not found")

data = {"id": id, "name": surname, "longName": surname, "foreName": fore_name}
return objects.StudentObject(data=data, parent=s._parent, session=s._session)

def get_teacher(self, surname, fore_name, dob=0):
"""
Search for a teacher by name
:param surname: family name
:type surname: str
:param fore_name: fore name
:type fore_name: str
:param dob: date of birth, use 0 if unknown -- unknown Unit!
:type dob: int
:return: a dummy TeacherObject with just the id and name filled
:raises: :exc:`KeyError`
"""
t = self._search(surname=surname, fore_name=fore_name, dob=dob, what=2)
id = t._data
if not id:
raise KeyError("Teacher not found")

data = {"id": id, "name": surname, "longName": surname, "foreName": fore_name, "title": ""}
return objects.TeacherObject(data=data, parent=t._parent, session=t._session)

@result_wrapper
def _search(self, surname, fore_name, dob=0, what=-1):
"""
search for student or teacher
:rtype: :py:class:`webuntis.objects._OnlyID`
"""
return objects.Result, 'getPersonId', {
"sn": surname, "fn": fore_name, "dob": dob, "type": what
}


class Session(JSONRPCSession, ResultWrapperMixin):
"""The origin of everything you want to do with the WebUntis API. Can be
Expand Down

0 comments on commit eb5aefe

Please sign in to comment.