allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [05/20] git commit: [#7257] ticket:562 Added proper index methods to Project model
Date Wed, 21 May 2014 14:44:53 GMT
[#7257] ticket:562 Added proper index methods to Project model


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/f4b70d3e
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/f4b70d3e
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/f4b70d3e

Branch: refs/heads/master
Commit: f4b70d3ee67f915948c2d8aa06494f238d95b6ef
Parents: d107c7f
Author: Ferens Dmitriy <ferensdima@gmail.com>
Authored: Wed Mar 26 16:34:29 2014 +0200
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Wed May 21 14:44:19 2014 +0000

----------------------------------------------------------------------
 Allura/allura/lib/search.py                |  8 ++++--
 Allura/allura/model/artifact.py            |  1 +
 Allura/allura/model/project.py             | 36 ++++++++++++++++++++-----
 Allura/allura/tasks/index_tasks.py         | 10 +++----
 Allura/allura/tests/model/test_artifact.py |  4 +++
 Allura/allura/tests/model/test_project.py  | 12 +++++++++
 6 files changed, 56 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/f4b70d3e/Allura/allura/lib/search.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/search.py b/Allura/allura/lib/search.py
index 42cf25a..08ad1a5 100644
--- a/Allura/allura/lib/search.py
+++ b/Allura/allura/lib/search.py
@@ -30,7 +30,6 @@ from pysolr import SolrError
 
 from allura.lib import helpers as h
 from allura.lib.solr import escape_solr_arg
-from allura.model import ArtifactReference
 
 log = getLogger(__name__)
 
@@ -79,7 +78,10 @@ class SearchIndexable(object):
         if doc is None:
             return None
         # if index() returned doc without text, assume empty text
-        text = doc.setdefault('text', '')
+        text = doc.get('text')
+        if text is None:
+            text = doc['text'] = ''
+
         # Convert text to plain text (It usually contains markdown markup).
         # To do so, we convert markdown into html, and then strip all html tags.
         text = g.markdown.convert(text)
@@ -238,6 +240,8 @@ def search_app(q='', fq=None, app=True, **kw):
                 return doc
 
             def paginate_comment_urls(doc):
+                from allura.model import ArtifactReference
+
                 if doc.get('type_s', '') == 'Post':
                     aref = ArtifactReference.query.get(_id=doc.get('id'))
                     if aref and aref.artifact:

http://git-wip-us.apache.org/repos/asf/allura/blob/f4b70d3e/Allura/allura/model/artifact.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/artifact.py b/Allura/allura/model/artifact.py
index 1b0e2ad..143c44a 100644
--- a/Allura/allura/model/artifact.py
+++ b/Allura/allura/model/artifact.py
@@ -77,6 +77,7 @@ class Artifact(MappedClass, SearchIndexable):
                 log.debug('Not updating mod_date')
             if c.project and not skip_last_updated:
                 c.project.last_updated = datetime.utcnow()
+ 
     type_s = 'Generic Artifact'
 
     # Artifact base schema

http://git-wip-us.apache.org/repos/asf/allura/blob/f4b70d3e/Allura/allura/model/project.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/project.py b/Allura/allura/model/project.py
index 271fe24..adc3226 100644
--- a/Allura/allura/model/project.py
+++ b/Allura/allura/model/project.py
@@ -42,6 +42,7 @@ from allura.lib import exceptions
 from allura.lib import security
 from allura.lib import validators as v
 from allura.lib.security import has_access
+from allura.lib.search import SearchIndexable
 from allura.model.types import MarkdownCache
 
 from .session import main_orm_session
@@ -174,7 +175,7 @@ class ProjectMapperExtension(MapperExtension):
         g.zarkov_event('project_create', project=obj)
 
 
-class Project(MappedClass, ActivityNode, ActivityObject):
+class Project(SearchIndexable, MappedClass, ActivityNode, ActivityObject):
     '''
     Projects contain tools, subprojects, and their own metadata.  They live
     in exactly one :class:`~allura.model.neighborhood.Neighborhood`
@@ -197,6 +198,8 @@ class Project(MappedClass, ActivityNode, ActivityObject):
         unique_indexes = [('neighborhood_id', 'shortname')]
         extensions = [ProjectMapperExtension]
 
+    type_s = 'Project'
+
     # Project schema
     _id = FieldProperty(S.ObjectId)
     parent_id = FieldProperty(S.ObjectId, if_missing=None)
@@ -1020,15 +1023,34 @@ class Project(MappedClass, ActivityNode, ActivityObject):
             return 'busy'
 
     def index_id(self):
-        id = 'unique id'
-        return id
+        id = '%s.%s#%s' % (
+            self.__class__.__module__,
+            self.__class__.__name__,
+            self._id)
+        return id.replace('.', '/')
 
     def index(self):
         provider = plugin.ProjectRegistrationProvider.get()
-        data = provider.index_project(self)
-        data.update(id=self.index_id(),
-                    title='Project %s' % self._id)
-        return data
+        fields = dict(id=self.index_id(),
+                      title='Project %s' % self._id,
+                      type_s=self.type_s,
+                      deleted_b=self.deleted,
+                      # Not analyzed fields
+                      category_id_s=str(self.category_id),
+                      neighborhood_id_s=str(self.neighborhood_id),
+                      last_updated_dt=self.last_updated,
+                      removal_changed_date_dt=self.removal_changed_date,
+                      # Analyzed fields
+                      name_t=self.name,
+                      shortname_t=self.shortname,
+                      labels_t=' '.join(self.labels),
+                      summary_t=self.summary,
+                      description_t=self.description,
+                      neighborhood_name_t=self.neighborhood.name)
+        if self.category:
+            fields.update(category_name_t=self.category.name,
+                          category_description_t=self.category.description)
+        return dict(provider.index_project(self), **fields)
 
     def __json__(self):
         result = dict(

http://git-wip-us.apache.org/repos/asf/allura/blob/f4b70d3e/Allura/allura/tasks/index_tasks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tasks/index_tasks.py b/Allura/allura/tasks/index_tasks.py
index 222a926..d507e5e 100644
--- a/Allura/allura/tasks/index_tasks.py
+++ b/Allura/allura/tasks/index_tasks.py
@@ -55,10 +55,7 @@ def add_artifacts(ref_ids, update_solr=True, update_refs=True, solr_hosts=None):
     from allura import model as M
     from allura.lib.search import find_shortlinks
 
-    if solr_hosts:
-        solr = make_solr_from_config(solr_hosts)
-    else:
-        solr = g.solr
+    solr = make_solr_from_config(solr_hosts) if solr_hosts else g.solr
     exceptions = []
     solr_updates = []
     with _indexing_disabled(M.session.artifact_orm_session._get()):
@@ -75,8 +72,9 @@ def add_artifacts(ref_ids, update_solr=True, update_refs=True, solr_hosts=None):
                         continue
                     # Find shortlinks in the raw text, not the escaped html
                     # created by the `solarize()`.
-                    ref.references = [
-                        link.ref_id for link in find_shortlinks(artifact.index().get('text')
or '')]
+                    link_text = artifact.index().get('text', '')
+                    shortlinks = find_shortlinks(link_text)
+                    ref.references = [link.ref_id for link in shortlinks)]
             except Exception:
                 log.error('Error indexing artifact %s', ref._id)
                 exceptions.append(sys.exc_info())

http://git-wip-us.apache.org/repos/asf/allura/blob/f4b70d3e/Allura/allura/tests/model/test_artifact.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_artifact.py b/Allura/allura/tests/model/test_artifact.py
index 367757b..b6d6832 100644
--- a/Allura/allura/tests/model/test_artifact.py
+++ b/Allura/allura/tests/model/test_artifact.py
@@ -98,6 +98,10 @@ def test_artifact():
     pg.acl.pop()
     ThreadLocalORMSession.flush_all()
     assert not security.has_access(pg, 'delete')(user=u)
+
+
+def test_artifact_index():
+    pg = WM.Page(title='TestPage1')
     idx = pg.index()
     assert 'title' in idx
     assert 'url_s' in idx

http://git-wip-us.apache.org/repos/asf/allura/blob/f4b70d3e/Allura/allura/tests/model/test_project.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_project.py b/Allura/allura/tests/model/test_project.py
index b33596c..8b72df6 100644
--- a/Allura/allura/tests/model/test_project.py
+++ b/Allura/allura/tests/model/test_project.py
@@ -95,6 +95,18 @@ def test_project():
     c.app.config.breadcrumbs()
 
 
+def test_project_index():
+    project, idx = c.project, c.project.index()
+    assert 'id' in idx
+    assert idx['id'] == project.index_id()
+    assert 'title' in idx
+    assert 'type_s' in idx
+    assert 'deleted_b' in idx
+    assert 'neighborhood_id_s' in idx
+    assert 'category_id_s' in idx
+    assert 'description_t' in idx
+
+
 def test_subproject():
     project = M.Project.query.get(shortname='test')
     with td.raises(ToolError):


Mime
View raw message