add support for django 1.11 in management commands (#21489)

This commit is contained in:
Elias Showk 2018-05-25 15:34:02 +02:00
parent 737a56f148
commit 75f7ef503a
7 changed files with 113 additions and 117 deletions

View File

@ -1,7 +1,6 @@
from __future__ import unicode_literals
import getpass
from optparse import make_option
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
@ -12,14 +11,16 @@ from django.core.exceptions import MultipleObjectsReturned
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--database', action='store', dest='database',
default=DEFAULT_DB_ALIAS, help='Specifies the database to use. Default is "default".'),
)
help = "Change a user's password for django.contrib.auth."
requires_system_checks = False
def add_arguments(self, parser):
parser.add_argument('args', nargs='*')
parser.add_argument('--database', action='store', dest='database',
default=DEFAULT_DB_ALIAS, help='Specifies the database to use. Default is "default".')
def _get_pass(self, prompt="Password: "):
p = getpass.getpass(prompt=force_str(prompt))
if not p:

View File

@ -1,4 +1,3 @@
from optparse import make_option
import logging
import datetime
@ -23,22 +22,23 @@ class Command(BaseCommand):
args = '<clean_threshold>'
help = '''Clean unused accounts'''
option_list = BaseCommand.option_list + (
make_option("--alert-thresholds",
help='list of durations before sending an alert '
'message for unused account, default is none',
default = None),
make_option("--period", type='int',
help='period between two calls to '
'clean-unused-accounts as days, default is 1',
default=1),
make_option("--fake", action='store_true', help='do nothing',
default=False),
make_option("--filter", help='filter to apply to the user queryset, '
'the Django filter key and value are separated by character =', action='append', default=[]),
make_option('--from-email', default=settings.DEFAULT_FROM_EMAIL,
help='sender address for notifications, default is DEFAULT_FROM_EMAIL from settings'),
)
def add_arguments(self, parser):
parser.add_argument('args', nargs='*')
parser.add_argument("--alert-thresholds",
help='list of durations before sending an alert '
'message for unused account, default is none',
default = None)
parser.add_argument("--period", type=int,
help='period between two calls to '
'clean-unused-accounts as days, default is 1',
default=1)
parser.add_argument("--fake", action='store_true', help='do nothing',
default=False)
parser.add_argument("--filter", help='filter to apply to the user queryset, '
'the Django filter key and value are separated by character =', action='append', default=[])
parser.add_argument('--from-email', default=settings.DEFAULT_FROM_EMAIL,
help='sender address for notifications, default is DEFAULT_FROM_EMAIL from settings')
def handle(self, *args, **options):
log = logging.getLogger(__name__)
@ -119,8 +119,8 @@ class Command(BaseCommand):
if not user.email:
log.debug('%s has no email, no mail sent', user)
subject = render_to_string(prefix + '_subject.txt', ctx).strip()
body = render_to_string(prefix + '_body.txt', ctx)
subject = render_to_string(prefix + '_subject.txt', context=ctx).strip()
body = render_to_string(prefix + '_body.txt', context=ctx)
if not self.fake:
try:
log.debug('sending mail to %s', user.email)

View File

@ -1,5 +1,4 @@
import logging
from optparse import make_option
import json
@ -90,51 +89,51 @@ def extra_attribute_parse(option, opt_str, value, parser):
class Command(BaseCommand):
'''Load LDAP ldif file'''
can_import_django_settings = True
requires_model_validation = True
option_list = BaseCommand.option_list + (
make_option('--first-name',
args = '<ldif_file...>'
help = 'Load/update LDIF files as users'
def add_arguments(self, parser):
parser.add_argument('args', nargs='*')
parser.add_argument('--first-name',
default='givenName',
help='attribute used to set the first name'),
make_option('--last-name',
help='attribute used to set the first name')
parser.add_argument('--last-name',
default='sn',
help='attribute used to set the last name'),
make_option('--email',
help='attribute used to set the last name')
parser.add_argument('--email',
default='mail',
help='attribute used to set the email'),
make_option('--username',
help='attribute used to set the email')
parser.add_argument('--username',
default='uid',
help='attribute used to set the username'),
make_option('--password',
help='attribute used to set the username')
parser.add_argument('--password',
default='userPassword',
help='attribute to extract the password from, OpenLDAP hashing algorithm are recognized'),
make_option('--object-class',
help='attribute to extract the password from, OpenLDAP hashing algorithm are recognized')
parser.add_argument('--object-class',
default='inetOrgPerson',
help='object class of records to load'),
make_option('--extra-attribute',
help='object class of records to load')
parser.add_argument('--extra-attribute',
default={},
action='callback',
nargs=2,
type='string',
type=str,
callback=extra_attribute_parse,
help='object class of records to load'),
make_option('--result',
help='object class of records to load')
parser.add_argument('--result',
default=None,
help='file to store a JSON log of created users'),
make_option('--fake',
help='file to store a JSON log of created users')
parser.add_argument('--fake',
action='store_true',
help='file to store a JSON log of created users'),
make_option('--realm',
parser.add_argument('--realm',
default=None,
help='realm for the new users'),
make_option('--callback',
help='realm for the new users')
parser.add_argument('--callback',
default=None,
help='python file containing a function callback(user, dn, entry, options, dump) it can return models that will be saved'),
make_option('--callback-arg',
help='python file containing a function callback(user, dn, entry, options, dump) it can return models that will be saved')
parser.add_argument('--callback-arg',
action='append',
help='arguments for the callback'),
)
args = '<ldif_file...>'
help = 'Load/update LDIF files as users'
help='arguments for the callback')
@atomic
def handle(self, *args, **options):

View File

@ -1,5 +1,4 @@
import getpass
from optparse import make_option
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
@ -9,14 +8,16 @@ from authentic2.utils import generate_password
from authentic2.models import PasswordReset
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--database', action='store', dest='database',
default=DEFAULT_DB_ALIAS, help='Specifies the database to use. Default is "default".'),
)
help = "Reset a user's password for django.contrib.auth."
require_model_validation = False
def add_arguments(self, parser):
parser.add_argument('args', nargs='*')
parser.add_argument('--database', action='store', dest='database',
default=DEFAULT_DB_ALIAS, help='Specifies the database to use. Default is "default".')
def _get_pass(self, prompt="Password: "):
p = getpass.getpass(prompt=prompt)
if not p:

View File

@ -1,4 +1,3 @@
from optparse import make_option
import sys
import xml.etree.ElementTree as etree
import os
@ -226,58 +225,62 @@ class Command(BaseCommand):
and LibertyIdentityProvider files'''
can_import_django_settings = True
output_transaction = True
requires_model_validation = True
option_list = BaseCommand.option_list + (
make_option(
args = '<metadata_file>'
help = 'Load the specified SAMLv2 metadata file'
def add_arguments(self, parser):
parser.add_argument('args', nargs='*')
parser.add_argument(
'--idp',
action='store_true',
dest='idp',
default=False,
help='Do nothing'),
make_option(
help='Do nothing')
parser.add_argument(
'--sp',
action='store_true',
dest='sp',
default=False,
help='Do nothing'),
make_option(
parser.add_argument(
'--sp-policy',
dest='sp_policy',
default=None,
help='SAML2 service provider options policy'),
make_option(
help='SAML2 service provider options policy')
parser.add_argument(
'--delete',
action='store_true',
dest='delete',
default=False,
help='Delete all providers defined in the metadata file (kind of uninstall)'),
make_option(
help='Delete all providers defined in the metadata file (kind of uninstall)')
parser.add_argument(
'--ignore-errors',
action='store_true',
dest='ignore-errors',
default=False,
help='If loading of one EntityDescriptor fails, continue loading'),
make_option(
help='If loading of one EntityDescriptor fails, continue loading')
parser.add_argument(
'--source',
dest='source',
default=None,
help='Tag the loaded providers with the given source string, \
existing providers with the same tag will be removed if they do not exist\
anymore in the metadata file.'),
make_option(
anymore in the metadata file.')
parser.add_argument(
'--reset-attributes',
action='store_true',
default=False,
help='When loading shibboleth attribute filter policies, start by '
'removing all existing SAML attributes for each provider'),
make_option(
'removing all existing SAML attributes for each provider')
parser.add_argument(
'--dont-load-attribute-consuming-service',
dest='load_attribute_consuming_service',
default=True,
action='store_false',
help='Prevent loading of the attribute policy from '
'AttributeConsumingService nodes in the metadata file.'),
make_option(
'AttributeConsumingService nodes in the metadata file.')
parser.add_argument(
'--shibboleth-attribute-filter-policy',
dest='attribute-filter-policy',
default=None,
@ -295,17 +298,13 @@ each provider. The following schema is supported:
</AttributeFilterPolicy>
Any other kind of attribute filter policy is unsupported.
'''),
make_option(
''')
parser.add_argument(
'--create-disabled',
dest='create-disabled',
action='store_true',
default=False,
help='When creating a new provider, make it disabled by default.'),
)
args = '<metadata_file>'
help = 'Load the specified SAMLv2 metadata file'
help='When creating a new provider, make it disabled by default.')
@commit_on_success
def handle(self, *args, **options):

View File

@ -1,7 +1,5 @@
import json
import pprint
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import ValidationError
@ -16,28 +14,29 @@ from django_rbac.utils import get_ou_model
class Command(BaseCommand):
'''Load LDAP ldif file'''
can_import_django_settings = True
requires_model_validation = True
option_list = BaseCommand.option_list + (
make_option('--issuer', help='do automatic registration of the issuer'),
make_option('--openid-configuration', help='file containing the OpenID Connect '
'configuration of the OP'),
make_option('--claim-mapping', default=[], action='append',
help='mapping from claim to attribute'),
make_option('--delete-claim', default=[], action='append',
help='delete mapping from claim to attribute'),
make_option('--client-id', help='registered client ID'),
make_option('--client-secret', help='register client secret'),
make_option('--scope', default=[], action='append',
help='extra scopes, openid is automatic'),
make_option('--no-verify', default=False, action='store_true',
help='do not verify TLS certificates'),
make_option('--show', default=False, action='store_true',
help='show provider configuration'),
make_option('--ou-slug', help='slug of the ou, if absent default ou is used'),
)
args = '<name>'
help = 'Register an OpenID Connect OP'
def add_arguments(self, parser):
parser.add_argument('args', nargs='*')
parser.add_argument('--issuer', help='do automatic registration of the issuer')
parser.add_argument('--openid-configuration', help='file containing the OpenID Connect '
'configuration of the OP')
parser.add_argument('--claim-mapping', default=[], action='append',
help='mapping from claim to attribute')
parser.add_argument('--delete-claim', default=[], action='append',
help='delete mapping from claim to attribute')
parser.add_argument('--client-id', help='registered client ID'),
parser.add_argument('--client-secret', help='register client secret')
parser.add_argument('--scope', default=[], action='append',
help='extra scopes, openid is automatic')
parser.add_argument('--no-verify', default=False, action='store_true',
help='do not verify TLS certificates')
parser.add_argument('--show', default=False, action='store_true',
help='show provider configuration')
parser.add_argument('--ou-slug', help='slug of the ou, if absent default ou is used')
@atomic
def handle(self, name, *args, **options):
openid_configuration = options.get('openid_configuration')
@ -118,4 +117,3 @@ class Command(BaseCommand):
print 'Mappings:'
for claim_mapping in provider.claim_mappings.all():
print '-', claim_mapping

View File

@ -1,4 +1,3 @@
from optparse import make_option
try:
import ldap
@ -21,22 +20,21 @@ REPLACE = 2
DELETE = 3
class Command(BaseCommand):
can_import_django_settings = True
output_transaction = True
requires_model_validation = True
option_list = BaseCommand.option_list + (
make_option('--fake',
def add_arguments(self, parser):
parser.add_argument('args', nargs='*')
parser.add_argument('--fake',
action='store_true',
default=False,
help='Do nothing, just simulate'),
make_option('--batch-size',
help='Do nothing, just simulate')
parser.add_argument('--batch-size',
action='store',
type='int',
type=int,
default=200,
help='Batch size'),
)
help='Batch size')
def handle(self, *args, **options):
ressources = app_settings.RESSOURCES