def create_container(request): """ Creates a container (empty object of type application/directory) """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) form = CreateContainerForm(request.POST or None) if form.is_valid(): container = form.cleaned_data['containername'] try: client.put_container(storage_url, auth_token, container, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _("Container created.")) actionlog.log(request.user.username, "create", container) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('Access denied.')) return redirect(containerview) context = utils.update_default_context(request, { 'form': form, }) return render_to_response('create_container.html', context, context_instance=RequestContext(request))
def containerview(request): """ Returns a list of all containers in current account. """ if not request.session.get('project_id'): messages.add_message(request, messages.ERROR, _('Select a project')) return HttpResponseRedirect(reverse('dashboard')) storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) page = request.GET.get('page', 1) try: account_stat, containers = client.get_account(storage_url, auth_token, http_conn=http_conn) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('Unable to list containers')) account_stat = {} containers = [] containers = _hide_containers_with_prefixes(containers) account_stat = replace_hyphens(account_stat) context = utils.update_default_context(request, { 'account_stat': account_stat, 'containers': utils.generic_pagination(containers, page), }) return render_to_response('containerview.html', context, context_instance=RequestContext(request))
def objectview(request, container, prefix=None): """ Returns list of all objects in current container. """ storage_url = get_endpoint(request, 'adminURL') public_url = get_endpoint(request, 'publicURL') + '/' + container auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) page = request.GET.get('page', 1) try: _, objects = client.get_container(storage_url, auth_token, container, delimiter='/', prefix=prefix, http_conn=http_conn) except client.ClientException as err: log.exception('{}{}'.format(ugettext('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, ugettext('Access denied.')) return redirect(containerview) prefixes = prefix_list(prefix) object_list = pseudofolder_object_list(objects, prefix, public_url) context = utils.update_default_context(request, { 'container': container, 'objects': utils.generic_pagination(object_list, page), 'prefix': prefix, 'prefixes': prefixes, }) return render_to_response("objectview.html", context, context_instance=RequestContext(request))
def object_versioning(request, container, prefix=None): storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) public_url = get_endpoint(request, 'publicURL') + '/' + container http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) objects = [] page = request.GET.get('page', 1) if request.method == 'GET': headers = client.head_container(storage_url, auth_token, container, http_conn=http_conn) version_location = headers.get('x-versions-location', None) if version_location: try: _, objects = client.get_container(storage_url, auth_token, version_location, prefix=prefix, delimiter='/', http_conn=http_conn) except client.ClientException: pass prefixes = prefix_list(prefix) object_list = pseudofolder_object_list(objects, prefix, public_url) context = utils.update_default_context(request, { 'container': container, 'objects': utils.generic_pagination(object_list, page), 'version_location': version_location, 'prefix': prefix, 'prefixes': prefixes, }) return render_to_response('container_versioning.html', dictionary=context, context_instance=RequestContext(request)) if request.method == 'POST': action = request.POST.get('action', None) if action == 'enable': enable_versioning(request, container) actionlog.log(request.user.username, "enable", 'Versioning. Container: %s' % container) elif action == 'disable': disable_versioning(request, container) actionlog.log(request.user.username, "disable", 'Versioning. Container: %s' % container) else: messages.add_message(request, messages.ERROR, ugettext('Action is required.')) return redirect(object_versioning, container=container)
def upload(request, container, prefix=None): """ Display upload form using swift formpost """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) redirect_url = 'http://{}'.format(request.get_host()) redirect_url += reverse('objectview', kwargs={ 'container': container, }) swift_url = storage_url + '/' + container + '/' if prefix: swift_url += prefix redirect_url += prefix redirect_url += '?p={}'.format(request.session.get('project_id')) url_parts = urlparse(swift_url) path = url_parts.path max_file_size = 5 * 1024 * 1024 * 1024 max_file_count = 1 expires = int(time.time() + 15 * 60) key = get_temp_key(storage_url, auth_token, http_conn) if not key: messages.add_message(request, messages.ERROR, _('Access denied.')) if prefix: return redirect(objectview, container=container, prefix=prefix) else: return redirect(objectview, container=container) hmac_body = '%s\n%s\n%s\n%s\n%s' % (path, redirect_url, max_file_size, max_file_count, expires) signature = hmac.new(key, hmac_body, sha1).hexdigest() prefixes = prefix_list(prefix) context = utils.update_default_context( request, { 'swift_url': swift_url, 'redirect_url': redirect_url, 'max_file_size': max_file_size, 'max_file_count': max_file_count, 'expires': expires, 'signature': signature, 'container': container, 'prefix': prefix if prefix is not None else '', 'prefixes': prefixes, }) return render_to_response('upload_form.html', context, context_instance=RequestContext(request))
def create_pseudofolder(request, container, prefix=None): """ Creates a pseudofolder (empty object of type application/directory) """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) form = PseudoFolderForm(request.POST or None) if form.is_valid(): foldername = request.POST.get('foldername', None) if prefix: foldername = prefix + '/' + foldername foldername = os.path.normpath(foldername) foldername = foldername.strip('/') foldername += '/' content_type = 'application/directory' obj = None try: client.put_object(storage_url, auth_token, container, foldername, obj, content_type=content_type, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('Pseudofolder created.')) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('Access denied.')) if prefix: actionlog.log(request.user.username, "create", foldername) return redirect(objectview, container=container, prefix=prefix) actionlog.log(request.user.username, "create", foldername) return redirect(objectview, container=container) prefixes = prefix_list(prefix) context = utils.update_default_context( request, { 'container': container, 'prefix': prefix, 'prefixes': prefixes, 'form': form, }) return render_to_response('create_pseudofolder.html', context, context_instance=RequestContext(request))
def upload(request, container, prefix=None): """ Display upload form using swift formpost """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) redirect_url = 'http://{}'.format(request.get_host()) redirect_url += reverse('objectview', kwargs={'container': container, }) swift_url = storage_url + '/' + container + '/' if prefix: swift_url += prefix redirect_url += prefix redirect_url += '?p={}'.format(request.session.get('project_id')) url_parts = urlparse(swift_url) path = url_parts.path max_file_size = 5 * 1024 * 1024 * 1024 max_file_count = 1 expires = int(time.time() + 15 * 60) key = get_temp_key(storage_url, auth_token, http_conn) if not key: messages.add_message(request, messages.ERROR, _('Access denied.')) if prefix: return redirect(objectview, container=container, prefix=prefix) else: return redirect(objectview, container=container) hmac_body = '%s\n%s\n%s\n%s\n%s' % (path, redirect_url, max_file_size, max_file_count, expires) signature = hmac.new(key, hmac_body, sha1).hexdigest() prefixes = prefix_list(prefix) context = utils.update_default_context(request, { 'swift_url': swift_url, 'redirect_url': redirect_url, 'max_file_size': max_file_size, 'max_file_count': max_file_count, 'expires': expires, 'signature': signature, 'container': container, 'prefix': prefix if prefix is not None else '', 'prefixes': prefixes, }) return render_to_response('upload_form.html', context, context_instance=RequestContext(request))
def create_pseudofolder(request, container, prefix=None): """ Creates a pseudofolder (empty object of type application/directory) """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) form = PseudoFolderForm(request.POST or None) if form.is_valid(): foldername = request.POST.get('foldername', None) if prefix: foldername = prefix + '/' + foldername foldername = os.path.normpath(foldername) foldername = foldername.strip('/') foldername += '/' content_type = 'application/directory' obj = None try: client.put_object(storage_url, auth_token, container, foldername, obj, content_type=content_type, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('Pseudofolder created.')) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('Access denied.')) if prefix: actionlog.log(request.user.username, "create", foldername) return redirect(objectview, container=container, prefix=prefix) actionlog.log(request.user.username, "create", foldername) return redirect(objectview, container=container) prefixes = prefix_list(prefix) context = utils.update_default_context(request, { 'container': container, 'prefix': prefix, 'prefixes': prefixes, 'form': form, }) return render_to_response('create_pseudofolder.html', context, context_instance=RequestContext(request))
def containerview(request): """ Returns a list of all containers in current account. """ if not request.session.get('project_id'): messages.add_message(request, messages.ERROR, _('Select a project')) return HttpResponseRedirect(reverse('dashboard')) storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) page = request.GET.get('page', 1) try: account_stat, containers = client.get_account(storage_url, auth_token, http_conn=http_conn) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('Unable to list containers')) account_stat = {} containers = [] containers = _hide_containers_with_prefixes(containers) account_stat = replace_hyphens(account_stat) context = utils.update_default_context( request, { 'account_stat': account_stat, 'containers': utils.generic_pagination(containers, page), }) return render_to_response('containerview.html', context, context_instance=RequestContext(request))
def objectview(request, container, prefix=None): """ Returns list of all objects in current container. """ storage_url = get_endpoint(request, 'adminURL') public_url = get_endpoint(request, 'publicURL') + '/' + container auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) page = request.GET.get('page', 1) try: _, objects = client.get_container(storage_url, auth_token, container, delimiter='/', prefix=prefix, http_conn=http_conn) except client.ClientException as err: log.exception('{}{}'.format( ugettext('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, ugettext('Access denied.')) return redirect(containerview) prefixes = prefix_list(prefix) object_list = pseudofolder_object_list(objects, prefix, public_url) context = utils.update_default_context( request, { 'container': container, 'objects': utils.generic_pagination(object_list, page), 'prefix': prefix, 'prefixes': prefixes, }) return render_to_response("objectview.html", context, context_instance=RequestContext(request))
def edit_cors(request, container): """ Edit CORS on given container. """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) if request.method == 'POST': form = AddCORSForm(request.POST) if form.is_valid(): cors = get_cors(storage_url, auth_token, container, http_conn) cors = remove_duplicates_from_cors(cors) host = form.cleaned_data['host'] if host: cors += " {}".format(host) headers = { 'x-container-meta-access-control-allow-origin': cors.strip() } try: client.post_container(storage_url, auth_token, container, headers=headers, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('CORS updated')) actionlog.log(request.user.username, "update", 'headers: %s, container: %s' % (headers, container)) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('CORS update failed.')) if request.method == 'GET': delete = request.GET.get('delete', None) if delete: host = delete.split(' ') cors = get_cors(storage_url, auth_token, container, http_conn) new_cors = '' for element in cors.split(' '): if element not in host: new_cors += element new_cors += ' ' headers = { 'x-container-meta-access-control-allow-origin': new_cors.strip() } try: client.post_container(storage_url, auth_token, container, headers=headers, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('CORS removed.')) actionlog.log(request.user.username, "delete", 'headers: %s, container: %s' % (headers, container)) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('CORS update failed.')) cors = get_cors(storage_url, auth_token, container, http_conn) context = utils.update_default_context(request, { 'container': container, 'session': request.session, 'cors': [], }) if cors != '': cors = remove_duplicates_from_cors(cors) for entry in cors.split(' '): context['cors'].append(entry) return render_to_response('edit_cors.html', context, context_instance=RequestContext(request))
def edit_acl(request, container): """ Edit ACLs on given container. """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) if request.method == 'POST': form = AddACLForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] (readers, writers) = get_acls(storage_url, auth_token, container, http_conn) readers = remove_duplicates_from_acl(readers) writers = remove_duplicates_from_acl(writers) if form.cleaned_data['read']: readers += ",%s" % username if form.cleaned_data['write']: writers += ",%s" % username headers = {'X-Container-Read': readers, 'X-Container-Write': writers} try: client.post_container(storage_url, auth_token, container, headers=headers, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('ACLs updated')) actionlog.log(request.user.username, "update", 'headers: %s, container: %s' % (headers, container)) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('ACL update failed.')) if request.method == 'GET': delete = request.GET.get('delete', None) if delete: users = delete.split(',') (readers, writers) = get_acls(storage_url, auth_token, container, http_conn) new_readers = '' for element in readers.split(','): if element not in users: new_readers += element new_readers += "," new_writers = '' for element in writers.split(','): if element not in users: new_writers += element new_writers += "," headers = {'X-Container-Read': new_readers, 'X-Container-Write': new_writers} try: client.post_container(storage_url, auth_token, container, headers=headers, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('ACL removed.')) actionlog.log(request.user.username, "delete", 'headers: %s, container: %s' % (headers, container)) except client.ClientException as err: log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('ACL update failed.')) (readers, writers) = get_acls(storage_url, auth_token, container, http_conn) acls = {} if readers != '': readers = remove_duplicates_from_acl(readers) for entry in readers.split(','): acls[entry] = {} acls[entry]['read'] = True acls[entry]['write'] = False if writers != '': writers = remove_duplicates_from_acl(writers) for entry in writers.split(','): if entry not in acls: acls[entry] = {} acls[entry]['read'] = False acls[entry]['write'] = True public = False if acls.get('.r:*', False): public = True context = utils.update_default_context(request, { 'container': container, 'session': request.session, 'acls': acls, 'public': public }) return render_to_response('edit_acl.html', context, context_instance=RequestContext(request))
def edit_cors(request, container): """ Edit CORS on given container. """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) if request.method == 'POST': form = AddCORSForm(request.POST) if form.is_valid(): cors = get_cors(storage_url, auth_token, container, http_conn) cors = remove_duplicates_from_cors(cors) host = form.cleaned_data['host'] if host: cors += " {}".format(host) headers = { 'x-container-meta-access-control-allow-origin': cors.strip() } try: client.post_container(storage_url, auth_token, container, headers=headers, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('CORS updated')) actionlog.log( request.user.username, "update", 'headers: %s, container: %s' % (headers, container)) except client.ClientException as err: log.exception('{}{}'.format( _('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('CORS update failed.')) if request.method == 'GET': delete = request.GET.get('delete', None) if delete: host = delete.split(' ') cors = get_cors(storage_url, auth_token, container, http_conn) new_cors = '' for element in cors.split(' '): if element not in host: new_cors += element new_cors += ' ' headers = { 'x-container-meta-access-control-allow-origin': new_cors.strip() } try: client.post_container(storage_url, auth_token, container, headers=headers, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('CORS removed.')) actionlog.log( request.user.username, "delete", 'headers: %s, container: %s' % (headers, container)) except client.ClientException as err: log.exception('{}{}'.format( _('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('CORS update failed.')) cors = get_cors(storage_url, auth_token, container, http_conn) context = utils.update_default_context(request, { 'container': container, 'session': request.session, 'cors': [], }) if cors != '': cors = remove_duplicates_from_cors(cors) for entry in cors.split(' '): context['cors'].append(entry) return render_to_response('edit_cors.html', context, context_instance=RequestContext(request))
def object_versioning(request, container, prefix=None): storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) public_url = get_endpoint(request, 'publicURL') + '/' + container http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) objects = [] page = request.GET.get('page', 1) if request.method == 'GET': headers = client.head_container(storage_url, auth_token, container, http_conn=http_conn) version_location = headers.get('x-versions-location', None) if version_location: try: _, objects = client.get_container(storage_url, auth_token, version_location, prefix=prefix, delimiter='/', http_conn=http_conn) except client.ClientException: pass prefixes = prefix_list(prefix) object_list = pseudofolder_object_list(objects, prefix, public_url) context = utils.update_default_context( request, { 'container': container, 'objects': utils.generic_pagination(object_list, page), 'version_location': version_location, 'prefix': prefix, 'prefixes': prefixes, }) return render_to_response('container_versioning.html', dictionary=context, context_instance=RequestContext(request)) if request.method == 'POST': action = request.POST.get('action', None) if action == 'enable': enable_versioning(request, container) actionlog.log(request.user.username, "enable", 'Versioning. Container: %s' % container) elif action == 'disable': disable_versioning(request, container) actionlog.log(request.user.username, "disable", 'Versioning. Container: %s' % container) else: messages.add_message(request, messages.ERROR, ugettext('Action is required.')) return redirect(object_versioning, container=container)
def edit_acl(request, container): """ Edit ACLs on given container. """ storage_url = get_endpoint(request, 'adminURL') auth_token = get_token_id(request) http_conn = client.http_connection(storage_url, insecure=settings.SWIFT_INSECURE) if request.method == 'POST': form = AddACLForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] (readers, writers) = get_acls(storage_url, auth_token, container, http_conn) readers = remove_duplicates_from_acl(readers) writers = remove_duplicates_from_acl(writers) if form.cleaned_data['read']: readers += ",%s" % username if form.cleaned_data['write']: writers += ",%s" % username headers = { 'X-Container-Read': readers, 'X-Container-Write': writers } try: client.post_container(storage_url, auth_token, container, headers=headers, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('ACLs updated')) actionlog.log( request.user.username, "update", 'headers: %s, container: %s' % (headers, container)) except client.ClientException as err: log.exception('{}{}'.format( _('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('ACL update failed.')) if request.method == 'GET': delete = request.GET.get('delete', None) if delete: users = delete.split(',') (readers, writers) = get_acls(storage_url, auth_token, container, http_conn) new_readers = '' for element in readers.split(','): if element not in users: new_readers += element new_readers += "," new_writers = '' for element in writers.split(','): if element not in users: new_writers += element new_writers += "," headers = { 'X-Container-Read': new_readers, 'X-Container-Write': new_writers } try: client.post_container(storage_url, auth_token, container, headers=headers, http_conn=http_conn) messages.add_message(request, messages.SUCCESS, _('ACL removed.')) actionlog.log( request.user.username, "delete", 'headers: %s, container: %s' % (headers, container)) except client.ClientException as err: log.exception('{}{}'.format( _('Exception:').encode('UTF-8'), err)) messages.add_message(request, messages.ERROR, _('ACL update failed.')) (readers, writers) = get_acls(storage_url, auth_token, container, http_conn) acls = {} if readers != '': readers = remove_duplicates_from_acl(readers) for entry in readers.split(','): acls[entry] = {} acls[entry]['read'] = True acls[entry]['write'] = False if writers != '': writers = remove_duplicates_from_acl(writers) for entry in writers.split(','): if entry not in acls: acls[entry] = {} acls[entry]['read'] = False acls[entry]['write'] = True public = False if acls.get('.r:*', False): public = True context = utils.update_default_context( request, { 'container': container, 'session': request.session, 'acls': acls, 'public': public }) return render_to_response('edit_acl.html', context, context_instance=RequestContext(request))