Ejemplo n.º 1
0
def update_user(request, user):
    new_values = json_from_request(request) or {}
    for key in new_values:
        try:
            model, field = key.split(".", 1)
            if model != "user":
                return bad_request_json_response('All fields should be prefixed with "user."')
            if field not in USER_PROFILE_FIELDS:
                return bad_request_json_response(field + " is not an updatable field")
        except ValueError:
            return bad_request_json_response('All fields should be prefixed with "user."')
        setattr(user, field, new_values[key])
    try:
        user.save()
        return {"ok": True}
    except ValidationError, ve:
        return bad_request_json_response(validation_error_dict=package_validation_errors("user", ve))
Ejemplo n.º 2
0
def update_user(request, user):
    new_values = json_from_request(request) or {}
    for key in new_values:
        try:
            model, field = key.split('.', 1)
            if model != 'user':
                return bad_request_json_response(
                    'All fields should be prefixed with "user."')
            if field not in USER_EDIT_FIELDS:
                return bad_request_json_response(field +
                                                 ' is not an updatable field')
        except ValueError:
            return bad_request_json_response(
                'All fields should be prefixed with "user."')
        setattr(user, field, new_values[key])
    try:
        user.save()
        return {"ok": True}
    except ValidationError, ve:
        return bad_request_json_response(
            validation_error_dict=package_validation_errors('user', ve))
Ejemplo n.º 3
0
def update_user(request, user):
    new_values = json_from_request(request) or {}
    for key in new_values:
        try:
            model, field = key.split('.', 1)
            if model != 'user':
                return bad_request_json_response(
                    'All fields should be prefixed with "user."')
            if field not in ['first_name', 'last_name', 'email']:
                return bad_request_json_response(
                    field + ' is not an updatable field')
        except ValueError:
            return bad_request_json_response(
                'All fields should be prefixed with "user."')
        setattr(user, field, new_values[key])
    try:
        user.save()
        return {"ok": True}
    except ValidationError, ve:
        return bad_request_json_response(
            validation_error_dict=package_validation_errors('user', ve))
Ejemplo n.º 4
0
def update_user(request, username):
    user = get_object_or_404(User, username=username)
    if user != request.user:
        return HttpResponseForbidden()

    new_values = json_from_request(request) or {}
    for key in new_values:
        try:
            model, field = key.split(".", 1)
            if model != "user":
                return bad_request_json_response('All fields should be prefixed with "user."')
            if field not in ["first_name", "last_name", "email"]:
                return bad_request_json_response(field + " is not an updatable field")
        except ValueError:
            return bad_request_json_response('All fields should be prefixed with "user."')
        setattr(user, field, new_values[key])
    try:
        user.save()
        return {"ok": True}
    except ValidationError, ve:
        return bad_request_json_response(validation_error_dict=package_validation_errors("user", ve))
Ejemplo n.º 5
0
def update_plot_and_tree_request(request, plot):
    try:
        plot = update_plot_and_tree(request, plot)
        # Refresh plot.instance in case geo_rev_hash was updated
        plot.instance = Instance.objects.get(id=plot.instance.id)
        return {
            "ok": True,
            "geoRevHash": plot.instance.geo_rev_hash,
            "plotId": plot.id,
            "enabled": plot.instance.feature_enabled("add_plot"),
        }
    except ValidationError as ve:
        return bad_request_json_response(validation_error_dict=ve.message_dict)
Ejemplo n.º 6
0
def upload_user_photo(request, user):
    """
    Saves a user profile photo whose data is in the request.
    The callee or decorator is reponsible for ensuring request.user == user
    """
    try:
        user.photo, user.thumbnail = save_image_from_request(
            request, name_prefix="user-%s" % user.pk, thumb_size=(85, 85))
        user.save_with_user(request.user)
    except ValidationError as e:
        # Most of these ValidationError are not field-errors and so their
        # messages are a Dict, which is why they simply joined together
        return bad_request_json_response('; '.join(e.messages))

    return {'url': user.thumbnail.url}
Ejemplo n.º 7
0
def upload_user_photo(request, user):
    """
    Saves a user profile photo whose data is in the request.
    The callee or decorator is reponsible for ensuring request.user == user
    """
    try:
        user.photo, user.thumbnail = save_image_from_request(
            request, name_prefix="user-%s" % user.pk, thumb_size=(85, 85))
        user.save_with_user(request.user)
    except ValidationError as e:
        # Most of these ValidationError are not field-errors and so their
        # messages are a Dict, which is why they simply joined together
        return bad_request_json_response('; '.join(e.messages))

    return {'url': user.thumbnail.url}
Ejemplo n.º 8
0
def update_plot_and_tree_request(request, plot):
    try:
        request_dict = json.loads(request.body)
        plot, tree = update_plot_and_tree(request_dict, request.user, plot)

        return {
            'ok': True,
            'geoRevHash': plot.instance.geo_rev_hash,
            'plotId': plot.id,
            'treeId': tree.id if tree else None,
            'enabled': plot.instance.feature_enabled('add_plot')
        }
    except ValidationError as ve:
        return bad_request_json_response(
            validation_error_dict=ve.message_dict)
Ejemplo n.º 9
0
def _request_to_update_map_feature(request, instance, feature):
    try:
        request_dict = json.loads(request.body)
        feature, tree = update_map_feature(request_dict, request.user, feature)

        # We need to reload the instance here since a new georev
        # may have been set
        instance = Instance.objects.get(pk=instance.pk)

        return {
            'ok': True,
            'geoRevHash': instance.geo_rev_hash,
            'featureId': feature.id,
            'treeId': tree.id if tree else None,
            'enabled': instance.feature_enabled('add_plot')
        }
    except ValidationError as ve:
        return bad_request_json_response(validation_error_dict=ve.message_dict)
Ejemplo n.º 10
0
def _request_to_update_map_feature(request, instance, feature):
    try:
        request_dict = json.loads(request.body)
        feature, tree = update_map_feature(request_dict, request.user, feature)

        # We need to reload the instance here since a new georev
        # may have been set
        instance = Instance.objects.get(pk=instance.pk)

        return {
            "ok": True,
            "geoRevHash": instance.geo_rev_hash,
            "featureId": feature.id,
            "treeId": tree.id if tree else None,
            "enabled": instance.feature_enabled("add_plot"),
        }
    except ValidationError as ve:
        return bad_request_json_response(validation_error_dict=ve.message_dict)
Ejemplo n.º 11
0
def add_tree_photo_view(request, instance, plot_id, tree_id=None):
    try:
        photo = add_tree_photo(request, instance, plot_id, tree_id)
        return {"url": photo.thumbnail.url}
    except ValidationError as e:
        return bad_request_json_response("; ".join(e.messages))