def delete_cohort(cohort_id): """ Deletes a cohort and all its associated links if it belongs to only current_user Removes the relationship between current_user and this cohort if it belongs to a user other than current_user """ session = db.get_session() try: owner_and_viewers = num_users(session, cohort_id) role = get_role(session, cohort_id) # Owner wants to delete, no other viewers or # Owner wants to delete, have other viewers, delete from other viewer's lists too if owner_and_viewers >= 1 and role == CohortUserRole.OWNER: delete_owner_cohort(session, cohort_id) session.commit() return json_redirect(url_for('cohorts_index')) # Viewer wants to delete cohort from their list, doesn't delete cohort from db;l, elif owner_and_viewers > 1 and role == CohortUserRole.VIEWER: delete_viewer_cohort(session, cohort_id) session.commit() return json_redirect(url_for('cohorts_index')) # None of the other cases fit. else: session.rollback() return json_error('This Cohort can not be deleted.') except DatabaseError as e: session.rollback() return json_error(e.message) finally: session.close()
def delete_cohort(cohort_id): """ Deletes a cohort and all its associated links if it belongs to only current_user Removes the relationship between current_user and this cohort if it belongs to a user other than current_user """ session = db.get_session() try: owner_and_viewers = num_users(session, cohort_id) role = get_role(session, cohort_id) # Owner wants to delete, no other viewers or # Owner wants to delete, have other viewers, delete from other viewer's lists too if owner_and_viewers >= 1 and role == CohortUserRole.OWNER: g.cohort_service.delete_owner_cohort(session, cohort_id) session.commit() return json_redirect(url_for('cohorts_index')) # Viewer wants to delete cohort from their list, doesn't delete cohort from db;l, elif owner_and_viewers > 1 and role == CohortUserRole.VIEWER: g.cohort_service.delete_viewer_cohort(session, current_user.id, cohort_id) session.commit() return json_redirect(url_for('cohorts_index')) # None of the other cases fit. else: session.rollback() return json_error('This Cohort can not be deleted.') except DatabaseError as e: session.rollback() return json_error(e.message)
def validate_cohort(cohort_id): name = None session = db.get_session() try: c = g.cohort_service.get_for_display(session, current_user.id, by_id=cohort_id) name = c.name # TODO we need some kind of global config that is not db specific vc = ValidateCohort(c) vc.task.delay(vc) return json_response(message='Validating cohort "{0}"'.format(name)) except Unauthorized: return json_error('You are not allowed to access this cohort') except NoResultFound: return json_error('This cohort does not exist')
def add_tag(cohort_id, tag): """ Checks if tag exists in the tag table and then adds tag to the cohort. """ if tag is None: return json_error(message='You cannot submit an empty tag.') parsed_tag = parse_tag(tag) session = db.get_session() data = {} try: t = session.query(TagStore).filter(TagStore.name == parsed_tag).first() if not t: t = TagStore( name=parsed_tag ) session.add(t) session.commit() # Check if cohort is already tagged with 'tag' try: if g.cohort_service.get_tag(session, t, cohort_id, current_user.id): return json_response(exists=True) except Unauthorized: return json_error(message='You are not allowed to access this Cohort') # Add tag try: g.cohort_service.add_tag(session, t, cohort_id, current_user.id) except Unauthorized: return json_error(message='You are not allowed to access this Cohort') data['tags'] = populate_cohort_tags(cohort_id, session) tagsAutocompleteList = g.tag_service.get_all_tags(session) data['tagsAutocompleteList'] = json.dumps(tagsAutocompleteList) except DatabaseError as e: session.rollback() return json_error(e.message) return json_response(data)
def report_result_json(result_key): celery_task, pj = get_celery_task(result_key) if not celery_task: return json_error('no task exists with id: {0}'.format(result_key)) if celery_task.ready() and celery_task.successful(): result = celery_task.get() json_result = pj.get_json_result(result) return json_response(json_result) else: return json_response(status=celery_task.status)
def add_tag(cohort_id, tag): """ Checks if tag exists in the tag table and then adds tag to the cohort. """ if tag is None: return json_error(message='You cannot submit an empty tag.') parsed_tag = parse_tag(tag) session = db.get_session() try: t = session.query(TagStore).filter(TagStore.name == parsed_tag).first() if not t: t = TagStore( name=parsed_tag ) session.add(t) session.commit() # Check if cohort is already tagged with 'tag' ct = session.query(CohortTagStore) \ .filter(CohortTagStore.tag_id == t.id) \ .filter(CohortTagStore.cohort_id == cohort_id) \ .all() if ct: return json_response(exists=True) cohort_tag = CohortTagStore( tag_id=t.id, cohort_id=cohort_id, ) session.add(cohort_tag) session.commit() except DatabaseError as e: session.rollback() return json_error(e.message) finally: session.close() data = {} populate_cohort_tags(data, cohort_id) return json_response(data)
def report_result_json(result_key): celery_task, pj = get_celery_task(result_key) if not celery_task: return json_error('no task exists with id: {0}'.format(result_key)) if celery_task.ready() and celery_task.successful(): task_result = get_celery_task_result(celery_task, pj) return json_response( result=task_result, parameters=prettify_parameters(pj), ) else: return json_response(status=celery_task.status)
def cohort_invalid_detail(cohort_id): session = db.get_session() try: cohort = g.cohort_service.get_for_display( session, current_user.id, by_id=cohort_id ) wikiusers = session\ .query(WikiUserStore.mediawiki_username, WikiUserStore.reason_invalid)\ .filter(WikiUserStore.validating_cohort == cohort.id) \ .filter(WikiUserStore.valid.in_([False, None])) \ .all() return json_response(invalid_wikiusers=[wu._asdict() for wu in wikiusers]) except Exception, e: app.logger.exception(str(e)) return json_error('Error fetching invalid users for {0}'.format(cohort_id))
def delete_cohort_wikiuser(cohort_id): """ Deletes all WikiUsers from the given Cohort with raw_id_or_name == <raw_id_or_name>. If the invalidOnly flag is passed, it will delete the ones that are invalid only. """ raw_id_or_name = request.form.get('raw_id_or_name') invalid_only = True if request.form.get('invalidOnly') == 'true' else False session = db.get_session() try: g.cohort_service.delete_cohort_wikiuser(raw_id_or_name, cohort_id, current_user.id, session, invalid_only) return json_response(message='success') except Exception as e: app.logger.exception(str(e)) return json_error(e.message)
def default_to_private(): """ Make authentication required by default, unless the endpoint requested has "is_public is True" """ if current_user.is_authenticated(): return if request.is_xhr: return json_error('Please Login to access {0}'.format(request.path)) if (request.endpoint and not request.path.startswith('/static/') and not request.path == 'favicon.ico' and not getattr( app.view_functions[request.endpoint], 'is_public', False)): flash('Please Login before visiting {0}'.format(request.path), 'info') return redirect(url_for('login', next=request.path))
def report_result_json(result_key): celery_task, pj = get_celery_task(result_key) if not celery_task: return json_error('no task exists with id: {0}'.format(result_key)) if celery_task.ready() and celery_task.successful(): result = celery_task.get() json_result = pj.get_json_result(result) if Aggregation.IND in result[result_key]: user_names = get_usernames_for_task_result(result[result_key]) json_result_with_names = add_user_names_to_json(json_result, user_names) return json_response(json_result_with_names) else: return json_response(json_result) else: return json_response(status=celery_task.status)
def default_to_private(): """ Make authentication required by default, unless the endpoint requested has "is_public is True" """ if current_user.is_authenticated(): return if request.is_xhr: return json_error('Please Login to access {0}'.format(request.path)) if ( request.endpoint and not request.path.startswith('/static/') and not request.path == 'favicon.ico' and not getattr(app.view_functions[request.endpoint], 'is_public', False) ): flash('Please Login before visiting {0}'.format(request.path), 'info') return redirect(url_for('login', next=request.path))
def report_result_csv(result_key): celery_task, pj = get_celery_task(result_key) if not celery_task: return json_error('no task exists with id: {0}'.format(result_key)) if celery_task.ready() and celery_task.successful(): task_result = get_celery_task_result(celery_task, pj) p = prettify_parameters(pj) if 'Metric_timeseries' in p and p['Metric_timeseries'] != TimeseriesChoices.NONE: csv_io = get_timeseries_csv(task_result, pj, p) else: csv_io = get_simple_csv(task_result, pj, p) res = Response(csv_io.getvalue(), mimetype='text/csv') res.headers['Content-Disposition'] =\ 'attachment; filename={0}.csv'.format(pj.name) return res else: return json_response(status=celery_task.status)
def report_result_csv(result_key): celery_task, pj = get_celery_task(result_key) if not celery_task: return json_error('no task exists with id: {0}'.format(result_key)) if celery_task.ready() and celery_task.successful(): result = celery_task.get() task_result = pj.get_result_safely(result) p = pj.pretty_parameters() user_names = get_usernames_for_task_result(task_result) if 'Metric_timeseries' in p and p['Metric_timeseries'] != TimeseriesChoices.NONE: csv_io = get_timeseries_csv(task_result, pj, p, user_names) else: csv_io = get_simple_csv(task_result, pj, p, user_names) res = Response(csv_io.getvalue(), mimetype='text/csv') res.headers['Content-Disposition'] =\ 'attachment; filename={0}.csv'.format(pj.name) return res else: return json_response(status=celery_task.status)