0

在使用 django 的自定义模型方面,我遇到了一些问题。这一点尤其没有引发任何错误。由于某种原因,在通过 steam 进行身份验证并返回登录页面后,steamuser_user(自定义用户)和 social_auth_usersocialauth 的数据库表都是空的。没有保存任何内容,没有显示错误等。

我的自定义模型与 django docs 官方页面上的模型非常相似,如下所示:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import BaseUserManager

# Create your models here.
class UserManager(BaseUserManager):
    def create_user(self, steamid, username, password=None):
        if not steamid:
            msg = 'User has no Steam ID set'
            raise ValueError(msg)
        if not username:
            msg = 'User has no name set'
            raise ValueError(msg)

        user = self.model(steamid=steamid,
                          username=username)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, steamid, username, password):
        super_user = self.create_user(steamid=steamid,
                                      username=username,
                                      password=password)
        super_user.is_staff = True
        super_user.is_admin = True
        super_user.is_mod = True
        super_user.save(using=self._db)
        return super_user


class User(AbstractBaseUser):
    steamid = models.CharField(max_length=20, unique=True)
    username = models.CharField(max_length=80)
    email = models.EmailField(null=True,blank=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    is_mod = models.BooleanField(default=False)
    date_joined = models.DateTimeField(auto_now_add=True)
    reputation = models.IntegerField(max_length=6, default=0)

    USERNAME_FIELD = 'steamid'

    objects = UserManager()

    def __unicode__(self):
        return self.username

    def get_full_name(self):
        return self.steamid

    def get_short_name(self):
        return self.username

我使用的设置如下:

SOCIAL_AUTH_USER_MODEL = 'steamuser.User'

AUTH_USER_MODEL = 'steamuser.User'

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
                                                                            'social.apps.django_app.context_processors.backends',
                                                                            'social.apps.django_app.context_processors.login_redirect',
                                                                            )

AUTHENTICATION_BACKENDS = (
                           'social.backends.steam.SteamOpenId',
                           'django.contrib.auth.backends.ModelBackend',
                           )
#Steam OpenAuth
SOCIAL_AUTH_STEAM_API_KEY = 'B1D7C629D093D4B72577F2F11DE4EBE2'
LOGIN_URL = '/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_ENABLED_BACKENDS = (
                                'steam',
                                )

任何帮助,将不胜感激!

编辑

后端 steam.py

    def get_user_details(self, response):
    player = self.get_json(USER_INFO, params={
        'key': self.setting('API_KEY'),
        'steamids': self._user_id(response)
    })
    if len(player['response']['players']) > 0:
        player = player['response']['players'][0]
        details = {'steamid': player.get('steamid'),
                   'username': player.get('personaname'),
                   }
    else:
        details = {}
    return details

编辑 2

好吧,尽管我的逻辑推理,我只是放弃并创建了一个自定义管道来创建新的蒸汽用户,如下所示:

from django.contrib.auth import get_user_model

def create_steamuser(details, user=None, *args, **kwargs):
    if user:
        return {'is_new': False}

    if not details:
        return

    try:
        steam_user = get_user_model().objects.get(steamid=details['steamid'])
    except steam_user.DoesNotExist:
        get_user_model().objects.create_user(details['steamid'], details['username'])

    return {
        'is_new': True,
    }

现在我仍然有未创建 social_user 的问题。我已将社交用户模型设置为使用我的新自定义模型,但一定有一些我遗漏的东西。

4

1 回答 1

0

python-social-auth将无法将steamidanddate_joined参数传递给create_user()管理器中的自定义方法。为了使这成为可能,您有三个选择:

  1. 设置=None这些参数并为它们设置一些默认值
  2. 覆盖默认create_user管道并传递额外的参数。
  3. 在前面添加一个自定义管道函数并用andcreate_user填充,然后定义.detailssteamiddate_joinedSOCIAL_AUTH_STEAM_USER_FIELDS = ('username', 'email', 'steamid', 'date_joined')
于 2014-08-11T14:31:23.087 回答