def test_update_common_task_offices(client): # TODO: test tickets migration after you refactor `common.fill_tickets()` with client.application.app_context(): common_task = None tasks = Task.query.all() while not common_task: task = tasks.pop() if len(task.offices) > 1: common_task = task unchecked_office = task.offices[0] checked_office = task.offices[1] old_name = task.name new_name = f'{uuid4()}'.replace('-', '') response = client.post(f'/task/{task.id}', data={ 'name': new_name, f'check{checked_office.id}': True }, follow_redirects=True) updated_task = Task.query.filter_by(name=new_name).first() assert Task.query.filter_by(name=old_name).first() is None assert updated_task is not None assert len(task.offices) > len(updated_task.offices) assert checked_office.id in ids(updated_task.offices) assert unchecked_office.id not in ids(updated_task.offices)
def test_update_common_task_offices(c): task = Task.get_first_common() unchecked_office = task.offices[0] checked_office = task.offices[1] unchecked_office_tickets_numbers = [ ticket.number for ticket in Serial.query.filter_by(task_id=task.id, office_id=unchecked_office.id) ] old_name = task.name new_name = f'{uuid4()}'.replace('-', '') c.post(f'/task/{task.id}', data={ 'name': new_name, f'check{checked_office.id}': True }, follow_redirects=True) updated_task = Task.query.filter_by(name=new_name).first() assert Task.query.filter_by(name=old_name).first() is None assert updated_task is not None assert len(task.offices) > len(updated_task.offices) assert checked_office.id in ids(updated_task.offices) assert unchecked_office.id not in ids(updated_task.offices) # Test unchecked office tickets were migrated for number in unchecked_office_tickets_numbers: ticket = Serial.query.filter_by(number=number).first() assert ticket is not None assert ticket.office_id != unchecked_office.id
def test_add_common_task(c): with c.application.app_context(): offices = Office.query.limit(5).all() name = f'{uuid4()}'.replace('-', '') c.post(f'/common_task_a', data={ 'name': name, **{ f'check{o.id}': True for o in offices } }, follow_redirects=True) added_task = Task.query.filter_by(name=name).first() assert added_task is not None assert sorted(ids(added_task.offices)) == sorted(ids(offices))
def task_a(o_id): """ to add a task """ form = forms.Task_a(session.get('lang')) office = data.Office.get(o_id) if office is None: flash('Error: wrong entry, something went wrong', 'danger') return redirect(url_for("core.root")) if is_operator() and data.Operators.get(current_user.id) is None: flash('Error: operators are not allowed to access the page ', 'danger') return redirect(url_for('core.root')) if is_operator() and o_id != data.Operators.get(current_user.id).office_id: flash('Error: operators are not allowed to access the page ', 'danger') return redirect(url_for('core.root')) if form.validate_on_submit(): if data.Task.query.filter_by(name=form.name.data).first() is not None: flash('Error: name is used by another one, choose another name', 'danger') return redirect(url_for("manage_app.task_a", o_id=o_id)) task = data.Task(form.name.data) db.session.add(task) db.session.commit() if office.id not in ids(task.offices): task.offices.append(office) db.session.commit() initial_ticket = data.Serial.query.filter_by(task_id=task.id, office_id=o_id, number=100)\ .first() if not initial_ticket: db.session.add( data.Serial(office_id=task.offices[0].id, task_id=task.id, p=True)) db.session.commit() flash("Notice: New task been added.", 'info') return redirect(url_for("manage_app.offices", o_id=o_id)) return render_template( "task_add.html", form=form, offices=data.Office.query, serial=data.Serial.query.filter(data.Serial.number != 100), tasks=data.Task.query, operators=data.Operators.query, navbar="#snb1", common=False, dropdown="#dropdown-lvl" + str(o_id), hash="#t3" + str(o_id), page_title="Add new task")
def test_add_task(c): office = Office.query.first() name = f'{uuid4()}'.replace('-', '') c.post(f'/task_a/{office.id}', data={'name': name}, follow_redirects=True) added_task = Task.query.filter_by(name=name).first() assert added_task is not None assert office.id in ids(added_task.offices)
def test_add_task(client): with client.application.app_context(): office = Office.query.first() name = f'{uuid4()}'.replace('-', '') response = client.post(f'/task_a/{office.id}', data={'name': name}, follow_redirects=True) added_task = Task.query.filter_by(name=name).first() assert added_task is not None assert office.id in ids(added_task.offices)
def task_a(office): ''' to add a task ''' form = TaskForm() if is_operator() and not is_office_operator(office.id): flash('Error: operators are not allowed to access the page ', 'danger') return redirect(url_for('core.root')) if form.validate_on_submit(): if data.Task.query.filter_by(name=form.name.data).first() is not None: flash('Error: name is used by another one, choose another name', 'danger') return redirect(url_for('manage_app.task_a', o_id=office.id)) task = data.Task(form.name.data, form.hidden.data) db.session.add(task) db.session.commit() if office.id not in ids(task.offices): task.offices.append(office) db.session.commit() initial_ticket = data.Serial.query.filter_by(task_id=task.id, office_id=office.id, number=100)\ .first() if not initial_ticket: db.session.add( data.Serial(office_id=task.offices[0].id, task_id=task.id, p=True)) db.session.commit() flash('Notice: New task been added.', 'info') return redirect(url_for('manage_app.offices', o_id=office.id)) return render_template('task_add.html', form=form, offices=data.Office.query, serial=data.Serial.all_clean(), tasks=data.Task.query, operators=data.Operators.query, navbar='#snb1', common=False, dropdown='#dropdown-lvl' + str(office.id), hash='#t3' + str(office.id), page_title='Add new task')
def task(o_id, ofc_id=None): ''' view specific task. ''' task = data.Task.get(o_id) if task is None: flash('Error: wrong entry, something went wrong', 'danger') return redirect(url_for('core.root')) form = forms.Task_a(session.get('lang'), task.common) if is_operator() and not is_common_task_operator(task.id): flash('Error: operators are not allowed to access the page ', 'danger') return redirect(url_for('core.root')) page = request.args.get('page', 1, type=int) tickets = data.Serial.all_task_tickets(ofc_id, task.id) last_ticket_pulled = tickets.filter_by(p=True).first() pagination = tickets.paginate(page, per_page=10, error_out=False) if form.validate_on_submit(): if data.Task.query.filter_by(name=form.name.data).count() > 1: flash('Error: name is used by another one, choose another name', 'danger') return redirect(url_for('manage_app.task', o_id=o_id, ofc_id=ofc_id)) task = data.Task.get(o_id) task.name = form.name.data if task.common: checked_offices = [o for o in data.Office.query.all() if form[f'check{o.id}'].data] removed_offices = [o for o in task.offices if o.id not in ids(checked_offices)] to_add_offices = [o for o in checked_offices if o.id not in ids(task.offices)] if not checked_offices: flash('Error: one office must be selected at least', 'danger') return redirect(url_for('manage_app.common_task_a')) for office in removed_offices: task.migrate_tickets(office, checked_offices[0]) task.offices.remove(office) for office in to_add_offices: task.offices.append(office) db.session.commit() flash('Notice: task has been updated .', 'info') return redirect(url_for('manage_app.task', o_id=o_id, ofc_id=ofc_id)) if not form.errors: form.name.data = task.name for office in task.offices: form[f'check{office.id}'].data = True if not ofc_id: # NOTE: sidebar collapse failsafe, just incase the office id wasn't passed ofc_id = task.offices[0].id return render_template('tasks.html', form=form, page_title='Task : ' + task.name, tasksp=pagination.items, pagination=pagination, serial=tickets, o_id=o_id, ofc_id=ofc_id, common=task.common, len=len, offices=data.Office.query, tasks=data.Task.query, users=data.User.query, operators=data.Operators.query, task=task, navbar='#snb1', dropdown='#dropdown-lvl%i' % ofc_id, # dropdown a list of offices hash='#tt%i%i' % (ofc_id, o_id), last_ticket_pulled=last_ticket_pulled, edit_task=len(task.offices) == 1 or not is_operator(), office=data.Office.get(ofc_id))
def task(o_id, ofc_id=None): """ view for specific task """ task = data.Task.query.filter_by(id=o_id).first() if task is None: flash('Error: wrong entry, something went wrong', 'danger') return redirect(url_for('core.root')) form = forms.Task_a(session.get('lang'), True if len(task.offices) > 1 else False) if is_operator() and data.Operators.query.filter_by( id=current_user.id).first() is None: flash('Error: operators are not allowed to access the page ', 'danger') return redirect(url_for('core.root')) office_ids = [o.id for o in task.offices] if request.method == 'POST' and is_operator() and any([ len(office_ids) > 1, ofc_id not in office_ids, data.Operators.query.filter_by( id=current_user.id).first().office_id != ofc_id ]): flash('Error: operators are not allowed to access the page ', 'danger') return redirect(url_for('core.root')) page = request.args.get('page', 1, type=int) tickets = data.Serial.query.filter(data.Serial.task_id == o_id, data.Serial.number != 100)\ .order_by(data.Serial.timestamp.desc()) last_ticket_pulled = tickets.filter_by(p=True).first() pagination = tickets.paginate(page, per_page=10, error_out=False) if form.validate_on_submit(): if data.Task.query.filter_by(name=form.name.data).count() > 1: flash('Error: name is used by another one, choose another name', 'danger') return redirect(url_for("manage_app.task", o_id=o_id)) task = data.Task.get(o_id) task.name = form.name.data if len(task.offices) > 1: checked_offices = [ o for o in data.Office.query.all() if form[f'check{o.id}'].data ] removed_offices = [ o for o in task.offices if o.id not in ids(checked_offices) ] to_add_offices = [ o for o in checked_offices if o.id not in ids(task.offices) ] if not checked_offices: flash('Error: one office must be selected at least', 'danger') return redirect(url_for('manage_app.common_task_a')) for office in removed_offices: task.migrate_tickets(office, checked_offices[0]) task.offices.remove(office) for office in to_add_offices: task.offices.append(office) db.session.commit() flash('Notice: task has been updated .', 'info') return redirect(url_for("manage_app.task", o_id=o_id, ofc_id=ofc_id)) if not form.errors: form.name.data = task.name for office in task.offices: form[f'check{office.id}'].data = True if not ofc_id: # FIXME: to workaround indexing sidebar without rewriting the whole thing ofc_id = task.offices[0].id return render_template( 'tasks.html', form=form, page_title="Task : " + task.name, tasksp=pagination.items, pagination=pagination, serial=data.Serial.query.filter(data.Serial.number != 100), o_id=o_id, ofc_id=ofc_id, common=True if len(task.offices) > 1 else False, len=len, offices=data.Office.query, tasks=data.Task.query, users=data.User.query, operators=data.Operators.query, task=task, navbar="#snb1", dropdown="#dropdown-lvl%i" % ofc_id, # dropdown a list of offices hash="#tt%i%i" % (ofc_id, o_id), last_ticket_pulled=last_ticket_pulled, edit_task=len(task.offices) == 1 or not is_operator())