コード例 #1
0
    def post(self, token_id):
        """
        This endpoint is for creating a new contract,
        rather registering a token with an existing smart contract on the system.
        To create a new token contract, use api/token/.
        """
        post_data = request.get_json()

        # We use almost the exact same flow as a subflow of create organisation, so create a helper function
        response_object, response_code = deploy_cic_token(post_data)

        return make_response(jsonify(response_object)), response_code
コード例 #2
0
    def post(self, organisation_id):
        post_data = request.get_json()

        organisation_name = post_data.get('organisation_name')
        custom_welcome_message_key = post_data.get(
            'custom_welcome_message_key')
        timezone = post_data.get('timezone')

        country_code = post_data.get('country_code')
        default_disbursement = post_data.get('default_disbursement')
        require_transfer_card = post_data.get('require_transfer_card')
        default_lat = post_data.get('default_lat')
        default_lng = post_data.get('default_lng')

        token_id = post_data.get('token_id')
        deploy_cic = post_data.get('deploy_cic', False)

        if organisation_name is None or country_code is None:
            return make_response(
                jsonify({
                    'message':
                    'Must provide name and ISO 2 country_code to create organisation.'
                })), 400

        existing_organisation = Organisation.query.filter_by(
            name=organisation_name).execution_options(show_all=True).first()
        if existing_organisation is not None:
            return make_response(
                jsonify({
                    'message':
                    'Must be unique name. Organisation already exists for name: {}'
                    .format(organisation_name),
                    'data': {
                        'organisation':
                        organisation_schema.dump(existing_organisation).data
                    }
                })), 400

        try:
            new_organisation = Organisation(
                name=organisation_name,
                custom_welcome_message_key=custom_welcome_message_key,
                timezone=timezone,
                country_code=country_code,
                default_disbursement=default_disbursement,
                require_transfer_card=require_transfer_card,
                default_lat=default_lat,
                default_lng=default_lng)
        except Exception as e:
            response_object = {
                'message': str(e),
            }
            return make_response(jsonify(response_object)), 400

        db.session.add(new_organisation)
        db.session.flush()

        response_object = {
            'message': 'Created Organisation',
            'data': {
                'organisation': organisation_schema.dump(new_organisation).data
            },
        }

        if token_id:
            token = Token.query.get(token_id)
            if token is None:
                return make_response(jsonify({'message':
                                              'Token not found'})), 404
            new_organisation.bind_token(token)

        elif deploy_cic:

            cic_response_object, cic_response_code = deploy_cic_token(
                post_data, new_organisation)
            if cic_response_code == 201:
                response_object['data']['token_id'] = cic_response_object[
                    'data']['token_id']
            else:
                return make_response(
                    jsonify(cic_response_object)), cic_response_code

        return make_response(jsonify(response_object)), 201
コード例 #3
0
    def post(self, organisation_id):
        post_data = request.get_json()

        organisation_name = post_data.get('organisation_name')
        custom_welcome_message_key = post_data.get(
            'custom_welcome_message_key')
        timezone = post_data.get('timezone')

        country_code = post_data.get('country_code')
        default_disbursement = post_data.get('default_disbursement')
        minimum_vendor_payout_withdrawal = post_data.get(
            'minimum_vendor_payout_withdrawal')
        require_transfer_card = post_data.get('require_transfer_card')
        default_lat = post_data.get('default_lat')
        default_lng = post_data.get('default_lng')
        account_types = post_data.get('account_types', [])

        token_id = post_data.get('token_id')
        deploy_cic = post_data.get('deploy_cic', False)

        for at in account_types:
            if at not in ASSIGNABLE_TIERS.keys():
                raise Exception(f'{at} not an assignable role')

        if organisation_name is None or country_code is None or timezone is None:
            return make_response(
                jsonify({
                    'message':
                    'Must provide organisation_name, country_code and timezone to create organisation.'
                })), 400

        existing_organisation = Organisation.query.filter_by(
            name=organisation_name).execution_options(show_all=True).first()
        if existing_organisation is not None:
            return make_response(
                jsonify({
                    'message':
                    'Must be unique name. Organisation already exists for name: {}'
                    .format(organisation_name),
                    'data': {
                        'organisation':
                        organisation_schema.dump(existing_organisation).data
                    }
                })), 400

        try:
            new_organisation = Organisation(
                name=organisation_name,
                custom_welcome_message_key=custom_welcome_message_key,
                timezone=timezone,
                country_code=country_code,
                default_disbursement=default_disbursement,
                minimum_vendor_payout_withdrawal=
                minimum_vendor_payout_withdrawal,
                require_transfer_card=require_transfer_card,
                default_lat=default_lat,
                default_lng=default_lng,
                valid_roles=account_types)
        except Exception as e:
            response_object = {
                'message': str(e),
            }
            return make_response(jsonify(response_object)), 400

        db.session.add(new_organisation)
        db.session.flush()

        response_object = {
            'message': 'Created Project',
            'data': {
                'organisation': organisation_schema.dump(new_organisation).data
            },
        }

        if token_id:
            token = Token.query.get(token_id)
            if token is None:
                return make_response(jsonify({'message':
                                              'Token not found'})), 404
            new_organisation.bind_token(token)

        elif deploy_cic:

            cic_response_object, cic_response_code = deploy_cic_token(
                post_data, new_organisation)
            if cic_response_code == 201:
                response_object['data']['token_id'] = cic_response_object[
                    'data']['token_id']
            else:
                return make_response(
                    jsonify(cic_response_object)), cic_response_code

        if AccessControl.has_suffient_role(g.user.roles,
                                           {'ADMIN': 'superadmin'}):
            g.user.add_user_to_organisation(new_organisation, is_admin=True)

        return make_response(jsonify(response_object)), 201