def get(self, request): if request.user.is_authenticated: return redirect(settings.LOGIN_REDIRECT_URL) else: context = Util.get_context(request) Util.pop_value(request, context, 'otp_hidden', True) return render(request, 'dashboard/login.html', context=context)
def get(self, request, app_id): cmd = request.GET.get('cmd', None) platform = request.GET.get('platform', 'python3') adapter = DjangoAdapter(app_id, request) allocate_resource_in_background(adapter) if cmd == 'download_sdk': Util.log('app-overview', request.user, 'download-sdk-{}'.format(platform)) sdk_bin = adapter.generate_sdk(platform) if sdk_bin is None: Util.add_alert( request, 'API 를 초기화 하고 있습니다. 상황에 따라 최대 3분 정도 소요될 수 있습니다.') return redirect(request.path_info) response = HttpResponse(sdk_bin, content_type='application/x-binary') response[ 'Content-Disposition'] = 'attachment; filename=%s' % os.path.basename( 'AWS Interface SDK.zip') return response else: context = Util.get_context(request) context['app_id'] = app_id app = App.objects.get(id=app_id, user=request.user) context['app_name'] = app.name return render(request, 'dashboard/app/overview.html', context=context)
def get(self, request, app_id, function_name, function_version=None): context = Util.get_context(request) context['app_id'] = app_id s = time.time() adapter = DjangoAdapter(app_id, request) with adapter.open_api_logic() as logic_api: print('s:', time.time() - s) function = logic_api.get_function(function_name, function_version) print('s:', time.time() - s) function = function['item'] file_paths = logic_api.get_function_file_paths( function_name, function_version).get('file_paths', []) handler_prefix = '/'.join(function['handler'].split('.')[:-1]) print('s:', time.time() - s) current_path = None for file_path in file_paths: if file_path.startswith(handler_prefix): current_path = file_path context['function'] = function context['file_paths'] = file_paths context['current_path'] = current_path context['current_file'] = logic_api.get_function_file( function_name, current_path, function_version).get('item') print('s:', time.time() - s) return render(request, 'dashboard/app/logic_edit.html', context=context)
def get(self, request, app_id): context = Util.get_context(request) adapter = DjangoAdapter(app_id, request) event_source = request.GET.get('event_source', None) event_name = request.GET.get('event_name', None) event_param = request.GET.get('event_param', None) user_id = request.GET.get('user_id', None) context['app_id'] = app_id if event_source: context['event_source'] = event_source if event_name: context['event_name'] = event_name if event_param: context['event_param'] = event_param if user_id: context['user_id'] = user_id with adapter.open_api_log() as log: log_result = log.get_logs(event_source, event_name, event_param, user_id) context['logs'], context['end_key'] = log_result[ 'items'], log_result['end_key'] return render(request, 'dashboard/app/log.html', context=context)
def get(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id app = App.objects.get(id=app_id, user=request.user) context['app_name'] = app.name return render(request, 'dashboard/app/guide.html', context=context)
def post(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) with adapter.open_api_storage() as storage_api: cmd = request.POST['cmd'] if cmd == 'upload_b64': # 분할 업로드 file_bin = request.FILES['file_bin'] file_name = request.POST['file_name'] read_groups = json.loads(request.POST.get('read_groups')) write_groups = json.loads(request.POST.get('write_groups')) parent_file_id = None def divide_chunks(text, n): for i in range(0, len(text), n): yield text[i:i + n] raw_base64 = file_bin.read() raw_base64 = base64.b64encode(raw_base64) raw_base64 = raw_base64.decode('utf-8') base64_chunks = divide_chunks(raw_base64, 1024 * 1024 * 4) # 4mb for base64_chunk in base64_chunks: result = storage_api.upload_b64(parent_file_id, file_name, base64_chunk, read_groups, write_groups) parent_file_id = result.get('file_id') return JsonResponse(result) elif cmd == 'get_b64_info_items': # For admins start_key = request.POST.get('start_key', None) result = storage_api.get_b64_info_items(start_key, reverse=True) return JsonResponse(result) elif cmd == 'delete_b64': file_id = request.POST['file_id'] result = storage_api.delete_b64(file_id) return JsonResponse(result) elif cmd == 'get_policy_code': mode = request.POST.get('mode') result = storage_api.get_policy_code(mode) return JsonResponse(result) elif cmd == 'put_policy': mode = request.POST.get('mode') code = request.POST.get('code') result = storage_api.put_policy(mode, code) return JsonResponse(result) elif cmd == 'get_file_rows': start_key = request.POST.get('start_key', None) result = storage_api.get_b64_info_items(start_key, reverse=True) template = loader.get_template('dashboard/app/component/storage_file_table_row.html') files = result.get('items', []) end_key = result.get('end_key', None) context = { 'app_id': app_id, 'files': files, } result = { 'file_rows': template.render(context, request), 'end_key': end_key } return JsonResponse(result)
def post(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id cmd = request.POST.get('cmd', None) adapter = DjangoAdapter(app_id, request) with adapter.open_api_logic() as logic_api: if cmd == 'create_function': zip_file = request.FILES['zip_file'] function_name = request.POST['function_name'] description = request.POST['description'] runtime = request.POST['runtime'] handler = request.POST['handler'] sdk_config = get_sdk_config(adapter) zip_file.seek(0) zip_file_bin = zip_file.read() zip_file_bin = base64.b64encode(zip_file_bin) zip_file_bin = zip_file_bin.decode('utf-8') if not description: description = None logic_api.create_function(function_name, description, runtime, handler, sdk_config, zip_file_bin, True) elif cmd == 'create_function_test': test_name = request.POST.get('test_name') function_name = request.POST.get('function_name') test_input = request.POST.get('test_input') logic_api.create_function_test(test_name, function_name, test_input) elif cmd == 'run_function': with adapter.open_sdk() as sdk_client: # TODO SDK Sign-up Login and authentication takes a lot of time. # We are planning to store the SDK ID Password in the session. function_name = request.POST.get('function_name') payload = request.POST.get('payload') payload = json.loads(payload) start = time.time() data = sdk_client.logic_run_function( function_name, payload) end = time.time() data['duration'] = end - start return JsonResponse(data) elif cmd == 'delete_function_test': test_name = request.POST.get('test_name') logic_api.delete_function_test(test_name) elif cmd == 'delete_function': function_name = request.POST.get('function_name') logic_api.delete_function(function_name) elif cmd == 'create_webhook': name = request.POST.get('name') description = request.POST.get('description') function_name = request.POST.get('function_name') logic_api.create_webhook(name, description, function_name) elif cmd == 'delete_webhook': name = request.POST.get('name') logic_api.delete_webhook(name) return redirect(request.path_info) # Redirect back
def get(self, request): context = Util.get_context(request) try: apps = App.objects.filter(user=request.user) apps = [{'id': app.id, 'creation_date': app.creation_date, 'name': app.name} for app in apps] context['apps'] = apps except ObjectDoesNotExist: context['apps'] = [] return render(request, 'dashboard/apps.html', context=context)
def get(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) with adapter.open_api_auth() as auth_api, adapter.open_api_logic( ) as logic_api: context['user_groups'] = auth_api.get_user_groups()['groups'] context['functions'] = logic_api.get_functions()['items'] return render(request, 'dashboard/app/schedule.html', context=context)
def get(self, request, app_id): context = Util.get_context(request) app = App.objects.get(id=app_id, user=request.user) context['app_id'] = app_id context['app_name'] = app.name context['categories'] = CATEGORIES.items() adapter = DjangoAdapter(app_id, request) with adapter.open_api_logic() as logic_api: context['functions'] = logic_api.get_functions()['items'] return render(request, 'dashboard/app/marketplace_create.html', context=context)
def get(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) # allocate_resource_in_background(adapter) with adapter.open_api_auth() as auth_api, adapter.open_api_storage( ) as storage_api: cmd = request.GET.get('cmd', None) if cmd == 'download_file': file_path = request.GET['file_path'] file_name = file_path.split('/').pop() file_bin_b64 = storage_api.download_b64(file_path) file_bin = base64.b64decode(file_bin_b64) response = HttpResponse(file_bin, content_type='application/x-binary') file_name = file_name.encode('utf8').decode('ISO-8859-1') response[ 'Content-Disposition'] = 'attachment; filename=%s' % os.path.basename( file_name) return response elif cmd == 'download_b64': file_id = request.GET['file_id'] string_file_b64 = None file_name = 'file' while file_id: result = storage_api.download_b64(file_id) file_id = result.get('parent_file_id', None) string_b64_chuck = result.get('file_b64') if string_file_b64: string_file_b64 = string_b64_chuck + string_file_b64 else: string_file_b64 = string_b64_chuck file_name = result.get('file_name', file_name) string_b64 = string_file_b64.encode('utf-8') file_bin = base64.b64decode(string_b64) response = HttpResponse(file_bin, content_type='application/x-binary') file_name = file_name.encode('utf8').decode('ISO-8859-1') response[ 'Content-Disposition'] = 'attachment; filename=%s' % os.path.basename( file_name) return response else: result = storage_api.get_b64_info_items(start_key=None, reverse=True) context['app_id'] = app_id context['b64_info'] = result context['user_groups'] = auth_api.get_user_groups()['groups'] return render(request, 'dashboard/app/storage.html', context=context)
def post(self, request, app_id, function_name, function_version=None): context = Util.get_context(request) context['app_id'] = app_id cmd = request.POST.get('cmd', None) adapter = DjangoAdapter(app_id, request) with adapter.open_api_logic() as logic_api: if cmd == 'get_function_file': function_name = request.POST.get('function_name') function_version = request.POST.get('function_version', None) file_path = request.POST.get('file_path') result = logic_api.get_function_file(function_name, file_path, function_version) return JsonResponse(result) elif cmd == 'put_function_file': function_name = request.POST.get('function_name') function_version = request.POST.get('function_version', None) file_path = request.POST.get('file_path') file_content = request.POST.get('file_content') file_type = request.POST.get('file_type', 'text') result = logic_api.put_function_file(function_name, file_path, file_content, file_type, function_version) # allocate_resource_in_background(adapter) return JsonResponse(result) elif cmd == 'update_function': function_name = request.POST.get('function_name', None) function_version = request.POST.get('function_version', None) description = request.POST.get('description', None) handler = request.POST.get('handler', None) runtime = request.POST.get('runtime', None) use_logging = request.POST.get('use_logging', None) == 'true' use_traceback = request.POST.get('use_traceback', None) == 'true' print('use_logging:', use_logging, type(use_logging)) print('use_traceback:', use_traceback, type(use_traceback)) sdk_config = get_sdk_config(adapter) result = logic_api.update_function( function_name=function_name, description=description, handler=handler, runtime=runtime, sdk_config=sdk_config, function_version=function_version, use_logging=use_logging, use_traceback=use_traceback) return JsonResponse(result) elif cmd == 'get_function_file_paths': function_name = request.POST.get('function_name') function_version = request.POST.get('function_version', None) item = logic_api.get_function_file_paths( function_name, function_version) return JsonResponse(item)
def get(self, request, app_id): cache_key = 'bill-context-' + app_id context = Util.get_cache(request, cache_key) if context: return render(request, 'dashboard/app/bill.html', context=context) context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) with adapter.open_api_bill() as api: context['cost'] = api.get_current_cost() context['usages'] = api.get_current_usage_costs() Util.set_cache(request, cache_key, context) return render(request, 'dashboard/app/bill.html', context=context)
def get(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) with adapter.open_api_auth() as auth_api, adapter.open_api_logic( ) as logic_api: context['user_groups'] = auth_api.get_user_groups()['groups'] context['functions'] = logic_api.get_functions()['items'] context['function_tests'] = logic_api.get_function_tests()['items'] webhooks = logic_api.get_webhooks()['items'] for webhook in webhooks: webhook['url'] = logic_api.get_webhook_url( webhook['name'])['url'] context['webhooks'] = webhooks return render(request, 'dashboard/app/logic.html', context=context)
def get(self, request, app_id, marketplace_logic_id): context = Util.get_context(request) app = App.objects.get(id=app_id, user=request.user) try: marketplace_logic = MarketplaceLogic.objects.get( id=marketplace_logic_id) except ObjectDoesNotExist as ex: print(ex) return redirect('marketplace', app_id) context['app_id'] = app_id context['app_name'] = app.name context['marketplace_logic'] = marketplace_logic return render(request, 'dashboard/app/marketplace_detail.html', context=context)
def post(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id cmd = request.POST.get('cmd', None) adapter = DjangoAdapter(app_id, request) with adapter.open_api_schedule() as schedule_api: if cmd == 'get_schedules': start_key = request.POST.get('start_key', None) return JsonResponse(schedule_api.get_schedules(start_key)) elif cmd == 'create_schedule': schedule_name = request.POST.get('schedule_name') schedule_expression = request.POST.get('schedule_expression') function_name = request.POST.get('function_name') payload = request.POST.get('payload') print(schedule_name, schedule_expression, function_name, payload) try: session_id = adapter.generate_session_id(['admin']) result = schedule_api.create_schedule( schedule_name, schedule_expression, function_name, payload, session_id) return JsonResponse(result) except Exception as e: return JsonResponse({'message': str(e), 'success': False}) elif cmd == 'delete_schedule': schedule_name = request.POST.get('schedule_name') return JsonResponse( schedule_api.delete_schedule(schedule_name)) elif cmd == 'get_schedule': schedule_name = request.POST.get('schedule_name') return JsonResponse(schedule_api.get_schedule(schedule_name)) elif cmd == 'get_schedule_rows': start_key = request.POST.get('start_key', None) result = schedule_api.get_schedules(start_key, reverse=True) template = loader.get_template( 'dashboard/app/component/schedule_table_row.html') items = result.get('items', []) print(items) end_key = result.get('end_key', None) context = { 'items': items, } result = { 'rows': template.render(context, request), 'end_key': end_key } return JsonResponse(result)
def post(self, request, app_id): context = Util.get_context(request) app = App.objects.get(id=app_id, user=request.user) context['app_id'] = app_id context['app_name'] = app.name adapter = DjangoAdapter(app_id, request) cmd = request.POST.get('cmd', None) if cmd == 'upload_marketplace_logic': title = request.POST.get('title') description = request.POST.get('description') category = request.POST.get('category') logo_image = request.FILES.get('logo_image') content = request.POST.get('content') function_name = request.POST.get('function_name') with adapter.open_api_logic() as logic_api: result = logic_api.get_function(function_name) if 'error' in result: return JsonResponse(result) logic_function = result['item'] handler = logic_function['handler'] runtime = logic_function['runtime'] function_zip_b64 = logic_api.get_function_zip_b64( function_name)['item']['base64'] function_zip_b64 = function_zip_b64.encode('utf-8') function_zip = base64.b64decode(function_zip_b64) function_zip_name = '{}.zip'.format(function_name) function_zip_file = SimpleUploadedFile( function_zip_name, function_zip, 'application/octet-stream') marketplace_logic = MarketplaceLogic(user=request.user) marketplace_logic.title = title marketplace_logic.description = description marketplace_logic.category = category marketplace_logic.logo_image = logo_image marketplace_logic.content = content marketplace_logic.function_zip_file = function_zip_file marketplace_logic.function_name = function_name marketplace_logic.handler = handler marketplace_logic.runtime = runtime marketplace_logic.save() return JsonResponse( data={'marketplace_logic_id': marketplace_logic.id})
def get(self, request, app_id): context = Util.get_context(request) app = App.objects.get(id=app_id, user=request.user) my_marketplace_logics = MarketplaceLogic.objects.filter( user=request.user).order_by('creation_date').reverse()[:100] marketplace_logics = MarketplaceLogic.objects.order_by( 'creation_date').reverse()[:100] top_setup_marketplace_logics = MarketplaceLogic.objects.order_by( 'setup_count').reverse()[:2] context['app_id'] = app_id context['app_name'] = app.name context['marketplace_logics'] = marketplace_logics context['my_marketplace_logics'] = my_marketplace_logics context['top_setup_marketplace_logics'] = top_setup_marketplace_logics return render(request, 'dashboard/app/marketplace.html', context=context)
def get(self, request, app_id): start = time.time() context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) # allocate_resource_in_background(adapter) with adapter.open_api_auth() as api: user_groups = api.get_user_groups()['groups'] for user_group in user_groups: user_group.setdefault('permissions', []) context['user_groups'] = user_groups context['user_count'] = api.get_user_count(count_system_user=True) context['session_count'] = api.get_session_count() context['users'] = api.get_users() context['visible_user_fields'] = [ 'id', 'creation_date', 'email', 'groups' ] context['none_extra_fields'] = [ 'id', 'creation_date', 'email', 'groups', 'password_hash', 'salt', 'partition', 'login_method' ] context['sessions'] = api.get_sessions() context['email_login'] = api.get_login_method( 'email_login')['item'] context['guest_login'] = api.get_login_method( 'guest_login')['item'] context['facebook_login'] = api.get_login_method( 'facebook_login')['item'] context['google_login'] = api.get_login_method( 'google_login')['item'] context['naver_login'] = api.get_login_method( 'naver_login')['item'] context['kakao_login'] = api.get_login_method( 'kakao_login')['item'] context['all_permissions'] = api.get_all_permissions( )['permissions'] return render(request, 'dashboard/app/auth.html', context=context)
def post(self, request, app_id, marketplace_logic_id): context = Util.get_context(request) app = App.objects.get(id=app_id, user=request.user) marketplace_logic = MarketplaceLogic.objects.get( id=marketplace_logic_id) cmd = request.POST.get('cmd', None) if cmd == 'delete_marketplace_logic': if marketplace_logic.user == request.user: marketplace_logic.delete() return JsonResponse({}) else: return JsonResponse({'error': error.PERMISSION_DENIED}) elif cmd == 'setup_marketplace_logic': adapter = DjangoAdapter(app_id, request) function_name = request.POST.get('function_name', None) if not function_name: function_name = marketplace_logic.function_name marketplace_logic_id = self.setup_marketplace_logic( adapter, marketplace_logic, function_name) self.increase_setup_count(marketplace_logic, request.user) return marketplace_logic_id
def get(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) allocate_resource_in_background(adapter) with adapter.open_api_auth() as auth_api, adapter.open_api_database( ) as database_api: partitions = database_api.get_partitions().get('items', []) partition_dict = {} for partition in partitions: name = partition['name'] result = database_api.get_item_count(name) partition_dict[name] = { 'name': name, 'item_count': result['item']['count'] } partitions = partition_dict.values() context['user_groups'] = auth_api.get_user_groups()['groups'] context['partitions'] = partitions return render(request, 'dashboard/app/database.html', context=context)
def get(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) allocate_resource_in_background(adapter) with adapter.open_api_auth() as api: context['user_groups'] = api.get_user_groups()['groups'] context['user_count'] = api.get_user_count() context['session_count'] = api.get_session_count() context['users'] = api.get_users() context['visible_user_fields'] = ['id', 'creation_date', 'email', 'groups'] context['none_extra_fields'] = ['id', 'creation_date', 'email', 'groups', 'password_hash', 'salt', 'partition', 'login_method'] context['sessions'] = api.get_sessions() context['email_login'] = api.get_login_method('email_login')['item'] context['guest_login'] = api.get_login_method('guest_login')['item'] context['facebook_login'] = api.get_login_method('facebook_login')['item'] context['all_permissions'] = api.get_all_permissions()['permissions'] return render(request, 'dashboard/app/auth.html', context=context)
def post(self, request, app_id, function_name): context = Util.get_context(request) context['app_id'] = app_id cmd = request.POST.get('cmd', None) adapter = DjangoAdapter(app_id, request) with adapter.open_api_logic() as logic_api: if cmd == 'get_function_file': function_name = request.POST.get('function_name') file_path = request.POST.get('file_path') result = logic_api.get_function_file(function_name, file_path) return JsonResponse(result) elif cmd == 'put_function_file': function_name = request.POST.get('function_name') file_path = request.POST.get('file_path') file_content = request.POST.get('file_content') file_type = request.POST.get('file_type', 'text') result = logic_api.put_function_file(function_name, file_path, file_content, file_type) allocate_resource_in_background(adapter) return JsonResponse(result) elif cmd == 'update_function': function_name = request.POST.get('function_name', None) description = request.POST.get('description', None) handler = request.POST.get('handler', None) runtime = request.POST.get('runtime', None) sdk_config = get_sdk_config(adapter) result = logic_api.update_function(function_name=function_name, description=description, handler=handler, runtime=runtime, sdk_config=sdk_config) return JsonResponse(result) elif cmd == 'get_function_file_paths': function_name = request.POST.get('function_name') item = logic_api.get_function_file_paths(function_name) return JsonResponse(item)
def post(self, request, app_id, marketplace_logic_id): context = Util.get_context(request) app = App.objects.get(id=app_id, user=request.user) context['app_id'] = app_id context['app_name'] = app.name adapter = DjangoAdapter(app_id, request) cmd = request.POST.get('cmd', None) if cmd == 'edit_marketplace_logic': title = request.POST.get('title') description = request.POST.get('description') category = request.POST.get('category') logo_image = request.FILES.get('logo_image') content = request.POST.get('content') function_name = request.POST.get('function_name') change_logo_image = request.POST.get('change_logo_image', False) change_function = request.POST.get('change_function', False) if change_logo_image == 'true': change_logo_image = True elif change_logo_image == 'false': change_logo_image = False if change_function == 'true': change_function = True elif change_function == 'false': change_function = False with adapter.open_api_logic() as logic_api: logic_function = logic_api.get_function(function_name)['item'] handler = logic_function['handler'] runtime = logic_function['runtime'] function_zip_b64 = logic_api.get_function_zip_b64( function_name)['item']['base64'] function_zip_b64 = function_zip_b64.encode('utf-8') function_zip = base64.b64decode(function_zip_b64) function_zip_name = '{}.zip'.format(function_name) function_zip_file = SimpleUploadedFile( function_zip_name, function_zip, 'application/octet-stream') marketplace_logic = MarketplaceLogic.objects.get( id=marketplace_logic_id, user=request.user) marketplace_logic.title = title marketplace_logic.description = description marketplace_logic.category = category if change_logo_image: marketplace_logic.logo_image = logo_image if change_function: # Needs security verification by AWS-I team marketplace_logic.verified = False marketplace_logic.function_zip_file = function_zip_file marketplace_logic.function_name = function_name marketplace_logic.handler = handler marketplace_logic.runtime = runtime marketplace_logic.content = content marketplace_logic.save() return JsonResponse( data={'marketplace_logic_id': marketplace_logic.id})
def get(self, request): context = Util.get_context(request) context['email'] = request.user.email return render(request, 'dashboard/accesskey.html', context=context)
def get(self, request): context = Util.get_context(request) return render(request, 'dashboard/register.html', context=context)
def post(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) with adapter.open_api_database() as database_api: cmd = request.POST['cmd'] if cmd == 'add_partition': partition_name = request.POST['partition_name'] _ = database_api.create_partition(partition_name) elif cmd == 'add_item': partition = request.POST['partition'] read_groups = request.POST.getlist('read_groups[]') write_groups = request.POST.getlist('write_groups[]') _ = database_api.create_item(partition, {}, read_groups, write_groups) elif cmd == 'add_field': item_id = request.POST['item_id'] field_name = request.POST['field_name'] field_value = request.POST['field_value'] field_type = request.POST['field_type'] if field_type == 'S': field_value = str(field_value) elif field_type == 'N': field_value = Decimal(field_value) elif field_type == 'L': field_value = list(field_value) _ = database_api.put_item_field(item_id, field_name, field_value) elif cmd == 'delete_partition': partition_name = request.POST['partition_name'] _ = database_api.delete_partition(partition_name) elif cmd == 'delete_partitions': partitions = request.POST.getlist('partitions[]') _ = database_api.delete_partitions(partitions) elif cmd == 'get_items': partition = request.POST['partition'] start_key = request.POST.get('start_key', None) result = database_api.get_items(partition, start_key=start_key) result = Util.encode_dict(result) return JsonResponse(result) elif cmd == 'get_item': item_id = request.POST['item_id'] result = database_api.get_item(item_id) result = Util.encode_dict(result) return JsonResponse(result) elif cmd == 'delete_item': item_id = request.POST['item_id'] result = database_api.delete_item(item_id) return JsonResponse(result) elif cmd == 'delete_items': item_ids = request.POST.getlist('item_ids[]') result = database_api.delete_items(item_ids) return JsonResponse(result) elif cmd == 'delete_field': item_id = request.POST['item_id'] field_name = request.POST['field_name'] result = database_api.put_item_field(item_id, field_name, None) return JsonResponse(result) elif cmd == 'delete_fields': field_names = request.POST.getlist('field_names[]') item_id = request.POST['item_id'] for field_name in field_names: result = database_api.put_item_field( item_id, field_name, None) return JsonResponse(result) elif cmd == 'get_item_count': partition = request.POST['partition'] result = database_api.get_item_count(partition) result = Util.encode_dict(result) return JsonResponse(result) elif cmd == 'query_items': partition = request.POST['partition'] query = request.POST['query'] start_key = request.POST.get('start_key', None) query = json.loads(query) instructions = query['instructions'] result = database_api.query_items(partition, instructions, start_key) return JsonResponse(result) elif cmd == 'get_policy_code': partition_to_apply = request.POST.get('partition_to_apply') mode = request.POST.get('mode') result = database_api.get_policy_code(partition_to_apply, mode) return JsonResponse(result) elif cmd == 'put_policy': partition_to_apply = request.POST.get('partition_to_apply') mode = request.POST.get('mode') code = request.POST.get('code') result = database_api.put_policy(partition_to_apply, mode, code) return JsonResponse(result) return redirect(request.path_info) # Redirect back
def post(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id adapter = DjangoAdapter(app_id, request) with adapter.open_api_auth() as api: cmd = request.POST['cmd'] if cmd == 'delete_group': name = request.POST['group_name'] error = api.delete_user_group(name).get('error', None) if error: Util.add_alert( request, '{}: {}'.format(error['code'], error['message'])) elif cmd == 'put_group': name = request.POST['group_name'] description = request.POST['group_description'] api.put_user_group(name, description) elif cmd == 'set_login_method': login_method = request.POST['login_method'] default_group = request.POST['default_group_name'] enabled = request.POST['enabled'] register_policy_code = request.POST.get( 'register_policy_code', None) if enabled == 'true': enabled = True else: enabled = False api.set_login_method(login_method, enabled, default_group, register_policy_code) elif cmd == 'put_user': email = request.POST['user_email'] password = request.POST['user_password'] api.create_user(email, password, {}) elif cmd == 'delete_user': user_id = request.POST['user_id'] api.delete_user(user_id) elif cmd == 'delete_sessions': session_ids = request.POST.getlist('session_ids[]') api.delete_sessions(session_ids) elif cmd == 'delete_users': user_ids = request.POST.getlist('user_ids[]') api.delete_users(user_ids) elif cmd == 'detach_group_permission': group_name = request.POST.get('group_name') permission = request.POST.get('permission') api.detach_group_permission(group_name, permission) elif cmd == 'attach_group_permission': group_name = request.POST.get('group_name') permission = request.POST.get('permission') api.attach_group_permission(group_name, permission) elif cmd == 'set_users': user_ids = request.POST.getlist('user_ids[]') field_name = request.POST.get('field_name') field_type = request.POST.get('field_type') field_value = request.POST.get('field_value', None) if field_type == 'S': field_value = str(field_value) elif field_type == 'N': field_value = Decimal(field_value) with ThreadPoolExecutor(max_workers=32) as exc: for user_id in user_ids: exc.submit(api.set_user, user_id, field_name, field_value) elif cmd == 'attach_user_group': user_id = request.POST.get('user_id') group_name = request.POST.get('group_name') api.attach_user_group(user_id, group_name) elif cmd == 'detach_user_group': user_id = request.POST.get('user_id') group_name = request.POST.get('group_name') api.detach_user_group(user_id, group_name) elif cmd == 'get_sessions': start_key = request.POST.get('start_key', None) result = api.get_sessions(start_key=start_key, limit=20) return JsonResponse(result) elif cmd == 'get_users': start_key = request.POST.get('start_key', None) result = api.get_users(start_key=start_key, limit=20) return JsonResponse(result) elif cmd == 'get_user_rows': start_key = request.POST.get('start_key', None) query = request.POST.getlist('query[]', []) if start_key: start_key = json.loads(start_key) result = self._get_user_rows(request, app_id, start_key=start_key, query=query) return JsonResponse(result) elif cmd == 'get_session_rows': start_key = request.POST.get('start_key', None) result = self._get_session_rows(request, app_id, start_key) return JsonResponse(result) elif cmd == 'set_group_session_security_enhancement': group_name = request.POST.get('group_name') enabled = request.POST.get('enabled') if enabled == 'false': enabled = False elif enabled == 'true': enabled = True result = api.set_group_session_security_enhancement( group_name, enabled) return JsonResponse(result) elif cmd == 'get_policy_code': mode = request.POST.get('mode') result = api.get_policy_code('user', mode) print(result) return JsonResponse(result) elif cmd == 'put_policy': mode = request.POST.get('mode') code = request.POST.get('code') result = api.put_policy('user', mode, code) return JsonResponse(result) return redirect(request.path_info) # Redirect back
def get(self, request): context = Util.get_context(request) context['email'] = request.user.email return render(request, 'dashboard/changepassword.html', context=context)
def post(self, request, app_id): context = Util.get_context(request) context['app_id'] = app_id cmd = request.POST.get('cmd', None) adapter = DjangoAdapter(app_id, request) with adapter.open_api_logic() as logic_api: if cmd == 'create_function': zip_file = request.FILES['zip_file'] function_name = request.POST['function_name'] description = request.POST['description'] runtime = request.POST['runtime'] handler = request.POST['handler'] use_logging = request.POST['use_logging'] == 'true' use_traceback = request.POST['use_traceback'] == 'true' use_standalone = request.POST['use_standalone'] == 'true' sdk_config = get_sdk_config(adapter, use_localhost=False) zip_file.seek(0) zip_file_bin = zip_file.read() zip_file_bytes = zip_file_bin zip_file_bin = base64.b64encode(zip_file_bin) zip_file_bin = zip_file_bin.decode('utf-8') if not description: description = None try: requirements_text = get_requirements_text_from_zipfile( zip_file_bytes) except Exception as ex: requirements_text = None print(ex) print(traceback.format_exc()) requirements_zip_file_id = None response_stdout = None if requirements_text: print('requirements_text:', requirements_text) with adapter.open_sdk() as sdk_client: response = sdk_client.logic_create_packages_zip( requirements_text) print('logic_create_packages_zip:', response) requirements_zip_file_id = response.get( 'zip_file_id', None) response_stdout = response.get('response_stdout', None) if not requirements_zip_file_id: raise RuntimeError( 'Requirements.txt error! retry or check this.') # response = logic_api.create_packages_zip(requirements_text) # print('logic_create_packages_zip:', response) # requirements_zip_file_id = response.get('zip_file_id', None) response_function_creation = logic_api.create_function( function_name, description, runtime, handler, sdk_config, zip_file_bin, True, use_logging, use_traceback, requirements_zip_file_id, use_standalone) return JsonResponse({ 'package_install_response_stdout': response_stdout, 'response_function_creation': response_function_creation }) elif cmd == 'create_function_test': test_name = request.POST.get('test_name') function_name = request.POST.get('function_name') test_input = request.POST.get('test_input') logic_api.create_function_test(test_name, function_name, test_input) elif cmd == 'run_function': with adapter.open_sdk() as sdk_client: # TODO SDK Sign-up Login and authentication takes a lot of time. # We are planning to store the SDK ID Password in the session. function_name = request.POST.get('function_name') payload = request.POST.get('payload') payload = json.loads(payload) start = time.time() data = sdk_client.logic_run_function( function_name, payload) end = time.time() data['duration'] = end - start # print('data:', data) return JsonResponse(data) elif cmd == 'delete_function_test': test_name = request.POST.get('test_name') logic_api.delete_function_test(test_name) elif cmd == 'delete_function': function_name = request.POST.get('function_name') function_version = request.POST.get('function_version', None) logic_api.delete_function(function_name, function_version) elif cmd == 'create_webhook': name = request.POST.get('name') description = request.POST.get('description') function_name = request.POST.get('function_name') logic_api.create_webhook(name, description, function_name) elif cmd == 'delete_webhook': name = request.POST.get('name') logic_api.delete_webhook(name) return redirect(request.path_info) # Redirect back