1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import absolute_import, unicode_literals |
3
|
|
|
|
4
|
|
|
from school_api.utils import to_text, ObjectDict |
5
|
|
|
from school_api.config import URL_PATH_LIST, CLASS_TIME |
6
|
|
|
from school_api.client.base import BaseUserClient |
7
|
|
|
from school_api.client.api.score import Score |
8
|
|
|
from school_api.client.api.schedule import Schedule |
9
|
|
|
from school_api.client.api.place_schedule import PlaceSchedule |
10
|
|
|
from school_api.client.api.user_info import UserlInfo |
11
|
|
|
from school_api.client.utils import UserType, error_handle, ApiPermissions, get_time_list |
12
|
|
|
from school_api.session.memorystorage import MemoryStorage |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class SchoolClient(object): |
16
|
|
|
''' 学校实例 ''' |
17
|
|
|
|
18
|
|
|
def __init__(self, url, name=None, code=None, use_ex_handle=True, exist_verify=True, lan_url=None, proxies=None, |
19
|
|
|
priority_proxy=False, timeout=10, login_url_path=None, url_path_list=None, |
20
|
|
|
class_time_list=None, session=None): |
21
|
|
|
school = { |
22
|
|
|
'code': code, |
23
|
|
|
'lan_url': lan_url, |
24
|
|
|
'proxies': proxies, |
25
|
|
|
'timeout': timeout, |
26
|
|
|
'name': to_text(name), |
27
|
|
|
'exist_verify': exist_verify, |
28
|
|
|
'use_ex_handle': use_ex_handle, |
29
|
|
|
'priority_proxy': priority_proxy, |
30
|
|
|
'login_url': login_url_path or "/default2.aspx", |
31
|
|
|
'url_path_list': url_path_list or URL_PATH_LIST, |
32
|
|
|
'time_list': get_time_list(class_time_list or CLASS_TIME) |
33
|
|
|
} |
34
|
|
|
self.base_url = url.split('/default')[0] if url[-4:] == 'aspx' else url |
35
|
|
|
self.session = session or MemoryStorage() |
36
|
|
|
self.school = ObjectDict(school) |
37
|
|
|
|
38
|
|
|
def user_login(self, account, password, use_cookie_login=True, **kwargs): |
39
|
|
|
''' 用户登录入口 |
40
|
|
|
进行首次绑定操作时,请将 use_cookie_login 设置为False,避免其他用户进行会话登录 |
41
|
|
|
:param account: 用户账号 |
42
|
|
|
:param password: 用户密码 |
43
|
|
|
:param user_type: 0.学生 1.教师 2.部门 |
44
|
|
|
:param use_cookie_login: 是否使用会话登陆 |
45
|
|
|
:param requests模块参数 |
46
|
|
|
return 用户实例 |
47
|
|
|
''' |
48
|
|
|
use_cookie_login = kwargs.pop("use_login_cookie", use_cookie_login) # 兼容低版本 |
49
|
|
|
user_type = kwargs.pop('user_type', UserType.STUDENT) |
50
|
|
|
user = UserClient(self, account, password, user_type) |
51
|
|
|
user = user.user_login(use_cookie_login, **kwargs) |
52
|
|
|
return user |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
class UserClient(BaseUserClient): |
56
|
|
|
''' 用户实例 ''' |
57
|
|
|
|
58
|
|
|
score = Score() |
59
|
|
|
info = UserlInfo() |
60
|
|
|
schedule = Schedule() |
61
|
|
|
place_schedule = PlaceSchedule() |
62
|
|
|
|
63
|
|
|
@error_handle |
64
|
|
|
def user_login(self, use_cookie_login, **kwargs): |
65
|
|
|
''' 登录:通过SchoolClient类调用 ''' |
66
|
|
|
login_session = None |
67
|
|
|
if use_cookie_login: |
68
|
|
|
login_session = self.session_management() |
69
|
|
|
|
70
|
|
|
if login_session is None: |
71
|
|
|
self.login.get_login(self.school, **kwargs) |
72
|
|
|
self.save_login_session() |
73
|
|
|
login_session = self |
74
|
|
|
|
75
|
|
|
return login_session |
76
|
|
|
|
77
|
|
|
@error_handle |
78
|
|
|
def get_schedule(self, *args, **kwargs): |
79
|
|
|
return self.schedule.get_schedule(*args, **kwargs) |
80
|
|
|
|
81
|
|
|
@error_handle |
82
|
|
|
@ApiPermissions([UserType.STUDENT, UserType.TEACHER]) |
83
|
|
|
def get_info(self, **kwargs): |
84
|
|
|
return self.info.get_info(**kwargs) |
85
|
|
|
|
86
|
|
|
@error_handle |
87
|
|
|
@ApiPermissions([UserType.STUDENT]) |
88
|
|
|
def get_score(self, *args, **kwargs): |
89
|
|
|
return self.score.get_score(*args, **kwargs) |
90
|
|
|
|
91
|
|
|
@error_handle |
92
|
|
|
@ApiPermissions([UserType.DEPT]) |
93
|
|
|
def get_place_schedule(self, *args, **kwargs): |
94
|
|
|
return self.place_schedule.get_schedule(*args, **kwargs) |
95
|
|
|
|