def catalog_items(status, user): if status.endswith('libro'): wfname = 'procesos_tecnicos' elif status.endswith('revista'): wfname = 'hemeroteca' else: raise Exception, 'revista or libro' filter_items = WFItem.view('couchflow/filter_items') items_query = filter_items[status] selected_items = [item['id'] for item in items_query] if not selected_items: return workflows = WorkFlow.view("couchflow/orig_workflows", include_docs=True) workflows = workflows[wfname] workflow = workflows.one() clone = clone_this_wf(workflow._id, user._id) clone.set_enabled() try: clone.save() except Exception, error: print error
def filter_wflows(user): for i in ('libro', 'revista'): catalog_items('a catalogar %s' % i, user) # TODO return db = FilterWFItems.get_db() wf_tasks = db.view("couchflow/wf_by_first_task_type", include_docs=True)["FilterWFItems":"FilterWFItems"] tasks = {} wflows = [] wf_tasks = wf_tasks.all() for value in wf_tasks: if value["doc"]["doc_type"] == "WorkFlow": wflows.append(WorkFlow.wrap(value["doc"])) elif value["doc"]["doc_type"] == "FilterWFItems": task = FilterWFItems.wrap(value["doc"]) tasks[task._id] = task for wf in wflows: task = tasks.get(wf.first_task_id, None) if not task: raise Exception, "Check" field = task.item_fields.get("99989", None) print "Filter by", field if not field: print "cant get status field" continue filter_items = WFItem.view('couchflow/filter_items') items_query = filter_items[field] selected_items = [item['id'] for item in items_query] if not selected_items: continue clone = clone_this_wf(wf._id, user._id) clone.set_enabled() try: clone.save() except Exception, error: print error tasks = Task.view("couchflow/activetask", include_docs=True) tasks = tasks[clone._id] task = tasks.one() task.wfitems_ids = selected_items task.save() task.active_conector()
def get_status(user=None, exclude=None, include=None): exclude = exclude or [] include = include or [] db = WorkFlow.get_db() workflows = db.view("couchflow/user_wf", include_docs=True) if user: workflows = workflows[user._id] workflows = workflows.all() wf_ids = [x['id'] for x in workflows] all_items_tasks = db.view("couchflow/flowtask2", include_docs=True, keys=wf_ids) items_tasks_dict = {} for row in all_items_tasks: items_tasks_dict.setdefault(row['key'], []).append(row) tasks_dic = {} for row in workflows: wf = row['doc'] wf_type = wf.get('workflow_type', '').split('_', 1)[0] if exclude and wf_type in exclude: continue if include and wf_type not in include: continue items_tasks = items_tasks_dict.get(row['id'], []) items, tasks = get_item_tasks(items_tasks) for task in tasks: item = items[task['wfitems_ids'][0]] status = get_task_status(item) if not status: continue if wf['nro_pedido'] not in tasks_dic: tasks_dic[wf['nro_pedido']] = [wf['nro_pedido']] tasks_dic[wf['nro_pedido']].append(task) ret_tasks = [task for values in tasks_dic.values() for task in values] return ret_tasks