def test_instance_with_many_moderations(self): # Register a moderator that keeps the history # un-register UserProfile from moderation import moderation class KeepHistoryModerator(GenericModerator): keep_history = True moderation.unregister(UserProfile) moderation.register(UserProfile, KeepHistoryModerator) profile = UserProfile(description='Profile for new user', url='http://www.yahoo.com', user=self.user) profile.save() # There should only be one ModeratedObject self.assertEqual(1, ModeratedObject.objects.count()) profile.url = 'http://www.google.com' profile.save() # Should now be two self.assertEqual(2, ModeratedObject.objects.count()) # Getting for instance should return the one with the google url moderated_object = ModeratedObject.objects.get_for_instance(profile) self.assertEqual(profile.url, moderated_object.changed_object.url) # Get the first object, and does it have the yahoo address moderated_object_pk1 = ModeratedObject.objects.get(pk=1) self.assertEqual('http://www.yahoo.com', moderated_object_pk1.changed_object.url)
def test_save_handler_keep_history(self): # de-register current Moderator and replace it with one that # has keep_history set to True from moderation import moderation class KeepHistoryModerator(GenericModerator): keep_history = True notify_moderator = False moderation.unregister(UserProfile) moderation.register(UserProfile, KeepHistoryModerator) from django.db.models import signals signals.pre_save.connect(self.moderation.pre_save_handler, sender=UserProfile) signals.post_save.connect(self.moderation.post_save_handler, sender=UserProfile) profile = UserProfile(description='Profile for new user', url='http://www.yahoo.com', user=User.objects.get(username='******')) profile.save() moderated_object = ModeratedObject.objects.get_for_instance(profile) self.assertEqual(moderated_object.content_object, profile) # Now update it and make sure it gets the right history object... profile.url = 'http://www.google.com' profile.save() moderated_object = ModeratedObject.objects.get_for_instance(profile) self.assertEqual(moderated_object.content_object, profile) # There should only be two moderated objects self.assertEqual(2, ModeratedObject.objects.count()) # Approve the change moderated_object.approve(by=self.user, reason='Testing post save handlers') # There should *still* only be two moderated objects self.assertEqual(2, ModeratedObject.objects.count()) signals.pre_save.disconnect(self.moderation.pre_save_handler, UserProfile) signals.post_save.disconnect(self.moderation.post_save_handler, UserProfile) self.moderation = False
def load_growing_places(file_name='growing_places.csv'): reader = csv.DictReader(open(settings.DATA_ROOT + '/' + file_name)) # Stop moderating GrowingPlace objects for now moderation.unregister(GrowingPlace) for place in reader: # skip empty rows if not place['Organization']: continue address = _strip_string(place['Address']) city = _strip_string(place['City']) state = _strip_string(place['State']) zipcode = _strip_string(place['ZIP']) # watch out for weird stuff in the zipcode field if not re.match('^[\d-]+$', zipcode): zipcode = None lng, lat = geocode(' '.join(filter(None, (address, city, state, zipcode)))) try: centroid = Point(lng, lat) except TypeError: centroid = None try: saved_place, created = GrowingPlace.objects.get_or_create( name=place['Organization'].strip(), city=city, state_province=state, defaults={ 'address_line1': address, 'centroid': centroid, 'country': 'USA', 'mission': place['Mission'].strip(), 'contact': place['Phone'].strip(), 'postal_code': zipcode, 'url': place['Website'].strip(), }, ) saved_place.activities = _get_activities(place) saved_place.save() print 'added', saved_place.name except Exception: print 'Exception while adding "%s"' % place['Organization'] traceback.print_exc() continue # Start moderating GrowingPlace objects again moderation.register(GrowingPlace)
def teardown_moderation(): from moderation import moderation for model in list(moderation._registered_models.keys()): moderation.unregister(model)