我如何修补补丁'或覆盖User.is_authenticated()?使用django-lazysignup创建问题

时间:2015-07-06 21:43:45

标签: python django django-allauth monkeypatching

我安装了django-lazysignup,现在面临着User.is_authenticated()的挑战,返回True,对于那些不是实际身份验证的用户,而是lazy-signup用户。我可以使用自己的函数更新代码中User.is_authenticated()的任何检查。但是,像django-allauth这样的其他软件包,请检查此方法以确定用户是否已登录,并从尝试访问登录页面重定向。

2015-07-06T05:53:18.502200+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-07-06T05:53:18.502200+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-07-06T05:53:19.595264+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-06T05:53:19.522763+00:00 heroku[web.1]: Process exited with status 137
2015-07-06T05:53:22.172217+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=serene-temple-3871.herokuapp.com request_id=160dd22d-0149-4427-825e-0cfe187d50f0 fwd="71.198.39.241" dyno= connect= service= status=503 bytes=
2015-07-06T06:58:38.061909+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-06T06:58:43.330632+00:00 heroku[web.1]: Starting process with command `bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}`
2015-07-06T06:58:48.463218+00:00 app[web.1]: [3] Puma starting in cluster mode...
2015-07-06T06:58:48.463238+00:00 app[web.1]: [3] * Version 2.11.3 (ruby 2.2.2-p95), codename: Intrepid Squirrel
2015-07-06T06:58:48.463240+00:00 app[web.1]: [3] * Min threads: 5, max threads: 5
2015-07-06T06:58:48.463241+00:00 app[web.1]: [3] * Environment: production
2015-07-06T06:58:48.463242+00:00 app[web.1]: [3] * Process workers: 2
2015-07-06T06:58:48.463245+00:00 app[web.1]: [3] * Preloading application
2015-07-06T06:58:52.639573+00:00 app[web.1]: [2015-07-06 06:58:52] INFO  WEBrick 1.3.1
2015-07-06T06:58:52.639582+00:00 app[web.1]: [2015-07-06 06:58:52] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
2015-07-06T06:58:52.640986+00:00 app[web.1]: [2015-07-06 06:58:52] INFO  WEBrick::HTTPServer#start: pid=3 port=2010
2015-07-06T06:59:43.770715+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-07-06T06:59:43.770935+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-07-06T06:59:44.670643+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-06T06:59:44.659706+00:00 heroku[web.1]: Process exited with status 137
2015-07-06T13:23:13.180759+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-06T13:23:17.913267+00:00 heroku[web.1]: Starting process with command `bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}`
2015-07-06T13:23:20.049616+00:00 app[web.1]: [3] Puma starting in cluster mode...
2015-07-06T13:23:20.049642+00:00 app[web.1]: [3] * Version 2.11.3 (ruby 2.2.2-p95), codename: Intrepid Squirrel
2015-07-06T13:23:20.049644+00:00 app[web.1]: [3] * Min threads: 5, max threads: 5
2015-07-06T13:23:20.049645+00:00 app[web.1]: [3] * Environment: production
2015-07-06T13:23:20.049646+00:00 app[web.1]: [3] * Process workers: 2
2015-07-06T13:23:20.049647+00:00 app[web.1]: [3] * Preloading application
2015-07-06T13:23:21.404107+00:00 app[web.1]: [2015-07-06 13:23:21] INFO  WEBrick 1.3.1
2015-07-06T13:23:21.404116+00:00 app[web.1]: [2015-07-06 13:23:21] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
2015-07-06T13:23:21.405443+00:00 app[web.1]: [2015-07-06 13:23:21] INFO  WEBrick::HTTPServer#start: pid=3 port=2010
2015-07-06T13:24:18.189387+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-07-06T13:24:18.189437+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-07-06T13:24:19.089242+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-06T13:24:19.072229+00:00 heroku[web.1]: Process exited with status 137
2015-07-06T15:22:24.594997+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-06T15:22:35.297440+00:00 heroku[web.1]: Starting process with command `bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}`
2015-07-06T15:22:37.447910+00:00 app[web.1]: [3] Puma starting in cluster mode...
2015-07-06T15:22:37.447935+00:00 app[web.1]: [3] * Version 2.11.3 (ruby 2.2.2-p95), codename: Intrepid Squirrel
2015-07-06T15:22:37.447936+00:00 app[web.1]: [3] * Min threads: 5, max threads: 5
2015-07-06T15:22:37.447939+00:00 app[web.1]: [3] * Process workers: 2
2015-07-06T15:22:37.447938+00:00 app[web.1]: [3] * Environment: production
2015-07-06T15:22:37.447940+00:00 app[web.1]: [3] * Preloading application
2015-07-06T15:22:38.766153+00:00 app[web.1]: [2015-07-06 15:22:38] INFO  WEBrick 1.3.1
2015-07-06T15:22:38.766821+00:00 app[web.1]: [2015-07-06 15:22:38] INFO  WEBrick::HTTPServer#start: pid=3 port=2010
2015-07-06T15:22:38.766189+00:00 app[web.1]: [2015-07-06 15:22:38] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
2015-07-06T15:23:35.760684+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-07-06T15:23:35.760778+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-07-06T15:23:36.539883+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-06T15:23:36.527478+00:00 heroku[web.1]: Process exited with status 137
2015-07-06T17:30:37.325877+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-06T17:30:42.021403+00:00 heroku[web.1]: Starting process with command `bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}`
2015-07-06T17:30:44.494517+00:00 app[web.1]: [3] Puma starting in cluster mode...
2015-07-06T17:30:44.494549+00:00 app[web.1]: [3] * Version 2.11.3 (ruby 2.2.2-p95), codename: Intrepid Squirrel
2015-07-06T17:30:44.494552+00:00 app[web.1]: [3] * Min threads: 5, max threads: 5
2015-07-06T17:30:44.494555+00:00 app[web.1]: [3] * Environment: production
2015-07-06T17:30:44.494561+00:00 app[web.1]: [3] * Process workers: 2
2015-07-06T17:30:44.494566+00:00 app[web.1]: [3] * Preloading application
2015-07-06T17:30:46.066668+00:00 app[web.1]: [2015-07-06 17:30:46] INFO  WEBrick 1.3.1
2015-07-06T17:30:46.066681+00:00 app[web.1]: [2015-07-06 17:30:46] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
2015-07-06T17:30:46.067472+00:00 app[web.1]: [2015-07-06 17:30:46] INFO  WEBrick::HTTPServer#start: pid=3 port=2010
2015-07-06T17:31:42.216702+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-07-06T17:31:42.216702+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-07-06T17:31:43.293523+00:00 heroku[web.1]: Process exited with status 137
2015-07-06T17:31:43.303355+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-06T18:08:50.438380+00:00 heroku[api]: Deploy 4925673 by myemail@gmail.com
2015-07-06T18:08:50.438380+00:00 heroku[api]: Release v20 created by myemail@gmail.com
2015-07-06T18:08:50.484157+00:00 heroku[slug-compiler]: Slug compilation started
2015-07-06T18:08:50.484189+00:00 heroku[slug-compiler]: Slug compilation finished
2015-07-06T18:08:50.604065+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-06T18:08:55.633537+00:00 heroku[web.1]: Starting process with command `bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}`
2015-07-06T18:08:58.092779+00:00 app[web.1]: [3] Puma starting in cluster mode...
2015-07-06T18:08:58.092816+00:00 app[web.1]: [3] * Version 2.11.3 (ruby 2.2.2-p95), codename: Intrepid Squirrel
2015-07-06T18:08:58.092818+00:00 app[web.1]: [3] * Min threads: 5, max threads: 5
2015-07-06T18:08:58.092820+00:00 app[web.1]: [3] * Environment: production
2015-07-06T18:08:58.092821+00:00 app[web.1]: [3] * Process workers: 2
2015-07-06T18:08:58.092822+00:00 app[web.1]: [3] * Preloading application
2015-07-06T18:08:59.669817+00:00 app[web.1]: [2015-07-06 18:08:59] INFO  WEBrick 1.3.1
2015-07-06T18:08:59.669828+00:00 app[web.1]: [2015-07-06 18:08:59] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
2015-07-06T18:08:59.671165+00:00 app[web.1]: [2015-07-06 18:08:59] INFO  WEBrick::HTTPServer#start: pid=3 port=2010
2015-07-06T18:09:56.054524+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-07-06T18:09:56.054740+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-07-06T18:09:57.007247+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-06T18:09:57.008402+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-06T18:09:56.997273+00:00 heroku[web.1]: Process exited with status 137
2015-07-06T18:10:04.094757+00:00 heroku[web.1]: Starting process with command `bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}`
2015-07-06T18:10:08.213157+00:00 app[web.1]: [3] Puma starting in cluster mode...
2015-07-06T18:10:08.213182+00:00 app[web.1]: [3] * Version 2.11.3 (ruby 2.2.2-p95), codename: Intrepid Squirrel
2015-07-06T18:10:08.213184+00:00 app[web.1]: [3] * Min threads: 5, max threads: 5
2015-07-06T18:10:08.213186+00:00 app[web.1]: [3] * Environment: production
2015-07-06T18:10:08.213187+00:00 app[web.1]: [3] * Process workers: 2
2015-07-06T18:10:08.213188+00:00 app[web.1]: [3] * Preloading application
2015-07-06T18:10:09.863174+00:00 app[web.1]: [2015-07-06 18:10:09] INFO  WEBrick 1.3.1
2015-07-06T18:10:09.863277+00:00 app[web.1]: [2015-07-06 18:10:09] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
2015-07-06T18:10:09.864197+00:00 app[web.1]: [2015-07-06 18:10:09] INFO  WEBrick::HTTPServer#start: pid=3 port=2010
2015-07-06T18:10:30.791209+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=serene-temple-3871.herokuapp.com request_id=27f05fb1-5011-46d0-bdbd-68afe5d4103c fwd="71.198.39.241" dyno= connect= service= status=503 bytes=
2015-07-06T18:11:04.587671+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-07-06T18:11:04.587671+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-07-06T18:11:06.054299+00:00 heroku[web.1]: Process exited with status 137
2015-07-06T18:11:06.070843+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-06T18:11:07.079354+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=serene-temple-3871.herokuapp.com request_id=bb9ba93e-5892-40c1-bf69-fe4cbaba929b fwd="71.198.39.241" dyno= connect= service= status=503 bytes=

是否有任何建议不需要在我包含的每个包中替换is_authenticated()?似乎大多数人都希望它能以某种特定的方式发挥作用,而django-lazysignup将其转变为头脑。我可以使用新的is_authenticated()方法修补User模型吗?如果可能,我会在哪里这样做,以便它附加到页面请求?

2 个答案:

答案 0 :(得分:0)

您正在使用的

django-lazysignup允许您提供自定义的LazyUser类(here)。您需要做的就是使用定义的lazysignup.models.LazyUser方法编写is_authenticated的子类,并将其设置为settings.LAZYSIGNUP_USER_MODEL

但这不是你烦恼的结束。很多django应用程序 假设经过身份验证的用户具有某些属性。主要是is_staffis_superuserpermissionsgroups。首先,django.contrib.admin需要他们检查是否可以让用户进入并向他展示什么。看看django.contrib.auth.models.AnonymousUser如何嘲笑他们并复制它。备注:查看AnonymousUser 如何继承任何用户类和db.Model。提供的用户类只需要嘎嘎叫。

答案 1 :(得分:0)

结束只需要替换对User.is_authenticated()的所有调用。

为了防止django-allauth从登录页面重定向懒惰用户,最终看起来像这样:

from allauth.account.views import AjaxCapableProcessFormViewMixin

def _ajax_response(request, response, form=None):
    if request.is_ajax():
        if (isinstance(response, HttpResponseRedirect)
            or isinstance(response, HttpResponsePermanentRedirect)):
            redirect_to = response['Location']
        else:
            redirect_to = None
        response = get_adapter().ajax_response(request,
                                           response,
                                           form=form,
                                           redirect_to=redirect_to)
    return response


class RedirectUserWithAccountMixin(object):
    def dispatch(self, request, *args, **kwargs):
        self.request = request
        if user_has_account(request.user):
            redirect_to = self.get_authenticated_redirect_url()
            response = HttpResponseRedirect(redirect_to)
            return _ajax_response(request, response)
        else:
            response = super(RedirectUserWithAccountMixin,
                             self).dispatch(request,
                                            *args,
                                            **kwargs)
        return response

    def get_authenticated_redirect_url(self):
        redirect_field_name = self.redirect_field_name
        return get_login_redirect_url(self.request,
                                      url=self.get_success_url(),
                                      redirect_field_name=redirect_field_name)

class LoginView(RedirectUserWithAccountMixin,
            AjaxCapableProcessFormViewMixin,
            FormView):
...

其中user_has_account()是我自己检查用户是否实际登录的方法。

相关问题