Example #1
0
def edits(request, instance, user_id):
    if (int(user_id) != request.user.pk):
        return create_401unauthorized()

    user = request.user

    result_offset = int(request.REQUEST.get("offset", 0))
    num_results = min(int(request.REQUEST.get("length", 15)), 15)

    audits = Audit.objects.filter(instance=instance)\
                          .filter(user=user)\
                          .filter(model_in=['Tree', 'Plot'])\
                          .order_by('-created', 'id')

    audits = audits[result_offset:(result_offset+num_results)]

    keys = []
    for audit in audits:
        d = {}
        plot = extract_plot_from_audit(audit)
        d["plot_id"] = plot.pk

        if plot.pk:
            d["plot"] = context_dict_for_plot(request, plot)

        d["id"] = audit.pk
        d["name"] = audit.display_action
        d["created"] = datetime_to_iso_string(audit.created)
        d["value"] = audit.current_value

        keys.append(d)

    return keys
Example #2
0
def edits(request, instance, user_id):
    if (int(user_id) != request.user.pk):
        return create_401unauthorized()

    user = request.user

    result_offset = int(request.GET.get("offset", 0))
    num_results = min(int(request.GET.get("length", 15)), 15)

    audits = Audit.objects.filter(instance=instance)\
                          .filter(user=user)\
                          .filter(model_in=['Tree', 'Plot'])\
                          .order_by('-created', 'id')

    audits = audits[result_offset:(result_offset + num_results)]

    keys = []
    for audit in audits:
        d = {}
        plot = extract_plot_from_audit(audit)
        d["plot_id"] = plot.pk

        if plot.pk:
            d["plot"] = context_dict_for_plot(request, plot)

        d["id"] = audit.pk
        d["name"] = audit.display_action
        d["created"] = datetime_to_iso_string(audit.created)
        d["value"] = audit.current_value

        keys.append(d)

    return keys
Example #3
0
def update_or_create_plot(request, instance, plot_id=None):
    # The API communicates via nested dictionaries but
    # our internal functions prefer dotted pairs (which
    # is what inline edit form users)
    request_dict = json.loads(request.body)

    data = {}

    for model in ["plot", "tree"]:
        if model in request_dict:
            for key, val in request_dict[model].iteritems():
                data["%s.%s" % (model, key)] = val

    # We explicitly disallow setting a plot's tree id.
    # We ignore plot's updated at and by because
    # auditing sets them automatically.
    keys = ["tree.plot",
            "tree.udfs",
            "tree.instance",
            "plot.updated_at",
            "plot.updated_by",
            "plot.instance",
            "plot.udfs"]

    for key in keys:
        if key in data:
            del data[key]

    if "tree.species" in data:
        sp = data["tree.species"]
        if sp and "id" in sp:
            data["tree.species"] = sp['id']
        else:
            data["tree.species"] = None

    if plot_id:
        plot = get_object_or_404(Plot, pk=plot_id, instance=instance)
    else:
        plot = Plot(instance=instance)

    plot, __ = update_map_feature(data, request.user, plot)

    context_dict = context_dict_for_plot(request, plot)

    # Add geo rev hash so clients will know if a tile refresh is required
    context_dict["geoRevHash"] = plot.instance.geo_rev_hash
    context_dict["universalRevHash"] = plot.instance.universal_rev_hash

    return context_dict
Example #4
0
def remove_current_tree_from_plot(request, instance, plot_id):
    plot = get_object_or_404(Plot, pk=plot_id, instance=instance)
    tree = plot.current_tree()

    if tree:
        try:
            tree.delete_with_user(request.user)
            updated_plot = Plot.objects.get(pk=plot_id)
            return context_dict_for_plot(request, updated_plot)
        except:
            raise PermissionDenied('%s does not have permission to the '
                                   'current tree from plot %s' %
                                   (request.user.username, plot_id))
    else:
        raise HttpResponseBadRequest("Plot %s does not have a current tree" %
                                     plot_id)
Example #5
0
def _approve_or_reject_pending_edit(request, instance, user, pending_edit_id,
                                    approve):
    audit = Audit.objects.get(pk=pending_edit_id, instance=instance)
    approve_or_reject_audit_and_apply(audit, user, approve)

    updated_plot = extract_plot_from_audit(audit)

    # Reject remaining audits on specified field if
    # we approved this audit
    # TODO: Should this logic be moved to app_or_rej_audit_and_ap?
    if approve:
        for pending_audit in updated_plot.get_active_pending_audits()\
                                         .filter(field=audit.field):
            approve_or_reject_audit_and_apply(pending_audit, user, False)

    return context_dict_for_plot(request, updated_plot)
Example #6
0
def _approve_or_reject_pending_edit(
        request, instance, user, pending_edit_id, approve):
    audit = Audit.objects.get(pk=pending_edit_id, instance=instance)
    approve_or_reject_audit_and_apply(audit, user, approve)

    updated_plot = extract_plot_from_audit(audit)

    # Reject remaining audits on specified field if
    # we approved this audit
    # TODO: Should this logic be moved to app_or_rej_audit_and_ap?
    if approve:
        for pending_audit in updated_plot.get_active_pending_audits()\
                                         .filter(field=audit.field):
            approve_or_reject_audit_and_apply(pending_audit, user, False)

    return context_dict_for_plot(request, updated_plot)
Example #7
0
def remove_current_tree_from_plot(request, instance, plot_id):
    plot = get_object_or_404(Plot, pk=plot_id, instance=instance)
    tree = plot.current_tree()

    if tree:
        try:
            tree.delete_with_user(request.user)
            updated_plot = Plot.objects.get(pk=plot_id)
            return context_dict_for_plot(request, updated_plot)
        except:
            raise PermissionDenied(
                '%s does not have permission to the '
                'current tree from plot %s' %
                (request.user.username, plot_id))
    else:
        raise HttpResponseBadRequest(
            "Plot %s does not have a current tree" % plot_id)
Example #8
0
def update_or_create_plot(request, instance, plot_id=None):
    # The API communicates via nested dictionaries but
    # our internal functions prefer dotted pairs (which
    # is what inline edit form users)
    request_dict = json.loads(request.body)

    data = {}

    for model in ["plot", "tree"]:
        if model in request_dict:
            for key, val in request_dict[model].iteritems():
                data["%s.%s" % (model, key)] = val

    # We explicitly disallow setting a plot's tree id.
    # We ignore plot's updated at and by because
    # auditing sets them automatically.
    keys = [
        "tree.plot", "tree.udfs", "tree.instance", "plot.updated_at",
        "plot.updated_by", "plot.instance", "plot.udfs"
    ]

    for key in keys:
        if key in data:
            del data[key]

    if "tree.species" in data:
        sp = data["tree.species"]
        if sp and "id" in sp:
            data["tree.species"] = sp['id']
        else:
            data["tree.species"] = None

    if plot_id:
        plot = get_object_or_404(Plot, pk=plot_id, instance=instance)
    else:
        plot = Plot(instance=instance)

    plot, __ = update_map_feature(data, request.user, plot)

    context_dict = context_dict_for_plot(request, plot)

    # Add geo rev hash so clients will know if a tile refresh is required
    context_dict["geoRevHash"] = plot.instance.geo_rev_hash
    context_dict["universalRevHash"] = plot.instance.universal_rev_hash

    return context_dict
Example #9
0
def get_plot(request, instance, plot_id):
    return context_dict_for_plot(request, Plot.objects.get(pk=plot_id))
Example #10
0
 def ctxt_for_plot(plot):
     return context_dict_for_plot(request, plot)
Example #11
0
def get_plot(request, instance, plot_id):
    return context_dict_for_plot(request, Plot.objects.get(pk=plot_id))
Example #12
0
 def ctxt_for_plot(plot):
     return context_dict_for_plot(request, plot)
Example #13
0
def plot_detail(request, instance, feature_id, edit=False, tree_id=None):
    feature = get_map_feature_or_404(feature_id, instance, 'Plot')
    return context_dict_for_plot(request, feature, edit=edit, tree_id=tree_id)
Example #14
0
def plot_detail(request, instance, feature_id, edit=False, tree_id=None):
    feature = get_map_feature_or_404(feature_id, instance, 'Plot')
    return context_dict_for_plot(request, feature, edit=edit, tree_id=tree_id)