コード例 #1
0
ファイル: robust.py プロジェクト: gitdlam/geraldo
def sendRobust(
    signal=Any, 
    sender=Anonymous, 
    *arguments, **named
):
    """Send signal from sender to all connected receivers catching errors
    
    signal -- (hashable) signal value, see connect for details

    sender -- the sender of the signal
    
        if Any, only receivers registered for Any will receive
        the message.

        if Anonymous, only receivers registered to receive
        messages from Anonymous or Any will receive the message

        Otherwise can be any python object (normally one
        registered with a connect if you actually want
        something to occur).

    arguments -- positional arguments which will be passed to
        *all* receivers. Note that this may raise TypeErrors
        if the receivers do not allow the particular arguments.
        Note also that arguments are applied before named
        arguments, so they should be used with care.

    named -- named arguments which will be filtered according
        to the parameters of the receivers to only provide those
        acceptable to the receiver.

    Return a list of tuple pairs [(receiver, response), ... ]

    if any receiver raises an error (specifically any subclass of Exception),
    the error instance is returned as the result for that receiver.
    """
    # Call each receiver with whatever arguments it can accept.
    # Return a list of tuple pairs [(receiver, response), ... ].
    responses = []
    for receiver in liveReceivers(getAllReceivers(sender, signal)):
        try:
            response = robustApply(
                receiver,
                signal=signal,
                sender=sender,
                *arguments,
                **named
            )
        except Exception as err:
            responses.append((receiver, err))
        else:
            responses.append((receiver, response))
    return responses
コード例 #2
0
def sendRobust(signal=Any, sender=Anonymous, *arguments, **named):
    """Send signal from sender to all connected receivers catching errors
    
    signal -- (hashable) signal value, see connect for details

    sender -- the sender of the signal
    
        if Any, only receivers registered for Any will receive
        the message.

        if Anonymous, only receivers registered to receive
        messages from Anonymous or Any will receive the message

        Otherwise can be any python object (normally one
        registered with a connect if you actually want
        something to occur).

    arguments -- positional arguments which will be passed to
        *all* receivers. Note that this may raise TypeErrors
        if the receivers do not allow the particular arguments.
        Note also that arguments are applied before named
        arguments, so they should be used with care.

    named -- named arguments which will be filtered according
        to the parameters of the receivers to only provide those
        acceptable to the receiver.

    Return a list of tuple pairs [(receiver, response), ... ]

    if any receiver raises an error (specifically any subclass of Exception),
    the error instance is returned as the result for that receiver.
    """
    # Call each receiver with whatever arguments it can accept.
    # Return a list of tuple pairs [(receiver, response), ... ].
    responses = []
    for receiver in liveReceivers(getAllReceivers(sender, signal)):
        try:
            response = robustApply(receiver,
                                   signal=signal,
                                   sender=sender,
                                   *arguments,
                                   **named)
        except Exception as err:
            responses.append((receiver, err))
        else:
            responses.append((receiver, response))
    return responses
コード例 #3
0

def create_superuser(app, created_models, verbosity, **kwargs):
    from django.contrib.auth.models import User
    from django.core.management import call_command
    if User in created_models and kwargs.get('interactive', True):
        msg = "\nYou just installed Django's auth system, which means you don't have " \
                "any superusers defined.\nWould you like to create one now? (yes/no): "
        confirm = raw_input(msg)
        while 1:
            if confirm not in ('yes', 'no'):
                confirm = raw_input('Please enter either "yes" or "no": ')
                continue
            if confirm == 'yes':
                call_command("createsuperuser", interactive=True)
            break


if 'create_permissions' not in [
        i.__name__
        for i in dispatcher.getAllReceivers(signal=signals.post_syncdb)
]:
    dispatcher.connect(create_permissions, signal=signals.post_syncdb)
if 'create_superuser' not in [
        i.__name__
        for i in dispatcher.getAllReceivers(signal=signals.post_syncdb,
                                            sender=auth_app)
]:
    dispatcher.connect(create_superuser,
                       sender=auth_app,
                       signal=signals.post_syncdb)
コード例 #4
0
    from django.contrib.auth.models import Permission
    app_models = get_models(app)
    if not app_models:
        return
    for klass in app_models:
        ctype = ContentType.objects.get_for_model(klass)
        for codename, name in _get_all_permissions(klass._meta):
            p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,
                defaults={'name': name, 'content_type': ctype})
            if created and verbosity >= 2:
                print("Adding permission '%s'" % p)

def create_superuser(app, created_models, verbosity, **kwargs):
    from django.contrib.auth.models import User
    from django.core.management import call_command
    if User in created_models and kwargs.get('interactive', True):
        msg = "\nYou just installed Django's auth system, which means you don't have " \
                "any superusers defined.\nWould you like to create one now? (yes/no): "
        confirm = input(msg)
        while 1:
            if confirm not in ('yes', 'no'):
                confirm = input('Please enter either "yes" or "no": ')
                continue
            if confirm == 'yes':
                call_command("createsuperuser", interactive=True)
            break

if 'create_permissions' not in [i.__name__ for i in dispatcher.getAllReceivers(signal=signals.post_syncdb)]:
    dispatcher.connect(create_permissions, signal=signals.post_syncdb)
if 'create_superuser' not in [i.__name__ for i in dispatcher.getAllReceivers(signal=signals.post_syncdb, sender=auth_app)]:
    dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)