/
ipm-p1.py
executable file
·488 lines (398 loc) · 15.2 KB
/
ipm-p1.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import gi
import os
import re
import random
import json
import webbrowser
import sys
import tkinter
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, GLib, Gdk, GObject
from models import Workout, Exercise, ExerciseToWorkout
from client import define_connection, close
import locale
class MyWindow(Gtk.Window):
def __init__(self):
if locale.getdefaultlocale()[0] == "en_US":
language = "en"
self.get_en_translation()
else:
language = "es"
self.get_es_translation()
Gtk.Window.__init__(self, title="IPM P1")
self.set_default_size(700, 500)
self.wk_grid = Gtk.Grid()
self.wk_grid.set_column_homogeneous(True)
self.wk_grid.set_row_spacing(20)
self.wk_grid.set_column_spacing(20)
self.scrolled_window = Gtk.ScrolledWindow()
self.scrolled_window.set_border_width(10)
self.add(self.scrolled_window)
self.scrolled_window.add(self.wk_grid)
# get collections from database
self.workouts = define_connection('search_workouts', 'workouts')
self.render_workouts()
self.exercises = define_connection('search_exercises', 'exercises')
self.render_exercises()
self.all_exercises = []
# make objects from all exercises in database
for exercise_db in self.exercises:
try:
exercise = Exercise(exercise_db['_id'], exercise_db['description'], exercise_db['image'], exercise_db['name'], exercise_db['video'])
self.all_exercises.append(exercise)
except:
pass
self.all_workouts = []
# make objects from all workouts in database
exercises_not_in_db = []
for index, workout_db in enumerate(self.workouts):
workout = Workout(workout_db['_id'], workout_db['name'], workout_db['description'], workout_db['image'])
# attach proper exercises to the workout
exercises_of_workout = []
no = 1
for workout_ex in workout_db['exercises']:
found_exercise_in_db = False
for exercise in self.all_exercises:
if found_exercise_in_db is False and workout_ex[0] == exercise.name:
id_ = random.randint(1, 10000000)
found_exercise_in_db = True
ex_of_workout = ExerciseToWorkout(id_, workout.id, exercise.id, workout_ex[1], no)
no = no + 1
exercises_of_workout.append(ex_of_workout)
# if the exercise given with workout is not in the exercises colllection in database
if found_exercise_in_db is False and len(sys.argv) > 1 and sys.argv[1] == "1":
id_ = random.randint(1, 10000000)
exercise = Exercise(id_, "None", "None", workout_ex[0], "None")
exercises_not_in_db.append("{}".format(str(json.dumps(exercise.__dict__))))
self.all_exercises.append(exercise)
ex_of_workout = ExerciseToWorkout(workout.id, exercise.id, workout_ex[1], no)
no = no + 1
exercises_of_workout.append(ex_of_workout)
# add list of exercises to the routine for faster displaying
workout.set_exercises(exercises_of_workout)
self.all_workouts.append(workout)
if len(sys.argv) > 1 and sys.argv[1] == "1":
result = define_connection("insert_ex", "{}".format(exercises_not_in_db))
exercises_json = []
for ex in exercises_of_workout:
ex_json = json.dumps(ex.__dict__)
exercises_json.append(ex_json)
define_connection("insert_ex_to_wk", "{}".format(exercises_json))
self.make_workouts_grid()
def render_workouts(self):
self.workouts = self.workouts.replace("'",'"')
self.workouts = self.workouts.replace('""','min"')
self.workouts = self.workouts.replace('l"',"l'")
self.workouts = json.loads(self.workouts)
self.workouts = self.workouts['result']['workouts']
def render_exercises(self):
self.exercises = self.exercises.replace("'",'"')
self.exercises = self.exercises.replace('they"re',"they're")
self.exercises = self.exercises.replace('That"s',"That's")
self.exercises = self.exercises.replace('arm"s',"arm's")
self.exercises = json.loads(self.exercises)
self.exercises = self.exercises['result']['exercises']
def make_workouts_grid(self):
while(self.wk_grid.get_child_at(0,0)!=None):
self.wk_grid.remove_column(0)
i = 0
current_item_index = 0
while i < len(self.all_workouts):
# 3 workouts in a row
for j in range(4):
if current_item_index <= len(self.all_workouts)-1:
workout = self.all_workouts[current_item_index]
img = None
try:
img = self.get_image(workout)
except GLib.Error:
pass
self.add_workout_to_grid(workout, img, j, i/4)
current_item_index = current_item_index + 1
i = i + 4
self.queue_draw()
self.show_all()
def add_workout_to_grid(self, workout, img, column, row):
cell_grid = Gtk.Grid()
name = Gtk.Label()
name.set_markup("<big><b>{}</b></big>".format(workout.name))
frame = Gtk.Frame()
frame.add(name)
cell_grid.attach(frame, 0, 0, 2, 1)
description = ""
try:
for text in workout.description:
description = description + '\n' + text
except:
pass
if not description:
descr_lb = Gtk.Label()
descr_lb.set_markup("<i>no description</i>")
else:
descr_lb = Gtk.Label(label=description)
descr_lb.set_line_wrap(True)
descr_lb.set_size_request(75, 1)
frame_d = Gtk.Frame()
frame_d.add(descr_lb)
scrolled_window = Gtk.ScrolledWindow(hexpand=True, vexpand=True)
scrolled_window.set_border_width(10)
scrolled_window.set_min_content_height(200)
scrolled_window.add(frame_d)
cell_grid.attach(scrolled_window, 0, 3, 2, 1)
if img is not None:
cell_grid.attach(img, 0, 1, 2, 1)
else:
error = Gtk.Label()
error.set_markup("<span color='red'><i>No image available</i></span>")
frame_err = Gtk.Frame()
frame_err.add(error)
cell_grid.attach(frame_err, 0, 1, 2, 1)
btn_show = Gtk.Button(label=self.show_btn_name)
btn_show.connect("clicked", self.show_exercises, workout)
cell_grid.attach(btn_show, 0, 4, 1, 1)
btn_delete = Gtk.Button(label=self.delete_btn_name)
btn_delete.connect("clicked", self.deletion_message, 1, workout, self.workouts)
cell_grid.attach(btn_delete, 1, 4, 1, 1)
colorh="#FF3333"
color=Gdk.RGBA()
color.parse(colorh)
color.to_string()
btn_delete.override_background_color(Gtk.StateFlags.NORMAL, color)
self.wk_grid.attach(cell_grid, column, row, 1, 1)
def confirm_workout_deletion(self, widget, response_id, *data):
if response_id == Gtk.ResponseType.OK:
result = define_connection("delete", ("workout", data[0][1].id))
try:
self.all_workouts.remove(data[0][1])
self.make_workouts_grid()
self.create_info_message_dialog(self.operation_succeded_name, self.deletion_confirmation_name)
except:
self.create_info_message_dialog(self.error_occured_name, self.deletion_error_name)
elif response_id == Gtk.ResponseType.CANCEL:
pass
widget.destroy()
def create_info_message_dialog(self, text, secondary_text):
dialog = Gtk.MessageDialog(parent=self,
modal=True,
message_type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK,
text=text)
dialog.format_secondary_text(secondary_text)
dialog.run()
dialog.destroy()
def show_exercises(self, widget, *data):
# make exercise window
self.ex_window = Gtk.Window()
self.ex_window.maximize()
title = "{}".format(data[0].name)
self.ex_window.set_title(title)
self.ex_window.show()
# prepare grid with exercises
self.ex_grid = Gtk.Grid()
self.ex_grid.set_column_homogeneous(True)
self.ex_grid.set_column_spacing(20)
# make window scrollable
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_border_width(10)
self.ex_window.add(scrolled_window)
scrolled_window.add(self.ex_grid)
self.make_exercises_grid(data[0])
def make_exercises_grid(self, workout):
# remove exercises from the view
while(self.ex_grid.get_child_at(0,0)!=None):
self.ex_grid.remove_column(0)
# add exercises to the view from self.exercise_list
i = 0
exercises_of_workout = []
while i < len(workout.exercises):
for ex in self.all_exercises:
if ex.id == workout.exercises[i].exercise_id:
exercises_of_workout.append([ex, i])
i = i + 1
for ex, ex_to_wk in zip(exercises_of_workout, workout.exercises):
self.add_exercise_to_grid(ex_to_wk, ex[0], ex[1], workout)
# refresh view
self.ex_window.show_all()
def add_exercise_to_grid(self, exercise_to_routine, exercise, column, workout):
# prepare grid in given cell
cell_grid = Gtk.Grid()
cell_grid.set_column_homogeneous(True)
number = Gtk.Label()
number.set_markup("<b>{}</b>".format(exercise_to_routine.no))
frame_number = Gtk.Frame()
frame_number.add(number)
cell_grid.attach(frame_number, 0, 0, 2, 1)
btn_left = Gtk.Button(label="<- {}".format(self.left_name))
btn_left.connect("clicked", self.change_exercise_position, 0, exercise_to_routine, workout)
cell_grid.attach(btn_left, 0, 1, 1, 1)
if column == 0:
btn_left.set_sensitive(False)
btn_right = Gtk.Button(label="{} ->".format(self.right_name))
btn_right.connect("clicked", self.change_exercise_position, 1, exercise_to_routine, workout)
cell_grid.attach(btn_right, 1, 1, 1, 1)
if column == len(workout.exercises)-1:
btn_right.set_sensitive(False)
name = Gtk.Label()
name.set_markup("<big><b>{}</b></big>".format(exercise.name))
frame_name = Gtk.Frame()
frame_name.add(name)
cell_grid.attach(frame_name, 0, 2, 2, 1)
image = self.get_image(exercise)
if image is not None:
cell_grid.attach(image, 0, 3, 2, 1)
else:
error = Gtk.Label()
error.set_markup("<span color='red'><i>No image available</i></span>")
frame_err = Gtk.Frame()
frame_err.add(error)
cell_grid.attach(frame_err, 0, 3, 2, 1)
length = Gtk.Label()
length.set_markup("<b>{}</b>".format(exercise_to_routine.length))
frame_lenght = Gtk.Frame()
frame_lenght.add(length)
cell_grid.attach(frame_lenght, 0, 4, 2, 1)
if exercise.video is not None:
btn_video = Gtk.Button(label=self.link_to_video_name)
btn_video.connect("clicked", self.open_video, exercise.video)
cell_grid.attach(btn_video, 0, 5, 2, 1)
description = ""
if exercise.description != "None":
try:
for text in exercise.description:
description = description + "\n" + text
except:
pass
if not description or description == "None":
descr_lb = Gtk.Label()
descr_lb.set_markup("<i>no description</i>")
else:
descr_lb = Gtk.Label(label=description)
descr_lb.set_line_wrap(True)
descr_lb.set_size_request(75, 1)
frame_d = Gtk.Frame()
frame_d.add(descr_lb)
scrolled_window = Gtk.ScrolledWindow(hexpand=True, vexpand=True)
scrolled_window.set_border_width(10)
scrolled_window.set_min_content_height(200)
scrolled_window.set_min_content_width(200)
scrolled_window.add(frame_d)
cell_grid.attach(scrolled_window, 0, 6, 2, 1)
btn_delete = Gtk.Button(label=self.delete_btn_name)
btn_delete.connect("clicked", self.deletion_message, 0, exercise_to_routine, workout)
cell_grid.attach(btn_delete, 0, 7, 2, 1)
colorh="#FF3333"
color=Gdk.RGBA()
color.parse(colorh)
color.to_string()
btn_delete.override_background_color(Gtk.StateFlags.NORMAL, color)
# attach cell grid to grid with exercises
self.ex_grid.attach(cell_grid, column, 0, 1, 1)
def open_video(self, widget, *data):
webbrowser.open(data[0], new=0, autoraise=True)
def change_exercise_position(self, widget, *data):
exercises = data[2].exercises
exercises_in_new_order = []
first_obj = None
first_old_no = None
second_obj = None
second_old_no = None
if data[0] == 0:
for ex in exercises:
if ex.no == data[1].no - 1:
second_old_no = ex.no
ex.no = ex.no + 1
first_old_no = data[1].no
data[1].no = data[1].no - 1
exercises_in_new_order.append(data[1])
first_obj = data[1]
exercises_in_new_order.append(ex)
second_obj = ex
elif ex.no != data[1].no:
exercises_in_new_order.append(ex)
elif data[0] == 1:
exercise_to_right = None
for ex in exercises:
if ex.no == data[1].no + 1:
element_to_left = ex.__copy__()
element_to_left.no = ex.no - 1
element_to_right = data[1].__copy__()
element_to_right.no = data[1].no + 1
exercises_in_new_order.append(element_to_left)
first_obj = element_to_left
first_old_no = ex.no
exercises_in_new_order.append(element_to_right)
second_obj = element_to_right
second_old_no = data[1].no
elif ex.no != data[1].no:
exercises_in_new_order.append(ex)
if first_obj is not None and second_obj is not None:
query = str(first_obj.routine_id) + "," + str(first_obj.exercise_id) + "," + str(first_old_no) + "," + str(first_obj.no)
result = define_connection("update_exercise", query)
data[2].set_exercises(exercises_in_new_order)
self.make_exercises_grid(data[2])
def confirm_exercise_deletion(self, widget, response_id, *data):
if response_id == Gtk.ResponseType.OK:
result = define_connection("delete", ("exercise", data[0][1].exercise_id))
result = result.replace("'",'"')
result = json.loads(result)
if result['result'] == "OK":
data[0][2].exercises.remove(data[0][1])
self.make_exercises_grid(data[0][2])
self.create_info_message_dialog(self.operation_succeded_name, self.deletion_confirmation_name)
else:
self.create_info_message_dialog(self.error_occured_name, self.deletion_error_name)
elif response_id == Gtk.ResponseType.CANCEL:
pass
widget.destroy()
def deletion_message(self, widget, *data):
messagedialog = Gtk.MessageDialog(parent=self,
modal=True,
message_type=Gtk.MessageType.WARNING,
buttons=Gtk.ButtonsType.OK_CANCEL,
text=self.deletion_question_name)
if data[0] == 0:
messagedialog.connect("response", self.confirm_exercise_deletion, data)
else:
messagedialog.connect("response", self.confirm_workout_deletion, data)
messagedialog.show()
def get_image(self, object_):
if object_.image is None:
return None
image = None
path = ("img/" + object_.image)
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, 200, 200, True)
image = Gtk.Image.new_from_pixbuf(pixbuf)
except GLib.Error as err:
pass
return image
def get_es_translation(self):
current_directory = os.path.dirname(os.path.realpath(__file__))
with open(current_directory + '/settings/en-es.txt', 'r') as file_:
data = json.load(file_)
self.delete_btn_name = data['Delete']
self.show_btn_name = data['Show']
self.operation_succeded_name = data['Operation succedeed']
self.deletion_confirmation_name = data['The object has been deleted.']
self.link_to_video_name = data['Link to video']
self.deletion_question_name = data['Are you sure you want to delete this object?']
self.error_occured_name = data['Error occured']
self.deletion_error_name = data['The object has not been deleted. Contact your administrator.']
self.left_name = data['Left']
self.right_name = data['Right']
def get_en_translation(self):
self.delete_btn_name = 'Delete'
self.show_btn_name = 'Show'
self.operation_succeded_name = 'Operation succedded'
self.deletion_confirmation_name = 'The object has been deleted.'
self.link_to_video_name = 'Link to video'
self.deletion_question_name = 'Are you sure you want to delete this object?'
self.error_occured_name = 'Error occured'
self.deletion_error_name = 'The object has not been deleted. Contact your administrator.'
self.left_name = 'Left'
self.right_name = 'Right'
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.maximize()
win.show_all()
Gtk.main()