1.背景:因项目需要在hue上暴露一些http接口,但是hue有登录验证,当访问这些接口时被拦截到了hue的登录界面。于是
研究查询资料找到这个登录验证是由desktop app中middleware.py里面 LoginAndPermissionMiddleware类控制的。文件位置: hue/desktop/core/src/desktop/middleware.py 。关键代码
class LoginAndPermissionMiddleware(object):
"""
Middleware that forces all views (except those that opt out) through authentication.
"""
def process_view(self, request, view_func, view_args, view_kwargs):
"""
We also perform access logging in ``process_view()`` since we have the view function,
which tells us the log level. The downside is that we don't have the status code,
which isn't useful for status logging anyways.
"""
request.ts = time.time()
request.view_func = view_func
access_log_level = getattr(view_func, 'access_log_level', None)
# First, skip views not requiring login
# If the view has "opted out" of login required, skip
if hasattr(view_func, "login_notrequired"):
log_page_hit(request, view_func, level=access_log_level or logging.DEBUG)
return None
process_view如果返回None,就不会被拦截到登录界面。所以在此方法中通过参数request获得请求的url,判断这个url是否为自己想要免登录开放的接口,如果是则返回None即可。
2.hue增加ORM映射
a.首先通过继承Django的model类,来实现增加模型TestTable
b.使用下面的命令产生迁移文件,如下命令是给项目中desktop的app生成模型TestTable对应的数据库迁移文件:
./hue schemamigration desktop --add-model TestTable
生成的数据库迁移文件会存于APP的migrations文件夹下
c.使用如下命令按照上面生成的迁移文件修改数据:
./hue migrate