neulab/docprompting-conala huggingface dataset:
class ModelBackend  
This is the default authentication backend used by Django. It authenticates using credentials consisting of a user identifier and password. For Django’s default user model, the user identifier is the username, for custom user models it is the field specified by USERNAME_FIELD (see Customizing Users and authentication). It also handles the default permissions model as defined for User and PermissionsMixin. has_perm(), get_all_permissions(), get_user_permissions(), and get_group_permissions() allow an object to be passed as a parameter for object-specific permissions, but this backend does not implement them other than returning an empty set of permissions if obj is not None. with_perm() also allows an object to be passed as a parameter, but unlike others methods it returns an empty queryset if obj is not None.  
authenticate(request, username=None, password=None, **kwargs)  
Tries to authenticate username with password by calling User.check_password. If no username is provided, it tries to fetch a username from kwargs using the key CustomUser.USERNAME_FIELD. Returns an authenticated user or None. request is an HttpRequest and may be None if it wasn’t provided to authenticate() (which passes it on to the backend). 
  
get_user_permissions(user_obj, obj=None)  
Returns the set of permission strings the user_obj has from their own user permissions. Returns an empty set if is_anonymous or is_active is False. 
  
get_group_permissions(user_obj, obj=None)  
Returns the set of permission strings the user_obj has from the permissions of the groups they belong. Returns an empty set if is_anonymous or is_active is False. 
  
get_all_permissions(user_obj, obj=None)  
Returns the set of permission strings the user_obj has, including both user permissions and group permissions. Returns an empty set if is_anonymous or is_active is False. 
  
has_perm(user_obj, perm, obj=None)  
Uses get_all_permissions() to check if user_obj has the permission string perm. Returns False if the user is not is_active. 
  
has_module_perms(user_obj, app_label)  
Returns whether the user_obj has any permissions on the app app_label. 
  
user_can_authenticate()  
Returns whether the user is allowed to authenticate. To match the behavior of AuthenticationForm which prohibits inactive users from logging in, this method returns False for users with is_active=False. Custom user models that don’t have an is_active field are allowed. 
  
with_perm(perm, is_active=True, include_superusers=True, obj=None)  
Returns all active users who have the permission perm either in the form of "<app label>.<permission codename>" or a Permission instance. Returns an empty queryset if no users who have the perm found. If is_active is True (default), returns only active users, or if False, returns only inactive users. Use None to return all users irrespective of active state. If include_superusers is True (default), the result will include superusers.
authenticate(request, username=None, password=None, **kwargs)  
Tries to authenticate username with password by calling User.check_password. If no username is provided, it tries to fetch a username from kwargs using the key CustomUser.USERNAME_FIELD. Returns an authenticated user or None. request is an HttpRequest and may be None if it wasn’t provided to authenticate() (which passes it on to the backend).
get_all_permissions(user_obj, obj=None)  
Returns the set of permission strings the user_obj has, including both user permissions and group permissions. Returns an empty set if is_anonymous or is_active is False.
get_group_permissions(user_obj, obj=None)  
Returns the set of permission strings the user_obj has from the permissions of the groups they belong. Returns an empty set if is_anonymous or is_active is False.
get_user_permissions(user_obj, obj=None)  
Returns the set of permission strings the user_obj has from their own user permissions. Returns an empty set if is_anonymous or is_active is False.
has_module_perms(user_obj, app_label)  
Returns whether the user_obj has any permissions on the app app_label.
has_perm(user_obj, perm, obj=None)  
Uses get_all_permissions() to check if user_obj has the permission string perm. Returns False if the user is not is_active.
user_can_authenticate()  
Returns whether the user is allowed to authenticate. To match the behavior of AuthenticationForm which prohibits inactive users from logging in, this method returns False for users with is_active=False. Custom user models that don’t have an is_active field are allowed.
with_perm(perm, is_active=True, include_superusers=True, obj=None)  
Returns all active users who have the permission perm either in the form of "<app label>.<permission codename>" or a Permission instance. Returns an empty queryset if no users who have the perm found. If is_active is True (default), returns only active users, or if False, returns only inactive users. Use None to return all users irrespective of active state. If include_superusers is True (default), the result will include superusers.
class RemoteUserBackend  
Use this backend to take advantage of external-to-Django-handled authentication. It authenticates using usernames passed in request.META['REMOTE_USER']. See the Authenticating against REMOTE_USER documentation. If you need more control, you can create your own authentication backend that inherits from this class and override these attributes or methods:  
create_unknown_user  
True or False. Determines whether or not a user object is created if not already in the database Defaults to True. 
  
authenticate(request, remote_user)  
The username passed as remote_user is considered trusted. This method returns the user object with the given username, creating a new user object if create_unknown_user is True. Returns None if create_unknown_user is False and a User object with the given username is not found in the database. request is an HttpRequest and may be None if it wasn’t provided to authenticate() (which passes it on to the backend). 
  
clean_username(username)  
Performs any cleaning on the username (e.g. stripping LDAP DN information) prior to using it to get or create a user object. Returns the cleaned username. 
  
configure_user(request, user)  
Configures a newly created user. This method is called immediately after a new user is created, and can be used to perform custom setup actions, such as setting the user’s groups based on attributes in an LDAP directory. Returns the user object. request is an HttpRequest and may be None if it wasn’t provided to authenticate() (which passes it on to the backend). 
  
user_can_authenticate()  
Returns whether the user is allowed to authenticate. This method returns False for users with is_active=False. Custom user models that don’t have an is_active field are allowed.
authenticate(request, remote_user)  
The username passed as remote_user is considered trusted. This method returns the user object with the given username, creating a new user object if create_unknown_user is True. Returns None if create_unknown_user is False and a User object with the given username is not found in the database. request is an HttpRequest and may be None if it wasn’t provided to authenticate() (which passes it on to the backend).
clean_username(username)  
Performs any cleaning on the username (e.g. stripping LDAP DN information) prior to using it to get or create a user object. Returns the cleaned username.
configure_user(request, user)  
Configures a newly created user. This method is called immediately after a new user is created, and can be used to perform custom setup actions, such as setting the user’s groups based on attributes in an LDAP directory. Returns the user object. request is an HttpRequest and may be None if it wasn’t provided to authenticate() (which passes it on to the backend).
create_unknown_user  
True or False. Determines whether or not a user object is created if not already in the database Defaults to True.
user_can_authenticate()  
Returns whether the user is allowed to authenticate. This method returns False for users with is_active=False. Custom user models that don’t have an is_active field are allowed.
login_required(redirect_field_name='next', login_url=None)  
As a shortcut, you can use the convenient login_required() decorator: from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...
 login_required() does the following:  If the user isn’t logged in, redirect to settings.LOGIN_URL, passing the current absolute path in the query string. Example: /accounts/login/?next=/polls/3/. If the user is logged in, execute the view normally. The view code is free to assume the user is logged in.  By default, the path that the user should be redirected to upon successful authentication is stored in a query string parameter called "next". If you would prefer to use a different name for this parameter, login_required() takes an optional redirect_field_name parameter: from django.contrib.auth.decorators import login_required

@login_required(redirect_field_name='my_redirect_field')
def my_view(request):
    ...
 Note that if you provide a value to redirect_field_name, you will most likely need to customize your login template as well, since the template context variable which stores the redirect path will use the value of redirect_field_name as its key rather than "next" (the default). login_required() also takes an optional login_url parameter. Example: from django.contrib.auth.decorators import login_required

@login_required(login_url='/accounts/login/')
def my_view(request):
    ...
 Note that if you don’t specify the login_url parameter, you’ll need to ensure that the settings.LOGIN_URL and your login view are properly associated. For example, using the defaults, add the following lines to your URLconf: from django.contrib.auth import views as auth_views

path('accounts/login/', auth_views.LoginView.as_view()),
 The settings.LOGIN_URL also accepts view function names and named URL patterns. This allows you to freely remap your login view within your URLconf without having to update the setting.
permission_required(perm, login_url=None, raise_exception=False)  
It’s a relatively common task to check whether a user has a particular permission. For that reason, Django provides a shortcut for that case: the permission_required() decorator.: from django.contrib.auth.decorators import permission_required

@permission_required('polls.add_choice')
def my_view(request):
    ...
 Just like the has_perm() method, permission names take the form "<app label>.<permission codename>" (i.e. polls.add_choice for a permission on a model in the polls application). The decorator may also take an iterable of permissions, in which case the user must have all of the permissions in order to access the view. Note that permission_required() also takes an optional login_url parameter: from django.contrib.auth.decorators import permission_required

@permission_required('polls.add_choice', login_url='/loginpage/')
def my_view(request):
    ...
 As in the login_required() decorator, login_url defaults to settings.LOGIN_URL. If the raise_exception parameter is given, the decorator will raise PermissionDenied, prompting the 403 (HTTP Forbidden) view instead of redirecting to the login page. If you want to use raise_exception but also give your users a chance to login first, you can add the login_required() decorator: from django.contrib.auth.decorators import login_required, permission_required

@login_required
@permission_required('polls.add_choice', raise_exception=True)
def my_view(request):
    ...
 This also avoids a redirect loop when LoginView’s redirect_authenticated_user=True and the logged-in user doesn’t have all of the required permissions.
user_passes_test(test_func, login_url=None, redirect_field_name='next')  
As a shortcut, you can use the convenient user_passes_test decorator which performs a redirect when the callable returns False: from django.contrib.auth.decorators import user_passes_test

def email_check(user):
    return user.email.endswith('@example.com')

@user_passes_test(email_check)
def my_view(request):
    ...
 user_passes_test() takes a required argument: a callable that takes a User object and returns True if the user is allowed to view the page. Note that user_passes_test() does not automatically check that the User is not anonymous. user_passes_test() takes two optional arguments:  
login_url  Lets you specify the URL that users who don’t pass the test will be redirected to. It may be a login page and defaults to settings.LOGIN_URL if you don’t specify one. 
redirect_field_name  Same as for login_required(). Setting it to None removes it from the URL, which you may want to do if you are redirecting users that don’t pass the test to a non-login page where there’s no “next page”.  For example: @user_passes_test(email_check, login_url='/login/')
def my_view(request):
    ...
class AdminPasswordChangeForm  
A form used in the admin interface to change a user’s password. Takes the user as the first positional argument.
class AuthenticationForm  
A form for logging a user in. Takes request as its first positional argument, which is stored on the form instance for use by sub-classes.  
confirm_login_allowed(user)  
By default, AuthenticationForm rejects users whose is_active flag is set to False. You may override this behavior with a custom policy to determine which users can log in. Do this with a custom form that subclasses AuthenticationForm and overrides the confirm_login_allowed() method. This method should raise a ValidationError if the given user may not log in. For example, to allow all users to log in regardless of “active” status: from django.contrib.auth.forms import AuthenticationForm

class AuthenticationFormWithInactiveUsersOkay(AuthenticationForm):
    def confirm_login_allowed(self, user):
        pass
 (In this case, you’ll also need to use an authentication backend that allows inactive users, such as AllowAllUsersModelBackend.) Or to allow only some active users to log in: class PickyAuthenticationForm(AuthenticationForm):
    def confirm_login_allowed(self, user):
        if not user.is_active:
            raise ValidationError(
                _("This account is inactive."),
                code='inactive',
            )
        if user.username.startswith('b'):
            raise ValidationError(
                _("Sorry, accounts starting with 'b' aren't welcome here."),
                code='no_b_users',
            )
confirm_login_allowed(user)  
By default, AuthenticationForm rejects users whose is_active flag is set to False. You may override this behavior with a custom policy to determine which users can log in. Do this with a custom form that subclasses AuthenticationForm and overrides the confirm_login_allowed() method. This method should raise a ValidationError if the given user may not log in. For example, to allow all users to log in regardless of “active” status: from django.contrib.auth.forms import AuthenticationForm

class AuthenticationFormWithInactiveUsersOkay(AuthenticationForm):
    def confirm_login_allowed(self, user):
        pass
 (In this case, you’ll also need to use an authentication backend that allows inactive users, such as AllowAllUsersModelBackend.) Or to allow only some active users to log in: class PickyAuthenticationForm(AuthenticationForm):
    def confirm_login_allowed(self, user):
        if not user.is_active:
            raise ValidationError(
                _("This account is inactive."),
                code='inactive',
            )
        if user.username.startswith('b'):
            raise ValidationError(
                _("Sorry, accounts starting with 'b' aren't welcome here."),
                code='no_b_users',
            )
class PasswordResetForm  
A form for generating and emailing a one-time use link to reset a user’s password.  
send_mail(subject_template_name, email_template_name, context, from_email, to_email, html_email_template_name=None)  
Uses the arguments to send an EmailMultiAlternatives. Can be overridden to customize how the email is sent to the user.    
Parameters:

 
subject_template_name – the template for the subject. 
email_template_name – the template for the email body. 
context – context passed to the subject_template, email_template, and html_email_template (if it is not None). 
from_email – the sender’s email. 
to_email – the email of the requester. 
html_email_template_name – the template for the HTML body; defaults to None, in which case a plain text email is sent.     By default, save() populates the context with the same variables that PasswordResetView passes to its email context.
send_mail(subject_template_name, email_template_name, context, from_email, to_email, html_email_template_name=None)  
Uses the arguments to send an EmailMultiAlternatives. Can be overridden to customize how the email is sent to the user.    
Parameters:

 
subject_template_name – the template for the subject. 
email_template_name – the template for the email body. 
context – context passed to the subject_template, email_template, and html_email_template (if it is not None). 
from_email – the sender’s email. 
to_email – the email of the requester. 
html_email_template_name – the template for the HTML body; defaults to None, in which case a plain text email is sent.     By default, save() populates the context with the same variables that PasswordResetView passes to its email context.
class SetPasswordForm  
A form that lets a user change their password without entering the old password.
class UserChangeForm  
A form used in the admin interface to change a user’s information and permissions.
class UserCreationForm  
A ModelForm for creating a new user. It has three fields: username (from the user model), password1, and password2. It verifies that password1 and password2 match, validates the password using validate_password(), and sets the user’s password using set_password().
get_user(request)  
Returns the user model instance associated with the given request’s session. It checks if the authentication backend stored in the session is present in AUTHENTICATION_BACKENDS. If so, it uses the backend’s get_user() method to retrieve the user model instance and then verifies the session by calling the user model’s get_session_auth_hash() method. Returns an instance of AnonymousUser if the authentication backend stored in the session is no longer in AUTHENTICATION_BACKENDS, if a user isn’t returned by the backend’s get_user() method, or if the session auth hash doesn’t validate.
get_user_model()  
Instead of referring to User directly, you should reference the user model using django.contrib.auth.get_user_model(). This method will return the currently active user model – the custom user model if one is specified, or User otherwise. When you define a foreign key or many-to-many relations to the user model, you should specify the custom model using the AUTH_USER_MODEL setting. For example: from django.conf import settings
from django.db import models

class Article(models.Model):
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
 When connecting to signals sent by the user model, you should specify the custom model using the AUTH_USER_MODEL setting. For example: from django.conf import settings
from django.db.models.signals import post_save

def post_save_receiver(sender, instance, created, **kwargs):
    pass

post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)
 Generally speaking, it’s easiest to refer to the user model with the AUTH_USER_MODEL setting in code that’s executed at import time, however, it’s also possible to call get_user_model() while Django is importing models, so you could use models.ForeignKey(get_user_model(), ...). If your app is tested with multiple user models, using @override_settings(AUTH_USER_MODEL=...) for example, and you cache the result of get_user_model() in a module-level variable, you may need to listen to the setting_changed signal to clear the cache. For example: from django.apps import apps
from django.contrib.auth import get_user_model
from django.core.signals import setting_changed
from django.dispatch import receiver

@receiver(setting_changed)
def user_model_swapped(**kwargs):
    if kwargs['setting'] == 'AUTH_USER_MODEL':
        apps.clear_cache()
        from myapp import some_module
        some_module.UserModel = get_user_model()
check_password(password, encoded)  
If you’d like to manually authenticate a user by comparing a plain-text password to the hashed password in the database, use the convenience function check_password(). It takes two arguments: the plain-text password to check, and the full value of a user’s password field in the database to check against, and returns True if they match, False otherwise.
is_password_usable(encoded_password)  
Returns False if the password is a result of User.set_unusable_password().
make_password(password, salt=None, hasher='default')  
Creates a hashed password in the format used by this application. It takes one mandatory argument: the password in plain-text (string or bytes). Optionally, you can provide a salt and a hashing algorithm to use, if you don’t want to use the defaults (first entry of PASSWORD_HASHERS setting). See Included hashers for the algorithm name of each hasher. If the password argument is None, an unusable password is returned (one that will never be accepted by check_password()).
login(request, user, backend=None)  
To log a user in, from a view, use login(). It takes an HttpRequest object and a User object. login() saves the user’s ID in the session, using Django’s session framework. Note that any data set during the anonymous session is retained in the session after a user logs in. This example shows how you might use both authenticate() and login(): from django.contrib.auth import authenticate, login

def my_view(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(request, username=username, password=password)
    if user is not None:
        login(request, user)
        # Redirect to a success page.
        ...
    else:
        # Return an 'invalid login' error message.
        ...
logout(request)  
To log out a user who has been logged in via django.contrib.auth.login(), use django.contrib.auth.logout() within your view. It takes an HttpRequest object and has no return value. Example: from django.contrib.auth import logout

def logout_view(request):
    logout(request)
    # Redirect to a success page.
 Note that logout() doesn’t throw any errors if the user wasn’t logged in. When you call logout(), the session data for the current request is completely cleaned out. All existing data is removed. This is to prevent another person from using the same web browser to log in and have access to the previous user’s session data. If you want to put anything into the session that will be available to the user immediately after logging out, do that after calling django.contrib.auth.logout().