Пример #1
0
def delete_snapshot(request):
    """
    Todo: write doc.
    """
    print(request.POST)
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect("containers")
    if (
        "id" not in request.POST
        or not request.POST.get("id").isdgit()
        or "ct_id" not in request.POST
        or not request.get("ct_id").isdgit()
    ):
        messages.error(request, "Invalid POST request.")
        return redirect("containers")

    snapshot_id = int(request.POST.get("id"))
    params = {}
    params["id"] = snapshot_id
    ct_id = params["ct_id"] = int(request.POST.get("ct_id"))

    client = get_httpclient_instance(request)

    # create snapshot
    try:
        client.containers.snapshots(snapshot_id).delete()
        messages.success(request, "Sucessfully deleted snapshot.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect("container_snapshots", ct_id)
Пример #2
0
def restore_snapshot(request):
    """
    Todo: write doc.
    Todo: get name & description param from GUI.
    """
    print(request.POST)
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect("containers")
    if "id" not in request.POST or not request.POST.get("id").isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect("containers")

    params = {}
    id = int(request.POST.get("id"))

    client = get_httpclient_instance(request)

    snapshot = client.containers.snapshots(id).get()
    if snapshot:
        # restore snapshot
        try:
            client.containers.snapshots(id).restore.post()
            messages.success(request, "Sucessfully restored snapshot `{}`.".format(snapshot.name))
        except Exception as e:
            messages.error(request, api_error_message(e, ""))
    else:
        messages.error(request, "Snapshot not found")

    return redirect("container_snapshots", snapshot.container.id)
Пример #3
0
def resume(request):
    """
    Resume the container
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect("containers")
    if "id" not in request.POST or not request.POST.get("id").isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect("containers")

    ct_id = int(request.POST.get("id"))

    client = get_httpclient_instance(request)

    try:
        container = client.containers(ct_id).get()
    except HttpNotFoundError:
        messages.error(request, "Container does not exist.")

    if container:
        try:
            client.containers(ct_id).resume.post()
            messages.success(request, "Container is resuming.")
        except Exception as e:
            messages.error(request, api_error_message(e, ""))
    else:
        messages.error(request, "Container does not exist.")

    return redirect("containers")
Пример #4
0
def delete(request):
    """
    Todo: write doc.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect("containers")
    if "id" not in request.POST or not request.POST.get("id").isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect("containers")

    ct_id = int(request.POST.get("id"))

    client = get_httpclient_instance(request)

    try:
        container = client.containers(ct_id)
    except HttpNotFoundError:
        messages.error(request, "Container does not exist, or you don't have permissions to delete it.")

    if container:
        try:
            client.containers(ct_id).delete()
            messages.success(request, "Container deleted successfully.")
        except Exception as e:
            messages.error(request, api_error_message(e, ""))
    else:
        messages.error(request, "Container does not exist.")

    return redirect("containers")
Пример #5
0
def clone(request):
    """
    Todo: write doc.
    Todo: get name & description param from GUI.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect("containers")
    if "id" not in request.POST or not request.POST.get("id").isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect("containers")

    params = {}

    ct_id = int(request.POST.get("id"))
    client = get_httpclient_instance(request)
    container = client.containers(ct_id).get()
    params["name"] = "{}_clone".format(container.name)

    # create clone
    try:
        client.containers(ct_id).clone.post(params)
        messages.success(request, "Sucessfully created the clone `{}`.".format(params.get("name")))
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect("containers")
Пример #6
0
def leave(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('groups')
    if 'group_id' not in request.POST or not request.POST.get(
            'group_id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('groups')

    group_id = int(request.POST.get('group_id'))

    client = get_httpclient_instance(request)
    group = client.collaborationgroups(group_id).get()

    if group:
        try:
            client.collaborationgroups(group_id).leave.post()
            messages.success(
                request,
                "You are no longer a member of group {}.".format(group.name))
        except Exception as e:
            messages.error(request, api_error_message(e, ""))

        request.method = "GET"
        return redirect('groups')
    else:
        messages.error(request, "Group does not exist.")

    return redirect('groups')
Пример #7
0
def remove_admin(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'group_id' not in request.POST  or not request.POST.get('group_id').isdigit() \
        or 'user_id' not in request.POST  or not request.POST.get('user_id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    group_id = int(request.POST.get('group_id'))
    user_id = int(request.POST.get('user_id'))

    client = get_httpclient_instance(request)
    group = client.collaborationgroups(group_id).get()
    user = client.users(user_id).get()

    params = {}
    params["users"] = [user_id]

    if group:
        if request.user.is_superuser or request.user.backend_user.id == group.creator.id or request.user.backend_user.id in group.admins:
            try:
                client.collaborationgroups(group_id).remove_admins.post(params)
                messages.success(request, "{} is not a admin of {} anymore.".format(user.username, group.name))
            except Exception as e:
                messages.error(request, api_error_message(e, params))

            request.method = "GET"
            return redirect('group_manage', group.id)
        else:
            messages.error(request, "Not enough permissions to do this.")
        return redirect('group_manage', group.id)
    else:
        messages.error(request, "Group does not exist.")
        return redirect('group_manage', group.id)
Пример #8
0
def leave(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    share_id = int(request.POST.get('id'))

    client = get_httpclient_instance(request)
    share = client.shares(share_id).get()

    params = {"users": [request.user.id]}

    if share:
        if share.owner == request.user.backend_user.id:
            messages.error(
                request,
                "You cannot leave an owned share. Please delete it instead.")
        else:
            try:
                client.shares(share_id).remove_users.post(params)
                messages.success(request, "You successfully left the share.")
            except Exception as e:
                messages.error(request, api_error_message(e, params))
    else:
        messages.error(request, "Share does not exist.")

    return redirect('shares')
Пример #9
0
def delete(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'share_id' not in request.POST or not request.POST.get(
            'share_id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    share_id = int(request.POST.get('share_id'))

    client = get_httpclient_instance(request)
    share = client.shares(share_id).get()

    if share:
        if share.owner.id == request.user.backend_user.id:
            try:
                client.shares(share_id).delete()
                messages.success(
                    request,
                    "Share `{}` deleted sucessfully.".format(share.name))
            except Exception as e:
                messages.error(request, api_error_message(e, ""))
        else:
            messages.error(
                request,
                "You don't have enough permissions for the requested operation."
            )
    else:
        messages.error(request, "Share does not exist.")

    return redirect('shares')
Пример #10
0
def manage(request, share_id):
    if request.method == "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')

    client = get_httpclient_instance(request)
    share = client.shares(share_id).get()
    share['access_group_ids'] = [g.id for g in share.access_groups]
    users = client.users.get()
    groups = client.collaborationgroups.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    if share:
        return render(
            request, 'web/shares/manage.html', {
                'title': "Manage Share",
                'share': share,
                'members': share.members,
                'users': users,
                'groups': groups,
                'new_notifications_count': new_notifications_count
            })
    else:
        messages.error(request, "Share does not exist.")

    return redirect('shares')
Пример #11
0
def share_remove_access_group(request):
    """
    Add access groups to the share.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'share_id' not in request.POST or not request.POST.get(
            'share_id').isdigit() or 'access_group' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    group_id = request.POST.get('access_group')
    share_id = request.POST.get('share_id')
    client = get_httpclient_instance(request)

    # then call API to add the users to the group
    client.shares.get()
    params = {"access_groups": [group_id]}

    try:
        client.shares(share_id).remove_access_groups.post(params)
        messages.success(
            request, "The selected groups are now removed from this share.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))
        return (redirect('share_manage', share_id))

    return redirect('share_manage', share_id)
Пример #12
0
def create(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('notifications')
    # Todo: validate POST params: receiver_group, msg, type, rel objs
    if 'receiver_groups' not in request.POST or 'message' not in request.POST \
        or 'notification_type' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('notifications')

    client = get_httpclient_instance(request)

    params = {}
    params["message"] = request.POST.get('message', '')
    params["notification_type"] = request.POST.get('notification_type', '')
    params["sender"] = request.user.id
    params["container"] = request.POST.get('container', None)
    params["container_image"] = request.POST.get('container_image', None)
    params["share"] = request.POST.get('share', None)
    params["group"] = request.POST.get('group', None)
    params["receiver_groups"] = [int(request.POST.get('receiver_groups', None))]

    client.notifications.get()
    try:
        client.notifications.post(params)
        messages.success(request, "Notification sucessfully created.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('notifications')
Пример #13
0
def create_snapshot(request):
    """
    Todo: write doc.
    Todo: get name & description param from GUI.
    """
    print(request.POST)
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect("containers")
    if "ct_id" not in request.POST or not request.POST.get("ct_id").isdigit() or "name" not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect("containers")

    ct_id = int(request.POST.get("ct_id"))
    params = {}
    params["name"] = request.POST.get("name", "")
    description = request.POST.get("description", "")
    if description:
        params["description"] = description

    client = get_httpclient_instance(request)

    # create snapshot
    try:
        client.containers(ct_id).create_snapshot.post(params)
        messages.success(request, "Sucessfully created snapshot `{}`.".format(params.get("name")))
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect("container_snapshots", ct_id)
Пример #14
0
def create_snapshot(request):
    """
    Todo: write doc.
    Todo: get name & description param from GUI.
    """
    print(request.POST)
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('containers')
    if 'ct_id' not in request.POST  or not request.POST.get('ct_id').isdigit() \
        or 'name' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('containers')

    ct_id = int(request.POST.get('ct_id'))
    params = {}
    params['name'] = request.POST.get('name', '')
    description = request.POST.get('description', '')
    if description:
        params['description'] = description

    client = get_httpclient_instance(request)

    # create snapshot
    try:
        client.containers(ct_id).create_snapshot.post(params)
        messages.success(
            request,
            "Sucessfully created snapshot `{}`.".format(params.get('name')))
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('container_snapshots', ct_id)
Пример #15
0
def index(request):
    """
    Shares listing/index.
    """
    dateformat = "%Y-%m-%dT%H:%M:%S.%fZ"
    client = get_httpclient_instance(request)
    notificationlogs = client.notificationlogs.get()
    # get proper dates
    for notification in notificationlogs:
        notification["date"] = datetime.strptime(notification.notification.get('date'), dateformat)
    notificationtypes = client.notificationtypes.get()
    groups = client.collaborationgroups.get()
    containers = client.containers.get()
    container_images = client.containers.images.get()
    shares = client.shares.get()

    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(request, 'web/notifications/index.html', {
        'title': "Notifications",
        'notifications': notificationlogs,
        'notification_types': notificationtypes,
        'groups': groups,
        'containers': containers,
        'container_images': container_images,
        'shares': shares,
        'new_notifications_count': new_notifications_count
    })
Пример #16
0
def leave(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    share_id = int(request.POST.get('id'))

    client = get_httpclient_instance(request)
    share = client.shares(share_id).get()

    params = {"users": [request.user.id]}

    if share:
        if share.owner == request.user.backend_user.id:
            messages.error(request, "You cannot leave an owned share. Please delete it instead.")
        else:
            try:
                client.shares(share_id).remove_users.post(params)
                messages.success(request, "You successfully left the share.")
            except Exception as e:
                messages.error(request, api_error_message(e, params))
    else:
        messages.error(request, "Share does not exist.")

    return redirect('shares')
Пример #17
0
def add_members(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')

    users = request.POST.getlist('users')
    group_id = request.POST.get('group_id')

    client = get_httpclient_instance(request)

    user_list = []
    # validate existance of users first
    for u in users:
        user = client.users(u).get()
        if user:
            user_list.append(u)

    # then call API to add the users to the group
    params = {}
    params["users"] = user_list
    try:
        client.collaborationgroups(group_id).add_members.post(params)
        messages.success(request, "Users successfully added to the group.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('group_manage', group_id)
Пример #18
0
def delete(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'share_id' not in request.POST or not request.POST.get('share_id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    share_id = int(request.POST.get('share_id'))

    client = get_httpclient_instance(request)
    share = client.shares(share_id).get()

    if share:
        if share.owner.id == request.user.backend_user.id:
            try:
                client.shares(share_id).delete()
                messages.success(request, "Share `{}` deleted sucessfully.".format(share.name))
            except Exception as e:
                messages.error(request, api_error_message(e, ""))
        else:
            messages.error(request, "You don't have enough permissions for the requested operation.")
    else:
        messages.error(request, "Share does not exist.")

    return redirect('shares')
Пример #19
0
def resume(request):
    """
    Resume the container
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('containers')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('containers')

    ct_id = int(request.POST.get('id'))

    client = get_httpclient_instance(request)

    try:
        container = client.containers(ct_id).get()
    except HttpNotFoundError:
        messages.error(request, "Container does not exist.")

    if container:
        try:
            client.containers(ct_id).resume.post()
            messages.success(request, "Container is resuming.")
        except Exception as e:
            messages.error(request, api_error_message(e, ""))
    else:
        messages.error(request, "Container does not exist.")

    return redirect('containers')
Пример #20
0
def delete_snapshot(request):
    """
    Todo: write doc.
    """
    print(request.POST)
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('containers')
    if 'id' not in request.POST or not request.POST.get('id').isdgit() \
        or 'ct_id' not in request.POST or not request.get('ct_id').isdgit():
        messages.error(request, "Invalid POST request.")
        return redirect('containers')

    snapshot_id = int(request.POST.get('id'))
    params = {}
    params['id'] = snapshot_id
    ct_id = params['ct_id'] = int(request.POST.get('ct_id'))

    client = get_httpclient_instance(request)

    # create snapshot
    try:
        client.containers.snapshots(snapshot_id).delete()
        messages.success(request, "Sucessfully deleted snapshot.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('container_snapshots', ct_id)
Пример #21
0
def delete(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('images')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('images')

    img_id = int(request.POST.get('id'))

    client = get_httpclient_instance(request)

    try:
        image = client.containers.images(img_id).get()
    except HttpNotFoundError:
        messages.error(
            request,
            "Image does not exist or you don't have the permissions to delete it."
        )

    if image:
        try:
            client.containers.images(img_id).delete()
            messages.success(request, "Image deleted successfully.")
        except Exception as e:
            messages.error(request, api_error_message(e, ""))
    else:
        messages.error(
            request,
            "Image does not exist or you don't have the permissions to delete it."
        )

    return redirect('images')
Пример #22
0
def add_members(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')

    users = request.POST.getlist('users')
    group_id = request.POST.get('group_id')

    client = get_httpclient_instance(request)

    user_list = []
    # validate existance of users first
    for u in users:
        user = client.users(u).get()
        if user:
            user_list.append(u)

    # then call API to add the users to the group
    params = {}
    params["users"] = user_list
    try:
        client.collaborationgroups(group_id).add_members.post(params)
        messages.success(request, "Users successfully added to the group.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('group_manage', group_id)
Пример #23
0
def index(request):
    """
    Shares listing/index.
    """
    dateformat = "%Y-%m-%dT%H:%M:%S.%fZ"
    client = get_httpclient_instance(request)
    notificationlogs = client.notificationlogs.get()
    # get proper dates
    for notification in notificationlogs:
        notification["date"] = datetime.strptime(
            notification.notification.get('date'), dateformat)
    notificationtypes = client.notificationtypes.get()
    groups = client.collaborationgroups.get()
    containers = client.containers.get()
    container_images = client.containers.images.get()
    shares = client.shares.get()

    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(
        request, 'web/notifications/index.html', {
            'title': "Notifications",
            'notifications': notificationlogs,
            'notification_types': notificationtypes,
            'groups': groups,
            'containers': containers,
            'container_images': container_images,
            'shares': shares,
            'new_notifications_count': new_notifications_count
        })
Пример #24
0
def add_access_groups(request):

    """
    Add access groups to the image.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('images')
    if 'id' not in request.POST or not request.POST.get('id').isdigit() or 'access_groups' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('images')

    access_groups = request.POST.getlist('access_groups')
    img_id = request.POST.get('id')
    client = get_httpclient_instance(request)

    params = {}
    params['access_groups'] = access_groups

    try:
        response = client.containers.images(img_id).add_access_groups.post(params)
        count = response.get('count')
        if count == 0:
            messages.info(request, "{} groups successfully added to the image.".format(count))
        elif count == 1:
            messages.success(request, "{} group successfully added to the image.".format(count))
        else:
            messages.success(request, "{} groups successfully added to the image.".format(count))
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('image_manage', img_id)
Пример #25
0
def create(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('notifications')
    # Todo: validate POST params: receiver_group, msg, type, rel objs
    if 'receiver_groups' not in request.POST or 'message' not in request.POST \
        or 'notification_type' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('notifications')

    client = get_httpclient_instance(request)

    params = {}
    params["message"] = request.POST.get('message', '')
    params["notification_type"] = request.POST.get('notification_type', '')
    params["sender"] = request.user.id
    params["container"] = request.POST.get('container', None)
    params["container_image"] = request.POST.get('container_image', None)
    params["share"] = request.POST.get('share', None)
    params["group"] = request.POST.get('group', None)
    params["receiver_groups"] = [
        int(request.POST.get('receiver_groups', None))
    ]

    client.notifications.get()
    try:
        client.notifications.post(params)
        messages.success(request, "Notification sucessfully created.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('notifications')
Пример #26
0
def remove_access_group(request):

    """
    Add access groups to the image.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('images')
    if 'id' not in request.POST or not request.POST.get('id').isdigit() or 'access_group' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('images')

    group_id = request.POST.get('access_group')
    img_id = request.POST.get('id')
    client = get_httpclient_instance(request)

    params = {"access_groups": [group_id]}

    try:
        client.containers.images(img_id).remove_access_groups.post(params)
        messages.success(request, "Group successfully removed from image.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))
        return(redirect('image_manage', img_id))

    return redirect('image_manage', img_id)
Пример #27
0
def delete(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('images')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('images')

    img_id = int(request.POST.get('id'))

    client = get_httpclient_instance(request)

    try:
        image = client.containers.images(img_id).get()
    except HttpNotFoundError:
        messages.error(request, "Image does not exist or you don't have the permissions to delete it.")

    if image:
        try:
            client.containers.images(img_id).delete()
            messages.success(request, "Image deleted successfully.")
        except Exception as e:
            messages.error(request, api_error_message(e, ""))
    else:
        messages.error(request, "Image does not exist or you don't have the permissions to delete it.")

    return redirect('images')
Пример #28
0
def delete(request):
    """
    Todo: write doc.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('containers')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('containers')

    ct_id = int(request.POST.get('id'))

    client = get_httpclient_instance(request)

    try:
        container = client.containers(ct_id)
    except HttpNotFoundError:
        messages.error(
            request,
            "Container does not exist, or you don't have permissions to delete it."
        )

    if container:
        try:
            client.containers(ct_id).delete()
            messages.success(request, "Container deleted successfully.")
        except Exception as e:
            messages.error(request, api_error_message(e, ""))
    else:
        messages.error(request, "Container does not exist.")

    return redirect('containers')
Пример #29
0
def share_add_access_groups(request):

    """
    Add access groups to the share.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'id' not in request.POST or not request.POST.get('id').isdigit() or 'access_groups' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    access_groups = request.POST.getlist('access_groups')
    share_id = request.POST.get('id')
    client = get_httpclient_instance(request)

    # cannot validate existance of groups due to visibility permissions
    # i.e. of single_user_groups, let the API handle that
    params = {}
    params['access_groups'] = access_groups

    try:
        response = client.shares(share_id).add_access_groups.post(params)
        count = response.get('count')
        if count == 0:
            messages.info(request, "{} groups successfully added to the share.".format(count))
        elif count == 1:
            messages.success(request, "{} group successfully added to the share.".format(count))
        else:
            messages.success(request, "{} groups successfully added to the share.".format(count))

    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('share_manage', share_id)
Пример #30
0
def remove_access_group(request):
    """
    Add access groups to the image.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('images')
    if 'id' not in request.POST or not request.POST.get(
            'id').isdigit() or 'access_group' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('images')

    group_id = request.POST.get('access_group')
    img_id = request.POST.get('id')
    client = get_httpclient_instance(request)

    params = {"access_groups": [group_id]}

    try:
        client.containers.images(img_id).remove_access_groups.post(params)
        messages.success(request, "Group successfully removed from image.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))
        return (redirect('image_manage', img_id))

    return redirect('image_manage', img_id)
Пример #31
0
def manage(request, share_id):
    if request.method == "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')

    client = get_httpclient_instance(request)
    share = client.shares(share_id).get()
    share['access_group_ids'] = [g.id for g in share.access_groups]
    users = client.users.get()
    groups = client.collaborationgroups.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    if share:
            return render(request, 'web/shares/manage.html', {
                'title': "Manage Share",
                'share': share,
                'members': share.members,
                'users': users,
                'groups': groups,
                'new_notifications_count': new_notifications_count
            })
    else:
        messages.error(request, "Share does not exist.")

    return redirect('shares')
Пример #32
0
def restore_snapshot(request):
    """
    Todo: write doc.
    Todo: get name & description param from GUI.
    """
    print(request.POST)
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('containers')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('containers')

    params = {}
    id = int(request.POST.get('id'))

    client = get_httpclient_instance(request)

    snapshot = client.containers.snapshots(id).get()
    if snapshot:
        # restore snapshot
        try:
            client.containers.snapshots(id).restore.post()
            messages.success(
                request,
                "Sucessfully restored snapshot `{}`.".format(snapshot.name))
        except Exception as e:
            messages.error(request, api_error_message(e, ""))
    else:
        messages.error(request, "Snapshot not found")

    return redirect('container_snapshots', snapshot.container.id)
Пример #33
0
def create(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('groups')

    params = {}
    if 'name' not in request.POST:
        messages.error(request, "Invalid POST request.")
    else:
        params["name"] = request.POST.get('name')

    if 'public' in request.POST:
        params["is_public"] = True
    else:
        params["is_public"] = False

    client = get_httpclient_instance(request)

    try:
        client.collaborationgroups.post(params)
        messages.success(request, "Group `{}` created sucessfully.".format(params.get("name")))
    except HttpClientError:
        messages.error(request, "Bad Request. A group with this name already exists.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('groups')
Пример #34
0
def clone(request):
    """
    Todo: write doc.
    Todo: get name & description param from GUI.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('containers')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('containers')

    params = {}

    ct_id = int(request.POST.get('id'))
    client = get_httpclient_instance(request)
    container = client.containers(ct_id).get()
    params['name'] = "{}_clone".format(container.name)

    # create clone
    try:
        client.containers(ct_id).clone.post(params)
        messages.success(
            request,
            "Sucessfully created the clone `{}`.".format(params.get('name')))
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('containers')
Пример #35
0
def create(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('groups')

    params = {}
    if 'name' not in request.POST:
        messages.error(request, "Invalid POST request.")
    else:
        params["name"] = request.POST.get('name')

    if 'public' in request.POST:
        params["is_public"] = True
    else:
        params["is_public"] = False

    client = get_httpclient_instance(request)

    try:
        client.collaborationgroups.post(params)
        messages.success(
            request,
            "Group `{}` created sucessfully.".format(params.get("name")))
    except HttpClientError:
        messages.error(request,
                       "Bad Request. A group with this name already exists.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('groups')
Пример #36
0
def share_remove_access_group(request):

    """
    Add access groups to the share.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'share_id' not in request.POST or not request.POST.get('share_id').isdigit() or 'access_group' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    group_id = request.POST.get('access_group')
    share_id = request.POST.get('share_id')
    client = get_httpclient_instance(request)

    # then call API to add the users to the group
    client.shares.get()
    params = {"access_groups": [group_id]}

    try:
        client.shares(share_id).remove_access_groups.post(params)
        messages.success(request, "The selected groups are now removed from this share.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))
        return(redirect('share_manage', share_id))

    return redirect('share_manage', share_id)
Пример #37
0
def leave(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('groups')
    if 'group_id' not in request.POST or not request.POST.get('group_id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('groups')

    group_id = int(request.POST.get('group_id'))

    client = get_httpclient_instance(request)
    group = client.collaborationgroups(group_id).get()

    if group:
        try:
            client.collaborationgroups(group_id).leave.post()
            messages.success(request, "You are no longer a member of group {}.".format(group.name))
        except Exception as e:
            messages.error(request, api_error_message(e, ""))

        request.method = "GET"
        return redirect('groups')
    else:
        messages.error(request, "Group does not exist.")

    return redirect('groups')
Пример #38
0
def create(request):
    """
    Create a new share.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'name' not in request.POST or 'description' not in request.POST or 'tags' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    client = get_httpclient_instance(request)

    # dict to hold all the params to call the api
    params = {}

    params["name"] = request.POST.get('name')
    params["description"] = request.POST.get('description', '')
    params["owner"] = request.user.backend_user.id
    params["access_groups"] = request.POST.getlist('access_groups', [])

    tag_labels = request.POST.get('tags').split(',')

    tags = []
    for tag in tag_labels:
        if tag:
            # see if tag with this label exists already
            try:
                t = client.tags.by_name(tag).get()
            except Exception as e:
                t = None
            if not t:
                # create a new tag
                try:
                    tag_params = {"label": str(tag)}
                    t = client.tags.post(tag_params)
                except Exception as e:
                    messages.error(request, api_error_message(e, tag_params))
                    return redirect('shares')
            else:
                t = t[0]
            tags.append(t.id)

    params["tags"] = tags

    client.shares.get()
    try:
        client.shares.post(params)
        messages.success(request, "Share created sucessfully.")
    except HttpClientError:
        messages.error(request,
                       "Bad Request. A share with this name already exists.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))
    return redirect('shares')
Пример #39
0
def create(request):
    """
    Create a new share.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'name' not in request.POST or 'description' not in request.POST or 'tags' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    client = get_httpclient_instance(request)

    # dict to hold all the params to call the api
    params = {}

    params["name"] = request.POST.get('name')
    params["description"] = request.POST.get('description', '')
    params["owner"] = request.user.backend_user.id
    params["access_groups"] = request.POST.getlist('access_groups', [])

    tag_labels = request.POST.get('tags').split(',')

    tags = []
    for tag in tag_labels:
        if tag:
            # see if tag with this label exists already
            try:
                t = client.tags.by_name(tag).get()
            except Exception as e:
                t = None
            if not t:
                # create a new tag
                try:
                    tag_params = {"label": str(tag)}
                    t = client.tags.post(tag_params)
                except Exception as e:
                    messages.error(request, api_error_message(e, tag_params))
                    return redirect('shares')
            else:
                t = t[0]
            tags.append(t.id)

    params["tags"] = tags

    client.shares.get()
    try:
        client.shares.post(params)
        messages.success(request, "Share created sucessfully.")
    except HttpClientError:
        messages.error(request, "Bad Request. A share with this name already exists.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))
    return redirect('shares')
Пример #40
0
def index(request):
    """
    Shares listing/index.
    """
    client = get_httpclient_instance(request)
    shares = client.shares.get()
    new_notifications_count = len(client.notificationlogs.unread.get())
    return render(request, 'web/shares/index.html', {
        'title': "Shares",
        'shares': shares,
        'new_notifications_count': new_notifications_count
    })
Пример #41
0
def index(request):
    """
    Shares listing/index.
    """
    client = get_httpclient_instance(request)
    shares = client.shares.get()
    new_notifications_count = len(client.notificationlogs.unread.get())
    return render(
        request, 'web/shares/index.html', {
            'title': "Shares",
            'shares': shares,
            'new_notifications_count': new_notifications_count
        })
Пример #42
0
def index(request):
    client = get_httpclient_instance(request)

    containers = client.containers.get()
    images = client.containers.images.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(request, 'web/images/index.html', {
        'title': "Images",
        'containers': containers,
        'images': images,
        'new_notifications_count': new_notifications_count,
        'request': request.GET
    })
Пример #43
0
def dashboard(request):
    '''
    Dashboard view listing the running containers.
    '''

    client = get_httpclient_instance(request)
    containers = client.containers.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(request, 'web/dashboard.html', {
        'title': "Dashboard",
        'containers': filter(lambda ct: ct.is_running is True, containers),
        'new_notifications_count': new_notifications_count
    })
Пример #44
0
def index(request):
    client = get_httpclient_instance(request)

    containers = client.containers.get()
    images = client.containers.images.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(
        request, 'web/images/index.html', {
            'title': "Images",
            'containers': containers,
            'images': images,
            'new_notifications_count': new_notifications_count,
            'request': request.GET
        })
Пример #45
0
def manage(request, image_id):
    client = get_httpclient_instance(request)
    image = client.containers.images(image_id).get()
    new_notifications_count = len(client.notificationlogs.unread.get())
    image['access_group_ids'] = [g.id for g in image.access_groups]
    users = client.users.get()
    groups = client.collaborationgroups.get()

    return render(request, 'web/images/manage.html', {
        'title': "Image",
        'image': image,
        'users': users,
        'groups': groups,
        'new_notifications_count': new_notifications_count
    })
Пример #46
0
def index(request, ct_id):
    """
    Get a list of all snapshots for this container.
    """
    client = get_httpclient_instance(request)
    container = client.containers(ct_id).get()
    container_snapshots = client.containers(ct_id).snapshots.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(request, 'web/container_snapshots/index.html', {
        'title': "Container Snapshots",
        'container_snapshots': container_snapshots,
        'container': container,
        'new_notifications_count': new_notifications_count
    })
Пример #47
0
def dashboard(request):
    '''
    Dashboard view listing the running containers.
    '''

    client = get_httpclient_instance(request)
    containers = client.containers.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(
        request, 'web/dashboard.html', {
            'title': "Dashboard",
            'containers': filter(lambda ct: ct.is_running is True, containers),
            'new_notifications_count': new_notifications_count
        })
Пример #48
0
def manage(request, image_id):
    client = get_httpclient_instance(request)
    image = client.containers.images(image_id).get()
    new_notifications_count = len(client.notificationlogs.unread.get())
    image['access_group_ids'] = [g.id for g in image.access_groups]
    users = client.users.get()
    groups = client.collaborationgroups.get()

    return render(
        request, 'web/images/manage.html', {
            'title': "Image",
            'image': image,
            'users': users,
            'groups': groups,
            'new_notifications_count': new_notifications_count
        })
Пример #49
0
def index(request, ct_id):
    """
    Get a list of all snapshots for this container.
    """
    client = get_httpclient_instance(request)
    container = client.containers(ct_id).get()
    container_snapshots = client.containers(ct_id).snapshots.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(
        request, 'web/container_snapshots/index.html', {
            'title': "Container Snapshots",
            'container_snapshots': container_snapshots,
            'container': container,
            'new_notifications_count': new_notifications_count
        })
Пример #50
0
def index(request):
    """
    Groups listing/index.
    """
    client = get_httpclient_instance(request)
    users = client.users.get()
    collab_groups = client.collaborationgroups.get()
    new_notifications_count = len(client.notificationlogs.unread.get())
    for group in collab_groups:
        group["member_ids"] = [member.id for member in group.members]

    return render(request, 'web/collaborationgroups/index.html', {
        'title': "Groups",
        'groups': collab_groups,
        'users': users,
        'new_notifications_count': new_notifications_count
    })
Пример #51
0
def index(request):
    """
    Get a list of all containers and render it.
    """
    client = get_httpclient_instance(request)
    # containers = Container.objects.filter(owner=request.user.backend_user)
    containers = client.containers.get()
    images = client.containers.images.get()
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(
        request, 'web/containers/index.html', {
            'title': "Containers",
            'containers': containers,
            'images': images,
            'new_notifications_count': new_notifications_count,
            'request': request.GET
        })
Пример #52
0
def index(request):
    """
    Groups listing/index.
    """
    client = get_httpclient_instance(request)
    users = client.users.get()
    collab_groups = client.collaborationgroups.get()
    new_notifications_count = len(client.notificationlogs.unread.get())
    for group in collab_groups:
        group["member_ids"] = [member.id for member in group.members]

    return render(
        request, 'web/collaborationgroups/index.html', {
            'title': "Groups",
            'groups': collab_groups,
            'users': users,
            'new_notifications_count': new_notifications_count
        })
Пример #53
0
def manage(request, group_id):
    """
    Manage single group.
    """
    client = get_httpclient_instance(request)
    group = client.collaborationgroups(group_id).get()
    members = group.members
    users = client.users.get()
    group["member_ids"] = [member.id for member in members]
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(request, 'web/collaborationgroups/manage.html', {
        'title': "Group",
        'group': group,
        'members': members,
        'users': users,
        'new_notifications_count': new_notifications_count
    })
Пример #54
0
def manage(request, group_id):
    """
    Manage single group.
    """
    client = get_httpclient_instance(request)
    group = client.collaborationgroups(group_id).get()
    members = group.members
    users = client.users.get()
    group["member_ids"] = [member.id for member in members]
    new_notifications_count = len(client.notificationlogs.unread.get())

    return render(
        request, 'web/collaborationgroups/manage.html', {
            'title': "Group",
            'group': group,
            'members': members,
            'users': users,
            'new_notifications_count': new_notifications_count
        })
Пример #55
0
def create(request):
    """
    Todo: write doc.
    """
    print(request.POST)
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect("images")
    if (
        "name" not in request.POST
        or "description" not in request.POST
        or "image_id" not in request.POST
        or not request.POST.get("image_id").isdigit()
    ):
        messages.error(request, "Invalid POST request.")
        return redirect("images")

    params = {}
    params["name"] = request.POST.get("name")
    params["description"] = request.POST.get("description")
    params["image"] = int(request.POST.get("image_id"))

    client = get_httpclient_instance(request)

    try:
        image = client.containers.images(params.get("image_id")).get()
    except HttpNotFoundError:
        messages.error(
            request,
            "Container bootstrap image does not exist or you don't have enough permissions for the requested operation.",
        )
    except Exception as ex:
        messages.error(request, api_error_message(ex, params))

    if image:
        try:
            # server and owner get set by the core automatically
            client.containers.post(params)
            messages.success(request, "Container created successfully.")
        except Exception as ex:
            messages.error(request, api_error_message(ex, params))

    return redirect("containers")
Пример #56
0
def commit(request):
    """
    Todo: write doc.
    """

    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('images')
    if 'ct_id' not in request.POST or not request.POST.get('ct_id').isdigit() \
        or 'img_name' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('images')

    params = {}
    ct_id = int(request.POST.get('ct_id'))
    params["name"] = request.POST.get('img_name')
    description = request.POST.get('description', None)
    if description:
        params["description"] = description
    short_description = request.POST.get('short_description', None)
    if short_description:
        params["short_description"] = short_description
    params["public"] = request.POST.get('public', "") == "on"

    client = get_httpclient_instance(request)
    try:
        container = client.containers(ct_id).get()
    except HttpNotFoundError:
        messages.error(request, "Selected base container does not exist.")
    except Exception:
        messages.error(request, "Some other error.")

    if container:
        try:
            client.containers(ct_id).commit.post(params)
            messages.success(request, "Sucessfully created the image.")
        except Exception as e:
            messages.error(request, api_error_message(e, params))
            return redirect('images')
    else:
        messages.error(request, "Selected base container does not exist.")

    return redirect('images')
Пример #57
0
def commit(request):
    """
    Todo: write doc.
    """

    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect("images")
    if "ct_id" not in request.POST or not request.POST.get("ct_id").isdigit() or "img_name" not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect("images")

    params = {}
    ct_id = int(request.POST.get("ct_id"))
    params["name"] = request.POST.get("img_name")
    description = request.POST.get("description", None)
    if description:
        params["description"] = description
    short_description = request.POST.get("short_description", None)
    if short_description:
        params["short_description"] = short_description
    params["public"] = request.POST.get("public", "") == "on"

    client = get_httpclient_instance(request)
    try:
        container = client.containers(ct_id).get()
    except HttpNotFoundError:
        messages.error(request, "Selected base container does not exist.")
    except Exception:
        messages.error(request, "Some other error.")

    if container:
        try:
            client.containers(ct_id).commit.post(params)
            messages.success(request, "Sucessfully created the image.")
        except Exception as e:
            messages.error(request, api_error_message(e, params))
            return redirect("images")
    else:
        messages.error(request, "Selected base container does not exist.")

    return redirect("images")
Пример #58
0
def share_add_access_groups(request):
    """
    Add access groups to the share.
    """
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('shares')
    if 'id' not in request.POST or not request.POST.get(
            'id').isdigit() or 'access_groups' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('shares')

    access_groups = request.POST.getlist('access_groups')
    share_id = request.POST.get('id')
    client = get_httpclient_instance(request)

    # cannot validate existance of groups due to visibility permissions
    # i.e. of single_user_groups, let the API handle that
    params = {}
    params['access_groups'] = access_groups

    try:
        response = client.shares(share_id).add_access_groups.post(params)
        count = response.get('count')
        if count == 0:
            messages.info(
                request,
                "{} groups successfully added to the share.".format(count))
        elif count == 1:
            messages.success(
                request,
                "{} group successfully added to the share.".format(count))
        else:
            messages.success(
                request,
                "{} groups successfully added to the share.".format(count))

    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('share_manage', share_id)
Пример #59
0
def mark_as_read(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('notifications')
    if 'id' not in request.POST or not request.POST.get('id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('notifications')

    client = get_httpclient_instance(request)

    n_id = request.POST.get('id')

    params = {"read": True}

    try:
        client.notificationlogs(n_id).patch(params)
        messages.success(request, "Notification marked as read.")
    except Exception as e:
        messages.error(request, api_error_message(e, params))

    return redirect('notifications')
Пример #60
0
def remove_member(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('groups')
    if 'group_id' not in request.POST or not request.POST.get('group_id').isdigit() \
         or 'user_id' not in request.POST  or not request.POST.get('user_id').isdigit():
        messages.error(request, "Invalid POST request.")
        return redirect('groups')

    group_id = int(request.POST.get('group_id'))
    user_id = int(request.POST.get('user_id'))

    client = get_httpclient_instance(request)

    user = client.users(user_id).get()
    group = client.collaborationgroups(group_id).get()

    if group:
        if user:
            params = {}
            params["users"] = [user_id]
            # API call
            try:
                client.collaborationgroups(group_id).remove_members.post(
                    params)
                messages.success(
                    request, "Sucessfully removed {} from the group.".format(
                        user.username))
            except Exception as e:
                messages.error(request, api_error_message(e, params))

            request.method = "GET"
            return redirect('group_manage', group.id)
        else:
            messages.error(request, "User does not exist.")
            return redirect('group_manage', group.id)
    else:
        messages.error(request, "Group does not exist.")

    return redirect('group_manage', group_id)