2015-03-25 71 views
0

我有一个看起来像一个端点: api.add_resource(UserForm,'/app/user/form/<int:form_id>', endpoint='user_form')瓶:调用类,需要一个资源

我的窗体的样子:

class UserForm(Resource): 
    def get(self, form_id): 
     # GET stuff here 
     return user_form_dictionary 

如果我有一个函数调用get_user_form(form_id)和我想要根据传入的form_id参数从UserForm的get方法中获取返回值。Flask中有一种方法允许某种方式在程序中调用UserForm的get方法吗?

def get_user_form(form_id): 
    user_form_dictionary = # some way to call UserForm class 
    # user_form_dictionary will store return dictionary from 
    # user_form_dictionary, something like: {'a': 'blah', 'b': 'blah'} 

回答

0

我不知道是否有直接从您的应用程序中访问窗体类的get方法的一种方式,即弹簧想到的唯一的事情就是调用网址为资源,但我不不建议这样做。

你有没有使用烧瓶宁静的延伸?如果是的话下面是基于有网站here

建议的中间项目结构在一个共同的模块(这包含了将在整个应用程序中使用的功能)

共同\ util.py

def get_user_form(form_id): 
    # logic to return the form data 

然后在你的.py包含窗体类,进口依照该通讯模块的util.py文件,然后做下面的

class UserForm(Resource): 
    def get(self, form_id): 
     user_form_dictionary = get_user_form(form_id) 

     # any additional logic. i try and keep it to a minimum as the function called 
     # would contain it. also this way maintanence is easier 

     return user_form_dictionary 

然后在导入公共模块后,您的应用程序中的其他位置可以重复使用相同的功能。

def another_function(form_id): 
    user_form_dictionary = get_user_form(form_id) 
    # any additional logic. 
    # same rules as before 

    return user_form_dictionary