def run(item_id, job_type): inv_item = Inventory.query.get(item_id) if inv_item.status == 'pending': return 'ERROR: Not ready' if inv_item.has_active_jobs(job_type=job_type): return 'ERROR: I already have an active job of that type' job = Job(job_type=job_type) job.set_status('queued') inv_item.set_status('active') inv_item.jobs.append(job) db_session.commit() job.run() return redirect('/items/{0}'.format(inv_item.id))