def ajax_groups_add_action(): data = request.json try: group = Group() group.name = data['name'] group.begin = datetime.strptime(data['date'] + ' ' + data['time'], '%d.%m.%Y %H:%M') group.schedule = str.join( ', ', data['weekdays'] if 'weekdays' in data else []) group.trainer_id = data['trainer_id'] if data['trainer_id'] else None db.session.add(group) db.session.commit() showNotify('Успешно!', 'Группа {} создана!'.format(group.name), type='success') except IntegrityError: traceback.print_exc() return json.dumps([{ 'status': 'danger', 'title': 'Внимание!', 'text': 'Группа {} уже существует.'.format(group.name) }]) except Exception: traceback.print_exc() return json.dumps([{ 'status': 'danger', 'title': 'Внимание!', 'text': 'Невозможно создать группу {}.'.format(group.name) }]) if group.trainer: calendarId = Account.query.filter_by().filter_by( main=True, trainer=group.trainer, oauth_name='google-plus').first().email else: calendarId = 'primary' try: event_data = create_event( group.name, group.begin.strftime('%Y-%m-%dT%H:%M:00'), (group.begin + timedelta(hours=2)).strftime('%Y-%m-%dT%H:%M:00'), group.schedule) # Create event if not exists. event = get_service('calendar', 'v3').events().insert(calendarId=calendarId, body=event_data).execute() group.event_id = event['id'] db.session.commit() except Exception: traceback.print_exc() return json.dumps([{'exec': 'document.location.href = \'/groups\''}])