def delete_shared_user(user_id): body = request.get_json() if body is None: abort(400) # Project ID to share shared_proj = body.get('shared_proj', None) # Email of user to delete from project email = body.get('email', None) if shared_proj is None or email is None: abort(400) # Get user from auth0 database new_user = access_auth0(email, user_id, shared_proj) new_user_id = new_user.user_id project = get_authorized_project(user_id, shared_proj) if new_user_id not in project.shared_users: abort(422) project.shared_users.remove(new_user_id) project.update() return jsonify({'success': True, 'project': project.format()})
def get_project_similarity(user_id, project_id): project = get_authorized_project(user_id, project_id) update_project_access_date(project) sources = project.sources index_map = {} for i in range(len(sources)): index_map[sources[i].id] = i # Create upper-triangluar (since symmetric) sim = {} for i in range(len(sources)): dict = {} for j in range(i + 1): # Ignore empty contents if (sources[i].content is None or sources[i].content == "" or sources[j].content is None or sources[j].content == ""): dict[j] = 0 else: dict[j] = round( similarity(sources[i].content, sources[j].content), 3) sim[i] = dict return jsonify({ 'success': True, 'index': index_map, 'similarity': sim }), 200
def get_clusters(user_id, project_id): # Will only get the top level clusters associated with the project # project = Project.query.filter(Project.user_id == user_id, # Project.id == project_id).first() project = get_authorized_project(user_id, project_id) clusters = project.clusters return jsonify({ 'success': True, 'clusters': [cluster.format() for cluster in clusters] })
def update_project(user_id, project_id): project = get_authorized_project(user_id, project_id) body = request.get_json() if body is None: abort(400) new_title = body.get('title', None) new_description = body.get('description', None) if new_title is None and new_description is None: abort(400) project.title = new_title if new_title is not None else project.title project.description = new_description if new_description is not None \ else project.description project.update() return jsonify({'success': True, 'project': project.format()})
def update_shared_user(user_id, project_id): body = request.get_json() if body is None: abort(400) role = body.get('role', None) if role is None: abort(400) project = get_authorized_project(user_id, project_id) if user_id != project.user_id: abort(422) project.shared_users.role = role project.update() return jsonify({'success': True, 'project': project.format()})
def get_project_items(user_id, project_id): project = get_authorized_project(user_id, project_id) update_project_access_date(project) search_query = request.args.get('query', None) if search_query is None: # Returns all items return jsonify({ 'success': True, 'items': [i.format() for i in project.items] }) pattern = '%' + search_query + '%' filter_query = request.args.getlist('filter', None) if not filter_query: results = Item.query.join(Source) \ .filter(Item.parent_project == project_id) \ .filter(Source.url.ilike(pattern) | Source.title.ilike(pattern) | Source.content.ilike(pattern) | Item.content.ilike(pattern)).order_by(Item.id).all() return jsonify({ 'success': True, 'items': [i.format() for i in results] }) results = [] for filter_type in filter_query: if filter_type == 'notes' or filter_type == 'highlights': temp = Item.query.filter(Item.parent_project == project_id) \ .filter(Item.content .ilike(pattern)).order_by(Item.id).all() else: temp = Item.query.join(Source) \ .filter(Item.parent_project == project_id) \ .filter(getattr(Source, filter_type) .ilike(pattern)).order_by(Item.id).all() for item in temp: if item not in results: results.append(item) return jsonify({ 'success': True, 'items': [i.format() for i in results] })
def create_share_from_ids(user_id): body = request.get_json() if body is None: abort(400) # Project ID to share shared_proj = body.get('shared_proj', None) # Email of the user to share email = body.get('email', None) # Role of the new user role = body.get('role', None) if shared_proj is None or email is None or role is None: abort(400) # Get user from auth0 database new_user = access_auth0(email) new_user_id = new_user.user_id project = get_authorized_project(user_id, shared_proj) if new_user.email_verified is False: abort(400) status_code = 200 # In case a user tries to add themself if project.user_id == user_id: return jsonify({ 'success': True, 'project': project.format() }), status_code # Only share project if the project isn't already shared elif new_user_id not in project.shared_users: project.shared_users.append(new_user_id) project.update() # Set status_code to 201 to indicate new shared user created status_code = 201 return jsonify({ 'success': True, 'project': project.format() }), status_code
def get_project_sources(user_id, project_id): project = get_authorized_project(user_id, project_id) search_query = request.args.get('query', None) if search_query is None: # Returns all sources return jsonify({ 'success': True, 'sources': [s.format() for s in project.sources] }) pattern = '%' + search_query + '%' filter_query = request.args.getlist('filter', None) if not filter_query: results = Source.query.filter(Source.project_id == project_id) \ .filter(Source.url.ilike(pattern) | Source.title.ilike(pattern) | Source.content .ilike(pattern)).order_by(Source.id).all() return jsonify({ 'success': True, 'sources': [source.format() for source in results] }) results = [] for filter_type in filter_query: temp = Source.query.filter(Source.project_id == project_id) \ .filter(getattr(Source, filter_type) .ilike(pattern)).order_by(Source.id).all() for sources in temp: if sources not in results: results.append(sources) return jsonify({ 'success': True, 'sources': [source.format() for source in results] })
def get_project_statistics(user_id, project_id): project = get_authorized_project(user_id, project_id) return jsonify(get_statistics_for_project(project)), 200
def delete_project(user_id, project_id): project = get_authorized_project(user_id, project_id) project.delete() return jsonify({'success': True, 'deleted': project_id})
def create_item(user_id): body = request.get_json() if body is None: abort(400) url = body.get('url', None) content = body.get('content', None) parent_project = body.get('parent_project', None) x = body.get('x_position', None) y = body.get('y_position', None) is_note = body.get('is_note', None) parent_cluster = body.get('parent_cluster', None) if parent_cluster is None and parent_project is None: # Need at least one of them abort(400) # Find root project if we are adding to a cluster if parent_project is None: cluster = get_authorized_cluster(user_id, parent_cluster) temp_cluster = cluster while temp_cluster.project_id is None: temp_cluster = temp_cluster.parent_cluster parent_project = temp_cluster.project_id # Method call to make sure user is authorized get_authorized_project(user_id, parent_project) if url is None and content is None: # Neither url nor content, so abort abort(400) if url is None and not is_note: # Highlight without url is not allowed abort(400) if is_note and content is None: # Note without content abort(400) # Referenced source is by default None source_id_temp = None # Check if source already exists in sources table temp_filter = Source.query.filter(Source.url == url, Source.project_id == parent_project) existing_source = temp_filter.first() if existing_source is not None: # Refer source_id to existing source source_id_temp = existing_source.id elif url is not None and existing_source is None: # Create new source for item source = create_and_insert_source(url, parent_project) source_id_temp = source.id elif url is None and is_note: # Regular Note source_id_temp = None # Add item if parent_cluster is None: item = create_and_insert_item(content, is_note, source_id_temp, parent_project, None, x, y) else: item = create_and_insert_item(content, is_note, source_id_temp, None, parent_cluster, x, y) return jsonify({ 'success': True, 'item': item.format() }), 201