如何使用石墨烯进行服务器端验证?

时间:2018-09-14 13:52:36

标签: graphql graphene-python

我使用graphql在客户端和服务器之间进行通信。

我的服务器如下:

Request -> Graphene Field -> Application Logic -> Database actions

以下是Application Logic函数的示例:

def add_license_code(distributor, license_type, duration_string, user_id, one_time_usage, valid_from_string, valid_to_string, code_user_defined):
    if valid_from_string:
        try:
            valid_from = datetime.datetime.strptime(valid_from_string, '%Y%m%d')
        except ValueError:
           raise Exception('valid from parameter needs to be in YYYYMMDD format') 
    else:
        valid_from = None

    if valid_to_string:
        try:
            valid_to = datetime.datetime.strptime(valid_to_string + '235959', '%Y%m%d%H%M%S')
        except ValueError:
           raise Exception('valid to parameter needs to be in YYYYMMDD format') 
    else:
        valid_to = None

    if duration_string: 
        duration = isodate.parse_duration(duration_string)
    else:
        duration = None

    if code_user_defined:
        code = code_user_defined
    else:
        code = _generate_license_code()

    if license_type != 'Premium':
        raise Exception('invalid license type')

    if one_time_usage != 'Y' and one_time_usage != 'N':
       raise Exception('one time usage must be Y or N')

    if one_time_usage == 'Y':
        if (not duration):
            raise Exception('duration missing')

        if isinstance(duration, datetime.timedelta):
            if not duration > datetime.timedelta(0):
                raise Exception('duration invalid')
            if duration > datetime.timedelta(3660):
                raise Exception('duration cannot be longer than 10 years')
        elif isinstance(duration, isodate.Duration):
            if not duration > 0:
                raise Exception('duration invalid')
            if duration > isodate.duration.Duration(0, 0, 0, years=10, months=0):
                log.debug("duration %s isodate %s" % (duration, isodate.duration.Duration(0, 0, 0, years=10, months=0)))
                raise Exception('duration cannot be longer than 10 years')

        if valid_from_string or valid_to_string:
            raise Exception('valid_from and valid_to not allowed with one usage license code.')

        if code_user_defined:
            raise Exception('user defined code not allowed with one usage license code.')

    if one_time_usage == 'N':
        if not code_user_defined:
            raise Exception('user defined code required to create multiple usage code.')

        if not (valid_from and valid_to):
            raise Exception('invalid valid_from or valid_to dates')

        if valid_to < datetime.datetime.utcnow():
            raise Exception('License code has expired')

        if valid_to < valid_from:
            raise Exception('Valid from date cannot be smaller than valid to.')

        if valid_to > datetime.timedelta(days=3660):
            raise Exception('Valid to date cannot be more than 10 years in the future.')

        if valid_from < datetime.timedelta(days=-3660):
            raise Exception('Valid from date cannot be more than 10 years back.')

        if duration:
            raise Exception('Duration not allowed with multiple usage license code.')

    success = environment.DB.add_license_code(
        code=code, 
        distributor=distributor, 
        license_type=license_type, 
        user_id=user_id,
        one_time_usage=one_time_usage, 
        duration=duration_string,
        valid_from=valid_from,
        valid_to=valid_to)

    if not (success):
       raise Exception('Failed writing to database') 

    licenseCodeResponse = {
        "code": code,
        "distributor": distributor,
        "license_type": license_type,
        "duration": duration_string,
        "one_time_usage": one_time_usage,
        "valid_from": valid_from,
        "valid_to": valid_to
    }

    return licenseCodeResponse 

对我来说,将一堆验证与应用程序逻辑混合在一起似乎很混乱。如果使用石墨烯,通常推荐的方法是什么?

  • 我应该将服务器更改为

    请求->石墨烯字段->验证->应用逻辑->数据库操作

  • 应该将验证放到GraphQL模式中还是应该在以后与模式无关地做呢?

0 个答案:

没有答案