-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.py
190 lines (152 loc) · 6.72 KB
/
globals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#import os
import json
from cryptography.fernet import Fernet
user_session = ''
cipher_suite = Fernet('jSboVvVD09GgSAZz82LssvuFjNt9OCe_BOYKc1isMYI=')
nombres = ''
apellidos = ''
dpi = ''
fecha_nacimiento = ''
avatar = ''
usuario = ''
contrasena = ''
email = ''
phone = ''
user_type = ''
class User:
def __init__(self, filename='users.json'):
self.filename = filename # Store filename as an instance variable
def create(self, nombres, apellidos, dpi, fecha_nacimiento, avatar, usuario, contrasena, email, phone, user_type):
# Read existing data
with open(self.filename, 'r') as f:
try:
users_dict = json.load(f)
except json.decoder.JSONDecodeError: # Handles an empty or non-existent file
users_dict = {}
# Add new user data
users_dict[usuario] = {
"Nombres": nombres,
"Apellidos": apellidos,
"DPI": dpi,
"Nacimiento": fecha_nacimiento,
"Avatar": avatar,
"Contraseña": contrasena,
"Correo": email,
"Teléfono": phone,
"Tipo": user_type,
"Confirmación": True # la cuenta se crea pero esta bloqueada por defecto
}
# Write the updated data back to the file
with open(self.filename, 'w') as f:
json.dump(users_dict, f, indent=4)
def check(self, usuario):
try:
with open(self.filename, 'r') as f:
users_dict = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError): # Handles a non-existent or empty file
return False # Return False if the file doesn't exist or is empty
return usuario in users_dict # Return True if usuario is found, False otherwise
# Write the updated data back to the file
with open(self.filename, 'w') as f:
json.dump(users_dict, f, indent=4)
def block(self, usuario):
# Read existing data
with open(self.filename, 'r') as f:
try:
users_dict = json.load(f)
except json.decoder.JSONDecodeError: # Handles an empty or non-existent file
raise ValueError(f'No user found with usuario: {usuario}') # Raise an error if the file is empty or non-existent
if usuario not in users_dict:
raise ValueError(f'No user found with usuario: {usuario}') # Raise an error if usuario is not found
# Update the confirm value to False
users_dict[usuario]['Confirmación'] = False
# Write the updated data back to the file
with open(self.filename, 'w') as f:
json.dump(users_dict, f, indent=4)
class Course:
def __init__(self, filename='courses.json'):
self.filename = filename
def create(self, codigo, nombre, coste, horario, cupo, cat):
with open(self.filename, 'r') as f:
try:
courses_dict = json.load(f)
except json.decoder.JSONDecodeError: # Handles an empty or non-existent file
courses_dict = {}
# Add new course data
courses_dict[codigo] = {
"Código": codigo,
"Nombre": nombre,
"Coste": coste,
"Horario": horario,
"Cupo": cupo,
"Cat.": cat,
"Alumnos": [] # Initialize an empty list for items
}
# Write the updated data back to the file
with open(self.filename, 'w') as f:
json.dump(courses_dict, f, indent=4)
def assign(self, codigo, item):
with open(self.filename, 'r') as f:
try:
courses_dict = json.load(f)
except json.decoder.JSONDecodeError:
raise ValueError(f'No course found with codigo: {codigo}')
if codigo not in courses_dict:
raise ValueError(f'No course found with codigo: {codigo}')
# Append item to the course's Items list
courses_dict[codigo]['Alumnos'].append(item)
courses_dict[codigo]['Cupo'] = courses_dict[codigo]['Cupo'] - 1
# Ensure "Cupo" hasn't gone negative
if courses_dict[codigo]['Cupo'] < 0:
raise ValueError(f'Cupo value has gone negative for course with codigo: {codigo}')
# Write the updated data back to the file
with open(self.filename, 'w') as f:
json.dump(courses_dict, f, indent=4)
def unassign(self, codigo, item):
with open(self.filename, 'r') as f:
try:
courses_dict = json.load(f)
except json.decoder.JSONDecodeError:
raise ValueError(f'No course found with codigo: {codigo}')
if codigo not in courses_dict:
raise ValueError(f'No course found with codigo: {codigo}')
# Remove item from the course's Alumnos list
try:
courses_dict[codigo]['Alumnos'].remove(item)
except ValueError:
raise ValueError(f'Item: {item} not found in course: {codigo}')
courses_dict[codigo]['Cupo'] = courses_dict[codigo]['Cupo'] + 1
# Write the updated data back to the file
with open(self.filename, 'w') as f:
json.dump(courses_dict, f, indent=4)
def check(self, codigo, item):
with open(self.filename, 'r') as f:
try:
courses_dict = json.load(f)
except json.decoder.JSONDecodeError:
raise ValueError(f'No course found with codigo: {codigo}')
if codigo not in courses_dict:
raise ValueError(f'No course found with codigo: {codigo}')
return item in courses_dict[codigo]['Alumnos']
def count(self, codigo):
with open(self.filename, 'r') as f:
try:
courses_dict = json.load(f)
except json.decoder.JSONDecodeError:
raise ValueError(f'No course found with codigo: {codigo}')
if codigo not in courses_dict:
raise ValueError(f'No course found with codigo: {codigo}')
# Get the 'Cupo' value for the specified course
cupo = courses_dict[codigo]['Cupo'] # Assume 'Cupo' is a string that represents a number
# Check the count of items in the Alumnos list
item_count = len(courses_dict[codigo]['Alumnos'])
return item_count == cupo # returns True if item_count is greater than or equal to cupo, otherwise False
def delete(self, codigo):
try:
with open(self.filename, 'r') as file:
courses_dict = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
courses_dict = {}
courses_dict.pop(codigo, None)
with open(self.filename, 'w') as file:
json.dump(courses_dict, file, indent=4)