def get(self, request, id): try: dapp = Dapp.objects.prefetch_related('constructor', 'users').get(slug=id) except Dapp.DoesNotExist: return error_response("Dapp not found") user = auth(request) if not dapp.has_public_access: if isinstance(user, HttpResponse): return user # error if user not in dapp.users.all(): return error_response('Dapp not found') if not dapp.address: return error_response('Dapp is not yet deployed') cust_title = None if isinstance(user, User): try: user_dapp = UserDapp.objects.get(user=user, dapp=dapp) cust_title = user_dapp.title except UserDapp.DoesNotExist: pass return JsonResponse( dapp_pub_info(dapp, user in dapp.users.all(), cust_title))
def get(self, http_request, dapp_slug): user = auth(http_request) if isinstance(user, HttpResponse): # todo (error) return Response([]) requests = Request.objects.order_by('execution_datetime').filter(dapp__slug=dapp_slug, user=user) serializer = RequestSerializer(requests, many=True) return Response(serializer.data)
def get(self, request): user = auth(request) if isinstance(user, HttpResponse): return user # error dapps = Dapp.objects.filter(user=user).exclude(address='').prefetch_related('constructor') return JsonResponse( [_prepare_instance_details(i) for i in dapps], safe=False )
def get(self, request, *args, **kwargs): user = auth(request) constructors_query = Constructor.objects.order_by('sorting_order')\ .reverse().annotate(Count('dapps')) if isinstance(user, HttpResponse): # todo constructors = constructors_query.filter(is_public=True) else: constructors = constructors_query.filter( Q(is_public=True) | Q(user=user)) return JsonResponse(constructors_pub_info(constructors), safe=False)
def post(self, request, id): user = auth(request) if isinstance(user, HttpResponse): return user # error try: dapp = Dapp.objects.get(slug=id, has_public_access=True) except Dapp.DoesNotExist: return error_response("Dapp not found") if not UserDapp.objects.filter(dapp=dapp, user=user): UserDapp.objects.create(dapp=dapp, user=user, title=dapp.title) return JsonResponse({'ok': True}) # todo
def post(self, request): user = auth(request) if isinstance(user, HttpResponse): return user # error if 'address' not in request.data or type( request.data['address'] ) is not str or not request.data['address']: return error_response("Address is missing") if 'network_id' not in request.data or type( request.data['network_id'] ) is not str or not request.data['network_id']: return error_response("Network id is missing") if 'abi' not in request.data: return error_response('Abi not specified') if 'blockchain' not in request.data or request.data[ 'blockchain'] not in dict(BLOCKCHAINS): return error_response('Invalid blockchain') if 'name' not in request.data or type( request.data['name']) is not str or not request.data['name']: title = 'Dapp' else: title = request.data['name'] dapp = Dapp.create() dapp.blockchain = request.data['blockchain'] dapp.address = request.data['address'] dapp.network_id = request.data['network_id'] dapp.title = title dapp.abi = json.dumps(request.data['abi']) dapp.source = '' dapp.binary = '' dapp.function_specs = json.dumps( self.contracts_processors_manager.require_contract_processor(dapp.blockchain)\ .process_functions_specs(request.data['abi'], {}) ) dapp.dashboard_functions = json.dumps([]) dapp.has_public_access = True dapp.save() UserDapp.objects.create(user=user, dapp=dapp, title=dapp.title) return JsonResponse({'ok': True}) # todo
def post(self, request, id): user = auth(request) if isinstance(user, HttpResponse): return user # error try: dapp = Dapp.objects.get(slug=id, has_public_access=True) except Dapp.DoesNotExist: return error_response("Dapp not found") dapp.slug = Dapp.create().slug dapp.pk = None dapp.user = user dapp.save() return JsonResponse({'ok': True}) # todo
def post(self, request, id): if 'address' not in request.data or type( request.data['address'] ) is not str or not request.data['address']: return error_response("Address is missing") if 'network_id' not in request.data or type( request.data['network_id'] ) is not str or not request.data['network_id']: return error_response("Network id is missing") user = auth(request) if isinstance(user, HttpResponse): return user # error try: contract_ui = ContractUI.objects.get(id=id) except ContractUI.DoesNotExist: return error_response("Ui not found") dapp = Dapp.create() dapp.blockchain = contract_ui.blockchain dapp.address = request.data['address'] dapp.network_id = request.data['network_id'] dapp.title = contract_ui.name abi = contract_ui.abi if 'abi' not in request.data else request.data[ 'abi'] dapp.abi = json.dumps(abi) dapp.source = '' dapp.binary = '' dapp.function_specs = json.dumps( self.contracts_processors_manager.require_contract_processor( contract_ui.blockchain).process_functions_specs( abi, contract_ui.functions)) dapp.dashboard_functions = json.dumps(contract_ui.dashboard_functions) dapp.contract_ui = contract_ui dapp.has_public_access = True dapp.save() UserDapp.objects.create(dapp=dapp, user=user, title=contract_ui.name) return JsonResponse({'ok': True}) # todo
def post(self, http_request, dapp_slug): user = auth(http_request) if isinstance(user, HttpResponse): # todo (error) return user try: dapp = Dapp.objects.get(slug=dapp_slug) except Dapp.DoesNotExist: return error_response("Dapp not found") serializer = RequestSerializer(data=http_request.data) if not serializer.is_valid(): self.logger.info("Bad request: %s", json.dumps(serializer.errors)) return error_response('Bad request', status.HTTP_400_BAD_REQUEST) serializer.save(user=user, dapp=dapp) return Response(serializer.data, status=status.HTTP_201_CREATED)
def get(self, request, id): try: dapp = Dapp.objects.prefetch_related('constructor').get(slug=id) except Dapp.DoesNotExist: return error_response("Dapp not found") if not dapp.has_public_access: user = auth(request) if isinstance(user, HttpResponse): return user # error if dapp.user_id != user.pk: return error_response('Dapp not found') if not dapp.address: return error_response('Dapp is not yet deployed') return JsonResponse(_prepare_instance_details(dapp))
def get(self, request): user = auth(request) if isinstance(user, HttpResponse): return user # error dapps = Dapp.objects.filter(users=user).exclude( address='').order_by('-created_at').prefetch_related( 'constructor', 'users') users_dapps = dict([ (user_dapp.dapp_id, user_dapp) for user_dapp in UserDapp.objects.filter(user=user, dapp__in=dapps) ]) return JsonResponse([ dapp_pub_info(dapp, True, users_dapps[dapp.id].title) for dapp in dapps ], safe=False)
def post(self, request, id): user = auth(request) if isinstance(user, HttpResponse): return user # error try: dapp = Dapp.objects.get(slug=id, users=user) except Dapp.DoesNotExist: return error_response("Dapp not found") # [TODO] refactor all checks (address and network_id validation should be somewhere near Instance() class # Later validation of Ethereum address will differ from EOS address, so Instance() class will have some "blockchain_id" member to # differ parameters of dapps on different blockchains address = request.data.get('address') if address is not None: if not isinstance(address, str): return error_response("Param 'address' is empty or not string") dapp.address = address network_id = request.data.get('network_id') if network_id is not None: if type(network_id) not in (int, str): return error_response("Param 'network_id' is empty or not int") dapp.network_id = str(network_id) has_public_access = request.data.get('has_public_access') if has_public_access is not None: dapp.has_public_access = bool(has_public_access) title = request.data.get('title') if title is not None: for user_dapp in UserDapp.objects.filter(dapp=dapp, user=user): user_dapp.title = title user_dapp.save() dapp.save() return JsonResponse({'ok': True}) # todo
def post(self, request): args = request.data price = str(args.get('price', 0)) if 'payment_address' in args: if not re.findall('^0x[0-9a-fA-F]{40}$', args['payment_address']): return error_response("Invalid payment address") else: args['payment_address'] = '' if float(price) and not args['payment_address']: return error_response( "Payment address must be specified with price >0") user = auth(request) if isinstance(user, HttpResponse): return user # error name = nonempty(args_string(args, 'name')) descr = nonempty(args_string( args, 'description')) if 'description' in args else '' filename = tempfile.mktemp('ctor') if 'constructor_id' in args: try: current_constructor = Constructor.objects.get( slug=args['constructor_id']) except Constructor.DoesNotExist: return error_response('Constructor does not exists') if current_constructor.user_id != user.pk: return error_response('Access denied') if Constructor.objects.filter(name=name).exclude( slug=args['constructor_id']).exists(): return error_response( 'Constructor with this name already exists') else: if Constructor.objects.filter(name=name).exists(): return error_response( 'Constructor with this name already exists') current_constructor = Constructor.create() if 'ctor_file' in args: file_base64 = re.sub('^data:.+;base64,', '', args['ctor_file']) try: file_source = base64.b64decode(file_base64).decode('utf-8') except Exception: return error_response("Invalid input/0") file = open(filename, "w") file.write(file_source) file.close() is_public = False else: return error_response("Constructor file is missing") if float(price): with open(filename) as f: if not '%payment_code%' in f.read( ): # todo check on deploy in source of contract? return error_response( "Payment code must be in contract constructor") current_constructor.name = name current_constructor.description = descr current_constructor.payment_address = args['payment_address'] current_constructor.price = Decimal(price) current_constructor.is_public = is_public current_constructor.user = user version_info = self.constructor_engine.get_constructor_version( file_source) if 'error' == version_info['result']: return error_response(version_info['error_descr']) current_constructor.version = version_info['version'] current_constructor.blockchain = version_info['blockchain'] params = self.constructor_engine.get_constructor_params(file_source) if 'error' == params['result']: return error_response(params['error_descr']) current_constructor.schema = json.dumps(params.get('schema', {})) current_constructor.ui_schema = json.dumps(params.get('ui_schema', {})) current_constructor.save() self.constructor_engine.register_constructor(current_constructor.slug, filename) return JsonResponse({'ok': True})
def post(self, request, constructor_id): # parsed input data POST JSON payload args = request.data # field -> error string # TODO - rename to better name fields = args.get('fields') if fields is None: return error_response( "Constructor({}), empty fields passed to constructor".format( constructor_id)) try: constructor = Constructor.objects.get(slug=constructor_id) except Constructor.DoesNotExist: return error_response( "Constructor with id '{}' not found".format(constructor_id)) user = auth(request) if isinstance(user, HttpResponse): return user dapp_title = args.get('dapp_title') if not dapp_title or not isinstance(dapp_title, str): return error_response("Wrong dapp title") constructor_schema = _process_ctor_schema(constructor.blockchain, constructor.get_schema()) validator_cls = validator_for(constructor_schema) validator_cls.check_schema(constructor_schema) validator = validator_cls(constructor_schema) errors = dict() for error in validator.iter_errors(fields): if not error.path: return error_response(error.message) errors[error.path[0]] = error.message if errors: return JsonResponse({"result": "error", "errors": errors}) result = self.constructor_engine.construct(constructor, fields) if not isinstance(result, dict): return error_response( "Constructor({}), construct error, result is not dict".format( constructor_id)) if 'error' == result['result']: return engine_error_response(result) dapp = Dapp.create() dapp.title = dapp_title dapp.abi = json.dumps(result['abi']) dapp.source = result['source'] dapp.binary = result['bin'] dapp.function_specs = json.dumps(result['function_specs']) dapp.dashboard_functions = json.dumps(result['dashboard_functions']) dapp.constructor = constructor dapp.blockchain = constructor.blockchain dapp.compiler_version = result['compiler_version'] dapp.compiler_optimization = result['compiler_optimization'] dapp.contract_name = result['contract_name'] dapp.deploy_price = constructor.price dapp.save() UserDapp.objects.create(dapp=dapp, user=user, title=dapp_title) return JsonResponse(dapp_pub_info(dapp, True))