def test_treephoto_overrides_tree_and_plot(self):
        tree = Tree(diameter=10, plot=self.plot, instance=self.instance)
        tree.save_with_user(self.user)
        tree.add_photo(self.image, self.other)

        self.clear_and_set_and_reload()
        self.assertEqual(self.plot.updated_by_id, self.other.pk)
    def test_treephoto_overrides_tree_and_plot(self):
        tree = Tree(diameter=10, plot=self.plot, instance=self.instance)
        tree.save_with_user(self.user)
        tree.add_photo(self.image, self.other)

        self.clear_and_set_and_reload()
        self.assertEqual(self.plot.updated_by_id, self.other.pk)
Example #3
0
def add_tree_photo_helper(request, instance, feature_id, tree_id=None):
    plot = get_map_feature_or_404(feature_id, instance, 'Plot')
    tree_ids = [t.pk for t in plot.tree_set.all()]

    if tree_id and int(tree_id) in tree_ids:
        tree = Tree.objects.get(pk=tree_id)
    elif tree_id is None:
        # See if a tree already exists on this plot
        tree = plot.current_tree()

        if tree is None:
            # A tree doesn't exist, create a new tree create a
            # new tree, and attach it to this plot
            tree = Tree(plot=plot, instance=instance)

            # TODO: it is possible that a user has the ability to
            # 'create tree photos' but not trees. In this case we
            # raise an authorization exception here.
            # It is, however, possible to have both a pending
            # tree and a pending tree photo
            # This will be added later, when auth/admin work
            # correctly with this system
            tree.save_with_user(request.user)

    else:
        # Tree id is invalid or not in this plot
        raise Http404('Tree id %s not found on plot %s'
                      % (tree_id, feature_id))

    #TODO: Auth Error
    data = get_image_from_request(request)
    treephoto = tree.add_photo(data, request.user)

    return treephoto, tree
    def test_treephoto_overrides_tree_and_plot_updated(self):
        tree = Tree(diameter=10, plot=self.plot, instance=self.instance)
        tree.save_with_user(self.user)
        tree.add_photo(self.image, self.user)

        tree_audit = self.max_audit_for_model_type('Tree')
        treephoto_audit = self.max_audit_for_model_type(
            ['MapFeaturePhoto', 'TreePhoto'])
        plot_audit = self.max_audit_for_model_type('Plot')
        # Backdate the audits so photo it is definitely the newsest
        plot_audit.created = treephoto_audit.created - timedelta(days=2)
        plot_audit.save()
        tree_audit.created = treephoto_audit.created - timedelta(days=1)
        tree_audit.save()

        self.clear_and_set_and_reload()
        self.assertEqual(self.plot.updated_at, treephoto_audit.created)
Example #5
0
    def test_treephoto_overrides_tree_and_plot_updated(self):
        tree = Tree(diameter=10, plot=self.plot, instance=self.instance)
        tree.save_with_user(self.user)
        tree.add_photo(self.image, self.user)

        tree_audit = self.max_audit_for_model_type('Tree')
        treephoto_audit = self.max_audit_for_model_type(['MapFeaturePhoto',
                                                         'TreePhoto'])
        plot_audit = self.max_audit_for_model_type('Plot')
        # Backdate the audits so photo it is definitely the newsest
        plot_audit.created = treephoto_audit.created - timedelta(days=2)
        plot_audit.save()
        tree_audit.created = treephoto_audit.created - timedelta(days=1)
        tree_audit.save()

        self.clear_and_set_and_reload()
        self.assertEqual(self.plot.updated_at, treephoto_audit.created)
Example #6
0
    def test_user_cannot_delete_tree_photo(self):
        commander = make_commander_user(self.instance)
        plot = Plot(instance=self.instance, geom=self.p)
        plot.save_with_user(commander)
        tree = Tree(plot=plot, instance=self.instance)
        tree.save_with_user(commander)
        image = self.load_resource('tree1.gif')

        photo = tree.add_photo(image, commander)

        user_no = make_user(instance=self.instance,
                            make_role=lambda inst: self.role_no)
        self.assertFalse(photo.user_can_delete(user_no))
Example #7
0
    def test_user_cannot_delete_tree_photo(self):
        commander = make_commander_user(self.instance)
        plot = Plot(instance=self.instance, geom=self.p)
        plot.save_with_user(commander)
        tree = Tree(plot=plot, instance=self.instance)
        tree.save_with_user(commander)
        image = self.load_resource('tree1.gif')

        photo = tree.add_photo(image, commander)

        user_no = make_user(instance=self.instance,
                            make_role=lambda inst: self.role_no)
        self.assertFalse(photo.user_can_delete(user_no))
Example #8
0
    def test_user_can_delete_tree_photo(self):
        commander = make_commander_user(self.instance)
        plot = Plot(instance=self.instance, geom=self.p)
        plot.save_with_user(commander)
        tree = Tree(plot=plot, instance=self.instance)
        tree.save_with_user(commander)
        image = self.load_resource('tree1.gif')

        photo = tree.add_photo(image, commander)

        self._add_builtin_permission(self.role_yes, TreePhoto,
                                     'delete_treephoto')
        user_yes = make_user(instance=self.instance,
                             make_role=lambda inst: self.role_yes)
        self.assertTrue(photo.user_can_delete(user_yes))
Example #9
0
    def test_user_can_delete_tree_photo(self):
        commander = make_commander_user(self.instance)
        plot = Plot(instance=self.instance, geom=self.p)
        plot.save_with_user(commander)
        tree = Tree(plot=plot, instance=self.instance)
        tree.save_with_user(commander)
        image = self.load_resource('tree1.gif')

        photo = tree.add_photo(image, commander)

        self._add_builtin_permission(self.role_yes, TreePhoto,
                                     'delete_treephoto')
        user_yes = make_user(instance=self.instance,
                             make_role=lambda inst: self.role_yes)
        self.assertTrue(photo.user_can_delete(user_yes))
Example #10
0
def add_tree_photo(request, instance, plot_id, tree_id=None):
    plot = get_object_or_404(Plot, pk=plot_id, instance=instance)
    tree_ids = [t.pk for t in plot.tree_set.all()]

    if tree_id and int(tree_id) in tree_ids:
        tree = Tree.objects.get(pk=tree_id)
    elif tree_id is None:
        # See if a tree already exists on this plot
        tree = plot.current_tree()

        if tree is None:
            # A tree doesn't exist, create a new tree create a
            # new tree, and attach it to this plot
            tree = Tree(plot=plot, instance=instance)

            # TODO: it is possible that a user has the ability to
            # 'create tree photos' but not trees. In this case we
            # raise an authorization exception here.
            # It is, however, possible to have both a pending
            # tree and a pending tree photo
            # This will be added later, when auth/admin work
            # correctly with this system
            tree.save_with_user(request.user)

    else:
        # Tree id is invalid or not in this plot
        raise Http404("Tree id %s not found on plot %s" % (tree_id, plot_id))

    # TODO: Validation Error
    # TODO: Auth Error
    if "file" in request.FILES:
        # TODO: Check size before reading
        data = request.FILES["file"].file
    else:
        data = request.body

    photo = tree.add_photo(data, request.user)

    return photo
Example #11
0
def add_tree_photo_helper(request, instance, feature_id, tree_id=None):
    plot = get_map_feature_or_404(feature_id, instance, 'Plot')
    tree_ids = [t.pk for t in plot.tree_set.all()]

    if tree_id and int(tree_id) in tree_ids:
        tree = Tree.objects.get(pk=tree_id)
    elif tree_id is None:
        # See if a tree already exists on this plot
        tree = plot.current_tree()

        if tree is None:
            # A tree doesn't exist, create a new tree create a
            # new tree, and attach it to this plot
            tree = Tree(plot=plot, instance=instance)

            # TODO: it is possible that a user has the ability to
            # 'create tree photos' but not trees. In this case we
            # raise an authorization exception here.
            # It is, however, possible to have both a pending
            # tree and a pending tree photo
            # This will be added later, when auth/admin work
            # correctly with this system
            tree.save_with_user(request.user)

    else:
        # Tree id is invalid or not in this plot
        raise Http404('Tree id %s not found on plot %s' %
                      (tree_id, feature_id))

    #TODO: Auth Error
    data = get_image_from_request(request)
    treephoto = tree.add_photo(data, request.user)

    # We must update a rev so that missing photo searches are up to date
    instance.update_universal_rev()

    return treephoto, tree