本文整理汇总了Python中django.contrib.auth.base_user.AbstractBaseUser方法的典型用法代码示例。如果您正苦于以下问题:Python base_user.AbstractBaseUser方法的具体用法?Python base_user.AbstractBaseUser怎么用?Python base_user.AbstractBaseUser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth.base_user
的用法示例。
在下文中一共展示了base_user.AbstractBaseUser方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: schedule
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def schedule(
self,
recipients: Optional[Union[Iterable[Recipient], Recipient]] = None,
sender: Optional[AbstractBaseUser] = None,
priority: Optional[int] = None
) -> Tuple[Message, List[Dispatch]]:
"""Schedules message for a delivery.
Puts message (and dispatches if any) data into DB.
Returns a tuple with message model and a list of dispatch models.
:param recipients: recipient (or a list) or None.
If `None` Dispatches should be created before send using `prepare_dispatches()`.
:param sender: Django User model heir instance
:param priority: number describing message priority
"""
if priority is None:
priority = self.priority
self._message_model, self._dispatch_models = Message.create(
self.get_alias(), self.get_context(), recipients=recipients, sender=sender, priority=priority
)
return self._message_model, self._dispatch_models
示例2: authenticate_by_login_data
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def authenticate_by_login_data(
data: Dict[str, Any], **kwargs) -> 'AbstractBaseUser':
serializer = kwargs.get('serializer')
if serializer:
# TODO: Issue #114 - remove code supporting deprecated behavior
get_authenticated_user = getattr(
serializer, 'get_authenticated_user', None)
if callable(get_authenticated_user):
user = get_authenticated_user()
if not user:
raise UserNotFound()
return user
login_field_names = get_user_login_field_names()
password = data.get('password')
login = data.get('login')
if password is None:
raise UserNotFound()
auth_tests = [] # type: List[Dict[str, Any]]
if login is not None:
auth_tests.extend({
field_name: login,
'password': password,
} for field_name in login_field_names)
for field_name in login_field_names:
field_value = data.get(field_name)
if field_value is None:
continue
auth_tests.append({
field_name: field_value,
'password': password,
})
for auth_kwargs in auth_tests:
user = auth.authenticate(**auth_kwargs)
if user:
return user
raise UserNotFound()
示例3: build_initial_user
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def build_initial_user(data: Dict[str, Any]) -> 'AbstractBaseUser':
user_field_names = get_user_field_names(allow_primary_key=False)
user_data = {}
for field_name in user_field_names:
if field_name in data:
user_data[field_name] = data[field_name]
user_class = get_user_model()
return user_class(**user_data)
示例4: provide_token
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def provide_token(self, user: 'AbstractBaseUser') -> AuthToken:
"""
Get or create token for given user.
If there is no token to provide,
raise ``rest_registration.exceptions.AuthTokenError``.
"""
raise NotImplementedError()
示例5: revoke_token
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def revoke_token(
self, user: 'AbstractBaseUser', *,
token: Optional[AuthToken] = None) -> None:
"""
Revoke the given token for a given user. If the token is not provided,
revoke all tokens for given user.
If the provided token is invalid or there is no token to revoke,
raise ``rest_registration.exceptions.AuthTokenError``.
This method may not be implemented in all cases - for instance, in case
when the token is cryptographically generated and not stored
in the database.
"""
raise AuthTokenNotRevoked()
示例6: get_for_user
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def get_for_user(cls, user: AbstractBaseUser) -> Union[List['Subscription'], QuerySet]:
"""Returns subscriptions for a given user.
:param user:
"""
if user.id is None:
return []
return cls.objects.filter(recipient=user)
示例7: replace_for_user
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def replace_for_user(
cls,
user: AbstractBaseUser,
prefs: List[Tuple[Type['MessageBase'], Type['MessengerBase']]]
) -> bool:
"""Set subscription preferences for a given user.
:param user:
:param prefs: List of tuples (message_cls, messenger_cls)
"""
uid = user.id
if uid is None:
return False
# Remove previous prefs.
cls.objects.filter(recipient_id=uid).delete()
new_prefs = []
for pref in prefs:
new_prefs.append(
cls(**cls._get_base_kwargs(uid, pref[0], pref[1]))
)
if new_prefs:
cls.objects.bulk_create(new_prefs)
return True
示例8: schedule_messages
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def schedule_messages(
messages: Union[str, MessageBase, List[Union[str, MessageBase]]],
recipients: Optional[Union[Iterable[Recipient], Recipient]] = None,
sender: Optional[AbstractBaseUser] = None,
priority: Optional[int] = None
) -> List[Tuple[Message, List[Dispatch]]]:
"""Schedules a message or messages.
:param messages: str or MessageBase heir or list - use str to create PlainTextMessage.
:param recipients: recipients addresses or Django User model heir instances
If `None` Dispatches should be created before send using `prepare_dispatches()`.
:param User|None sender: User model heir instance
:param priority: number describing message priority. If set overrides priority provided with message type.
"""
if not is_iterable(messages):
messages = (messages,)
results = []
for message in messages:
if isinstance(message, str):
message = PlainTextMessage(message)
resulting_priority = message.priority
if priority is not None:
resulting_priority = priority
results.append(message.schedule(sender=sender, recipients=recipients, priority=resulting_priority))
return results
示例9: validate
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def validate(self, password: str, user: Optional[AbstractBaseUser] = None):
amount = api.pwned_password(password)
if amount is None:
# HIBP API failure. Instead of allowing a potentially compromised
# password, check Django's list of common passwords generated from
# the same database.
common_password_validator.validate(password, user)
elif amount:
raise ValidationError(
ngettext(
self.error_message["singular"], self.error_message["plural"], amount
),
params={"amount": amount},
code="pwned_password",
)
示例10: create
# 需要导入模块: from django.contrib.auth import base_user [as 别名]
# 或者: from django.contrib.auth.base_user import AbstractBaseUser [as 别名]
def create(
cls,
message_class: str,
context: dict,
recipients: Optional[Union[Iterable[Recipient], Recipient]] = None,
sender: Optional[AbstractBaseUser] = None,
priority: Optional[int] = None
) -> Tuple['Message', List['Dispatch']]:
"""Creates a message (and dispatches).
Returns a tuple: (message_model, list_of_dispatches)
:param message_class: alias of MessageBase heir
:param context: context for a message
:param recipients: recipient (or a list) or None.
If `None` Dispatches should be created before send using `prepare_dispatches()`.
:param sender: Django User model heir instance
:param priority: number describing message priority
"""
dispatches_ready = False
if recipients is not None:
dispatches_ready = True
msg_kwargs = {
'cls': message_class,
'context': context,
'sender': sender,
'dispatches_ready': dispatches_ready
}
if priority is not None:
msg_kwargs['priority'] = priority
message_model = cls(**msg_kwargs)
message_model.save()
dispatch_models = Dispatch.create(message_model, recipients)
return message_model, dispatch_models