def call_resource(self, request): response = TrainingDiaryResponse() try: tdm = TrainingDataManager() tdm.delete_workout_for_primary_key(request.POST['primary_key']) response.set_status(response.SUCCESS) response.add_data('primary_key', request.POST['primary_key']) response.add_message( response.MSG_INFO, f"Workout Deleted: {request.POST['primary_key']}") except Exception as e: response.set_status(response.ERROR) response.add_message(response.MSG_ERROR, f"Delete failed: {str(e)}") return JsonResponse(data=response.as_dict())
def call_resource(self, request): response = TrainingDiaryResponse() dd, errors = self._process_data(request.POST['json']) [response.add_message(response.MSG_ERROR, e) for e in errors] tdm = TrainingDataManager() primary_key = tdm.workout_primary_key(dd['date'], dd['workout_number']) try: if 'primary_key' not in dd or dd['primary_key'] == "": # new workout lastrowid = tdm.save_workout( **self._filtered_dict(dd, self.NEW_FIELDS)) response.add_message(response.MSG_INFO, f"New workout saved") response.add_data( 'workout', tdm.workout_for_rowid(lastrowid)[0].data_dictionary()) elif primary_key == dd['primary_key']: # update workout tdm.update_workout( **self._filtered_dict(dd, self.UPDATE_FIELDS)) response.add_message(response.MSG_INFO, f"Workout updated") response.add_data( 'workout', tdm.workout_for_primary_key(primary_key) [0].data_dictionary()) else: # changed the date. Need a new workout to get primary_keys right old_key = dd['primary_key'] tdm.delete_workout_for_primary_key(old_key) response.add_data('removed_primary_key', old_key) lastrowid = tdm.save_workout( **self._filtered_dict(dd, self.NEW_FIELDS)) workout = tdm.workout_for_rowid(lastrowid)[0] response.add_message( response.MSG_INFO, f"Workout date changed so old workout deleted and new one added. Remove {old_key} and added {workout.primary_key}" ) response.add_data('workout', workout.data_dictionary()) except TypeError as e: response.set_status(response.ERROR) response.add_message(response.MSG_ERROR, str(e)) return JsonResponse(data=response.as_dict())