Beispiel #1
0
def map_feature_detail(request,
                       instance,
                       feature_id,
                       render=False,
                       edit=False):
    feature = get_map_feature_or_404(feature_id, instance)

    ctx_fn = (context_dict_for_plot
              if feature.is_plot else context_dict_for_resource)
    context = ctx_fn(request, feature, edit=edit)
    add_map_info_to_context(context, instance)

    if render:
        if feature.is_plot:
            template = 'treemap/plot_detail.html'
        else:
            app = feature.__module__.split('.')[0]
            try:
                template = '%s/%s_detail.html' % (app, feature.feature_type)
                get_template(template)
            except TemplateDoesNotExist:
                template = 'treemap/resource_detail.html'
        return render_to_response(template, context, RequestContext(request))
    else:
        return context
Beispiel #2
0
def _add_map_feature_photo_helper(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    data = get_image_from_request(request)
    photo = feature.add_photo(data, request.user)
    # We must update a rev so that missing photo searches are up to date
    instance.update_universal_rev()
    return photo
Beispiel #3
0
def delete_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    if not map_feature_is_deletable(request.user.get_instance_user(instance), feature):
        raise ValidationError("map feature not deletable")
    feature.delete_with_user(request.user)
    update_hide_at_zoom_after_delete(feature)
    return {"ok": True}
Beispiel #4
0
def delete_map_feature(request, instance, feature_id, type='Plot'):
    feature = get_map_feature_or_404(feature_id, instance, type)
    try:
        feature.delete_with_user(request.user)
        return {'ok': True}
    except ValidationError as ve:
        return "; ".join(ve.messages)
Beispiel #5
0
def _add_map_feature_photo_helper(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    data = get_image_from_request(request)
    photo = feature.add_photo(data, request.user)
    # We must update a rev so that missing photo searches are up to date
    instance.update_universal_rev()
    return photo
Beispiel #6
0
def delete_map_feature(request, instance, feature_id, type='Plot'):
    feature = get_map_feature_or_404(feature_id, instance, type)
    try:
        feature.delete_with_user(request.user)
        return {'ok': True}
    except ValidationError as ve:
        return "; ".join(ve.messages)
Beispiel #7
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
Beispiel #8
0
def delete_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    if not map_feature_is_deletable(request.user.get_instance_user(instance),
                                    feature):
        raise ValidationError("map feature not deletable")
    feature.delete_with_user(request.user)
    return {'ok': True}
Beispiel #9
0
def map_feature_popup(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    context = context_dict_for_map_feature(request, feature)
    if instance.canopy_enabled:
        context['boundaries_with_canopy'] = \
            _get_boundaries_with_canopy(instance, feature.geom)
    return context
Beispiel #10
0
def map_feature_popup(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    context = context_dict_for_map_feature(request, feature)
    if instance.canopy_enabled:
        context['boundaries_with_canopy'] = \
            _get_boundaries_with_canopy(instance, feature.geom)
    return context
Beispiel #11
0
def map_feature_popup(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    context = {}
    context['features'] = [feature] + list(feature.nearby_map_features())
    if instance.canopy_enabled:
        context['boundaries_with_canopy'] = \
            _get_boundaries_with_canopy(instance, feature.geom)
    return context
Beispiel #12
0
def delete_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    if not map_feature_is_deletable(request.user.get_instance_user(instance),
                                    feature):
        raise ValidationError("map feature not deletable")
    feature.delete_with_user(request.user)
    update_hide_at_zoom_after_delete(feature)
    return {'ok': True}
Beispiel #13
0
def map_feature_popup(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    context = {}
    context['features'] = [feature] + list(feature.nearby_map_features())
    if instance.canopy_enabled:
        context['boundaries_with_canopy'] = \
            _get_boundaries_with_canopy(instance, feature.geom)
    return context
Beispiel #14
0
 def wrapper(request, instance, feature_id, *args, **kwargs):
     error = None
     try:
         fn(request, instance, feature_id, *args, **kwargs)
     except ValidationError as e:
         error = "; ".join(e.messages)
     feature = get_map_feature_or_404(feature_id, instance)
     photos = feature.photos()
     return {"photos": [context_dict_for_photo(request, photo) for photo in photos], "error": error}
Beispiel #15
0
def add_map_feature_photo(request, instance, feature_id):
    error = None
    try:
        _add_map_feature_photo_helper(request, instance, feature_id)
    except ValidationError as e:
        error = '; '.join(e.messages)
    feature = get_map_feature_or_404(feature_id, instance)
    photos = feature.photos()
    return {'photos': map(context_dict_for_photo, photos), 'error': error}
Beispiel #16
0
 def wrapper(request, instance, feature_id, *args, **kwargs):
     error = None
     try:
         fn(request, instance, feature_id, *args, **kwargs)
     except ValidationError as e:
         error = '; '.join(e.messages)
     feature = get_map_feature_or_404(feature_id, instance)
     photos = feature.photos()
     return {'photos': map(context_dict_for_photo, photos), 'error': error}
Beispiel #17
0
 def wrapper(request, instance, feature_id, *args, **kwargs):
     error = None
     try:
         fn(request, instance, feature_id, *args, **kwargs)
     except ValidationError as e:
         error = '; '.join(e.messages)
     feature = get_map_feature_or_404(feature_id, instance)
     photos = feature.photos()
     return {'photos': map(context_dict_for_photo, photos),
             'error': error}
Beispiel #18
0
def add_map_feature_photo(request, instance, feature_id):
    error = None
    try:
        _add_map_feature_photo_helper(request, instance, feature_id)
    except ValidationError as e:
        error = '; '.join(e.messages)
    feature = get_map_feature_or_404(feature_id, instance)
    photos = feature.photos()
    return {'photos': map(context_dict_for_photo, photos),
            'error': error}
Beispiel #19
0
def rotate_map_feature_photo(request, instance, feature_id, photo_id):
    orientation = request.REQUEST.get("degrees", None)
    if orientation not in {"90", "180", "270", "-90", "-180", "-270"}:
        raise ValidationError('"degrees" must be a multiple of 90°')

    degrees = int(orientation)
    feature = get_map_feature_or_404(feature_id, instance)
    mf_photo = get_object_or_404(MapFeaturePhoto, pk=photo_id, map_feature=feature)

    image_data = mf_photo.image.read(settings.MAXIMUM_IMAGE_SIZE)
    mf_photo.set_image(image_data, degrees_to_rotate=degrees)
    mf_photo.save_with_user(request.user)
Beispiel #20
0
def _map_feature_detail_context(request, instance, feature_id, edit=False):
    feature = get_map_feature_or_404(feature_id, instance)
    ctx_fn = (context_dict_for_plot if feature.is_plot
              else context_dict_for_resource)
    context = ctx_fn(request, feature, edit=edit)

    if feature.is_plot:
        partial = 'treemap/partials/plot_detail.html'
    else:
        app = feature.__module__.split('.')[0]
        partial = '%s/%s_detail.html' % (app, feature.feature_type)

    return context, partial
Beispiel #21
0
def _map_feature_detail_context(request, instance, feature_id, edit=False):
    feature = get_map_feature_or_404(feature_id, instance)
    ctx_fn = (context_dict_for_plot
              if feature.is_plot else context_dict_for_resource)
    context = ctx_fn(request, feature, edit=edit)

    if feature.is_plot:
        partial = 'treemap/partials/plot_detail.html'
        _add_plot_field_groups(context, instance)
    else:
        app = feature.__module__.split('.')[0]
        partial = '%s/%s_detail.html' % (app, feature.feature_type)

    return context, partial
Beispiel #22
0
def rotate_map_feature_photo(request, instance, feature_id, photo_id):
    orientation = request.REQUEST.get('degrees', None)
    if orientation not in {'90', '180', '270', '-90', '-180', '-270'}:
        raise ValidationError('"degrees" must be a multiple of 90°')

    degrees = int(orientation)
    feature = get_map_feature_or_404(feature_id, instance)
    mf_photo = get_object_or_404(MapFeaturePhoto,
                                 pk=photo_id,
                                 map_feature=feature)

    image_data = mf_photo.image.read(settings.MAXIMUM_IMAGE_SIZE)
    mf_photo.set_image(image_data, degrees_to_rotate=degrees)
    mf_photo.save_with_user(request.user)
Beispiel #23
0
def map_feature_detail(request, instance, feature_id, render=False):
    feature = get_map_feature_or_404(feature_id, instance)

    ctx_fn = context_dict_for_plot if feature.is_plot else context_dict_for_resource
    context = ctx_fn(request, feature)

    if render:
        if feature.is_plot:
            template = "treemap/plot_detail.html"
        else:
            template = "map_features/%s_detail.html" % feature.feature_type
        return render_to_response(template, context, RequestContext(request))
    else:
        return context
Beispiel #24
0
def map_feature_detail(request, instance, feature_id, render=False):
    feature = get_map_feature_or_404(feature_id, instance)

    ctx_fn = (context_dict_for_plot
              if feature.is_plot else context_dict_for_resource)
    context = ctx_fn(request, feature)

    if render:
        if feature.is_plot:
            template = 'treemap/plot_detail.html'
        else:
            template = 'map_features/%s_detail.html' % feature.feature_type
        return render_to_response(template, context, RequestContext(request))
    else:
        return context
Beispiel #25
0
def map_feature_hash(request, instance, feature_id, edit=False, tree_id=None):
    """
    Compute a unique hash for a given plot or tree

    tree_id is ignored since trees are included as a
    subset of the plot's hash. It is present here because
    this function is wrapped around views that can take
    tree_id as an argument
    """
    feature = get_map_feature_or_404(feature_id, instance)

    if request.user:
        pk = request.user.pk or ''

    return hashlib.md5(feature.hash + ':' + str(pk)).hexdigest()
Beispiel #26
0
def map_feature_hash(request, instance, feature_id, edit=False, tree_id=None):
    """
    Compute a unique hash for a given plot or tree

    tree_id is ignored since trees are included as a
    subset of the plot's hash. It is present here because
    this function is wrapped around views that can take
    tree_id as an argument
    """
    feature = get_map_feature_or_404(feature_id, instance)

    if request.user:
        pk = request.user.pk or ''

    return hashlib.md5(feature.hash + ':' + str(pk)).hexdigest()
Beispiel #27
0
def map_feature_detail(request, instance, feature_id, render=False, edit=False):
    feature = get_map_feature_or_404(feature_id, instance)

    ctx_fn = context_dict_for_plot if feature.is_plot else context_dict_for_resource
    context = ctx_fn(request, feature, edit=edit)
    add_map_info_to_context(context, instance)

    if render:
        if feature.is_plot:
            template = "treemap/plot_detail.html"
        else:
            app = feature.__module__.split(".")[0]
            template = "%s/%s_detail.html" % (app, feature.feature_type)
        return render_to_response(template, context, RequestContext(request))
    else:
        return context
Beispiel #28
0
def map_feature_detail(request, instance, feature_id,
                       render=False, edit=False):
    feature = get_map_feature_or_404(feature_id, instance)

    ctx_fn = (context_dict_for_plot if feature.is_plot
              else context_dict_for_resource)
    context = ctx_fn(request, feature, edit=edit)
    add_map_info_to_context(context, instance)

    if render:
        if feature.is_plot:
            template = 'treemap/plot_detail.html'
        else:
            app = feature.__module__.split('.')[0]
            try:
                template = '%s/%s_detail.html' % (app, feature.feature_type)
                get_template(template)
            except TemplateDoesNotExist:
                template = 'treemap/resource_detail.html'
        return render_to_response(template, context,
                                  RequestContext(request))
    else:
        return context
Beispiel #29
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
Beispiel #30
0
def favorite_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    Favorite.objects.get_or_create(user=request.user, map_feature=feature)

    return {'success': True}
Beispiel #31
0
def delete_photo(request, instance, feature_id, photo_id):
    feature = get_map_feature_or_404(feature_id, instance)
    photo_class = TreePhoto if feature.is_plot else MapFeaturePhoto
    mf_photo = get_object_or_404(photo_class, pk=photo_id, map_feature=feature)
    mf_photo.delete_with_user(request.user)  # may raise AuthorizeException
Beispiel #32
0
def delete_photo(request, instance, feature_id, photo_id):
    feature = get_map_feature_or_404(feature_id, instance)
    photo_class = TreePhoto if feature.is_plot else MapFeaturePhoto
    mf_photo = get_object_or_404(photo_class, pk=photo_id, map_feature=feature)
    mf_photo.delete_with_user(request.user)  # may raise AuthorizeException
Beispiel #33
0
def map_feature_popup(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    context = context_dict_for_map_feature(request, feature)
    return context
Beispiel #34
0
def map_feature_popup(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    context = context_dict_for_map_feature(request, feature)
    return context
Beispiel #35
0
def update_map_feature_detail(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    return _request_to_update_map_feature(request, feature)
Beispiel #36
0
def delete_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    feature.delete_with_user(request.user)  # may raise AuthorizeException
    update_hide_at_zoom_after_delete(feature)
    return {'ok': True}
Beispiel #37
0
def map_feature_photo_detail(request, instance, feature_id, photo_id):
    feature = get_map_feature_or_404(feature_id, instance)
    photo = get_object_or_404(MapFeaturePhoto,
                              pk=photo_id,
                              map_feature=feature)
    return {'photo': context_dict_for_photo(request, photo)}
Beispiel #38
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)
Beispiel #39
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)
Beispiel #40
0
def map_feature_photo_detail(request, instance, feature_id, photo_id):
    feature = get_map_feature_or_404(feature_id, instance)
    photo = get_object_or_404(MapFeaturePhoto, pk=photo_id,
                              map_feature=feature)
    return {'photo': context_dict_for_photo(request, photo)}
Beispiel #41
0
def _add_map_feature_photo_helper(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    data = get_image_from_request(request)
    return feature.add_photo(data, request.user)
Beispiel #42
0
def unfavorite_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    Favorite.objects.filter(user=request.user, map_feature=feature).delete()

    return {'success': True}
Beispiel #43
0
def update_map_feature_detail(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    return _request_to_update_map_feature(request, feature)
Beispiel #44
0
def unfavorite_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    Favorite.objects.filter(user=request.user, map_feature=feature).delete()

    return {'success': True}
Beispiel #45
0
def delete_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    feature.delete_with_user(request.user)  # may raise AuthorizeException
    update_hide_at_zoom_after_delete(feature)
    return {'ok': True}
Beispiel #46
0
def _add_map_feature_photo_helper(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    data = get_image_from_request(request)
    return feature.add_photo(data, request.user)
Beispiel #47
0
def favorite_map_feature(request, instance, feature_id):
    feature = get_map_feature_or_404(feature_id, instance)
    Favorite.objects.get_or_create(user=request.user, map_feature=feature)

    return {'success': True}