Pārlūkot izejas kodu

[search] Pull static fields list when /luke is not successfull

Dashboard will then work when end user is not a Solr Sentry admin.
Romain Rigaux 10 gadi atpakaļ
vecāks
revīzija
cc90c41
2 mainītis faili ar 62 papildinājumiem un 2 dzēšanām
  1. 21 2
      apps/search/src/search/models.py
  2. 41 0
      apps/search/src/search/tests.py

+ 21 - 2
apps/search/src/search/models.py

@@ -670,6 +670,19 @@ class Collection2(object):
   def _make_gridlayout_header_field(cls, field, isDynamic=False):
     return {'name': field['name'], 'type': field['type'], 'sort': {'direction': None}, 'isDynamic': isDynamic}
 
+
+  @classmethod
+  def _make_luke_from_schema_fields(cls, schema_fields):
+    return dict([
+          (f['name'], {
+              'copySources': [],
+              'type': f['type'],
+              'required': True,
+              'uniqueKey': f.get('uniqueKey'),
+              'flags': u'%s-%s-----OF-----l' % ('I' if f['indexed'] else '-', 'S' if f['stored'] else '-'), u'copyDests': []
+          })
+        for f in schema_fields['fields']])
+
   def get_absolute_url(self):
     return reverse('search:index') + '?collection=%s' % self.id
 
@@ -677,8 +690,14 @@ class Collection2(object):
     return sorted([str(field.get('name', '')) for field in self.fields_data(user)])
 
   def fields_data(self, user, name):
-    schema_fields = SolrApi(SOLR_URL.get(), user).fields(name)
-    schema_fields = schema_fields['schema']['fields']
+    api = SolrApi(SOLR_URL.get(), user)
+    try:
+      schema_fields = api.fields(name)
+      schema_fields = schema_fields['schema']['fields']
+    except Exception, e:
+      LOG.warn('/luke call did not succeed: %s' % e)
+      fields = api.schema_fields(name)
+      schema_fields = Collection2._make_luke_from_schema_fields(fields)
 
     return sorted([self._make_field(field, attributes) for field, attributes in schema_fields.iteritems()])
 

+ 41 - 0
apps/search/src/search/tests.py

@@ -128,6 +128,47 @@ class TestWithMockedSolr(TestSearchBase):
     response = '{"uid":"1111111","method":"check_user"}\x00'
     response = json.loads(response.replace('\x00', '')) # Does not call real API
 
+  def test_convert_schema_fields_to_luke(self):
+    schema_fields = {u'fields': [
+        {u'indexed': True, u'stored': True, u'type': u'long', u'name': u'_version_'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'tdate', u'name': u'created_at'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'string', u'name': u'expanded_url'},
+        {u'uniqueKey': True, u'name': u'id', u'required': True, u'stored': True, u'indexed': True, u'type': u'tlong'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'tlong', u'name': u'in_reply_to_status_id'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'tint', u'name': u'in_reply_to_user_id'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'string', u'name': u'media_url_https'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'tint', u'name': u'retweet_count'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'text_general', u'name': u'source'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'text_general', u'name': u'text'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'tint', u'name': u'user_followers_count'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'tint', u'name': u'user_friends_count'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'string', u'name': u'user_location'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'text_general', u'name': u'user_name'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'string', u'name': u'user_screen_name'},
+        {u'indexed': True, u'stored': True, u'required': True, u'type': u'tint', u'name': u'user_statuses_count'}
+        ], u'responseHeader': {u'status': 0, u'QTime': 1}
+    }
+    assert_equal([
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'long', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'string', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'string', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'string', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'string', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'tdate', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'text_general', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'text_general', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'text_general', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'tint', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'tint', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'tint', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'tint', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'tint', u'copyDests': []},
+        {'uniqueKey': None, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'tlong', u'copyDests': []},
+        {'uniqueKey': True, 'copySources': [], 'flags': u'I-S-----OF-----l', 'required': True, 'type': u'tlong', u'copyDests': []}
+        ],
+        sorted(Collection2._make_luke_from_schema_fields(schema_fields).values())
+    )
+
   def test_response_escaping_multi_value(self):
     MockResource.set_solr_response("""{
       "responseHeader": {