小编ver*_*4ka的帖子

python-social-auth AuthCanceled异常

我在我的Django应用程序中使用python-social-auth通过Facebook进行身份验证.但是当用户尝试登录并且当它被重定向到Facebook应用页面点击"取消"按钮时,会出现以下异常:

ERROR 2014-01-03 15:32:15,308 base :: Internal Server Error: /complete/facebook/
Traceback (most recent call last):
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 45, in wrapper
    return func(request, backend, *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/apps/django_app/views.py", line 21, in complete
    redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/actions.py", line 54, in do_complete
    *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/strategies/base.py", line 62, in complete
    return self.backend.auth_complete(*args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/facebook.py", line 63, in auth_complete
    self.process_error(self.data)
  File …
Run Code Online (Sandbox Code Playgroud)

python django facebook facebook-authentication python-social-auth

25
推荐指数
4
解决办法
8456
查看次数

Django:一个基于类的视图,具有mixins和dispatch方法

通常,我使用dispatch基于类的视图的方法来设置一些初始变量或根据用户的权限添加一些逻辑.

例如,

from django.views.generic import FormView
from braces.views import LoginRequiredMixin

class GenerateReportView(LoginRequiredMixin, FormView):
    template_name = 'reporting/reporting_form.html'
    form_class = ReportForm

    def get_form(self, form_class):
        form = form_class(**self.get_form_kwargs())
        if not self.request.user.is_superuser:
            form.fields['report_type'].choices = [
                choice for choice in form.fields['report_type'].choices
                if choice[0] != INVOICE_REPORT
            ]
        return form
Run Code Online (Sandbox Code Playgroud)

它按预期工作:当匿名用户访问页面时,调用LoginRequiredMixindispatch方法,然后将用户重定向到登录页面.

但是,如果我想为此视图添加一些权限或设置一些初始变量,例如,

class GenerateReportView(LoginRequiredMixin, FormView):

    def dispatch(self, *args, **kwargs):
        if not (
            self.request.user.is_superuser or
            self.request.user.is_manager
        ):
            raise Http404
        return super(GenerateReportView, self).dispatch(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

在某些情况下它不起作用,因为dispatch视图继承的mixins的方法尚未被调用.因此,例如,为了能够请求用户的权限,我必须重复以下验证LoginRequiredMixin:

class GenerateReportView(LoginRequiredMixin, FormView):

    def …
Run Code Online (Sandbox Code Playgroud)

django mixins django-views

22
推荐指数
1
解决办法
1万
查看次数

在网址中没有pk的Django UpdateView

是否有可能pk从与网址相关的消除UpdateView

例如,如果我有

url(r'^myobj/update/(?P<pk>\d+)/$', views.UpdateMyObj.as_view(), name="update")
Run Code Online (Sandbox Code Playgroud)

有什么方法可以写它

url(r'^myobj/update/$', views.UpdateMyObj.as_view(), name="update")
Run Code Online (Sandbox Code Playgroud)

然后pk作为参数发送POSTGET请求?

django django-urls django-views django-class-based-views

12
推荐指数
1
解决办法
1万
查看次数

ExtJS 4:有没有办法将QuickTip附加到表单字段?

我正在尝试将QuickTip添加到表单字段,但找不到让我的代码工作的方法.首先,我尝试使用qtip属性

    {
        fieldLabel: 'Last Name',
        qtip:'This tip is not showing at all',
        name: 'last'
    }
Run Code Online (Sandbox Code Playgroud)

然后使用Ext.tip.ToolTip类:

Ext.create('Ext.tip.ToolTip', {
    target: 'rating_field',
    anchor: 'right',
    trackMouse: true,
    html: 'This tip is not showing at all'
});

Ext.QuickTips.init();
Run Code Online (Sandbox Code Playgroud)

这是一个包含源代码的jsfiddle:jsfiddle

extjs extjs4

10
推荐指数
2
解决办法
1万
查看次数

python oauthlib:在转义中ValueError"只有unicode对象可以逃脱"

我正在使用python-social-auth我的Django应用程序登录社交网络.在我的本地机器上一切正常,但当我部署到服务器时,我收到以下错误:

oauthlib.oauth1.rfc5849.utils in escape
ValueError: Only unicode objects are escapable. Got None of type <type 'NoneType'>.
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

File "django/core/handlers/base.py", line 112, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "social/apps/django_app/utils.py", line 45, in wrapper
    return func(request, backend, *args, **kwargs)
File "social/apps/django_app/views.py", line 12, in auth
    return do_auth(request.social_strategy, redirect_name=REDIRECT_FIELD_NAME)
File "social/actions.py", line 25, in do_auth
    return strategy.start()
File "social/strategies/base.py", line 66, in start
    return self.redirect(self.backend.auth_url())
File "social/backends/oauth.py", line 99, in auth_url
    token = self.set_unauthorized_token()
File "social/backends/oauth.py", line 158, in …
Run Code Online (Sandbox Code Playgroud)

python django oauth python-requests python-social-auth

9
推荐指数
1
解决办法
2305
查看次数

在Django中使用python-social-auth和电子邮件注册复制电子邮件

我在我的项目中使用python-social-auth和电子邮件注册.对于用户模型,我使用AbstractBaseUser的子类:

class User(AbstractBaseUser):
    USERNAME_FIELD = 'email'


AUTH_USER_MODEL = 'userprofile.User'
Run Code Online (Sandbox Code Playgroud)

但是,如果用他的电子邮件注册的用户(demo@demo.com)和密码尝试使用与同一电子邮件地址关联的Facebook帐户登录,则会收到以下错误:

IntegrityError at /social/complete/facebook/
duplicate key value violates unique constraint "userprofile_user_email_key"
DETAIL:  Key (email)=(demo@demo.com) already exists.

/Users/vera/.virtualenvs/app/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                    response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
/Users/vera/.virtualenvs/app/lib/python2.7/site-packages/django/views/decorators/csrf.py in wrapped_view
        return view_func(*args, **kwargs) ...
/Users/vera/.virtualenvs/app/lib/python2.7/site-packages/social/apps/django_app/utils.py in wrapper
            return func(request, backend, *args, **kwargs) ...
/Users/vera/.virtualenvs/app/lib/python2.7/site-packages/social/apps/django_app/views.py in complete
                       redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs) ...
/Users/vera/.virtualenvs/app/lib/python2.7/site-packages/social/actions.py in do_complete
                                 *args, **kwargs) ...
/Users/vera/.virtualenvs/app/lib/python2.7/site-packages/social/strategies/base.py in complete
        return self.backend.auth_complete(*args, **kwargs) ...
/Users/vera/.virtualenvs/app/lib/python2.7/site-packages/social/backends/facebook.py in auth_complete
        return self.do_auth(access_token, response, *args, **kwargs) ...
/Users/vera/.virtualenvs/app/lib/python2.7/site-packages/social/backends/facebook.py …
Run Code Online (Sandbox Code Playgroud)

python django django-authentication python-social-auth

8
推荐指数
1
解决办法
2296
查看次数

如何有条件地在 Angular 中包含脚本元素

我正在尝试有条件地在我的页面上包含聊天:

<body ng-app="myApp">
  <!-- some code -->

  <script type="text/javascript" ng-if="expression">
    window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
    d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
    _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
    $.src="//v2.zopim.com/?my-zopim-id";z.t=+new Date;$.
    type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
  </script>
</body>
Run Code Online (Sandbox Code Playgroud)

但聊天脚本似乎是在ng-if指令之前执行的。

如何让我的 Angular 应用程序首先检查条件,然后从<script>标签执行脚本?

javascript angularjs angularjs-directive

7
推荐指数
1
解决办法
6258
查看次数

如何从Python(Django)中获取Google Analytics中最受欢迎的网页列表?

我正在尝试使用其文档提供的代码访问Google AnalyticsAPI:https://developers.google.com/analytics/solutions/articles/hello-analytics-api

import httplib2
import os

from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run


CLIENT_SECRETS = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'client_secrets.json')
MISSING_CLIENT_SECRETS_MESSAGE = '%s is missing' % CLIENT_SECRETS
FLOW = flow_from_clientsecrets('%s' % CLIENT_SECRETS,
    scope='https://www.googleapis.com/auth/analytics.readonly',
    message=MISSING_CLIENT_SECRETS_MESSAGE,
)
TOKEN_FILE_NAME = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'analytics.dat')


def prepare_credentials():
    storage = Storage(TOKEN_FILE_NAME)
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        credentials = run(FLOW, storage)

    return credentials


def initialize_service():
    http = httplib2.Http()
    credentials = prepare_credentials()
    http = credentials.authorize(http)

    return …
Run Code Online (Sandbox Code Playgroud)

python django google-analytics google-analytics-api

6
推荐指数
1
解决办法
1072
查看次数

如何对related_name 字段进行查询?

我必须通过外键连接模型

class User(AbstractUser):
   ...
Run Code Online (Sandbox Code Playgroud)

class PrivateMessage(models.Model):
    user_from = models.ForeignKey(
        User,
        verbose_name=u'From',
        related_name='sent_messages',
    )
    user_to = models.ForeignKey(
        User,
        verbose_name=u'To',
        related_name='received_messages',
    )
Run Code Online (Sandbox Code Playgroud)

有没有办法获取特定用户的所有地址。例如,如果

u = User.objects.get(id=1)
messages = PrivateMessage.objects.filter(user_from=u)
for m in messages:
    users.add(m.user_to)
Run Code Online (Sandbox Code Playgroud)

如何user_to仅使用 Django ORM 方法获取出现在这些消息中的用户列表?

django django-models django-orm

5
推荐指数
2
解决办法
1万
查看次数

如何在CKEDITOR中动态生成RichCombo的选项?

我的页面上有一个表单,包含textarea(CKEDITOR)和select字段<select id="_photogalleries" multiple="multiple"></select>.我希望RichCombo中的选项依赖于select with id中选择的选项#_photogalleries.有没有办法动态重新生成RichCombo?提前致谢.

CKEDITOR.plugins.add('style_plugin', {
        requires: ['richcombo'],
        init: function(editor) {
            var pluginName = 'style_plugin';
            var config = editor.config,
                lang = editor.lang.format;

            editor.ui.addRichCombo('photogalleries', {
                label: "????????????",
                title: "????????????",
                voiceLabel: "????????????",
                className: 'cke_format',
                multiSelect: false,
                icon: CKEDITOR.plugins.getPath('style_plugin') + 'photo-list-horizontal.png',

                panel: {
                    css: [config.contentsCss, CKEDITOR.getUrl(editor.skinPath + 'editor.css')],
                    voiceLabel: lang.panelVoiceLabel
                },

                init: function () {
                    this.startGroup("????????????");
                    var list=this;
                    $("#_photogalleries option:selected").each(function(index, value){
                        console.log(index, value);
                        list.add("#HORIZONTAL_GALLERY_"+ $(value).val()+"#", "(?) " + $(value).text(), "(?) " + $(value).text());
                        list.add("#VERTICAL_GALLERY_"+ $(value).val()+"#",   "(?) " + …
Run Code Online (Sandbox Code Playgroud)

javascript ckeditor

3
推荐指数
1
解决办法
4613
查看次数

针对某些特定 RSpec 测试的自定义 VCR 请求匹配选项

我的 Rails 项目中有很多 RSpec 测试,用于测试对外部 REST API 的 HTTP 调用,并使用 VCR 磁带记录请求和响应。目前我的 VCR 配置如下:

VCR.configure do |c|
  c.cassette_library_dir = 'spec/vcr_cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
end
Run Code Online (Sandbox Code Playgroud)

因此请求匹配规则仅匹配 HTTP 方法和 URI。我想更改此设置以匹配请求正文:

VCR.configure do |c|
  c.cassette_library_dir = 'spec/vcr_cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
  c.default_cassette_options = {
    :match_requests_on => [:uri, :method, :body],
  }
end
Run Code Online (Sandbox Code Playgroud)

但由于我的项目中已经有很多测试,我想逐步进行,仅对某些测试激活此新限制,这样其他测试(使用旧磁带)就不会中断。

有没有办法将参数传递给 RSpec 测试,以便仅针对某些特定测试或测试组制定自定义请求匹配规则?

我想象类似的事情

it 'reverts transaction', :vcr, :body_matching => true do
    # something
end
Run Code Online (Sandbox Code Playgroud)

然后根据body_matching参数动态更改设置。

ruby-on-rails rspec-rails vcr

2
推荐指数
1
解决办法
2056
查看次数