-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_input_checks_tk.py
214 lines (147 loc) · 5.16 KB
/
data_input_checks_tk.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import components_tk
def check_data_input(data, mainapp):
data_good = True
msg = None
for widget in data:
if data[widget][0] == 'entry':
data_good, msg = check_entry_input(data[widget])
elif data[widget][0] == 'combo':
data_good, msg = check_combo_input(data[widget])
elif data[widget][0] == 'title':
data_good, msg = check_title_input(data[widget], mainapp)
if not data_good:
msg = f'{widget} {msg}'
break
return data_good, msg
def check_combo_input(combo_data):
''' Possible Checks
in values
'''
combo_good = True
combo_msg = None
user_input = combo_data[1].get()
if 'in values' in combo_data[2]:
values = combo_data[1].config()['values'][-1]
if user_input not in values:
combo_good = False
combo_msg = 'Selection Not Available'
elif 'not empty' in combo_data[2]:
if user_input.strip() == '':
combo_good = False
combo_msg = 'Must be Entered'
elif 'int ' or 'float' in combo_data[2]:
try:
float(user_input)
except:
combo_good = False
combo_msg = 'Must be numeric'
if combo_good:
if 'int greater than' in combo_data[2]:
if user_input.strip() == '':
combo_good = False
combo_msg = 'Must be Entered'
if combo_good:
if 'equal' in combo_data[2]: #handle equal greater than or equal to
if int(user_input) < int(combo_data[2].split()[-1]):
combo_good = False
combo_msg = f'Should be an Integer Greater than or Equal to {int(combo_data[2].split()[-1])}'
else:
if int(user_input) <= int(combo_data[2].split()[-1]):
combo_good = False
combo_msg = f'Should be an Integer Greater than {int(combo_data[2].split()[-1])}'
return combo_good, combo_msg
def check_entry_input(entry_data):
''' Possible Checks
float positive
'''
entry_good = True
entry_msg = None
user_input = entry_data[1].get()
# If it should be a float, check first it can converted to a float
if 'float' in entry_data[2]:
if user_input.strip() != '':
try:
user_input = float(user_input)
except:
entry_good = False
else:
entry_good = False
if 'int ' in entry_data[2]:
if user_input.strip() != '':
try:
user_input = int(user_input)
except:
entry_good = False
entry_msg = 'Enter a'
else:
entry_good = False
entry_msg = 'Must be Numeric'
# If data good to this point, check if it is the right range
if entry_good:
if 'float positive' in entry_data[2] or 'int positive' in entry_data[2]:
if user_input <= 0:
entry_good = False
elif 'float greater equal zero' in entry_data[2]:
if user_input < 0:
entry_good = False
if not entry_good:
if 'float positive' in entry_data[2]:
entry_msg = 'Should be Numeric and Greater Than 0'
elif 'int positive' in entry_data[2]:
entry_msg = 'Should be a Positive Integer'
elif 'float greater equal zero' in entry_data[2]:
entry_msg = 'Should be Numberic and Greater Than or Equal to 0'
return entry_good, entry_msg
def check_title_input(entry_data, mainapp):
title_good = True
title_msg = None
user_input = entry_data[1].get().strip()
#Check title is not blank
if user_input.strip() == '':
title_good = False
title_msg = 'Must be Entered'
if title_good:
# Check if title in reserved ids
if user_input in mainapp.ids_not_allowed:
title_good = False
title_msg = 'is Reserved, Choose Another'
# Illegal characters
if title_good:
for c in [',', ':', '(', ')']:
if c in user_input:
title_good = False
title_msg = 'Title must not contain any of the following characters:\n , : ( )'
if title_good:
if user_input != entry_data[2]: # entry_data[2] is the original title, before user editted it
# Check Component Does not exits already
components = components_tk.get_all_components(mainapp, 'all')
for component in components['All']:
component_title = mainapp.frames[component].backend.title
if component_title == user_input:
title_good = False
title_msg = 'Already Exists, Choose Another'
break
return title_good, title_msg
def check_title_clash(user_input, original_title, mainapp, type = 'Title'):
title_good = True
title_msg = None
# Check if title is blank
if user_input.strip() == '':
title_good = False
title_msg = f'Blank {type} Not Allowed'
if title_good:
# Check if title in reserved ids
if user_input in mainapp.ids_not_allowed:
title_good = False
title_msg = '{user_input} is Reserved, Choose Another'
if title_good:
if user_input != original_title: # the original title, before user editted it
# Check Component Does not exits already
components = components_tk.get_all_components(mainapp, 'all')
for component in components['All']:
component_title = mainapp.frames[component].backend.title
if component_title == user_input:
title_good = False
title_msg = f'{user_input} Already Exists, Choose Another Title'
break
return title_good, title_msg