Exemple #1
0
    def from_id(cls, project_id, revision=None, using='default'):
        # if revision number is specified, get that revision, otherwise use
        # "exists" query to get the version where "revision" does NOT exist
        if project_id is None:
            raise DocumentNotFound()

        if revision:
            revision_filter = Q('match', **{'revision': revision})
        else:
            # Search for documents where revision is not specified.
            revision_filter = ~Q('exists', **{'field': 'revision'})

        id_filter = Q('term', **{'projectId._exact': project_id})
        search = cls.search(using=using).filter(id_filter & revision_filter)
        try:
            res = search.execute()
        except Exception as e:
            raise e
        if res.hits.total.value > 1:
            id_filter = Q('term', **{'_id': res[0].meta.id}) 
            # Delete all files indexed with the same system/path, except the first result
            delete_query = id_filter & ~id_filter
            cls.search(using=using).filter(delete_query).delete()
            return cls.get(res[0].meta.id, using=using)
        elif res.hits.total.value == 1:
            return cls.get(res[0].meta.id, using=using)
        else:
            raise DocumentNotFound("No document found for "
                                   "{}".format(project_id))
Exemple #2
0
    def from_id(cls, project_id):

        if project_id is None:
            raise DocumentNotFound()

        search = cls.search().query(Q({"term": {"name._exact": project_id}}))
        try:
            res = search.execute()
        except Exception as e:
            raise e
        if res.hits.total > 1:
            for doc in res[1:res.hits.total]:
                doc.delete()
            return res[0]
        elif res.hits.total == 1:
            return res[0]
        else:
            raise DocumentNotFound("No document found for "
                                   "{}".format(project_id))
Exemple #3
0
 def from_id(cls, project_id):
     if project_id is None:
         raise DocumentNotFound()
     id_filter = Q('term', **{'name._exact': project_id})
     search = cls.search().filter(id_filter)
     try:
         res = search.execute()
     except Exception as e:
         raise e
     if res.hits.total.value > 1:
         id_filter = Q('term', **{'_id': res[0].meta.id})
         # Delete all files indexed with the same system/path, except the first result
         delete_query = id_filter & ~id_filter
         cls.search().filter(delete_query).delete()
         return cls.get(res[0].meta.id)
     elif res.hits.total.value == 1:
         return cls.get(res[0].meta.id)
     else:
         raise DocumentNotFound("No document found for "
                                "{}".format(project_id))
Exemple #4
0
 def from_path(cls, system, path):
     search = cls.search()
     search = search.filter('term', **{'system._exact': system})
     search = search.filter('term', **{'path._exact': path})
     try:
         res = search.execute()
     except Exception as e:
         raise e
     if res.hits.total > 1:
         for doc in res[1:res.hits.total]:
             doc.delete()
         return res[0]
     elif res.hits.total == 1:
         return res[0]
     else:
         raise DocumentNotFound("No document found for "
                                "{}/{}".format(system, path))
Exemple #5
0
 def from_path(cls, system, path):
     Index(settings.ES_INDICES['files']['alias']).refresh()
     search = cls.search()
     sys_filter = Q('term', **{'system._exact': system})
     path_filter = Q('term', **{'path._exact': path})
     search = search.filter(sys_filter & path_filter)
     try:
         res = search.execute()
     except Exception as exc:
         raise exc
     if res.hits.total.value > 1:
         id_filter = Q('term', **{'_id': res[0].meta.id})
         # Delete all files indexed with the same system/path, except the first result
         delete_query = sys_filter & path_filter & ~id_filter
         cls.search().filter(delete_query).delete()
         return cls.get(res[0].meta.id)
     elif res.hits.total.value == 1:
         return cls.get(res[0].meta.id)
     else:
         raise DocumentNotFound("No document found for "
                                "{}/{}".format(system, path))