def setUp(self): self.factory = RequestFactory() self.instance, system_user = tm.make_instance_and_system_user() self.user = User(username="******") self.user.save_with_user(system_user) self.user.roles.add(tm.make_commander_role(self.instance)) self.species = Species(symbol='CEDR', genus='cedrus', species='atlantica', max_dbh=2000, max_height=100) self.species.save() p1 = Point(-8515941.0, 4953519.0) self.plot = Plot(geom=p1, instance=self.instance, created_by=self.user) self.plot.save_with_user(self.user) self.tree = Tree(plot=self.plot, instance=self.instance, readonly=False, species=self.species, diameter=1630, created_by=self.user) self.tree.save_with_user(self.user)
def test_assign_commander_role_can_delete(self): with self.assertRaises(AuthorizeException): self.tree.delete_with_user(self.outlaw_user) self._change_user_role( self.outlaw_user, make_commander_role(self.tree.get_instance())) self.tree.delete_with_user(self.outlaw_user) self.assertEqual(Tree.objects.count(), 0)
def test_type_and_permissions_are_added(self): role = make_commander_role(self.instance) current_map_feature_types = set(self.instance.map_feature_types) self.instance.add_map_feature_types(['RainGarden']) instance = Instance.objects.get(pk=self.instance.pk) self.assertEqual(set(instance.map_feature_types), current_map_feature_types | {'RainGarden'}) qs = FieldPermission.objects.filter(instance=instance, model_name='RainGarden', field_name='drainage_area', role=role) self.assertEqual(len(qs), 1)
def setUp(self): self.instance = make_instance(name='Test Instance') self.species = Species(instance=self.instance, common_name='Test Common Name', genus='Test Genus', cultivar='Test Cultivar', species='Test Species') self.species.save_base() self.user = make_user(username='******', password='******') self.import_event = ImportEvent(imported_by=self.user) self.import_event.save_base() self.plot = Plot(geom=Point(0, 0), instance=self.instance, address_street="123 Main Street") self.plot.save_base() self.tree = Tree(plot=self.plot, instance=self.instance) self.tree.save_base() self.boundary = make_simple_boundary("Test Boundary") self.role = make_commander_role(self.instance) self.role.name = "Test Role" self.role.save() self.field_permission = FieldPermission( model_name="Tree", field_name="readonly", permission_level=FieldPermission.READ_ONLY, role=self.role, instance=self.instance) self.field_permission.save_base() self.audit = Audit(action=Audit.Type.Update, model="Tree", field="readonly", model_id=1, user=self.user, previous_value=True, current_value=False) self.audit.save_base() self.reputation_metric = ReputationMetric(instance=self.instance, model_name="Tree", action="Test Action") self.reputation_metric.save_base()
def setUp(self): self.instance = make_instance() self.commander = make_commander_user(self.instance, "comm") # Note unicode '⅀' is on purpose self.user1 = User(username='******', password='******', email='*****@*****.**', organization='org111', first_name='therem', last_name='⅀straven') self.user1.save_with_user(self.commander) self.user2 = User(username='******', password='******', email='*****@*****.**', first_name='genly', last_name='ai', allow_email_contact=True) self.user2.save_with_user(self.commander) self.user3 = User(username='******', password='******', email='*****@*****.**') self.user3.save_with_user(self.commander) role = make_commander_role(self.instance) iuser1 = InstanceUser(instance=self.instance, user=self.user1, role=role) iuser1.save_with_user(self.user1) iuser2 = InstanceUser(instance=self.instance, user=self.user2, role=role) iuser2.save_with_user(self.user2) pt = Point(0, 0) self.plot = Plot(geom=pt, readonly=False, instance=self.instance, width=4) self.plot.save_with_user(self.user1) self.tree = Tree(instance=self.instance, plot=self.plot, diameter=3) self.tree.save_with_user(self.user2)
def setUp(self): self.instance = make_instance() self.commander = make_commander_user(self.instance, "comm") # Note unicode '⅀' is on purpose self.user1 = User( username="******", password="******", email="*****@*****.**", organization="karhide", first_name="therem", last_name="⅀straven", ) self.user1.save_with_user(self.commander) self.user2 = User( username="******", password="******", email="*****@*****.**", first_name="genly", last_name="ai", allow_email_contact=True, ) self.user2.save_with_user(self.commander) self.user3 = User(username="******", password="******", email="*****@*****.**") self.user3.save_with_user(self.commander) role = make_commander_role(self.instance) iuser1 = InstanceUser(instance=self.instance, user=self.user1, role=role) iuser1.save_with_user(self.user1) iuser2 = InstanceUser(instance=self.instance, user=self.user2, role=role) iuser2.save_with_user(self.user2) self.plot = Plot(geom=self.instance.center, readonly=False, instance=self.instance, width=4) self.plot.save_with_user(self.user1) self.tree = Tree(instance=self.instance, plot=self.plot, diameter=3) self.tree.save_with_user(self.user2)
def handle(self, *args, **options): if settings.DEBUG: print('In order to run this command you must manually' 'set DEBUG=False in your settings file.') return 1 if options['instance']: instance, system_user = self.setup_env(*args, **options) else: self.stdout.write('Invalid instance provided.') return 1 # look for fixtures of the form '<model>_fixture' that # were passed in as command line args and load them as # python objects json_hashes = { 'species': [], 'user': [], 'plot': [], 'tree': [], 'audit': [] } for model_name in json_hashes: option_name = model_name + '_fixture' try: model_file = open(options[option_name], 'r') json_hashes[model_name] = json.load(model_file) except: print('No valid %s fixture provided ... SKIPPING' % model_name) # iterate over the fixture hashes and save them as database # records. # # a few important things happen here: # # # # for models that server as dependencies for other models, # # their previous ids are stored in a map so that when dependant # # models are made, the correct pk can be retreived. # # # # for models that must be saved with a user, they are either # # saved immediately with a system_user, or an app user is tried # # first, depending on the model. # TODO: don't call this dependency anymore. # It's an idempotency checker too. dependency_id_maps = { 'plot': {}, 'species': {}, 'user': {}, 'tree': {}, 'audit': {}, } for relic in OTM1UserRelic.objects.filter(instance=instance): dependency_id_maps['user'][relic.otm1_id] = relic.otm2_user_id for relic in OTM1ModelRelic.objects.filter(instance=instance): map = dependency_id_maps[relic.otm2_model_name] map[relic.otm1_model_id] = relic.otm2_model_id for model in ('user', 'audit'): if json_hashes[model]: hashes_to_saved_objects(model, json_hashes[model], dependency_id_maps, instance, system_user) commander_role = make_commander_role(instance) for model in ('species', 'plot', 'tree'): if json_hashes[model]: hashes_to_saved_objects(model, json_hashes[model], dependency_id_maps, instance, system_user, commander_role, save_with_user=True)