def org_parent_add(): form = ParentOrgNew(request.form) if form.validate_on_submit(): name = form.new_parent_org_name.data.strip() try: parent_org_collection.insert({ 'org_name': name, 'reviewed_by': current_user.name, 'verified_by': current_user.name, 'manually_updated': True, 'name_ngrams': " ".join(make_ngrams(name)), 'name_prefixes': " ".join(make_ngrams_from_beginning_of_phrase(name)) }) flash('Parent org name successfully created!', 'success') except DuplicateKeyError: flash( "Error - parent org with that name already exists. Please link to the existing parent org.", "danger") print(form.errors) return redirect(form.new_parent_form_path.raw_data[0].strip())
def org_parent_edit(): form = ParentOrgEdit(request.form) if form.validate_on_submit(): try: parent_org_collection.update_one( {'_id': ObjectId(form.parent_edit_org_id.data)}, { '$set': { 'org_name': form.parent_edit_org_new_name.data, 'name_ngrams': " ".join( make_ngrams(form.parent_edit_org_new_name.data)), 'name_prefixes': " ".join( make_ngrams_from_beginning_of_phrase( form.parent_edit_org_new_name.data)), 'flagged': False, 'flagged_by': None } }) flash('Parent org name successfully updated!', 'success') except DuplicateKeyError: flash( "Error - parent org with that name already exists. Please link to the existing parent org.", "danger") else: flash('Error updating name. Please try again', 'danger') return redirect(form.parent_edit_form_path.raw_data[0].strip())
def dpoh_name_list(): form = DPHOSearchForm(request.form) processed_names = [] search_term = request.args.get('search', None) if search_term not in ("", None): dpoh_names = dpoh_name_collection.find( { "$text": { "$search": " ".join(make_ngrams(search_term)) } }, { "name": True, "title": True, "inst": True, "person_id": True, "score": { "$meta": "textScore" } }).sort([("score", { "$meta": "textScore" }), ("assumed_last_name", 1), ("name", 1)]).limit(100) processed_names = get_person_for_names_list(dpoh_names) return render_template('dpoh_names_list.html', form=form, dpoh_names=processed_names)
def org_review(org_id): org = child_org_collection.find_one({'org_id': org_id}) if confirm_has_lock(org['_id']) is None: flash( 'Org %s is being edited by someone else. You have been redirected to the next available org.' % org['org_name'], 'danger') return redirect(url_for('orgs.orgs_queue')) reviewed = org['reviewed_by'] != None if reviewed: flash("This org has already been reviewed.", 'danger') form = VerifyOrg(request.form, **org) org_choices = [(str(o['_id']), o['org_name']) for o in parent_org_collection.find().sort('org_name')] org_choices.insert(0, ("create", "Create new parent org")) org_choices.insert(0, ("", "Select one")) form.parent_org_id.choices = org_choices if request.method == "POST" and form.validate(): if form.parent_org_id.data == 'create': try: parent_org_id = parent_org_collection.insert({ 'org_name': org['org_name'], 'reviewed_by': current_user.name, 'verified_by': None, 'name_ngrams': " ".join(make_ngrams(org['org_name'])), 'name_prefixes': " ".join( make_ngrams_from_beginning_of_phrase(org['org_name'])) }) flash("Added new parent organization", "success") except DuplicateKeyError: flash( "Error - parent org already exists. Please link to the existing parent org.", "danger") return render_template('org_review.html', form=form, org=org, reviewed=reviewed) else: parent_org_id = ObjectId(form.parent_org_id.data) result = child_org_collection.update_many( {'org_name': org['org_name']}, { '$set': { 'parent_org_id': parent_org_id, 'reviewed_by': current_user.name } }) flash( "Organization %s and %d others with the same name linked to parent org." % (org_id, result.modified_count - 1), "success") r.delete(str(org['_id'])) return redirect(url_for('orgs.orgs_queue')) sims = get_org_sims(org['org_name'], org['_id']) return render_template('org_review.html', form=form, org=org, reviewed=reviewed, sims=sims)
def org_list(): search_term = request.args.get('search', None) form = OrgSearchForm(request.form) edit_parent_form = ParentOrgEdit(request.form) new_parent_form = ParentOrgNew(request.form) delete_parent_form = ParentOrgDelete(request.form) change_parent_form = ChangeParentOrg(request.form) change_parent_form.parent_org_id.choices = [ (str(o['_id']), o['org_name']) for o in parent_org_collection.find().sort('org_name') ] redirect_path = "%s?search=%s" % (request.path, search_term) delete_parent_form.parent_delete_form_path.data = redirect_path edit_parent_form.parent_edit_form_path.data = redirect_path change_parent_form.change_parent_form_path.data = redirect_path new_parent_form.new_parent_form_path.data = redirect_path if request.url_rule.rule.endswith("flagged"): parent_ids = parent_org_collection.find({ 'flagged': True }).distinct('_id') child_ids = child_org_collection.find({ 'flagged': True }).distinct('_id') orgs = org_aggregation(parent_ids, child_ids) elif search_term: possible_parents = parent_org_collection.find( {"$text": { "$search": " ".join(make_ngrams(search_term)) }}, { "org_name": True, "_id": True, "score": { "$meta": "textScore" } }) parent_ids = [o['_id'] for o in possible_parents] parent_score = { str(o['_id']): o['score'] for o in possible_parents.rewind() } possible_children = child_org_collection.find( {"$text": { "$search": " ".join(make_ngrams(search_term)) }}, { "org_id": True, "_id": True, "score": { "$meta": "textScore" } }) child_ids = [o['_id'] for o in possible_children] child_score = { str(o['org_id']): o['score'] for o in possible_children.rewind() } result = org_aggregation(parent_ids, child_ids) result = list(result) parent_ids = [] for r in result: n = 1 score = 0 score += parent_score.get(str(r['_id'].get('parent_id')), 0) for c in r['child_orgs']: score += child_score.get(c['org_id'], 0) n += 1 r['score'] = score / n parent_ids.append(r['_id'].get('parent_id')) for pp in possible_parents.rewind(): if pp['_id'] not in parent_ids: childless = { '_id': { 'parent_id': pp['_id'], 'parent_name': pp['org_name'] }, 'child_orgs': [], 'score': pp['score'] } result.append(childless) if len(result) > 10: filtered_results = [o for o in result if o['score'] > 500] else: filtered_results = result orgs = sorted(filtered_results, key=itemgetter('score'), reverse=True) else: orgs = None return render_template('orgs_list.html', form=form, edit_parent_form=edit_parent_form, delete_parent_form=delete_parent_form, change_parent_form=change_parent_form, new_parent_form=new_parent_form, orgs=orgs)