def test_record_is_created_when_nullables_are_still_pending(self): new_plot = Plot(geom=self.p1, instance=self.instance) new_plot.save_with_user(self.pending_user) new_tree = Tree(plot=new_plot, instance=self.instance, diameter=10, height=10, readonly=False) new_tree.save_with_user(self.pending_user) approve_or_reject_audits_and_apply( new_plot.audits(), self.commander_user, True) insert_audit = Audit.objects.filter(model='Tree')\ .get(field='id') field_audits = Audit.objects.filter(model='Tree')\ .filter(field__in=['readonly', 'diameter', 'plot']) for audit in field_audits: approve_or_reject_audit_and_apply( audit, self.commander_user, approved=True) approve_or_reject_audit_and_apply(insert_audit, self.commander_user, True) real_tree = Tree.objects.get(pk=new_tree.pk) self.assertEqual(real_tree.plot_id, new_plot.pk) self.assertEqual(real_tree.diameter, 10) self.assertEqual(real_tree.height, None) self.assertNotEqual(real_tree.readonly, True)
def test_approve_insert_without_required_raises_integrity_error(self): new_plot = Plot(geom=self.p1, instance=self.instance) new_plot.save_with_user(self.pending_user) new_tree = Tree(plot=new_plot, instance=self.instance, diameter=10, height=10, readonly=False) new_tree.save_with_user(self.pending_user) approve_or_reject_audits_and_apply(new_plot.audits(), self.commander_user, True) diameter_audit = Audit.objects.get(model='Tree', field='diameter', model_id=new_tree.pk) insert_audit = Audit.objects.get(model='Tree', model_id=new_tree.pk, field='id') approve_or_reject_audit_and_apply(diameter_audit, self.commander_user, approved=True) self.assertRaises(IntegrityError, approve_or_reject_audit_and_apply, insert_audit, self.commander_user, True)
def approve_or_reject_photo( request, instance, feature_id, photo_id, action): approved = action == 'approve' if approved: msg = trans('Approved') else: msg = trans('Rejected') resp = HttpResponse(msg) try: photo = (MapFeaturePhoto.objects .select_related('treephoto') .get(pk=photo_id)) try: photo = photo.treephoto except MapFeaturePhoto.DoesNotExist: pass # There is no tree photo, so use the superclass except MapFeaturePhoto.DoesNotExist: # This may be a pending tree. Let's see if there # are pending audits pending_audits = Audit.objects\ .filter(instance=instance)\ .filter(model__in=['TreePhoto', 'MapFeaturePhoto'])\ .filter(model_id=photo_id)\ .filter(requires_auth=True) if len(pending_audits) > 0: # Process as pending and quit approve_or_reject_audits_and_apply( pending_audits, request.user, approved) return resp else: # Error - no pending or regular raise Http404('Photo Not Found') # Handle the id audit first all_audits = [] for audit in photo.audits(): if audit.field == 'id': all_audits = [audit] + all_audits else: all_audits.append(audit) for audit in all_audits: approve_or_reject_existing_edit( audit, request.user, approved) return resp
def approve_or_reject_photo(request, instance, feature_id, photo_id, action): approved = action == 'approve' if approved: msg = trans('Approved') else: msg = trans('Rejected') resp = HttpResponse(msg) try: photo = (MapFeaturePhoto.objects.select_related('treephoto').get( pk=photo_id)) try: photo = photo.treephoto except MapFeaturePhoto.DoesNotExist: pass # There is no tree photo, so use the superclass except MapFeaturePhoto.DoesNotExist: # This may be a pending tree. Let's see if there # are pending audits pending_audits = Audit.objects\ .filter(instance=instance)\ .filter(model__in=['TreePhoto', 'MapFeaturePhoto'])\ .filter(model_id=photo_id)\ .filter(requires_auth=True) if len(pending_audits) > 0: # Process as pending and quit approve_or_reject_audits_and_apply(pending_audits, request.user, approved) return resp else: # Error - no pending or regular raise Http404('Photo Not Found') # Handle the id audit first all_audits = [] for audit in photo.audits(): if audit.field == 'id': all_audits = [audit] + all_audits else: all_audits.append(audit) for audit in all_audits: approve_or_reject_existing_edit(audit, request.user, approved) return resp
def approve_or_reject_photo( request, instance, feature_id, tree_id, photo_id, action): approved = action == 'approve' if approved: msg = trans('Approved') else: msg = trans('Rejected') resp = HttpResponse(msg) tree = get_object_or_404( Tree, plot_id=feature_id, instance=instance, pk=tree_id) try: photo = TreePhoto.objects.get(pk=photo_id, tree=tree) except TreePhoto.DoesNotExist: # This may be a pending tree. Let's see if there # are pending audits pending_audits = Audit.objects\ .filter(instance=instance)\ .filter(model='TreePhoto')\ .filter(model_id=photo_id)\ .filter(requires_auth=True) if len(pending_audits) > 0: # Process as pending and quit approve_or_reject_audits_and_apply( pending_audits, request.user, approved) return resp else: # Error - no pending or regular raise Http404('Tree Photo Not Found') # Handle the id audit first all_audits = [] for audit in photo.audits(): if audit.field == 'id': all_audits = [audit] + all_audits else: all_audits.append(audit) for audit in all_audits: approve_or_reject_existing_edit( audit, request.user, approved) return resp
def test_insert_writes_when_approved(self): new_plot = Plot(geom=self.p1, instance=self.instance) new_plot.save_with_user(self.pending_user) new_tree = Tree(plot=new_plot, instance=self.instance) new_tree.save_with_user(self.pending_user) self.assertEquals(Plot.objects.count(), 0) self.assertEquals(Tree.objects.count(), 0) approve_or_reject_audits_and_apply( list(new_tree.audits()) + list(new_plot.audits()), self.commander_user, True) self.assertEqual(Plot.objects.all().count(), 1) self.assertEqual(Tree.objects.all().count(), 1)
def test_create_and_apply_pending_collection(self): pending = self.plot.audits().filter(requires_auth=True) self.assertEqual(len(pending), 0) role = self.commander_user.get_role(self.instance) fp, _ = FieldPermission.objects.get_or_create( model_name='Plot', field_name='udf:Test collection', permission_level=FieldPermission.WRITE_WITH_AUDIT, role=role, instance=self.instance) self.plot.udfs['Test collection'] = [{ 'a choice': 'a', 'a string': 's' }] self.plot.save_with_user(self.commander_user) reloaded_plot = Plot.objects.get(pk=self.plot.pk) self.assertEqual(reloaded_plot.udfs['Test collection'], []) pending = self.plot.audits().filter(requires_auth=True) # Expecting 'model_id', 'id', 'field def id' # and two udf fields ('a string' and 'a choice') self.assertEqual(len(pending), 5) fp.permission_level = FieldPermission.WRITE_DIRECTLY fp.save() approve_or_reject_audits_and_apply(pending, self.commander_user, True) reloaded_plot = Plot.objects.get(pk=self.plot.pk) col = reloaded_plot.udfs['Test collection'] self.assertEqual(len(col), 1) self.assertEqual(col[0]['a choice'], 'a') self.assertEqual(col[0]['a string'], 's')
def test_create_and_apply_pending_collection(self): pending = self.plot.audits().filter(requires_auth=True) self.assertEqual(len(pending), 0) role = self.commander_user.get_role(self.instance) fp, _ = FieldPermission.objects.get_or_create( model_name='Plot', field_name='udf:Test collection', permission_level=FieldPermission.WRITE_WITH_AUDIT, role=role, instance=self.instance) self.plot.udfs['Test collection'] = [ {'a choice': 'a', 'a string': 's'}] self.plot.save_with_user(self.commander_user) reloaded_plot = Plot.objects.get(pk=self.plot.pk) self.assertEqual( reloaded_plot.udfs['Test collection'], []) pending = self.plot.audits().filter(requires_auth=True) # Expecting 'model_id', 'id', 'field def id' # and two udf fields ('a string' and 'a choice') self.assertEqual(len(pending), 5) fp.permission_level = FieldPermission.WRITE_DIRECTLY fp.save() approve_or_reject_audits_and_apply(pending, self.commander_user, True) reloaded_plot = Plot.objects.get(pk=self.plot.pk) col = reloaded_plot.udfs['Test collection'] self.assertEqual(len(col), 1) self.assertEqual(col[0]['a choice'], 'a') self.assertEqual(col[0]['a string'], 's')
def test_lots_of_trees_and_plots(self): """ Make 3 plots: 2 pending and 1 approved Make 4 trees: 1 on each pending plot, 2 on approved plot Approve one pending plot. Approve all trees. The one on the (Still) pending plot should fail. all else should pass. """ p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(2, 2) plot1 = Plot(geom=p1, instance=self.instance) plot2 = Plot(geom=p2, instance=self.instance) plot3 = Plot(geom=p3, instance=self.instance) plot1.save_with_user(self.commander_user) plot2.save_with_user(self.pending_user) plot3.save_with_user(self.pending_user) tree1 = Tree(plot=plot1, instance=self.instance) tree1.save_with_user(self.pending_user) tree2 = Tree(plot=plot1, instance=self.instance) tree2.save_with_user(self.pending_user) tree3 = Tree(plot=plot2, instance=self.instance) tree3.save_with_user(self.pending_user) tree4 = Tree(plot=plot3, instance=self.instance) tree4.save_with_user(self.pending_user) approve_or_reject_audits_and_apply( plot2.audits(), self.commander_user, True) approve_or_reject_audits_and_apply( tree1.audits(), self.commander_user, True) approve_or_reject_audits_and_apply( tree2.audits(), self.commander_user, True) approve_or_reject_audits_and_apply( tree3.audits(), self.commander_user, True) self.assertRaises(ObjectDoesNotExist, Plot.objects.get, pk=plot3.pk) self.assertRaises(ObjectDoesNotExist, approve_or_reject_audits_and_apply, tree4.audits(), self.commander_user, True)
def test_approve_insert_without_required_raises_integrity_error(self): new_plot = Plot(geom=self.p1, instance=self.instance) new_plot.save_with_user(self.pending_user) new_tree = Tree(plot=new_plot, instance=self.instance, diameter=10, height=10, readonly=False) new_tree.save_with_user(self.pending_user) approve_or_reject_audits_and_apply( new_plot.audits(), self.commander_user, True) diameter_audit = Audit.objects.get(model='Tree', field='diameter', model_id=new_tree.pk) insert_audit = Audit.objects.get(model='Tree', model_id=new_tree.pk, field='id') approve_or_reject_audit_and_apply( diameter_audit, self.commander_user, approved=True) self.assertRaises(IntegrityError, approve_or_reject_audit_and_apply, insert_audit, self.commander_user, True)
def test_lots_of_trees_and_plots(self): """ Make 3 plots: 2 pending and 1 approved Make 4 trees: 1 on each pending plot, 2 on approved plot Approve one pending plot. Approve all trees. The one on the (Still) pending plot should fail. all else should pass. """ plot1 = Plot(geom=self.instance.center, instance=self.instance) plot2 = Plot(geom=self.instance.center, instance=self.instance) plot3 = Plot(geom=self.instance.center, instance=self.instance) plot1.save_with_user(self.commander_user) plot2.save_with_user(self.pending_user) plot3.save_with_user(self.pending_user) tree1 = Tree(plot=plot1, instance=self.instance) tree1.save_with_user(self.pending_user) tree2 = Tree(plot=plot1, instance=self.instance) tree2.save_with_user(self.pending_user) tree3 = Tree(plot=plot2, instance=self.instance) tree3.save_with_user(self.pending_user) tree4 = Tree(plot=plot3, instance=self.instance) tree4.save_with_user(self.pending_user) approve_or_reject_audits_and_apply( plot2.audits(), self.commander_user, True) approve_or_reject_audits_and_apply( tree1.audits(), self.commander_user, True) approve_or_reject_audits_and_apply( tree2.audits(), self.commander_user, True) approve_or_reject_audits_and_apply( tree3.audits(), self.commander_user, True) self.assertRaises(ObjectDoesNotExist, Plot.objects.get, pk=plot3.pk) self.assertRaises(ObjectDoesNotExist, approve_or_reject_audits_and_apply, tree4.audits(), self.commander_user, True)