Ejemplo n.º 1
0
    def add(repo_id, type_code, data, import_uuid=None, fields_created=None):
        # TODO: does user have access to this repo?
        data_proc, type_info = DataManager._validateData(
            repo_id, type_code, data, fields_created)
        try:
            q = "MATCH (t:SchemaType) WHERE ID(t) = {type_id} CREATE (n:Data " + makeDataMapForCypher(
                data_proc) + ")-[:IS]->(t) RETURN ID(n) AS id"

            data_proc["type_id"] = type_info[
                "type_id"]  # add type_id to data before insert
            res = db.run(q, data_proc).peek()
            data_id = res["id"]

            if import_uuid is not None:
                db.run(
                    "MATCH (e:ImportEvent { uuid: {import_uuid}}), (n:Data) WHERE ID(n) = {data_id} CREATE (e)<-[x:IMPORTED_IN]-(n) RETURN ID(x) AS id",
                    {
                        "import_uuid": import_uuid,
                        "data_id": data_id
                    })

            #DataManager.logChange("I", data_proc)
            return data_id
        except Exception as e:
            raise DbError(message="Could not create data (" +
                          e.__class__.__name__ + ") " + e.message,
                          context="DataManager.add",
                          dberror=e.message)
Ejemplo n.º 2
0
    def getInfoForType(repo_id, type):
        repo_id = int(repo_id)

        # TODO validate params

        try:
            type_id = int(type)
        except Exception:
            type_id = str(type)

        # TODO: check that repository is owned by current user
        try:
            if isinstance(type_id, int):
                tres = db.run(
                    "MATCH (r:Repository)--(t:SchemaType) WHERE ID(t) = {type_id} AND ID(r) = {repo_id} RETURN ID(t) as id, t.name as name, t.code as code, t.description as description", {"type_id": type_id, "repo_id": repo_id}).peek()

                if tres is None:
                    return None

                result = db.run(
                    "MATCH (f:SchemaField)--(t:SchemaType)--(r:Repository) WHERE ID(t) = {type_id} AND ID(r) = {repo_id} RETURN ID(f) as id, f.name as name, f.code as code, f.type as type, f.description as description, properties(f) as props",
                    {"type_id": int(type_id), "repo_id": repo_id})
            else:
                tres = db.run(
                    "MATCH (r:Repository)--(t:SchemaType) WHERE t.code = {code} AND ID(r) = {repo_id} RETURN ID(t) as id, t.name as name, t.code as code, t.description as description", {"code": type_id, "repo_id": repo_id}).peek()
                if tres is None:
                    return None

                result = db.run(
                    "MATCH (f:SchemaField)--(t:SchemaType)--(r:Repository) WHERE t.code = {code} AND ID(r) = {repo_id}  RETURN ID(f) as id, f.name as name, f.code as code, f.type as type, f.description as description, properties(f) as props",
                    {"code": type_id, "repo_id": repo_id})

            info = {"type_id": tres['id'], "name": tres['name'], "code": tres['code'],
                    "description": tres['description']}

            fieldlist = []
            if result:
                for r in result:
                    ft = SchemaManager.getDataTypeInstance(r['type'])
                    if ft is None:
                        #raise ValidationError(message="Invalid field type", context="Schema.getFieldsForType")
                        continue

                    t = {'id': str(r['id']), 'name': r['name'], 'code': r['code'], 'description': r['description'], 'type': r['type'], 'settings': {}}

                    dc = SchemaManager.checkFieldForData(repo_id, type_id, r['code'])
                    t['has_data'] = dc['data']

                    for s in ft.getSettingsList():
                        if "settings_" + s in r['props']:
                            t["settings_" + s] = r['props']["settings_" + s]
                            t["settings"][s] = r["props"]["settings_" + s]
                    fieldlist.append(t)
            info["fields"] = fieldlist

            data_count = SchemaManager.getRecordCountForDataType(repo_id, type_id)
            info["data_count"] = data_count
            return info
        except Exception as e:
            raise DbError(message="Could not get fields for types", context="Schema.getFieldsForType", dberror=e.message)
Ejemplo n.º 3
0
    def getRecordCountForDataType(repo_id, type):
        repo_id = int(repo_id)

        try:
            type_id = int(type)
        except Exception:
            type_id = str(type)

        # TODO: check that repository is owned by current user
        try:
            if isinstance(type_id, int):
                count_obj = db.run(
                    "MATCH (r:Repository)--(t:SchemaType)--(d:Data) WHERE ID(t) = {type_id} AND ID(r) = {repo_id} RETURN COUNT(d) as data_count",
                    {
                        "type_id": type_id,
                        "repo_id": repo_id
                    }).peek()
            else:
                count_obj = db.run(
                    "MATCH (r:Repository)--(t:SchemaType)--(d:Data) WHERE t.code = {type_id} AND ID(r) = {repo_id} RETURN COUNT(d) as data_count",
                    {
                        "type_id": type_id,
                        "repo_id": repo_id
                    }).peek()

            return count_obj['data_count']
        except Exception as e:
            raise DbError(message="Could not get data count for Data Type",
                          context="Schema.getRecordCountForDataType",
                          dberror=e.message)
Ejemplo n.º 4
0
    def checkFieldForData(repo_id, type_id, field_code):
        try:
            type_id = int(type)
        except Exception:
            type_id = str(type)

        try:
            if isinstance(type_id, int):
                result = db.run(
                    "MATCH (r:Repository)--(s:SchemaType)--(d:Data) WHERE ID(r) = {repo_id} AND ID(s) = {type_id} AND d."
                    + field_code + " <> '' return count(d) as data_count", {
                        "repo_id": repo_id,
                        "type_id": type_id
                    }).peek()
            else:
                result = db.run(
                    "MATCH (r:Repository)--(s:SchemaType)--(d:Data) WHERE ID(r) = {repo_id} AND s.code = {type_id} AND d."
                    + field_code + " <> '' return count(d) as data_count", {
                        "repo_id": repo_id,
                        "type_id": type_id
                    }).peek()
            if result is not None:
                data_count = result['data_count']
                ret = {"data": False, "total": data_count}
                if data_count > 0:
                    ret['data'] = True
                return ret
        except Exception as e:
            raise DbError(message="Could not get data count: " + e.message,
                          context="Schema.checkFieldForData",
                          dberror=e.message)
Ejemplo n.º 5
0
    def deleteRelationship(start_node=None, end_node=None, type_id=None, rel_id=None):
        # TODO: does user have access to relationship?
        try:
            if type_id is not None:
                type_str = "r:" + re.sub(r'[^A-Za-z0-9_]+', '_', type_id)
            else:
                type_str = "r"

            # NOTE: type_id is substituted directly into the string as the Bolt Neo4j driver doesn't allow placeholders for relationship types (yet)

            if end_node is None:
                if rel_id is None:
                    rel_id = start_node     # if rel_id is not set assume first positional parameter (start_node) is the rel_id
                if rel_id is None:
                    return None

                # delete by rel_id
                db.run("MATCH (d1:Data)-[" + type_str + "]-(d2:Data) WHERE ID(r) = {rel_id} DELETE r", {"rel_id": rel_id})
            elif start_node is not None and end_node is not None:
                # delete by endpoints
                db.run("MATCH (d1:Data)-[" + type_str + "]-(d2:Data) WHERE ID(d1) = {start_node} AND ID(d2) = {end_node} DELETE r", {"start_node": start_node, "end_node": end_node})
            else:
                raise ParameterError(message="No relationship id or node id pair is set", context="DataManager.deleteRelationship")
            return True
        except Exception as e:
            raise DbError(message="Could not delete relationship", context="DataManager.deleteRelationship", dberror=e.message)
Ejemplo n.º 6
0
    def nameCheck(name, repo_id=None, identity=None, ident_str=None):
        if repo_id is not None:
            if identity is not None and ident_str is not None:
                res = db.run(
                    "MATCH (n:Repository {name: {name}})--(p:Person) WHERE n.repo_id <> {repo_id} AND "
                    + ident_str + " RETURN n", {
                        "name": name,
                        "repo_id": repo_id,
                        "identity": identity
                    })
            else:
                res = db.run(
                    "MATCH (n:Repository {name: {name}}) WHERE n.repo_id <> {repo_id} RETURN n",
                    {
                        "name": name,
                        "repo_id": repo_id
                    })
        else:
            if identity is not None and ident_str is not None:
                res = db.run(
                    "MATCH (n:Repository {name: {name}})--(p:Person) WHERE " +
                    ident_str + " RETURN n", {
                        "name": name,
                        "identity": identity
                    })
            else:
                res = db.run("MATCH (n:Repository {name: {name}}) RETURN n",
                             {"name": name})

        if len(list(res)) > 0:
            return False
        else:
            return True
Ejemplo n.º 7
0
    def getInfoForList(repo_id, code):
        repo_id = int(repo_id)
        try:
            code = int(code)
        except ValueError:
            pass
        try:
            if isinstance(code, int):
                list_res = db.run("MATCH (r:Repository)--(l:List) WHERE ID(l) = {code} AND ID(r) = {repo_id} RETURN ID(l) as id, l.name as name, l.code as code, l.description as description, l.merge_allowed as merge_allowed", {"code" :code, "repo_id": repo_id}).peek()
                if list_res is None:
                    return None

                items_res = db.run("MATCH (i:ListItem)--(l:List)--(r:Repository) WHERE ID(l) = {code} AND ID(r) = {repo_id} RETURN ID(i) as id, i.display as display, i.code as code, i.description as description", {"code": code, "repo_id": repo_id})
            else:
                list_res = db.run("MATCH (r:Repository)--(l:List) WHERE l.code = {code} AND ID(r) = {repo_id} RETURN ID(l) as id, l.name as name, l.code as code, l.description as description, l.merge_allowed as merge_allowed", {"code" :code, "repo_id": repo_id}).peek()
                if list_res is None:
                    return None

                items_res = db.run("MATCH (i:ListItem)--(l:List)--(r:Repository) WHERE l.code = {code} AND ID(r) = {repo_id} RETURN ID(i) as id, i.display as display, i.code as code, i.description as description", {"code": code, "repo_id": repo_id})

            info = {'list_id': list_res['id'], 'name': list_res['name'], 'code': list_res['code'], 'description': list_res['description'], 'merge_allowed': list_res['merge_allowed']}

            item_list = []
            if items_res:
                for r in items_res:
                    li = {'id': r['id'], 'display': r['display'], 'code': r['code'], 'description': r['description']}
                    item_list.append(li)

            info['items'] = item_list
            return info

        except Exception as e:
            raise DbError(message="Could not get list items for list", context="List.getInfoForList", dberror=e.message)
Ejemplo n.º 8
0
  def getInfo(identity):
    if is_number(identity):
      ident_str = "ID(p)={identity}"
    else:
      ident_str = "p.email={identity}"

    person = {}
    result = db.run("MATCH (p:Person) WHERE " + ident_str + " RETURN ID(p) AS id, p.surname AS surname, p.forename as forename, p.email AS email, " +
      "p.url AS url, p.location AS location, p.tagline AS tagline, p.is_admin AS is_admin, p.is_disabled AS is_disabled, p.nyunetid AS nyunetid", {"identity": identity})

    for p in result:
      repo_count = 0
      repos = db.run("MATCH (r:Repository)--(p:Person) WHERE " + ident_str + " RETURN COUNT(r) AS repo_count", {"identity": identity})
      for r in repos:
        repo_count = r["repo_count"]

      person['id'] = p['id']
      person['name'] = str(p['forename']) + " " + str(p['surname'])
      person['surname'] = p['surname']
      person['forename'] = p['forename']
      person['email'] = p['email']
      person['url'] = p['url']
      person['location'] = p['location']
      person['tagline'] = p['tagline']
      person['is_admin'] = p['is_admin']
      person['is_disabled'] = p['is_disabled']
      person['nyunetid'] = p['nyunetid']
      person['repo_count'] = repo_count

    return person
Ejemplo n.º 9
0
    def getByID(node_id):
        # TODO: does user have access to this node?

        try:
            id = int(node_id)
            result = db.run(
                "MATCH (d:Data)--(t:SchemaType)--(r:Repository) WHERE ID(d) = {node_id} RETURN d, t.name as typename, t.code as typecode, ID(t) as schema_id, ID(r) as repo_id",
                {"node_id": id})
        except:
            result = db.run(
                "MATCH (d:Data)--(t:SchemaType)--(r:Repository) WHERE d.uuid = {uuid} RETURN d, ID(d) as node_id, t.name as typename, t.code as typecode, ID(t) as schema_id, ID(r) as repo_id",
                {"uuid": node_id})

        if result.peek():
            for r in result:
                return {
                    "node_id": r["node_id"],
                    "typename": r["typename"],
                    "typecode": r["typecode"],
                    "schema_id": r["schema_id"],
                    "repo_id": r["repo_id"],
                    "data": r["d"].properties
                }
        else:
            raise FindError(message="Node does not exist")
Ejemplo n.º 10
0
    def addType(repo_id, name, code, description, fields):
        # TODO validate params

        # TODO: check that repository is owned by current user
        ret = { "exists": False}
        try:
            result = db.run("MATCH (t:SchemaType{code: {code}})--(r:Repository) WHERE ID(r) = {repo_id}  RETURN ID(t) as id, t.name as name, t.code as code, t.description as description", {"code": code, "repo_id": int(repo_id)})
            if result is not None and len(list(result)):
               ret = { "exists": True }
               for r in result:
                   ret['type'] = {
                       "id": r['id'],
                       "name": r['name'],
                       "code": r['code'],
                       "description": r['description']
                   }

               return ret
            else:
                result = db.run("MATCH (r:Repository) WHERE ID(r) = {repo_id} CREATE (t:SchemaType { name: {name}, code: {code}, description: {description}, storage: 'Graph'})-[:PART_OF]->(r) RETURN ID(t) as id",
                            {"repo_id": int(repo_id),"name": name, "code": code, "description": description})

            SchemaManager.resetTypeInfoCache()
        except Exception as e:
            raise DbError(message="Could not add type: " + e.message, context="Schema.addType",
                          dberror=e.message)

        # add/edit fields
        field_status = {}
        settings =  {f.replace("settings_", ""):v for f,v in fields.iteritems() if 'settings_' in f}

        for k in fields:

            # add field
            fret = SchemaManager.addField(repo_id, code, k['name'], k['code'], k['type'], k['description'], settings)

            if 'field_id' in fret:
                field_status[k['code']] = {'status_code': 200, 'field_id': fret['field_id'], 'msg': 'Created new field'}
            else:
                field_status[k['code']] = {'status_code': 200, 'field_id': None, 'msg': 'Could not create new field'}

        if result:
            for r in result:
                ret['type'] = {
                    "id": r['id'],
                    "name": name,
                    "code": code,
                    "description": description,
                    "field_status": field_status
                }
                break
        else:
            raise DbError(message="Could not add type", context="Schema.addType",
                          dberror="")


        return ret
Ejemplo n.º 11
0
  def getRepos(identity):
    repos = []

    if is_number(identity):
      ident_str = "ID(p)={identity}"
    else:
      ident_str = "p.email={identity}"

    result = db.run("MATCH (n:Repository)<-[x:OWNED_BY|COLLABORATES_WITH]-(p) WHERE " + ident_str + " RETURN ID(n) AS id, n.name AS name, n.readme AS readme, n.published AS published, n.license AS license, " +
      "n.url AS url, n.created_on AS created_on, n.published_on as published_on, n.featured as featured, x.access AS access", {"identity": identity})

    for item in result:

      owner = lib.managers.RepoManager.RepoManager.getOwner(int(item['id']))
      data  = lib.managers.RepoManager.RepoManager.getData(int(item['id']))
      users = lib.managers.RepoManager.RepoManager.getUsers(int(item['id']))

      repos.append({
        "id": item['id'],
        "name": item['name'],
        "readme": item['readme'],
        "created_on": item['created_on'],
        "published_on": item['published_on'],
        "featured": item['featured'],
        "url": item['url'],
        "data": data,
        "users": users,
        "owner": owner,
        "access": item['access'],
        "published": item['published'],
        "license": item['license'],
        "schema_type_count" : 0,
        "schema_field_count" : 0,
        "data_element_count": 0
      })

    for item in repos:
      result = db.run(
        "MATCH (n:Repository)--(t:SchemaType)--(d:Data) WHERE ID(n) = {repo_id} RETURN count(d) as data_element_count", {"repo_id": int(item['id'])})
      for r in result:
          item['data_element_count'] = r['data_element_count']

      result = db.run(
        "MATCH (n:Repository)--(t:SchemaType)WHERE ID(n) = {repo_id} RETURN count(DISTINCT(t)) as schema_type_count",
        {"repo_id": int(item['id'])})
      for r in result:
        item['schema_type_count'] = r['schema_type_count']

      result = db.run(
        "MATCH (n:Repository)--(t:SchemaType)--(f:SchemaField) WHERE ID(n) = {repo_id} RETURN count(DISTINCT(t)) as schema_type_count, count(DISTINCT(f)) as schema_field_count",
        {"repo_id": int(item['id'])})
      for r in result:
        item['schema_field_count'] = r['schema_field_count']
    return repos
Ejemplo n.º 12
0
 def getDataCounts(repo_id):
   RepoManager.validate_repo_id(repo_id)
   counts = {}
   schema_result = db.run("MATCH (r:Repository)--(f:SchemaType) WHERE ID(r)={repo_id} RETURN ID(f), f.name", {"repo_id": int(repo_id)})
   for schema in schema_result:
       schema_id = schema[0]
       dataCount = db.run("MATCH (f:SchemaType)--(n:Data) WHERE ID(f)={schema_id} RETURN COUNT(n) AS count", {"schema_id":
       int(schema_id)})
       schemaCount = dataCount.single()['count']
       counts[schema[1]] = schemaCount
   print counts
   return {"data": counts}
Ejemplo n.º 13
0
    def addList(repo_id, name, code, merge_setting, description='', items={}):
        try:
            repo_id = int(repo_id)
        except TypeError:
            raise DbError(message="Invalid repo_id provided", context="List.addList",
                          dberror="")
        ret = {"exists": False}
        try:
            result = db.run("MATCH (l:List{code: {code}})--(r:Repository) WHERE ID(r) = {repo_id} RETURN ID(l) as id, l.name as name, l.code as code, l.description as description", {"code": code, "repo_id": int(repo_id)}).peek()

            if result:
                print result
                ret = {
                    "exists": True,
                    "id": result['id'],
                    "code": result['code'],
                    "name": result['name'],
                    'description': result['description']
                }
                return ret
            else:
                result = db.run("MATCH (r:Repository) WHERE ID(r) = {repo_id} CREATE (l:List {name: {name}, code: {code}, description: {description}, merge_allowed: {merge}, storage: 'Graph'})-[:PART_OF]->(r) return ID(l) as id", {"repo_id": repo_id, "name": name, "code": code, "description": description, "merge": merge_setting}).peek()
        except Exception as e:
            raise DbError(message="Could not add list: " + e.message, context="List.addList",
                          dberror=e.message)

        #add/edit List Items
        item_status = {}
        for item in items:
            item_res = ListManager.addListItem(repo_id, code, item['display'], item['code'], item['description'])

            if 'item_id' in item_res:
                item_status[item['code']] = {'status_code': 200, 'item_id': item_res['item_id'], 'msg': 'Created new list item'}
            else:
                item_status[item['code']] = {'status_code': 200, 'item_id': None, 'msg': 'Could not create list item'}

        if result:
            ret = {
                'id': result['id'],
                'name': name,
                'code': code,
                'description': description,
                'list_status': item_status
            }
        else:
            raise DbError(message="Could not add list", context="List.addList",
                          dberror="")

        return ret
Ejemplo n.º 14
0
    def deleteDataFromRepo(repo_id, type_codes=None):
        # TODO: does user have access to this repo?

        type_str = ""
        if type_codes is not None:
            if all(isinstance(c, (int)) for c in type_codes):
                type_str = " AND ID(t) IN [" + ",".join(type_codes) + "] "
            else:
                type_codes = map(lambda x: '"' + re.sub(r'[^A-Za-z0-9_]+', '_', x) + '"', type_codes)
                type_str = " AND t.code IN [" + ",".join(type_codes) + "] "
        try:
            db.run("MATCH (r:Repository)--(t:SchemaType)--(d:Data) WHERE ID(r) = {repo_id} " + type_str + " DETACH DELETE d", {"repo_id": repo_id})
            return True
        except Exception as e:
            raise DbError(message="Could not delete data from repository", context="DataManager.deleteDataFromRepo", dberror=e.message)
Ejemplo n.º 15
0
 def getDataCounts(repo_id):
     RepoManager.validate_repo_id(repo_id)
     counts = {}
     schema_result = db.run(
         "MATCH (r:Repository)--(f:SchemaType) WHERE ID(r)={repo_id} RETURN ID(f), f.name",
         {"repo_id": int(repo_id)})
     for schema in schema_result:
         schema_id = schema[0]
         dataCount = db.run(
             "MATCH (f:SchemaType)--(n:Data) WHERE ID(f)={schema_id} RETURN COUNT(n) AS count",
             {"schema_id": int(schema_id)})
         schemaCount = dataCount.single()['count']
         counts[schema[1]] = schemaCount
     print counts
     return {"data": counts}
Ejemplo n.º 16
0
    def getTypes(repo_id):
        # TODO validate params

        # TODO: check that repository is owned by current user
        try:
            result = db.run(
                "MATCH (t:SchemaType)--(r:Repository) WHERE ID(r) = {repo_id}  RETURN ID(t) as id, t.name as name, t.code as code, t.description as description",
                {"repo_id": int(repo_id)})

            if result:
                typelist = []
                for r in result:
                    t = {
                        'id': str(r['id']),
                        'name': r['name'],
                        'code': r['code'],
                        'description': r['description']
                    }

                    # get fields
                    i = SchemaManager.getInfoForType(repo_id, r['id'])
                    t["fields"] = i["fields"]
                    t["data_count"] = i["data_count"]
                    typelist.append(t)
                return typelist
        except Exception as e:
            raise DbError(message="Could not get types",
                          context="Schema.getTypes",
                          dberror=e.message)
Ejemplo n.º 17
0
    def delete(repo_id):
        RepoManager.validate_repo_id(repo_id)

        owner = RepoManager.getOwner(repo_id)

        result = db.run(
            "MATCH (n:Repository) WHERE ID(n) = {repo_id} WITH n OPTIONAL MATCH (n)--(s:SchemaType)--(f:SchemaField) WITH n, s, f OPTIONAL MATCH (s)--(d:Data) DETACH DELETE n, s, f, d",
            {"repo_id": repo_id})
        summary = result.consume()

        # Was this the only repo for that owner?
        if "id" in owner:
            repos = lib.managers.PeopleManager.PeopleManager.getRepos(
                owner["id"])
            if len(repos) == 0:
                # create new default repo because user just deleted their only one.
                RepoManager.create(
                    app_config['default_repo_information']['url'],
                    app_config['default_repo_information']['name'],
                    app_config['default_repo_information']['description'],
                    app_config['default_repo_information']['license'], 0,
                    owner['id'], "ID(p) = {identity}")

        if summary.counters.nodes_deleted >= 1:
            return True

        raise FindError(message="Could not find repository",
                        context="Repositories.delete")
Ejemplo n.º 18
0
 def sendPasswordReset(email_address):
     reset_key = uuid.uuid4().hex
     result = db.run(
         "MATCH (p:Person) WHERE p.email={email} AND (p.is_disabled = NULL OR p.is_disabled = 0) SET p.password_reset_key = {reset_key} RETURN ID(p) AS id, p.email AS email",
         {
             "email": email_address,
             "reset_key": reset_key
         })
     for p in result:
         try:
             send_mail(
                 email_address, None,
                 "Password reset request for " + email_address,
                 "reset_password", {
                     "email":
                     email_address,
                     "reset_url":
                     app_config["base_url"] + "/#/password/reset?reset=" +
                     reset_key
                 })
         except Exception as e:
             # SMTP error
             raise AuthError("Could not send email: " + e.message)
         break
     # If email address isn't available for user return all is well (don't throw exception) but don't send anything
     # as we don't want or need to indicate to the caller that the address was invalid
     return {"recipient": email_address}
Ejemplo n.º 19
0
    def getAll():

        persons = []
        result = db.run(
            "MATCH (p:Person) OPTIONAL MATCH (p)--(r:Repository) RETURN COUNT(r) AS repo_count, ID(p) AS id, p.surname AS surname, p.forename as forename, p.location AS location, p.email AS email, p.url AS url, p.tagline AS tagline, p.is_admin as is_admin, p.is_disabled as is_disabled, p.nyunetid AS nyunetid ORDER BY p.surname, p.forename"
        )

        for p in result:

            persons.append({
                "id": p['id'],
                "name": str(p['forename']) + " " + str(p['surname']),
                "surname": p['surname'],
                "forename": p['forename'],
                "location": p['location'],
                "email": p['email'],
                "url": p['url'],
                "tagline": p['tagline'],
                "is_admin": p['is_admin'],
                "is_disabled": p['is_disabled'],
                "nyunetid": p['nyunetid'],
                "repo_count": p['repo_count']
            })

        return persons
Ejemplo n.º 20
0
  def nameCheck(name, repo_id=None, identity=None, ident_str=None):
    if repo_id is not None:
        if identity is not None and ident_str is not None:
            res = db.run("MATCH (n:Repository {name: {name}})--(p:Person) WHERE n.repo_id <> {repo_id} AND " + ident_str + " RETURN n", {"name": name, "repo_id": repo_id, "identity": identity})
        else:
            res = db.run("MATCH (n:Repository {name: {name}}) WHERE n.repo_id <> {repo_id} RETURN n", {"name": name, "repo_id": repo_id})
    else:
        if identity is not None and ident_str is not None:
            res = db.run("MATCH (n:Repository {name: {name}})--(p:Person) WHERE " + ident_str + " RETURN n", {"name": name, "identity": identity})
        else:
            res = db.run("MATCH (n:Repository {name: {name}}) RETURN n", {"name": name})

    if len(list(res)) > 0:
      return False
    else:
      return True
Ejemplo n.º 21
0
    def deleteListItem(repo_id, code, item_id):
        try:
            repo_id = int(repo_id)
        except TypeError:
            raise DbError(message="Invalid repo_id provided",
                          context="List.deleteListItem",
                          dberror="")
        try:
            result = db.run(
                "MATCH (r:Repository)--(l:List {code: {code}})-[x]-(i:ListItem) WHERE ID(r) = {repo_id} AND ID(i) = {item_id} DELETE i,x",
                {
                    "repo_id": int(repo_id),
                    "item_id": int(item_id),
                    "code": code
                })

            if result is not None:
                return True
            else:
                raise FindError(message="Could not find list item",
                                context="List.deleteListItem",
                                dberror="")
        except Exception as e:
            raise DbError(message="Could not delete list item",
                          context="List.deleteListItem",
                          dberror=e.message)
Ejemplo n.º 22
0
  def find(params):
    people = []
    criteria = []

    if ('surname' in params) and (params['surname']) and len(params['surname']) > 0:
      params['surname'] = params['surname'].lower()
      criteria.append("lower(p.surname) CONTAINS {surname}")
    if ('forename' in params) and (params['forename']) and len(params['forename']) > 0:
      params['forename'] = params['forename'].lower()
      criteria.append("lower(p.forename) CONTAINS {forename}")
    if ('email' in params) and (params['email']) and len(params['email']) > 0:
      params['email'] = params['email'].lower()
      criteria.append("lower(p.email) STARTS WITH {email}")

    if len(criteria) == 0:
      return None


    result = db.run("MATCH (p:Person) WHERE " + " OR ".join(criteria) + " RETURN ID(p) AS id, p.surname AS surname, p.forename AS forename, p.email AS email, " +
                    "p.url AS url, p.location AS location, p.tagline AS tagline",
                    params)

    for p in result:
      r = {'name': str(p['forename']) + ' ' + p['surname'] }
      for f in ['id', 'surname', 'forename', 'email', 'url', 'location', 'tagline']:
        r[f] = p[f]
      people.append(r)

    return people
Ejemplo n.º 23
0
    def deleteField(repo_id, typecode, field_id):
        # TODO validate params

        # TODO: check that repository is owned by current user
        SchemaManager.resetTypeInfoCache()

        try:
            result = db.run(
                "MATCH (r:Repository)--(t:SchemaType {code: {typecode}})-[x]-(f:SchemaField) WHERE ID(r) = {repo_id} AND ID(f) = {field_id} DELETE f,x",
                {
                    "repo_id": int(repo_id),
                    "field_id": int(field_id),
                    "typecode": typecode
                })

            if result is not None:
                return True
            else:
                raise FindError(message="Could not find field",
                                context="Schema.deleteField",
                                dberror="")
        except Exception as e:
            raise DbError(message="Could not delete field",
                          context="Schema.deleteField",
                          dberror=e.message)
Ejemplo n.º 24
0
    def deleteType(repo_id, type_id):
        # TODO validate params

        # TODO: check that repository is owned by current user

        try:
            result = db.run(
                "MATCH (t:SchemaType)-[x]-(r:Repository) WHERE ID(r) = {repo_id} AND ID(t) = {type_id} OPTIONAL MATCH (f:SchemaField)-[y]-(t) OPTIONAL MATCH (d:Data)-[z]-(t) DELETE x,y,t,f,z,d",
                {
                    "type_id": int(type_id),
                    "repo_id": int(repo_id)
                })
            print "type=" + type_id
            print "repo=" + repo_id
            if result is not None:
                SchemaManager.resetTypeInfoCache()
                return {"type_id": type_id}
            else:
                raise FindError(message="Could not find type",
                                context="Schema.deleteType",
                                dberror="")
        except Exception as e:
            raise DbError(message="Could not delete type",
                          context="Schema.deleteType",
                          dberror=e.message)
Ejemplo n.º 25
0
def editOrg(org_id):
    name = request.form.get('name')
    location = request.form.get('location')
    email = request.form.get('email')
    url = request.form.get('url')
    tagline = request.form.get('tagline')

    ret = {
      'status_code': '',
      'payload': {
        'msg': '',
        'org': ''
      }
    }

    update = []
    if name is not None:
        update.append("o.name = {name}")

    if location is not None:
        update.append("o.location = {location}")

    if email is not None:
        update.append("o.email = {email}")

    if url is not None:
        update.append("o.url = {url}")

    if tagline is not None:
        update.append("o.tagline = {tagline}")

    update_str = ''
    update_str = "%s" % ", ".join(map(str, update))

    if update_str:
        updated_org = {}
        result = db.run("MATCH (o:Organization) WHERE ID(o)={org_id} SET " + update_str +
                                " RETURN o.name AS name, o.location AS location, o.email AS email, o.url AS url, o.tagline AS tagline", {"org_id": org_id, "name": name, "location": location, "email": email, "url": url, "tagline": tagline})

        for o in result:
            updated_org['name'] = o['name']
            updated_org['location'] = o['location']
            updated_org['email'] = o['email']
            updated_org['url'] = o['url']
            updated_org['tagline'] = o['tagline']

        if updated_org:
            ret['status_code'] = 200
            ret['payload']['msg'] = 'Organization updated'
            ret['payload']['org'] = updated_org
        else:
            ret['status_code'] = 400
            ret['payload']['msg'] = 'Problem updating Organization'
            
    else:
        ret['status_code'] = 422 
        ret['payload']['msg'] = 'Nothing to update'

    return responseHandler(ret)
Ejemplo n.º 26
0
  def edit(repo_id, name, url, readme, license, published, featured):
    if url is None or name is None or readme is None:
      raise ValidationError(message="Name, URL and README must be set", context="Repositories.edit")

    if RepoManager.nameCheck(name, repo_id) is False:
      raise ValidationError(message="Name is in use", context="Repositories.edit")

    update = []
    if name is not None:
      update.append("n.name = {name}")

    if url is not None:
      update.append("n.url = {url}")

    if readme is not None:
      update.append("n.readme = {readme}")

    if featured is not None:
      update.append("n.featured = {featured}")

    published_on = None
    if ((int(published) != 0) and (int(published) != 1)) or published is None:
      published = 0
    elif int(published) == 1:
      ts = time.time()
      published_on = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
      update.append("n.published_on = {published_on}")
    if license not in RepoManager.licenses:
      license = ''

    update.append("n.published = {published}")
    update.append("n.license = {license}")

    update_str = "%s" % ", ".join(map(str, update))

    if update_str:
      print update_str, featured
      result = db.run("MATCH (n:Repository) WHERE ID(n)={repo_id} SET " + update_str +
                      " RETURN n.name AS name, n.url AS url, n.readme AS readme, n.license AS license, n.published AS published, n.featured as featured, n.published_on as published_on, ID(n) AS id",
                      {"repo_id": int(repo_id), "name": name, "url": url, "readme": readme, "published": published,
                       "license": license, "featured": featured, "published_on": published_on})

      updated_repo = {}
      for r in result:
        print r
        updated_repo['repo_id'] = r['id']
        updated_repo['name'] = r['name']
        updated_repo['url'] = r['url']
        updated_repo['readme'] = r['readme']
        updated_repo['license'] = r['license']
        updated_repo['published'] = r['published']
        updated_repo['published_on'] = r['published_on']
        updated_repo['featured'] = r['featured']

      summary = result.consume()
      if summary.counters.properties_set >= 1:
        return updated_repo

      raise FindError(message="Could not find repository", context="Repositories.edit")
Ejemplo n.º 27
0
    def addListItem(repo_id, code, display, item_code, description=None):
        try:
            repo_id = int(repo_id)
        except TypeError:
            raise DbError(message="Invalid repo_id provided", context="List.addListItem",
                          dberror="")
        ret = {}
        try:
            if code is None or len(code) == 0:
                raise ValidationError(message="List code is required", context="List.addListItem")
            list_info = ListManager.getInfoForList(repo_id, code)
            if list_info is None:
                raise ValidationError(message="List code is invalid", context="List.addListItem")

            if display is None:
                raise ValidationError(message="Display value is required", context="List.addListItem")

            if item_code is None:
                raise ValidationError(message="List Item Code is required", context="List.addListItem")
            if isinstance(code, int):
                item_result = db.run("MATCH (i:ListItem {display: {display}})--(l:List {code: {code}})--(r:Repository) WHERE ID(r) = {repo_id} RETURN ID(i) as id, i.display as display", {"display": display, "code": code, "repo_id": repo_id}).peek()
            else:
                item_result = db.run("MATCH (i:ListItem {display: {display}})--(l:List {code: {code}})--(r:Repository) WHERE ID(r) = {repo_id} RETURN ID(i) as id, i.display as display", {"display": display, "code": code, "repo_id": repo_id}).peek()
            if item_result is not None:
                ret['exists'] = True
                ret['item_id'] = item_result['id']
                ret['display'] = item_result['display']
                return ret
            else:
                item_flds = ["display: {display}", "code: {item_code}", "description: {description}"]
                item_params = {"list_code": code, "repo_id": repo_id, "display": display, "item_code": item_code, "description": description}

                add_result = db.run("MATCH (r:Repository)--(l:List {code: {list_code}}) WHERE ID(r) = {repo_id} CREATE (i:ListItem {" + ", ".join(item_flds) + "})-[:PART_OF]->(l) RETURN ID(i) as id, i.display as display, i.code as code", item_params)


                r = add_result.peek()
                if r:
                    ret['exists'] = False
                    ret['item_id'] = r['id']
                    ret['display'] = r['display']
                    ret['code'] = r['code']
                    return ret
                else:
                    raise DbError(message="Could not add List Item", context="List.addListItem", dberror="")
        except Exception as e:
            raise DbError(message="Could not add List Item", context="List.addListItem", dberror=e.message)
Ejemplo n.º 28
0
    def delete(node_id, import_uuid=None):
        # TODO: does user have access to this repo?
        try:
            id = int(node_id)
        except:
            id = node_id

        try:
            if isinstance(id, (int)):
                db.run("MATCH (d:Data) WHERE ID(d) = {node_id} DELETE d", {"node_id": id})
            else:
                db.run("MATCH (d:Data) WHERE d.uuid = {uuid} DELETE d", {"uuid": id})

            # TODO: return false is no node is deleted?
            return True
        except Exception as e:
            raise DbError(message="Could not delete data", context="DataManager.delete", dberror=e.message)
Ejemplo n.º 29
0
    def portalBrowse(type):
        ret = []
        if type == 'featured':
            match_string = "r.featured = {featured}"
            match_values = {"featured": "1"}
            qString = "MATCH (r:Repository) WHERE " + match_string + " RETURN r.uuid as repo_id, r.name as repo_name, r.readme as readme"
            repos = db.run(qString, match_values)
            for repo in repos:
                ret.append({
                    'type': 'Repository',
                    'id': repo['repo_id'],
                    'name': repo['repo_name'],
                    'description': repo['readme']
                })
        elif type == 'published':
            qString = "MATCH (r:Repository) WHERE r.published = {published} RETURN r.uuid as repo_id, r.name as repo_name, r.readme as readme, r.published_on as published_on"
            repos = db.run(qString, {"published": "1"})
            for repo in repos:
                ret.append({
                    'type': 'Repository',
                    'id': repo['repo_id'],
                    'name': repo['repo_name'],
                    'description': repo['readme'],
                    'published_on': repo['published_on']
                })
            print ret
            ret.sort(key=lambda x: datetime.strptime(x['published_on'],
                                                     "%Y-%m-%d %H:%M:%S"),
                     reverse=True)
            ret = ret[0:6]
        elif type == 'updated':
            qString = 'MATCH (r:Repository)<-[:IMPORTED_INTO]-(i:ImportEvent) WHERE r.published = {published} WITH r, max(i.started_on) as max RETURN max, r.uuid as repo_id, r.name as repo_name, r.readme as readme, r.published_on as published_on'
            repos = db.run(qString, {"published": "1"})
            for repo in repos:
                ret.append({
                    'type': 'Repository',
                    'id': repo['repo_id'],
                    'name': repo['repo_name'],
                    'description': repo['readme'],
                    'published_on': repo['published_on'],
                    'imported_on': repo['max']
                })
            ret.sort(key=lambda x: x['imported_on'], reverse=True)
            ret = ret[0:6]

        return {"nodes": ret}
Ejemplo n.º 30
0
    def getCountForType(repo_id, type_id):
        repo_id = int(repo_id)
        type_id = int(type_id)
        try:
            q_count = db.run("MATCH (r:Repository)--(t:SchemaType)--(d:Data) WHERE ID(r)={repo_id} AND ID(t)={type_id} RETURN count(d) as data_count", {"repo_id": repo_id, "type_id": type_id}).peek()

            return {"repo_id": repo_id, "type_id": type_id, "data_count": q_count['data_count']}
        except exception as e:
            raise DbError(message="Could get type Count", context="DataManager.getCountForType", dberror=e.message)
Ejemplo n.º 31
0
    def add(repo_id, type_code, data, import_uuid=None, fields_created=None):
        # TODO: does user have access to this repo?
        data_proc, type_info = DataManager._validateData(repo_id, type_code, data, fields_created)
        try:
            q = "MATCH (t:SchemaType) WHERE ID(t) = {type_id} CREATE (n:Data " + makeDataMapForCypher(data_proc) + ")-[:IS]->(t) RETURN ID(n) AS id"

            data_proc["type_id"] = type_info["type_id"]     # add type_id to data before insert
            res = db.run(q, data_proc).peek()
            data_id = res["id"]

            if import_uuid is not None:
                db.run("MATCH (e:ImportEvent { uuid: {import_uuid}}), (n:Data) WHERE ID(n) = {data_id} CREATE (e)<-[x:IMPORTED_IN]-(n) RETURN ID(x) AS id", { "import_uuid": import_uuid, "data_id": data_id})


            #DataManager.logChange("I", data_proc)
            return data_id
        except Exception as e:
            raise DbError(message="Could not create data (" + e.__class__.__name__ + ") " + e.message, context="DataManager.add", dberror=e.message)
Ejemplo n.º 32
0
 def addRelationship(start_node, end_node, type_id="RELATED", data=None):
     # TODO: does user have access to these nodes?
     try:
         type_id = re.sub(r'[^A-Za-z0-9_]+', '_', type_id)
         # NOTE: type_id is substituted directly into the string as the Bolt Neo4j driver doesn't allow placeholders for relationship types (yet)
         res = db.run("MATCH (d1:Data), (d2:Data) WHERE ID(d1) = {start_node} AND ID(d2) = {end_node} CREATE(d1)-[r:" + type_id + "]->(d2) RETURN ID(r) AS id", {"start_node": start_node, "end_node": end_node, "type_id": type_id}).peek()
         return res["id"]
     except Exception as e:
         raise DbError(message="Could not create relationship", context="DataManager.addRelationship", dberror=e.message)
Ejemplo n.º 33
0
    def getByID(node_id):
        # TODO: does user have access to this node?

        try:
            id = int(node_id)
            result = db.run(
                "MATCH (d:Data)--(t:SchemaType)--(r:Repository) WHERE ID(d) = {node_id} RETURN d, t.name as typename, t.code as typecode, ID(t) as schema_id, ID(r) as repo_id",
                {"node_id": id})
        except:
            result = db.run(
                "MATCH (d:Data)--(t:SchemaType)--(r:Repository) WHERE d.uuid = {uuid} RETURN d, ID(d) as node_id, t.name as typename, t.code as typecode, ID(t) as schema_id, ID(r) as repo_id",
                {"uuid": node_id})

        if result.peek():
            for r in result:
                    return {"node_id": r["node_id"], "typename": r["typename"], "typecode": r["typecode"], "schema_id": r["schema_id"], "repo_id": r["repo_id"], "data": r["d"].properties}
        else:
            raise FindError(message="Node does not exist")
Ejemplo n.º 34
0
    def portalBrowse(type):
        ret = []
        if type == 'featured':
            match_string = "r.featured = {featured}"
            match_values = {"featured": "1"}
            qString = "MATCH (r:Repository) WHERE " + match_string + " RETURN r.uuid as repo_id, r.name as repo_name, r.readme as readme"
            repos = db.run(qString, match_values)
            for repo in repos:
                ret.append({
                    'type': 'Repository',
                    'id': repo['repo_id'],
                    'name': repo['repo_name'],
                    'description': repo['readme']
                })
        elif type == 'published':
            qString = "MATCH (r:Repository) WHERE r.published = {published} RETURN r.uuid as repo_id, r.name as repo_name, r.readme as readme, r.published_on as published_on"
            repos = db.run(qString, {"published": "1"})
            for repo in repos:
                ret.append({
                    'type': 'Repository',
                    'id': repo['repo_id'],
                    'name': repo['repo_name'],
                    'description': repo['readme'],
                    'published_on': repo['published_on']
                })
            print ret
            ret.sort(key=lambda x: datetime.strptime(x['published_on'], "%Y-%m-%d %H:%M:%S"), reverse=True)
            ret = ret[0:6]
        elif type == 'updated':
            qString = 'MATCH (r:Repository)<-[:IMPORTED_INTO]-(i:ImportEvent) WHERE r.published = {published} WITH r, max(i.started_on) as max RETURN max, r.uuid as repo_id, r.name as repo_name, r.readme as readme, r.published_on as published_on'
            repos = db.run(qString, {"published": "1"})
            for repo in repos:
                ret.append({
                    'type': 'Repository',
                    'id': repo['repo_id'],
                    'name': repo['repo_name'],
                    'description': repo['readme'],
                    'published_on': repo['published_on'],
                    'imported_on': repo['max']
                })
            ret.sort(key=lambda x: x['imported_on'], reverse=True)
            ret = ret[0:6]

        return {"nodes": ret}
Ejemplo n.º 35
0
 def _getDataUUIDsForRepo(repo_id):
     repo_uuids = []
     try:
         qString = "MATCH (r:Repository)--(t:SchemaType)--(d:Data) WHERE ID(r) = {repo_id} RETURN d.uuid as uuid"
         data = db.run(qString, {"repo_id": repo_id})
     except Exception as e:
         pass
     for d in data:
         repo_uuids.append(d.uuid)
     return repo_uuids
Ejemplo n.º 36
0
  def addCollaborator(repo_id, person_id, access="read-only"):
    RepoManager.validate_repo_id(repo_id)

    result = db.run("MATCH (n:Repository) WHERE ID(n)={repo_id} MATCH (p:Person) WHERE ID(p)={person_id} MERGE (p)-[x:COLLABORATES_WITH]->(n) ON CREATE SET x.access = {access}",
      {"repo_id": repo_id, "person_id": person_id, "access": access})

    summary = result.consume()
    if summary.counters.relationships_created >= 1:
      return True
    raise FindError(message="Already exists", context="Repositories.addCollaborator")
Ejemplo n.º 37
0
    def createImportEvent(repo_id, type, upload_filepath, original_filename, ftype, row_count):
        result = db.run(
            "MATCH (r:Repository) WHERE ID(r) = {repo_id} CREATE (e:ImportEvent { original_filename: {original_filename}, type: {type}, filetype: {filetype}, size: {size}, rows: {rows}, started_on: {time}, ended_on: null})-[:IMPORTED_INTO]->(r) RETURN ID(e) AS id",
            {"repo_id": int(repo_id), "original_filename": original_filename, "type": type, "filetype": ftype, "size": os.path.getsize(upload_filepath), "rows": row_count, "time": time.time()})
        if result is None:
            return False
        r = result.peek()

        # We have to do a separate query because Neo4j doesn't make the uuid available on CREATE (argh)
        result = db.run(
            "MATCH (e:ImportEvent) WHERE ID(e) = {id} RETURN e.uuid AS uuid",
            {"id": int(r['id'])})
        if result is None:
            return False

        r = result.peek()
        if r and 'uuid' in r:
            return r['uuid']
        return True
Ejemplo n.º 38
0
 def closeImportEvent(uuid):
     result = db.run(
         "MATCH (e:ImportEvent { uuid: {uuid}}) SET e.ended_on = {time} RETURN e.uuid as uuid",
         {"uuid": uuid, "time": time.time()})
     if result is None:
         return False
     r = result.peek()
     if r and 'uuid' in r:
         return r['uuid']
     return True
Ejemplo n.º 39
0
 def _getDataUUIDsForRepo(repo_id):
     repo_uuids = []
     try:
         qString = "MATCH (r:Repository)--(t:SchemaType)--(d:Data) WHERE ID(r) = {repo_id} RETURN d.uuid as uuid"
         data = db.run(qString, {"repo_id": repo_id})
     except Exception as e:
         pass
     for d in data:
         repo_uuids.append(d.uuid)
     return repo_uuids
Ejemplo n.º 40
0
  def getData(repo_id, limit=20):
    RepoManager.validate_repo_id(repo_id)

    nodes = []
    result = db.run("MATCH (r:Repository)--(f:SchemaType)--(n:Data) WHERE ID(r)={repo_id} RETURN n LIMIT {limit}", {"repo_id": int(repo_id), "limit": limit})

    for data in result:
      nodes.append(data.items()[0][1].properties)

    return nodes
Ejemplo n.º 41
0
  def delete(organization_id):

    del_success = False
    result = db.run("MATCH (o:Organization) WHERE ID(o)={organization_id} OPTIONAL MATCH (o)-[r]-() DELETE r,o", {"organization_id": organization_id})

    summary = result.consume()
    if summary.counters.nodes_deleted >= 1:
      del_success = True

    return del_success
Ejemplo n.º 42
0
    def editType(repo_id, type_id, name, code, description, fields, fieldsToDelete):
        # TODO validate params

        # TODO: check that repository is owned by current user


        result = db.run(
            "MATCH (r:Repository)--(t:SchemaType) WHERE ID(r) = {repo_id} AND ID(t) = {type_id} SET t.name = {name}, t.code = {code}, t.description = {description} RETURN ID(t) AS id",
            {"repo_id": int(repo_id), "type_id": int(type_id), "name": name, "code": code, "description": description})

        # add/edit fields
        field_status = {}
        for k in fields:
            settings = {f.replace("settings_", ""): v for f, v in fields[k].iteritems() if 'settings_' in f}
            if 'id' in fields[k]:
                # edit existing field
                fret = SchemaManager.editField(repo_id, code, fields[k].get('id', ''), fields[k].get('name', ''), fields[k].get('code', ''), fields[k].get('type', ''),
                                               fields[k]['description'], settings)
                if 'field_id' in fret:
                    field_status[fields[k]['code']] = {'status_code': 200, 'field_id': fret['field_id'],
                                                       'msg': 'Edited field'}
                else:
                    field_status[fields[k]['code']] = {'status_code': 200, 'field_id': None,
                                                       'msg': 'Could not edit field'}
            else:
                # add field
                fret = SchemaManager.addField(repo_id, code, fields[k].get('name', ''), fields[k].get('code', ''), fields[k].get('type', ''), fields[k].get('description', ''), settings)

                if 'field_id' in fret:
                    field_status[fields[k]['code']] = {'status_code': 200, 'field_id': fret['field_id'], 'msg': 'Created new field'}
                else:
                    field_status[fields[k]['code']] = {'status_code': 200, 'field_id': None, 'msg': 'Could not create new field'}

        # delete fields
        if fieldsToDelete:
            for field_id in fieldsToDelete:
                SchemaManager.deleteField(repo_id, code, field_id)


        SchemaManager.resetTypeInfoCache()

        if result:
            ret = {}
            for r in result:
                ret['type'] = {
                    "id": r['id'],
                    "name": name,
                    "code": code,
                    "description": description,
                    "field_status": field_status
                }
                return ret
        else:
            raise DbError(message="Could not edit type", context="Schema.editType",
                          dberror="")
Ejemplo n.º 43
0
    def portalSearch(expression, start=0, end=25):
        client = Elasticsearch()
        ret = {'nodes': [], 'Counts': {}}
        q = Q("bool", must=[Q('match', _all=expression)])
        s = Search(using=client,
                   index="neo4j-inquisite-node",
                   doc_type="Repository,Data").query(q)
        q_total = s.count()
        s = s[0:q_total]
        s = s.highlight_options(require_field_match=False)
        s = s.highlight('*', fragment_size=45)
        res = s.execute()
        data = {}
        uuids = []
        pub_uuids = {}
        if res:
            for r in res:
                d = r.to_dict()
                if r.meta.doc_type == 'Repository':
                    if int(d['published']) == 0:
                        continue
                    repo_id = r.meta.id
                    ret['nodes'].append({
                        "id": r.meta.id,
                        "type": "Repository",
                        "name": d['name'],
                        "description": d['readme']
                    })
                    repo_uuids = SearchManager._getDataUUIDsForRepo(repo_id)
                    pub_uuids[repo_id] = repo_uuids
                else:
                    hits = []
                    highs = r.meta.highlight.to_dict()
                    for high_field, high_value in highs.items():
                        hits.append({high_field: high_value})
                    data[r.meta.id] = {'id': r.meta.id, "hits": hits}
                    uuids.append(r.meta.id)
            qString = "MATCH (r:Repository)--(t:SchemaType)--(d:Data) WHERE d.uuid IN {uuids} AND r.published = '1' RETURN d.uuid as uuid, r.name as repo_name, r.uuid as repo_id"
            pub_data = db.run(qString, {"uuids": uuids})
            data_max = 0
            for checked in pub_data:
                if data_max >= 32:
                    break
                ret['nodes'].append({
                    "id": checked['uuid'],
                    "type": "Data",
                    "repo_id": checked['repo_id'],
                    "repo_name": checked['repo_name'],
                    "hits": data[checked['uuid']]['hits']
                })
                data_max += 1

            return ret
        else:
            return ret
Ejemplo n.º 44
0
    def setPassword(person_id, password):
        # TODO: verify user has access to update password...
        if password is not None:
            if is_number(person_id):
                find_str = "ID(p)={person_id}"
            else:
                find_str = "p.password_reset_key = {person_id}"

            db_password_hash = ''
            # check if password matches person_id
            result = db.run(
                "MATCH (p:Person) WHERE " + find_str +
                " RETURN p.password AS password", {"person_id": person_id})
            for p in result:
                db_password_hash = p['password']

            if db_password_hash != '':
                # hash new password and update DB
                new_pass_hash = sha256_crypt.hash(password)

                if db_password_hash == new_pass_hash:
                    return True

                result = db.run(
                    "MATCH (p:Person) WHERE " + find_str +
                    " SET p.password = {new_pass_hash}, p.password_reset_key = '' RETURN p",
                    {
                        "person_id": person_id,
                        "new_pass_hash": new_pass_hash
                    })

                # Check we updated something
                summary = result.consume()
                if summary.counters.properties_set >= 1:
                    return True
                return False

            else:
                raise FindError("No user found")

        else:
            raise AuthError("Password is empty")
Ejemplo n.º 45
0
    def deleteRelationship(start_node=None,
                           end_node=None,
                           type_id=None,
                           rel_id=None):
        # TODO: does user have access to relationship?
        try:
            if type_id is not None:
                type_str = "r:" + re.sub(r'[^A-Za-z0-9_]+', '_', type_id)
            else:
                type_str = "r"

            # NOTE: type_id is substituted directly into the string as the Bolt Neo4j driver doesn't allow placeholders for relationship types (yet)

            if end_node is None:
                if rel_id is None:
                    rel_id = start_node  # if rel_id is not set assume first positional parameter (start_node) is the rel_id
                if rel_id is None:
                    return None

                # delete by rel_id
                db.run(
                    "MATCH (d1:Data)-[" + type_str +
                    "]-(d2:Data) WHERE ID(r) = {rel_id} DELETE r",
                    {"rel_id": rel_id})
            elif start_node is not None and end_node is not None:
                # delete by endpoints
                db.run(
                    "MATCH (d1:Data)-[" + type_str +
                    "]-(d2:Data) WHERE ID(d1) = {start_node} AND ID(d2) = {end_node} DELETE r",
                    {
                        "start_node": start_node,
                        "end_node": end_node
                    })
            else:
                raise ParameterError(
                    message="No relationship id or node id pair is set",
                    context="DataManager.deleteRelationship")
            return True
        except Exception as e:
            raise DbError(message="Could not delete relationship",
                          context="DataManager.deleteRelationship",
                          dberror=e.message)
Ejemplo n.º 46
0
  def addRepository(organization_id, repo_id):

    repository_added = False
    result = db.run("MATCH (o:Organization) WHERE ID(o)={organization_id} MATCH (r:Repository) WHERE ID(r)={repo_id} MERGE (r)-[:PART_OF]->(o)",
      {"organization_id": organization_id, "repo_id": repo_id})

    summary = result.consume()
    if summary.counters.relationships_created >= 1:
      repository_added = True

    return repository_added
Ejemplo n.º 47
0
  def removeFollower(repo_id, person_id):
    RepoManager.validate_repo_id(repo_id)

    result = db.run("START p=node(*) MATCH (p)-[rel:FOLLOWS]->(n) WHERE ID(p)={person_id} AND ID(n)={repo_id} DELETE rel",
      {"person_id": person_id, "repo_id": repo_id})

    summary = result.consume()
    if summary.counters.relationship_deleted >= 1:
      return True

    raise FindError(message="Could not find person", context="Repositories.removeFollower")
Ejemplo n.º 48
0
  def addFollower(repo_id, person_id):
    RepoManager.validate_repo_id(repo_id)

    result = db.run("MATCH (n:Repository) WHERE ID(n)={repo_id} MATCH (p:Person) WHERE ID(p)={person_id} MERGE (p)-[:FOLLOWS]->(n)",
      {"repo_id": repo_id, "person_id": person_id})

    summary = result.consume()
    if summary.counters.relationships_created >= 1:
      return True

    raise FindError(message="Could not find person or repository", context="Repositories.addFollower")
Ejemplo n.º 49
0
  def addPerson(organization_id, person_id):

    add_person = False
    result = db.run("MATCH (o:Organization) WHERE ID(o)={organization_id} MATCH (p:Person) WHERE ID(p)={person_id} " +
      "MERGE (p)-[:PART_OF]->(o)", {"organization_id": organization_id, "person_id": person_id})

    summary = result.consume()
    if summary.counters.relationship_created >= 1:
      add_person = True

    return add_person
Ejemplo n.º 50
0
  def removeRepository(organization_id, repo_id):

    del_repository = False
    result = db.run("START r=node(*) MATCH (r)-[rel:PART_OF]->(o) WHERE ID(r)={repo_id} AND ID(o)={organization_id} DELETE rel",
      {"organization_id": organization_id, "repo_id": repo_id})

    summary = result.consume()
    if summary.counters.relationships_deleted >= 1:
      del_respository = True

    return del_repository
Ejemplo n.º 51
0
  def deleteOwner(repo_id, owner_id):
    RepoManager.validate_repo_id(repo_id)

    result = db.run("START p=node(*) MATCH (p)-[rel:OWNED_BY]->(n) WHERE ID(p)={owner_id} AND ID(n)={repo_id} DELETE rel",
      {"owner_id": owner_id, "repo_id": repo_id})

    summary = result.consume()
    if summary.counters.relationships_deleted >= 1:
      return True

    raise FindError(message="Could not find person or repository", context="Repositories.deleteOwner")
Ejemplo n.º 52
0
  def removePerson(organization_id, person_id):

    del_person = False
    result = db.run("START p=node(*) MATCH (p)-[rel:PART_OF]->(n) WHERE ID(p)={person_id} AND ID(n)={organization_id} DELETE rel",
      {"organization_id": organization_id, "person_id": person_id})

    summary = result.consume()
    if summary.counters.relationships_deleted >= 1:
      del_person = True

    return del_person
Ejemplo n.º 53
0
    def getRepoForPortal(repo_uuid):
        try:
            result = db.run(
                "MATCH (r:Repository {uuid: {uuid}}) RETURN ID(r) AS id, r.name as name, r.readme as description",
                {
                    "uuid": repo_uuid
                }).peek()
            if result:
                repo_id = result['id']
                name = result['name']
                description = result['description']
        except:
            raise FindError("Could not load repository with UUID")

        repo_owner = RepoManager.getOwner(repo_id)
        owner_name = repo_owner['name']
        owner_location = repo_owner['location']
        owner_tagline = repo_owner['tagline']

        data = RepoManager.getData(repo_id, 48)

        return {
            "repo": repo_id,
            "repo_name": name,
            "description": description,
            "data": data,
            "owner": owner_name,
            "location": owner_location,
            "tagline": owner_tagline
        }

        #
        # ADMIN ONLY
        # Set a repository to be featured
        @staticmethod
        def setFeaturedRepo(repo_id, featured):
            RepoManager.validate_repo_id(repo_id)

            if int(featured) != 0 and int(featured) != 1:
                featured = 0

            result = db.run(
                "MATCH (r:Repository) WHERE ID(r)={repo_id} SET r.published = {featured} RETURN ID(r), r.featured as featured",
                {
                    "repo_id": int(repo_id),
                    "featured": featured
                }).peek()

            if result:
                if result['featured'] == featured:
                    return True

            return False
Ejemplo n.º 54
0
    def delete(node_id, import_uuid=None):
        # TODO: does user have access to this repo?
        try:
            id = int(node_id)
        except:
            id = node_id

        try:
            if isinstance(id, (int)):
                db.run("MATCH (d:Data) WHERE ID(d) = {node_id} DELETE d",
                       {"node_id": id})
            else:
                db.run("MATCH (d:Data) WHERE d.uuid = {uuid} DELETE d",
                       {"uuid": id})

            # TODO: return false is no node is deleted?
            return True
        except Exception as e:
            raise DbError(message="Could not delete data",
                          context="DataManager.delete",
                          dberror=e.message)
Ejemplo n.º 55
0
    def deleteDataFromRepo(repo_id, type_codes=None):
        # TODO: does user have access to this repo?

        type_str = ""
        if type_codes is not None:
            if all(isinstance(c, (int)) for c in type_codes):
                type_str = " AND ID(t) IN [" + ",".join(type_codes) + "] "
            else:
                type_codes = map(
                    lambda x: '"' + re.sub(r'[^A-Za-z0-9_]+', '_', x) + '"',
                    type_codes)
                type_str = " AND t.code IN [" + ",".join(type_codes) + "] "
        try:
            db.run(
                "MATCH (r:Repository)--(t:SchemaType)--(d:Data) WHERE ID(r) = {repo_id} "
                + type_str + " DETACH DELETE d", {"repo_id": repo_id})
            return True
        except Exception as e:
            raise DbError(message="Could not delete data from repository",
                          context="DataManager.deleteDataFromRepo",
                          dberror=e.message)
Ejemplo n.º 56
0
def addOrg():
    name = request.form.get('name')
    location = request.form.get('location')
    email = request.form.get('email')
    url = request.form.get('url')
    tagline = request.form.get('tagline')

    ret = {
      'status_code': '',
      'payload': {
        'msg': '',
        'organization': ''
      }
    }


    if name is not None and location is not None and email is not None and url is not None and tagline is not None:

        result = db.run(
            "CREATE (o:Organization {name: {name}, location: {location}, email: {email}, url: {url}, tagline: {tagline}}) RETURN o.name AS name, o.location AS location, o.email AS email, o.url AS url, o.tagline AS tagline, ID(o) AS org_id", {"name": name, "location": location, "email": email, "url": url, "tagline": tagline})

        new_org = {}
        for org in result:
            new_org = {
                'org_id': org['org_id'],
                'name': org['name'],
                'location': org['location'],
                'email': org['email'],
                'url': org['url'],
                'tagline': org['tagline']
            }

        summary = result.consume()

        node_created = False
        if summary.counters.nodes_created == 1:
            node_created = True

        if node_created:
            ret['status_code'] = 200
            ret['payload']['msg'] = 'Organization Added'
            ret['payload']['organization'] = new_org

        else:
            ret['status_code'] = 400
            ret['payload']['msg'] = 'Problem adding Organization'

    else:
        ret['status_code'] = 422
        ret['payload']['msg'] = 'Missing required fields'

    return responseHandler(ret)
Ejemplo n.º 57
0
    def getType(repo_id, schema_id):
        # TODO validate params

        # TODO: check that repository is owned by current user

        try:
            res = db.run(
                "MATCH (r:Repository)--(t:SchemaType) WHERE ID(r) = {repo_id} AND ID(t) = {schema_id}  RETURN ID(t) as id, t.name as name, t.code as code, t.description as description",
                {
                    "repo_id": int(repo_id),
                    "schema_id": int(schema_id)
                }).peek()
            if res:
                t = {
                    'id': str(res['id']),
                    'name': res['name'],
                    'code': res['code'],
                    'description': res['description']
                }

                count_res = db.run(
                    "MATCH (t:SchemaType)--(d:Data) WHERE ID(t) = {schema_id} RETURN count(d) as data_count",
                    {
                        "schema_id": int(schema_id)
                    }).peek()
                if count_res:
                    t['data_count'] = count_res['data_count']
                else:
                    t['data_count'] = 0

                # get fields
                i = SchemaManager.getInfoForType(repo_id, res['id'])
                t["fields"] = i["fields"]
                return t
        except Exception as e:
            raise DbError(message="Could not get types",
                          context="Schema.getType",
                          dberror=e.message)
Ejemplo n.º 58
0
  def getRepos(organization_id):
 
    repos = []
    result = db.run("MATCH (n:Repository)-[:PART_OF]->(o:Organization) WHERE ID(o)={organization_id} RETURN n.name AS name, n.url AS url, n.readme AS readme",
      {"organization_id": organization_id})

    for r in result:
      repos.append({
        "name": r['name'],
        "url": r['url'],
        "readme": r['readme']
      })

    return repos