Exemplo n.º 1
0
 def get_sources_under_clusters(user_id, cluster_id):
     cluster = get_authorized_cluster(user_id, cluster_id)
     if not cluster:
         abort(404)  # Cluster not found
     cluster_queue = [cluster]
     sources = []
     while len(cluster_queue) > 0:
         for c in cluster_queue:
             if c.child_clusters:
                 cluster_queue.extend(c.child_clusters)
         for item in cluster_queue[0].child_items:
             if item.source_id:
                 sources.append(item.source.format())
         cluster_queue.pop(0)
     return jsonify({'success': True, 'sources': sources}), 200
Exemplo n.º 2
0
    def remove_from_existing_cluster(user_id, cluster_id, item_id):
        cluster = get_authorized_cluster(user_id, cluster_id)
        item = get_authorized_item(user_id, item_id)

        if item.cluster != cluster:
            abort(400)

        if not cluster.parent_cluster:
            item.cluster = None
            cluster.project.items.append(item)
        else:
            cluster.parent_cluster.child_items.append(item)
        item.update()
        status_code = 200
        return jsonify({
            'success': True,
            'cluster': cluster.format()
        }), status_code
Exemplo n.º 3
0
    def update_cluster_name(user_id, cluster_id):
        cluster = get_authorized_cluster(user_id, cluster_id)
        body = request.get_json()
        if body is None:
            abort(400)

        new_name = body.get('name', None)
        new_x = body.get('x', None)
        new_y = body.get('y', None)
        # if both are somewhat missing
        if new_name is None and (new_x is None or new_y is None):
            abort(400)
        if new_name:
            cluster.name = new_name
        if new_x is not None and new_y is not None:
            cluster.x_position = new_x
            cluster.y_position = new_y
        cluster.update()

        return jsonify({'success': True, 'cluster': cluster.format()})
Exemplo n.º 4
0
    def add_to_existing_cluster(user_id, cluster_id, item_id):
        cluster = get_authorized_cluster(user_id, cluster_id)
        item = get_authorized_item(user_id, item_id)

        if item.cluster == cluster:
            abort(400)
        if cluster.parent_cluster is not None:
            if item.cluster != cluster.parent_cluster:
                abort(400)

        item.cluster = cluster
        item.parent_project = None

        item.update()
        cluster.child_items.append(item)
        cluster.update()
        status_code = 201

        return jsonify({
            'success': True,
            'cluster': cluster.format()
        }), status_code
Exemplo n.º 5
0
    def delete_cluster(user_id, cluster_id):
        cluster = get_authorized_cluster(user_id, cluster_id)
        # Set source parent cluster equal to the parent cluster
        # of the cluster being deleted(could be none)
        cluster_parent = cluster.parent_cluster
        for item in cluster.child_items:
            if cluster_parent is None:
                # items now just belong to the project -
                cluster.project.items.append(item)
                item.parent_cluster = None
            else:
                cluster_parent.child_items.append(item)
                # I may not need to explicitly do this
                item.parent_cluster = cluster_parent.id
            item.update()

        if cluster_parent is None:
            cluster.project.update()
        else:
            cluster_parent.update()
        cluster.delete()

        return jsonify({'success': True, 'deleted': cluster_id})
Exemplo n.º 6
0
    def get_cluster_info(user_id, cluster_id):
        cluster = get_authorized_cluster(user_id, cluster_id)

        return jsonify({'success': True, 'cluster': cluster.format()})
Exemplo n.º 7
0
    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