1 | # -*- coding: utf-8 -*- |
---|
2 | ################################################################################# |
---|
3 | # @file ous.py |
---|
4 | # @brief Module that encapsulates Opengnsys Rest API and database access |
---|
5 | # for Opengnsys Organizational Units (OUs). |
---|
6 | # @warning None |
---|
7 | # @note Use: None |
---|
8 | # @license GNU GPLv3+ |
---|
9 | # @author David Fuertes, EUPT, University of Zaragoza. |
---|
10 | # Opengnsys Api Rest support provided by Juan Carlos García, EUPT, University of Zaragoza. |
---|
11 | # @version 1.1.0 - First version |
---|
12 | # @date 2019-15-11 |
---|
13 | ################################################################################# |
---|
14 | import datetime |
---|
15 | |
---|
16 | from http_requests import HttpRequest, NotUsingPoolManagerConnector |
---|
17 | from ognsys_actions import GetLabsOu |
---|
18 | from ados import adoDB_ous, adoDB_timetable, adoDB_nip_groups, adoDB_openRlabs_setup |
---|
19 | |
---|
20 | |
---|
21 | def get_ou_credentials(db, ou_id): |
---|
22 | credentials = adoDB_ous.get_ou_credentials(db, ou_id) |
---|
23 | |
---|
24 | return credentials |
---|
25 | |
---|
26 | |
---|
27 | # ---- Return a list of LABS (dictionary) within the passed OU --- |
---|
28 | def get_labs(ou_id): |
---|
29 | http_request = HttpRequest() |
---|
30 | http_request.set_connector(NotUsingPoolManagerConnector()) |
---|
31 | labs = http_request.do_action(GetLabsOu(ou_id)) |
---|
32 | |
---|
33 | |
---|
34 | # if not exist labs return dict {'message': 'Cannot access this resource'} |
---|
35 | if type(labs) is dict: |
---|
36 | labs = [] |
---|
37 | |
---|
38 | labs_inremote = [] |
---|
39 | for lab in labs: |
---|
40 | if lab['inremotepc']: |
---|
41 | labs_inremote.append(lab) |
---|
42 | |
---|
43 | return labs_inremote |
---|
44 | |
---|
45 | # ---- Return a list of LABS (dictionary) with remotePC on --- |
---|
46 | def get_labs_on(ou_id): |
---|
47 | |
---|
48 | labs = get_labs(ou_id) |
---|
49 | |
---|
50 | labs_on = [] |
---|
51 | |
---|
52 | for lab in labs: |
---|
53 | if lab['inremotepc'] == True: |
---|
54 | # No funciona api rest |
---|
55 | |
---|
56 | #lab_status = __get_lab_status(str(lab['ou']['id']),str(lab['id'])) |
---|
57 | #lab['status'] = lab_status |
---|
58 | |
---|
59 | labs_on.append(lab) |
---|
60 | |
---|
61 | |
---|
62 | return labs_on |
---|
63 | |
---|
64 | |
---|
65 | def __check_in_time(lab_time): |
---|
66 | today = datetime.datetime.today().weekday() |
---|
67 | if lab_time['Init_Day'] <= today <= lab_time['End_Day']: |
---|
68 | now = datetime.datetime.now().time() |
---|
69 | #dummy_time = datetime.datetime.strptime("22:05:00", '%H:%M:%S').time() |
---|
70 | #print(dummy_time) |
---|
71 | #now = dummy_time |
---|
72 | ## Si ini > fin; fin pertenece al día siguiente. |
---|
73 | if lab_time['Init_time'] > lab_time['End_time']: |
---|
74 | if (lab_time['End_time'] < now < lab_time['Init_time']) : |
---|
75 | #print('bad time') |
---|
76 | return False |
---|
77 | else: |
---|
78 | #print('ok time') |
---|
79 | return True |
---|
80 | else: |
---|
81 | if lab_time['Init_time'] <= now <= lab_time['End_time']: |
---|
82 | #print('ok time') |
---|
83 | return True |
---|
84 | else: |
---|
85 | #print('bad time') |
---|
86 | return False |
---|
87 | else: |
---|
88 | #print('bad day') |
---|
89 | return False |
---|
90 | |
---|
91 | def __check_code_in_groups(db, lab, username): |
---|
92 | if lab['cod_asign'] and len(lab['cod_asign']) > 0: |
---|
93 | groups = adoDB_nip_groups.get_groups(db, username) |
---|
94 | if groups and lab['cod_asign'] in groups: |
---|
95 | return True |
---|
96 | else: |
---|
97 | return False |
---|
98 | else: |
---|
99 | return True |
---|
100 | |
---|
101 | def __check_using_AD(db): |
---|
102 | if adoDB_openRlabs_setup.get_auth_method(db) == 'active_directory': |
---|
103 | return True |
---|
104 | else: |
---|
105 | return False |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | ### |
---|
110 | # By default all labs "inremote" are listed but if there any lab record in timetable |
---|
111 | # check that is in time range and the code. |
---|
112 | # |
---|
113 | # if exist code check the first entry (get lab order by code) and finish. |
---|
114 | ### |
---|
115 | def filter_labs_by_time_and_code(db, labs, username): |
---|
116 | lab_in_time = [] |
---|
117 | timetable = adoDB_timetable.get_timetable_order_cod(db) |
---|
118 | |
---|
119 | |
---|
120 | for lab in labs: |
---|
121 | exist_in_timetable = False |
---|
122 | insert_lab = True |
---|
123 | |
---|
124 | for lab_timetable in timetable: |
---|
125 | if lab['id'] == lab_timetable['lab_id']: |
---|
126 | print(lab_timetable) |
---|
127 | if __check_in_time(lab_timetable): |
---|
128 | print('in time') |
---|
129 | if __check_using_AD(db): |
---|
130 | if __check_code_in_groups(db, lab_timetable, username): |
---|
131 | print('code ok') |
---|
132 | insert_lab = True |
---|
133 | break |
---|
134 | else: |
---|
135 | print('bad code') |
---|
136 | insert_lab = False |
---|
137 | break |
---|
138 | else: |
---|
139 | insert_lab = True |
---|
140 | else: |
---|
141 | print('Not in time') |
---|
142 | insert_lab = False |
---|
143 | |
---|
144 | if insert_lab and lab not in lab_in_time: |
---|
145 | lab_in_time.append(lab) |
---|
146 | |
---|
147 | return lab_in_time |
---|
148 | |
---|