school_api.client.utils.ApiPermissions.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
from __future__ import absolute_import, unicode_literals
3
4
from school_api.exceptions import SchoolException, LoginException, PermissionException
5
6
7
class UserType():
8
    ''' 用户类型参数 '''
9
    STUDENT = 0
10
    TEACHER = 1
11
    DEPT = 2
12
13
14
class ScheduleType():
15
    ''' 课表类型参数 '''
16
    PERSON = 0
17
    CLASS = 1
18
19
20
class LoginFail():
21
    ''' 登录失败返回错误信息 '''
22
23
    def __init__(self, tip=''):
24
        self.tip = tip
25
26
    def __getattr__(self, name):
27
        def func(*args, **kwargs):
28
            return {'error': self.tip}
29
        return func
30
31
    def __nonzero__(self):
32
        return True
33
34
35
def error_handle(func):
36
    def wrapper(self, *args, **kwargs):
37
        if not self.school.use_ex_handle:
38
            result = func(self, *args, **kwargs)
39
        else:
40
            try:
41
                result = func(self, *args, **kwargs)
42
43
            except LoginException as reqe:
44
                result = LoginFail(str(reqe))
45
46
            except SchoolException as reqe:
47
                result = {'error': str(reqe)}
48
49
        return result
50
    return wrapper
51
52
53
class ApiPermissions():
54
    ''' 接口权限判断 '''
55
56
    def __init__(self, permission_list):
57
        self.permission_list = permission_list
58
59
    def __call__(self, func):
60
        def wrapper(*args, **kwargs):
61
            func_object = args[0]
62
            if func_object.user.user_type not in self.permission_list:
63
                raise PermissionException(func_object.school.code, '暂无该接口权限')
64
            return func(*args, **kwargs)
65
        return wrapper
66
67
68
def get_time_list(class_time):
69
    ''' 上课时间处理 '''
70
    time_list = {1: [], 2: [], 3: [], 4: []}
71
    time_text = "{} ~ {}"
72
    for index, times in enumerate(class_time):
73
        if index % 2 == 0:
74
            time_list[1].append(time_text.format(times[0], times[1]))
75
            time_list[2].append(time_text.format(times[0], class_time[index+1][1]))
76
77
            if index < 8:
78
                time_list[3].append(time_text.format(times[0], class_time[index+2][1]))
79
                time_list[4].append(time_text.format(times[0], class_time[index+3][1]))
80
    return time_list
81