def task_reindex_solr(solr_location=None): """ Adds all ProfileUnits, Users, and SavedSearches to solr. Inputs: :solr_location: Dict of separate cores to be updated (Optional); defaults to the default instance from settings """ if solr_location is None: solr_location = settings.SOLR l = [] u = User.objects.all().values_list('id', flat=True) for x in u: l.append(profileunits_to_dict(x)) s = SavedSearch.objects.filter(user__isnull=False) for x in s: saved_search_dict = object_to_dict(SavedSearch, x) saved_search_dict['doc_type'] = 'savedsearch' l.append(saved_search_dict) u = User.objects.all() for x in u: l.append(object_to_dict(User, x)) l = split_list(l, 1000) for location in solr_location.values(): solr = pysolr.Solr(location) for x in l: x = filter(None, list(x)) solr.add(x)
def test_profileunit_to_dict(self): """ Confirms that a solr dictionary is being generated as expected by the profileunits_to_dict function. """ user = UserFactory(email="*****@*****.**") name = PrimaryNameFactory(user=user) content_type = ContentType.objects.get_for_model(ProfileUnits) expected = { "Name_content_type_id": [25], "Name_given_name": ["Alice"], "uid": "%s##%s" % (str(content_type.pk), str(user.pk)), "ProfileUnits_user_id": 1, "Name_user_id": [1], "Name_id": [name.pk], "Name_family_name": ["Smith"], "Name_primary": [True], } result = profileunits_to_dict(user.id) self.assertEqual(result['Name_id'], expected['Name_id']) self.assertEqual(result['uid'], expected['uid'])
def test_address_slabs(self): expected = { 'Address_content_type_id': [26], 'Address_address_line_two': [u'Apt. 8'], u'Address_id': [1], 'uid': '23##1', 'ProfileUnits_user_id': 1, 'Address_country_code': [u'USA'], 'Address_region': [u'USA##IN'], 'Address_country_sub_division_code': [u'IN'], 'Address_postal_code': [u'12345'], 'Address_address_line_one': [u'1234 Thing Road'], 'Address_user_id': [1], 'Address_label': [u'Home'], 'Address_full_location': [u'USA##IN##Indianapolis'], 'Address_city_name': [u'Indianapolis'] } user = UserFactory(email="*****@*****.**") AddressFactory(user=user) result = profileunits_to_dict(user.id) self.assertEqual(expected['Address_country_code'], result['Address_country_code']) self.assertEqual(expected['Address_region'], result['Address_region']) self.assertEqual(expected['Address_full_location'], result['Address_full_location'])
def update_solr_task(solr_location=None): """ Deletes all items scheduled for deletion, and then adds all items scheduled to be added to solr. Inputs: :solr_location: Dict of separate cores to be updated """ if hasattr(mail, 'outbox'): solr_location = settings.TEST_SOLR_INSTANCE elif solr_location is None: solr_location = settings.SOLR objs = Update.objects.filter(delete=True).values_list('uid', flat=True) if objs: objs = split_list(objs, 1000) for obj_list in objs: obj_list = filter(None, list(obj_list)) uid_list = " OR ".join(obj_list) for location in solr_location.values(): solr = pysolr.Solr(location) solr.delete(q="uid:(%s)" % uid_list) Update.objects.filter(delete=True).delete() objs = Update.objects.filter(delete=False) updates = [] for obj in objs: content_type, key = obj.uid.split("##") model = ContentType.objects.get_for_id(content_type).model_class() if model == SavedSearch: search = model.objects.get(pk=key) # Saved search recipients can currently be null; Displaying these # searches may be implemented in the future but will likely require # some template work. if search.user: updates.append(object_to_dict(model, search)) # If the user is being updated, because the user is stored on the # SavedSearch document, every SavedSearch belonging to that user # also has to be updated. elif model == User: searches = SavedSearch.objects.filter(user_id=key) [updates.append(object_to_dict(SavedSearch, s)) for s in searches] updates.append(object_to_dict(model, model.objects.get(pk=key))) else: updates.append(profileunits_to_dict(key)) updates = split_list(updates, 1000) for location in solr_location.values(): solr = pysolr.Solr(location) for update_subset in updates: update_subset = filter(None, list(update_subset)) solr.add(list(update_subset)) objs.delete()