def add_task(): db_sess = db_session.create_session() form = TaskForm() form.country.choices = [(country.id, country.name) for country in db_sess.query(Country).all()] form.type.choices = [(types.id, types.title) for types in db_sess.query(Type).all()] form.country.choices.insert( 0, form.country.choices.pop( form.country.choices.index( (current_user.country_from, db_sess.query(Country).get(current_user.country_from).name)))) if current_user.friends: friends = [ db_sess.query(User).get(friend_id) for friend_id in current_user.get_friends_list() ] form.friend_invited.choices = [(str(friend.id), f'{friend.name} {friend.surname}') for friend in friends] if form.validate_on_submit(): task = Task() task.creator_id = current_user.id task.creator = current_user task.title = form.title.data task.description = form.description.data task.type = form.type.data task.set_participates(form.friend_invited.data) task.country = form.country.data task.address = form.address.data task.date = form.date.data task.is_private = form.is_private.data task.is_address_displayed = form.is_address_displayed.data db_sess.add(task) picked_type = db_sess.query(Type).get(form.type.data) picked_type.tasks.append(task) picked_country = db_sess.query(Country).get(form.country.data) picked_country.tasks.append(task) for friend in [ db_sess.query(User).get(int(friend_id)) for friend_id in form.friend_invited.data ]: friend.tasks.append(task) current_user.tasks.append(task) db_sess.commit() if task.is_address_displayed: get_map_picture(task.id, task.address) return redirect('/') return render_template('tasks.html', title='Добавить мероприятие', form=form)
def add_task(): form = TaskForm() db_sess = db_session.create_session() categories = db_sess.query(Category).filter( Category.user_id == current_user.id).all() form.category.choices = [(i.id, i) for i in categories] form.category.default = [ i for i in categories if i.name == 'Без категории' ][0].id if form.validate_on_submit(): if request.form.get('category') is None: return render_template('task_add.html', title='Добавление задачи', form=form, message='Необходимо выбрать категорию', date=datetime.datetime.now()) elif form.date.data is not None and form.date.data < datetime.date.today( ): form.category.default = int(request.form.get('category')) return render_template('task_add.html', title='Добавление задачи', form=form, message='Выбрана дата из прошлого', date=datetime.datetime.now()) elif form.start_time.data is not None and form.end_time.data is not None and form.start_time.data > form.end_time.data: form.category.default = int(request.form.get('category')) return render_template( 'task_add.html', title='Добавление задачи', form=form, message='Время начала позже времени окончания', date=datetime.datetime.now()) elif form.start_time.data is not None and form.end_time.data is None: form.category.default = int(request.form.get('category')) return render_template('task_add.html', title='Добавление задачи', form=form, message='Добавьте время окончания', date=datetime.datetime.now()) elif form.start_time.data is None and form.end_time.data is not None: form.category.default = int(request.form.get('category')) return render_template( 'task_add.html', title='Добавление задачи', form=form, message='Время начала позже времени окончания', date=datetime.datetime.now()) elif len(form.title.data) > 100 or len(form.title.data) < 1: form.category.default = int(request.form.get('category')) return render_template( 'task_add.html', title='Добавление задачи', form=form, message='Заголовок задачи должен быть от 1 до 100 символов', date=datetime.datetime.now()) task = Task() task.title = form.title.data task.content = form.content.data task.date = form.date.data task.start_time = form.start_time.data task.end_time = form.end_time.data task.category_id = int(request.form.get('category')) current_user.tasks.append(task) db_sess.merge(current_user) db_sess.commit() return redirect('/tasks') return render_template('task_add.html', title='Добавление задачи', form=form, date=datetime.datetime.now())