示例#1
0
 def render_html(self, product, postfix):
     """
     Return a HTML snippet containing a rendered summary for this product.
     Build a template search path with `postfix` distinction.
     """
     if not self.label:
         msg = "The Product Serializer must be configured using a `label` field."
         raise exceptions.ImproperlyConfigured(msg)
     app_label = product._meta.app_label.lower()
     request = self.context['request']
     cache_key = 'product:{0}|{1}-{2}-{3}-{4}-{5}'.format(
         product.id, app_label, self.label, product.product_model, postfix,
         get_language_from_request(request))
     content = cache.get(cache_key)
     if content:
         return mark_safe(content)
     params = [
         (app_label, self.label, product.product_model, postfix),
         (app_label, self.label, 'product', postfix),
         ('shop', self.label, 'product', postfix),
     ]
     try:
         template = select_template(
             ['{0}/products/{1}-{2}-{3}.html'.format(*p) for p in params])
     except TemplateDoesNotExist:
         return SafeText(
             "<!-- no such template: '{0}/products/{1}-{2}-{3}.html' -->".
             format(*params[0]))
     # when rendering emails, we require an absolute URI, so that media can be accessed from
     # the mail client
     absolute_base_uri = request.build_absolute_uri('/').rstrip('/')
     context = RequestContext(request, {
         'product': product,
         'ABSOLUTE_BASE_URI': absolute_base_uri
     })
     content = strip_spaces_between_tags(template.render(context).strip())
     cache.set(cache_key, content,
               app_settings.CACHE_DURATIONS['product_html_snippet'])
     return mark_safe(content)
示例#2
0
 def cleandb(self):
     request = self.new_request()
     response, content = request.delete(self._cleandb_uri)
     if response.status != 200:
         warnings.warn('The CLEANDB_URI you specified is invalid: %s' %
                       self._cleandb_uri)
         # then try to do it with gremlin
         script = """
         try
         {
             indexManager = g.getRawGraph().index()
             indexManager.nodeIndexNames().each{g.dropIndex(it)}
             indexManager.relationshipIndexNames().each{g.dropIndex(it)}
             g.V.filter{it.id!=0}.sideEffect{g.removeVertex(it)}.iterate();
             results = true
         }
         catch(Exception e){results = false}
         """
         gremlin_ret = self.gremlin(script, raw=True)
         if gremlin_ret != 'true':
             error_msg = "\nDatabase couldn't be cleared - have you installed the cleandb extension at https://github.com/jexp/neo4j-clean-remote-db-addon?"
             raise exceptions.ImproperlyConfigured(error_msg)
示例#3
0
    def __add_raw_data(self, raw_data, regexp_for_split=r'[\s;:,]+'):
        """
        Add raw, unparsed data to be created in bulk.
        This data can be a combination of usernames and emails.

        If a username is provided, it is assumed that <username>@<DEVILRY_DEFAULT_EMAIL_SUFFIX> is a valid email.
        If an email is provided the username will be extracted based on the same logic.

        In order to split the raw data into usernames/emails, you may provide a regex for splitting.
        The default regex will split on `whitespaces`, `;`, `:` or `,`.

        result will be stored as a `dict` in `self.__parsed_userdata` for validation and creation of users.
        This data can optionally be retrieved using :func:`self.get_userdata`

        :param raw_data: the raw text to be split into usernames and/or emails
        :param regexp_for_split: valid regex to split usernames/emails.
        """
        reg = re.compile(regexp_for_split)
        values = reg.split(raw_data)
        email_domain = settings.DEVILRY_DEFAULT_EMAIL_SUFFIX
        if not email_domain:
            raise exceptions.ImproperlyConfigured(
                "Missing setting: DEVILRY_DEFAULT_EMAIL_SUFFIX")

        for value in values:
            value = value.strip()
            if value == '':
                continue
            split_email = value.split('@')

            if len(split_email) == 2:
                username = split_email[0]
                email = value
            else:
                username = value
                email = '{}{}'.format(value, email_domain)

            self.__parsed_userdata[username] = email
示例#4
0
def load_backends():
    backends = []
    configured_backends = getattr(
        settings, 'NOTIFICATION_BACKENDS', default_backends)

    for label, backend_path in configured_backends:
        dot = backend_path.rindex('.')
        backend_mod, backend_class = backend_path[:dot], backend_path[dot + 1:]

        try:
            # import the module and get the module from sys.modules
            __import__(backend_mod)
            mod = sys.modules[backend_mod]
        except ImportError as e:
            msg = 'Error importing notification backend {}: \'{}\''
            raise exceptions.ImproperlyConfigured(msg.format(backend_mod, e))

        # add the backend label and an instantiated backend class to the
        # backends list.
        backend_instance = getattr(mod, backend_class)(label)
        backends.append(((label, label.title()), backend_instance))

    return dict(backends)
示例#5
0
 def _cursor(self):
     if not self._valid_connection():
         kwargs = {'conv': base.django_conversions, 'dsn': None}
         settings_dict = self.settings_dict
         settings_dict.update(settings_dict.get('OPTIONS', {}))
         for settings_key, kwarg, required in _SETTINGS_CONNECT_ARGS:
             value = settings_dict.get(settings_key)
             if value:
                 kwargs[kwarg] = value
             elif required:
                 raise exceptions.ImproperlyConfigured(
                     "You must specify a '%s' for database '%s'" %
                     (settings_key, self.alias))
         self.connection = Connect(**kwargs)
         encoders = {
             safestring.SafeUnicode: self.connection.encoders[unicode],
             safestring.SafeString: self.connection.encoders[str]
         }
         self.connection.encoders.update(encoders)
         signals.connection_created.send(sender=self.__class__,
                                         connection=self)
     cursor = base.CursorWrapper(self.connection.cursor())
     return cursor
示例#6
0
    def stream(self):
        """
        An iterator of data from the upload

        This default implementation does not transform the data at all.
        Complex data sources, like spreadsheets with data in cells that aren't arranged
        in tables, will need to override it.
        """

        stream = tabulator.Stream(io.BytesIO(self.upload.raw),
                                  format=self.upload.file_type,
                                  encoding='utf-8',
                                  **UPLOAD_SETTINGS['STREAM_ARGS'])
        stream.open()
        if UPLOAD_SETTINGS['OLD_HEADER_ROW'] is not None:
            if not UPLOAD_SETTINGS['HEADERS']:
                raise exceptions.ImproperlyConfigured(
                    "use DATA_INGEST['OLD_HEADER_ROW'] only with DATA_INGEST['HEADERS']"
                )
            for row in range(UPLOAD_SETTINGS['OLD_HEADER_ROW']):
                next(stream)  # discard rows before header

        return stream
示例#7
0
    def ensure_defaults(self, alias):
        """
        Puts the defaults into the settings dictionary for a given connection
        where no settings is provided.
        """
        try:
            conn = self.databases[alias]
        except KeyError:
            raise ConnectionDoesNotExist("The connection %s doesn't exist" %
                                         alias)

        conn.setdefault('CLIENT',
                        'neo4django.neo4jclient.EnhancedGraphDatabase')
        if conn['CLIENT'] == 'django.db.backends.' or not conn['CLIENT']:
            conn['CLIENT'] = 'neo4django.neo4jclient.EnhancedGraphDatabase'
        conn.setdefault('OPTIONS', {})
        if 'HOST' not in conn or 'PORT' not in conn:
            raise exceptions.ImproperlyConfigured(
                'Each Neo4j database configured '
                'needs a configured host and '
                'port.')
        for setting in ['HOST', 'PORT']:
            conn.setdefault(setting, '')
示例#8
0
  def __call__(self, request):
    view_func = resolve(request.path)[0]
    if view_func in DJANGO_VIEW_AUTH_WHITELIST:
      return

    # AuthenticationMiddleware is required so that request.user exists.
    if not hasattr(request, 'user'):
      raise exceptions.ImproperlyConfigured(
        "The Django remote user auth middleware requires the"
        " authentication middleware to be installed.  Edit your"
        " MIDDLEWARE_CLASSES setting to insert"
        " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
        " before the SpnegoUserMiddleware class.")

    if request.GET.get('user.name'):
      try:
        username = request.GET.get('user.name')
        user = authenticate(username=username, password='')
        if user:
          request.user = user
          login(request, user)
          msg = 'Successful login for user: %s' % request.user.username
        else:
          msg = 'Failed login for user: %s' % request.user.username
        request.audit = {
          'operation': 'USER_LOGIN',
          'username': request.user.username,
          'operationText': msg
        }
        return
      except:
        LOG.exception('Unexpected error when authenticating')
        return

    response = self.get_response(request)

    return response
示例#9
0
    def _http_collector(self):
        s = requests.Session()
        url = self.url
        username = self.username
        password = self.password

        if not url:
            msg = 'HTTP URL is required to be set as COOKIECUTTER_CONTEXT_URL with COOKIECUTTER_CONTEXT_REMOTE = "http".'
            raise django_exc.ImproperlyConfigured(msg)

        if username and password:
            r = s.get(url, auth=(username, password))
        else:
            r = s.get(url)

        if r.status_code >= 300:
            msg = "Could not get remote file from HTTP URL %s:\nSTATUS CODE: %s\nRESPONSE:\n%s" % (
                url, str(r.status_code), r.text)
            LOG.error(msg)
            ctx = ""
        else:
            ctx = r.text

        return ctx
示例#10
0
    def handle(self, *args, **options):
        if jmbo.USE_GIS:
            call_command("migrate", "atlas")
            try:
                MigrationHistory.objects.get(app_name="jmbo", migration="0004_auto__add_field_modelbase_location")
                try:
                    # check that the migration added the location field
                    connection.cursor().execute("SELECT location_id FROM jmbo_modelbase;")
                except DatabaseError:
                    print("Running Jmbo GIS migration...")
                    from atlas.models import Location
                    from django.db import models
                    from south.db import db
                    db.add_column('jmbo_modelbase', 'location',
                        models.fields.related.ForeignKey(Location, null=True, blank=True),
                        keep_default=False)

            except MigrationHistory.DoesNotExist:
                #print("Jmbo GIS migration not run. Running it now...")
                call_command("migrate", "jmbo")

            print "Jmbo has been upgraded successfully."
        else:
            raise exceptions.ImproperlyConfigured("atlas and django.contrib.gis need to be added to your INSTALLED_APPS setting and your database engine must be set to one of the django.contrib.gis.db.backends.")
示例#11
0
    try:
        fc_module, fc_classname = app_settings.SIGNUP_FORM_CLASS.rsplit('.', 1)
    except ValueError:
        raise exceptions.ImproperlyConfigured('%s does not point to a form'
                                              ' class' %
                                              app_settings.SIGNUP_FORM_CLASS)
    try:
        mod = import_module(fc_module)
    except ImportError, e:
        raise exceptions.ImproperlyConfigured('Error importing form class %s:'
                                              ' "%s"' % (fc_module, e))
    try:
        fc_class = getattr(mod, fc_classname)
    except AttributeError:
        raise exceptions.ImproperlyConfigured('Module "%s" does not define a'
                                              ' "%s" class' %
                                              (fc_module, fc_classname))
    if not hasattr(fc_class, 'save'):
        raise exceptions.ImproperlyConfigured('The custom signup form must'
                                              ' implement a "save" method')
    return fc_class


class BaseSignupForm(_base_signup_form_class()):
    username = forms.CharField(label=_("Username"),
                               max_length=30,
                               widget=forms.TextInput())
    email = forms.EmailField(widget=forms.TextInput())

    def __init__(self, *args, **kwargs):
        super(BaseSignupForm, self).__init__(*args, **kwargs)
示例#12
0
def _transaction(extra_params):
    """
    Perform a transaction with PayPal.

    :extra_params: Additional parameters to include in the payload other than
    the user credentials.
    """
    if 'TRXTYPE' not in extra_params:
        raise RuntimeError("All transactions must specify a 'TRXTYPE' paramter")

    # Validate constraints on parameters
    constraints = {
        codes.AUTHORIZATION: ('ACCT', 'AMT', 'EXPDATE'),
        codes.SALE: ('AMT',),
        codes.DELAYED_CAPTURE: ('ORIGID',),
        codes.CREDIT: ('ORIGID',),
        codes.VOID: ('ORIGID',),
    }
    trxtype = extra_params['TRXTYPE']
    for key in constraints[trxtype]:
        if key not in extra_params:
            raise RuntimeError(
                "A %s parameter must be supplied for a %s transaction" % (
                    key, trxtype))

    # At a minimum, we require a vendor ID and a password.
    for setting in ('PAYPAL_PAYFLOW_VENDOR_ID',
                    'PAYPAL_PAYFLOW_PASSWORD'):
        if not hasattr(settings, setting):
            raise exceptions.ImproperlyConfigured(
                "You must define a %s setting" % setting
            )

    # Set credentials
    params = {
        'VENDOR': settings.PAYPAL_PAYFLOW_VENDOR_ID,
        'PWD': settings.PAYPAL_PAYFLOW_PASSWORD,
        'USER': getattr(settings, 'PAYPAL_PAYFLOW_USER',
                        settings.PAYPAL_PAYFLOW_VENDOR_ID),
        'PARTNER': getattr(settings, 'PAYPAL_PAYFLOW_PARTNER',
                           'PayPal')
    }
    params.update(extra_params)

    # Ensure that any amounts have a currency and are formatted correctly
    if 'AMT' in params:
        if 'CURRENCY' not in params:
            params['CURRENCY'] = getattr(settings,
                                         'PAYPAL_PAYFLOW_CURRENCY', 'USD')
        params['AMT'] = "%.2f" % params['AMT']

    if getattr(settings, 'PAYPAL_PAYFLOW_PRODUCTION_MODE', False):
        url = 'https://payflowpro.paypal.com'
    else:
        url = 'https://pilot-payflowpro.paypal.com'

    logger.info("Performing %s transaction (trxtype=%s)",
                codes.trxtype_map[trxtype], trxtype)

    pairs = gateway.post(
        url,
        '&'.join(['{}={}'.format(n,v) for n,v in params.items()]), 
        encode=False
    )

    # Beware - this log information will contain the Payflow credentials
    # only use it in development, not production.
    logger.debug("Raw request: %s", pairs['_raw_request'])
    logger.debug("Raw response: %s", pairs['_raw_response'])

    return models.PayflowTransaction.objects.create(
        comment1=params['COMMENT1'],
        trxtype=params['TRXTYPE'],
        tender=params.get('TENDER', None),
        amount=params.get('AMT', None),
        pnref=pairs.get('PNREF', None),
        ppref=pairs.get('PPREF', None),
        cvv2match=pairs.get('CVV2MATCH', None),
        avsaddr=pairs.get('AVSADDR', None),
        avszip=pairs.get('AVSZIP', None),
        result=pairs.get('RESULT', None),
        respmsg=pairs.get('RESPMSG', None),
        authcode=pairs.get('AUTHCODE', None),
        raw_request=pairs['_raw_request'],
        raw_response=pairs['_raw_response'],
        response_time=pairs['_response_time']
    )
示例#13
0
    def handle(self, *args, **options):
        self.style = no_style()
        if len(args) != 0:
            raise CommandError("Command does not accept any arguments")

        send_email = options.get('sendmail')
        dry_run = options.get('dryrun')

        email_status_info = []
        items_added = False
        try:
            # Go through provider list
            provider_list = getattr(settings, 'ACTIVITYSYNC_PROVIDERS', [])
            for provider_path in provider_list:
                try:
                    dot = provider_path.rindex('.')
                except ValueError:
                    raise exceptions.ImproperlyConfigured(
                        '%s is not an activity provider' % provider_path)
                provider_module, provider_classname = provider_path[:
                                                                    dot], provider_path[
                                                                        dot +
                                                                        1:]
                try:
                    mod = __import__(provider_module, {}, {}, [''])
                except ImportError, e:
                    raise exceptions.ImproperlyConfigured(
                        'Error importing provider %s: %s' %
                        (provider_module, e))
                try:
                    provider_class = getattr(mod, provider_classname)
                except AttributeError:
                    raise exceptions.ImproperlyConfigured(
                        'Provider module "%s" does not define a "%s" class' %
                        (provider_module, provider_classname))

                provider_instance = provider_class()
                email_status_info.append('\n\n%s\n\n' %
                                         provider_instance.name())

                # Create Provider model object if it does not exist
                try:
                    providerModelObject = Provider.objects.get(
                        sourceid=provider_instance.sourceid())
                except Provider.DoesNotExist:
                    print 'First time seeing provider with sourceid: %s' % provider_instance.sourceid(
                    )
                    providerModelObject = Provider.objects.create(
                        name=provider_instance.name(),
                        prefix=provider_instance.prefix(),
                        link=provider_instance.link(),
                        sourceid=provider_instance.sourceid())

                for activity_item in provider_instance.get_activity():
                    try:
                        Activity.objects.get(guid=activity_item.guid)
                    except Activity.DoesNotExist:
                        print "Created item: %s (%s)" % (activity_item.title,
                                                         activity_item.link)
                        email_status_info.append(
                            "Created item: %s (%s)\n" %
                            (activity_item.title, activity_item.link))
                        items_added = True

                        if dry_run:
                            print 'Dry run, not creating item'
                        else:
                            Activity.objects.create(
                                title=activity_item.title,
                                link=activity_item.link,
                                username=activity_item.username,
                                author=activity_item.author,
                                comments=activity_item.comments,
                                pub_date=activity_item.pub_date,
                                published=activity_item.published,
                                guid=activity_item.guid,
                                provider=providerModelObject)

        except:
            raise
            items_added = True
            print "Unexpected error:", sys.exc_info()[0]
            email_status_info.append("Unexpected error: %s\n\n" %
                                     sys.exc_info()[0])
        finally:
            if items_added:
                mailBody = u""
                for itemString in email_status_info:
                    try:
                        mailBody = mailBody.encode(
                            'utf-8') + itemString.encode('utf-8')
                    except UnicodeDecodeError:
                        mailBody = mailBody + "\n\nFAILED TO PARSE ACTIVITY\n\n"
                if send_email:
                    mail_admins('Update Activities command completed',
                                mailBody,
                                fail_silently=False)
                    print 'Mail sent to admins'
示例#14
0
    from django.utils.importlib import import_module

    path_components = path.rsplit('.', 1)
    if not len(path_components) == 2:
        raise exceptions.ImproperlyConfigured('%s isn\'t a classname' % path)
    try:
        mod = import_module(path_components[0])
    except ImportError, e:
        raise exceptions.ImproperlyConfigured(
            'Error importing module %s: "%s"' % (path_components[0], e))

    try:
        cls = getattr(mod, path_components[1])
    except AttributeError:
        raise exceptions.ImproperlyConfigured(
            'module "%s" does not define a "%s" class' %
            (path_components[0], path_components[1]))

    return cls()


def get_default_db():
    if not get_default_db._cache:
        try:
            from django.db import connections
        except ImportError:
            from django_digest.backend.db import FakeMultiDb
            get_default_db._cache = FakeMultiDb()
        else:
            from django_digest.backend.db import MultiDb
            get_default_db._cache = MultiDb(create=True)
示例#15
0
    def __new__(cls, name, bases, attrs):
        meta = attrs.get('Meta')

        if meta:
            if hasattr(meta, 'queryset') and not hasattr(meta, 'object_class'):
                setattr(meta, 'object_class', meta.queryset._document)

            if hasattr(meta, 'object_class') and not hasattr(meta, 'queryset'):
                if hasattr(meta.object_class, 'objects'):
                    setattr(meta, 'queryset', meta.object_class.objects.all())
                elif issubclass(meta.object_class,
                                mongoengine.EmbeddedDocument):
                    # Workaround for https://github.com/toastdriven/django-tastypie/pull/670
                    # We ignore queryset value later on, so we can set it here to empty one
                    setattr(meta, 'queryset', ListQuerySet())

        new_class = super(resources.ModelDeclarativeMetaclass,
                          cls).__new__(cls, name, bases, attrs)
        include_fields = getattr(new_class._meta, 'fields', [])
        excludes = getattr(new_class._meta, 'excludes', [])

        field_names = new_class.base_fields.keys()

        for field_name in field_names:
            if field_name == 'resource_uri':
                if hasattr(new_class, '_parent'):
                    if new_class._parent._meta.object_class and issubclass(
                            new_class._parent._meta.object_class,
                            mongoengine.EmbeddedDocument):
                        # TODO: We do not support yet nested resources
                        # If parent is embedded document, then also this one do not have its own resource_uri
                        del (new_class.base_fields[field_name])
                elif new_class._meta.object_class and issubclass(
                        new_class._meta.object_class,
                        mongoengine.EmbeddedDocument):
                    # Embedded documents which are not in lists (do not have _parent) do not have their own resource_uri
                    del (new_class.base_fields[field_name])
            if field_name in new_class.declared_fields:
                continue
            if len(include_fields) and field_name not in include_fields:
                del (new_class.base_fields[field_name])
            if len(excludes) and field_name in excludes:
                del (new_class.base_fields[field_name])

        # Add in the new fields
        new_class.base_fields.update(
            new_class.get_fields(include_fields, excludes))

        if getattr(new_class._meta, 'include_absolute_url', True):
            if 'absolute_url' not in new_class.base_fields:
                new_class.base_fields[
                    'absolute_url'] = tastypie_fields.CharField(
                        attribute='get_absolute_url', readonly=True)
        elif 'absolute_url' in new_class.base_fields and 'absolute_url' not in attrs:
            del (new_class.base_fields['absolute_url'])

        type_map = getattr(new_class._meta, 'polymorphic', {})

        if type_map and getattr(new_class._meta, 'include_resource_type',
                                True):
            if 'resource_type' not in new_class.base_fields:
                new_class.base_fields[
                    'resource_type'] = tastypie_fields.CharField(readonly=True)
        elif 'resource_type' in new_class.base_fields and 'resource_type' not in attrs:
            del (new_class.base_fields['resource_type'])

        seen_types = set()
        for typ, resource in type_map.iteritems():
            if resource == 'self':
                type_map[typ] = new_class
                break
            # In the code for polymorphic resources we are assuming
            # that document classes are not duplicated among used resources
            # (that each resource is linked to its own document class)
            # So we are checking this assumption here
            if type_map[typ]._meta.object_class in seen_types:
                raise exceptions.ImproperlyConfigured(
                    "Used polymorphic resources should each use its own document class."
                )
            else:
                seen_types.add(type_map[typ]._meta.object_class)

        if new_class._meta.object_class:
            # In MongoEngine 0.7.6+ embedded documents do not have exceptions anymore,
            # but this prevents are from reusing existing Tastypie code

            exceptions_to_merge = [
                exc for exc in (queryset.DoesNotExist,
                                queryset.MultipleObjectsReturned)
                if not hasattr(new_class._meta.object_class, exc.__name__)
            ]
            module = new_class._meta.object_class.__module__
            for exc in exceptions_to_merge:
                name = exc.__name__
                parents = tuple(
                    getattr(base, name)
                    for base in new_class._meta.object_class._get_bases(bases)
                    if hasattr(base, name)) or (exc, )
                # Create new exception and set to new_class
                exception = type(name, parents, {'__module__': module})
                setattr(new_class._meta.object_class, name, exception)

        return new_class
示例#16
0
    def process_request(self, request):
        """
    The process_request() method needs to communicate some state to the
    process_response() method. The two options for this are to return an
    HttpResponse object or to modify the META headers in the request object. In
    order to ensure that all of the middleware is properly invoked, this code
    currently uses the later approach. The following headers are currently used:

    GSS-String:
      This means that GSS authentication was successful and that we need to pass
      this value for the WWW-Authenticate header in the response.

    Return-401:
      This means that the SPNEGO backend is in use, but we didn't get an
      AUTHORIZATION header from the client. The way that the protocol works
      (http://tools.ietf.org/html/rfc4559) is by having the first response to an
      un-authenticated request be a 401 with the WWW-Authenticate header set to
      Negotiate. This will cause the browser to re-try the request with the
      AUTHORIZATION header set.
    """
        view_func = resolve(request.path)[0]
        if view_func in DJANGO_VIEW_AUTH_WHITELIST:
            return

        # AuthenticationMiddleware is required so that request.user exists.
        if not hasattr(request, 'user'):
            raise exceptions.ImproperlyConfigured(
                "The Django remote user auth middleware requires the"
                " authentication middleware to be installed.  Edit your"
                " MIDDLEWARE_CLASSES setting to insert"
                " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
                " before the SpnegoUserMiddleware class.")

        if 'HTTP_AUTHORIZATION' in request.META:
            type, authstr = request.META['HTTP_AUTHORIZATION'].split(' ', 1)

            if type == 'Negotiate':
                try:
                    result, context = kerberos.authGSSServerInit('HTTP')
                    if result != 1:
                        return

                    gssstring = ''
                    r = kerberos.authGSSServerStep(context, authstr)
                    if r == 1:
                        gssstring = kerberos.authGSSServerResponse(context)
                        request.META['GSS-String'] = 'Negotiate %s' % gssstring
                    else:
                        kerberos.authGSSServerClean(context)
                        return

                    username = kerberos.authGSSServerUserName(context)
                    kerberos.authGSSServerClean(context)

                    # In Trusted knox proxy, Hue must expect following:
                    #   Trusted knox user: KNOX_PRINCIPAL
                    #   Trusted knox proxy host: KNOX_PROXYHOSTS
                    if 'desktop.auth.backend.KnoxSpnegoDjangoBackend' in AUTH.BACKEND.get(
                    ):
                        knox_verification = False
                        principals = self.clean_principal(
                            KNOX.KNOX_PRINCIPAL.get())
                        principal = self.clean_principal(username)
                        if principal.intersection(principals):
                            # This may contain chain of reverse proxies, e.g. knox proxy, hue load balancer
                            # Compare hostname on both HTTP_X_FORWARDED_HOST & KNOX_PROXYHOSTS.
                            # Both of these can be configured to use either hostname or IPs and we have to normalize to one or the other
                            req_hosts = self.clean_host(
                                request.META['HTTP_X_FORWARDED_HOST'])
                            knox_proxy = self.clean_host(
                                KNOX.KNOX_PROXYHOSTS.get())
                            if req_hosts.intersection(knox_proxy):
                                knox_verification = True
                            else:
                                access_warn(
                                    request,
                                    'Failed to verify provided host %s with %s '
                                    % (req_hosts, knox_proxy))
                        else:
                            access_warn(
                                request,
                                'Failed to verify provided username %s with %s '
                                % (principal, principals))
                        # If knox authentication failed then generate 401 (Unauthorized error)
                        if not knox_verification:
                            request.META['Return-401'] = ''
                            return

                    if request.user.is_authenticated:
                        if request.user.username == self.clean_username(
                                username, request):
                            return

                    user = authenticate(username=username, request=request)
                    if user:
                        request.user = user
                        login(request, user)
                        msg = 'Successful login for user: %s' % request.user.username
                    else:
                        msg = 'Failed login for user: %s' % request.user.username
                    request.audit = {
                        'operation': 'USER_LOGIN',
                        'username': request.user.username,
                        'operationText': msg
                    }
                    access_warn(request, msg)
                    return
                except:
                    LOG.exception(
                        'Unexpected error when authenticating against KDC')
                    return
            else:
                request.META['Return-401'] = ''
                return
        else:
            if not request.user.is_authenticated:
                request.META['Return-401'] = ''
            return
示例#17
0
from django.core import exceptions
from django.conf import settings
from django.template import Context, loader

import tagging
from tagging.fields import TagField
from tagging.models import Tag

import re

import xmlrpclib

try:
    idavoll = xmlrpclib.ServerProxy(settings.IDAVOLL_XMLRPC_SERVICE)
except KeyError:
    raise exceptions.ImproperlyConfigured(
        'microblog-demo needs IDAVOLL_XMLRPC_SERVICE in settings.py')


class Profile(models.Model):
    followers = models.ManyToManyField('self',
                                       related_name='following',
                                       symmetrical=False)
    user = models.OneToOneField(auth_models.User,
                                related_name='microblog_profile')
    jid = models.CharField(max_length="255", blank=True)

    def feed(self):
        followed = Q(owner__followers=self)
        targeted = Q(targets=self)
        return Entry.objects.filter(followed | targeted)
示例#18
0
    'release': RELEASE_VERSION,
}

SITE_INFO = {
    'RELEASE_VERSION': RELEASE_VERSION,
    'IS_RAVEN_INSTALLED': RAVEN_CONFIG['dsn'] != ''
}

# SOCIAL MEDIA CONFIGURATIONS
# --------------------------------------------------------------------

LIMIT_POSTS = False
try:
    MAX_POSTS_AT_ONCE = int(env('MAX_POSTS_AT_ONCE', default=5))
except ValueError:
    raise exceptions.ImproperlyConfigured('MAX_POSTS_AT_ONCE should be an int')
FB_USER_ACCESS_TOKEN = env('FB_USER_ACCESS_TOKEN',
                           default='NO USER ACCESS TOKEN')
FB_PAGE_ID = env('FB_PAGE_ID', default='NO PAGE ID')

LINKEDIN_AUTH = {
    'access_token':
    env('LINKEDIN_ACCESS_TOKEN', default='FakeAccessToken'),
    'organization_id':
    env('LINKEDIN_ORGANIZATION_ID', default='FakeOrganizationId'),
}
LINKEDIN_API_URL_BASE = 'https://api.linkedin.com/v2/'

TWITTER_OAUTH = {
    'consumer_key':
    env('TWITTER_CONSUMER_KEY', default='FakeTwitterConsumerKey'),
示例#19
0
 def bind(self, field_name, parent):
     super().bind(field_name, parent)
     raise exceptions.ImproperlyConfigured(
         "The field '{field_name}' on {parent} must be explicitly declared when used with a ModelSerializer"
         .format(field_name=field_name, parent=parent.__class__.__name__))
示例#20
0
  def process_request(self, request):
    """
    The process_request() method needs to communicate some state to the
    process_response() method. The two options for this are to return an
    HttpResponse object or to modify the META headers in the request object. In
    order to ensure that all of the middleware is properly invoked, this code
    currently uses the later approach. The following headers are currently used:

    GSS-String:
      This means that GSS authentication was successful and that we need to pass
      this value for the WWW-Authenticate header in the response.

    Return-401:
      This means that the SPNEGO backend is in use, but we didn't get an
      AUTHORIZATION header from the client. The way that the protocol works
      (http://tools.ietf.org/html/rfc4559) is by having the first response to an
      un-authenticated request be a 401 with the WWW-Authenticate header set to
      Negotiate. This will cause the browser to re-try the request with the
      AUTHORIZATION header set.
    """
    view_func = resolve(request.path)[0]
    if view_func in DJANGO_VIEW_AUTH_WHITELIST:
      return

    # AuthenticationMiddleware is required so that request.user exists.
    if not hasattr(request, 'user'):
      raise exceptions.ImproperlyConfigured(
        "The Django remote user auth middleware requires the"
        " authentication middleware to be installed.  Edit your"
        " MIDDLEWARE_CLASSES setting to insert"
        " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
        " before the SpnegoUserMiddleware class.")

    if 'HTTP_AUTHORIZATION' in request.META:
      type, authstr = request.META['HTTP_AUTHORIZATION'].split(' ', 1)

      if type == 'Negotiate':
        try:
          result, context = kerberos.authGSSServerInit('HTTP')
          if result != 1:
            return

          gssstring=''
          r=kerberos.authGSSServerStep(context,authstr)
          if r == 1:
            gssstring=kerberos.authGSSServerResponse(context)
            request.META['GSS-String'] = 'Negotiate %s' % gssstring
          else:
            kerberos.authGSSServerClean(context)
            return

          username = kerberos.authGSSServerUserName(context)
          kerberos.authGSSServerClean(context)

          if request.user.is_authenticated():
            if request.user.username == self.clean_username(username, request):
              return

          user = authenticate(username=username)
          if user:
            request.user = user
            login(request, user)
            msg = 'Successful login for user: %s' % request.user.username
          else:
            msg = 'Failed login for user: %s' % request.user.username
          request.audit = {
            'operation': 'USER_LOGIN',
            'username': request.user.username,
            'operationText': msg
          }
          access_warn(request, msg)
          return
        except:
          LOG.exception('Unexpected error when authenticating against KDC')
          return
      else:
        request.META['Return-401'] = ''
        return
    else:
      if not request.user.is_authenticated():
        request.META['Return-401'] = ''
      return
示例#21
0
class RowwiseValidator(Validator):
    '''Subclass this for any validator applied to one row at a time.

    Rule file should be JSON or YAML with fields

    Then each subclass only needs an `evaluate(self, rule, row)`
    method returning Boolean
    '''

    SUPPORTS_HEADER_OVERRIDE = True

    if 'headers' not in UPLOAD_SETTINGS['STREAM_ARGS']:
        raise exceptions.ImproperlyConfigured(
            "setting DATA_INGEST['STREAM_ARGS']['headers'] is required")

    if UPLOAD_SETTINGS.get('OLD_HEADER_ROW') and not isinstance(
            UPLOAD_SETTINGS['STREAM_ARGS']['headers'], list):
        raise exceptions.ImproperlyConfigured(
            """DATA_INGEST['OLD_HEADER_ROW'] should be used with a
            list of headers in DATA_INGEST['STREAM_ARGS']['header']""")

    @staticmethod
    def cast_value(value):
        newval = value
        if type(newval) == str:
            newval = newval.strip()
            try:
                dnewval = Decimal(newval.replace(',', ''))
                try:
                    inewval = int(dnewval)
                    fnewval = float(dnewval)
                    if inewval == fnewval:
                        newval = inewval
                    else:
                        newval = fnewval
                except ValueError:
                    # will take the newval.strip() as the value
                    pass
            except InvalidOperation:
                # will take the newval.strip() as the value
                pass

        return newval

    @staticmethod
    def cast_values(row_values):
        # This will help clean up the data and cast them to numbers when
        # appropriate
        return [RowwiseValidator.cast_value(value) for value in row_values]

    def validate(self, source):
        table = {
            'invalid_row_count': 0,
            'valid_row_count': 0,
            'whole_table_errors': [],
        }

        rows = []
        (table['headers'], numbered_rows) = rows_from_source(source)
        for (rn, row) in numbered_rows.items():

            # This is to remove the header row
            if rn == UPLOAD_SETTINGS['OLD_HEADER_ROW']:
                continue

            errors = []
            # Check for columns required by validator
            received_columns = set(table['headers'])
            for rule in self.validator:
                expected_columns = set(rule['columns'])
                missing_columns = expected_columns.difference(received_columns)
                if missing_columns:
                    errors.append({
                        'severity': 'Error',
                        'code': rule['error_code'],
                        'message':
                        f'Unable to evaluate, missing columns: {missing_columns}',
                        'error_columns': []
                    })
                    continue
                try:
                    if rule['code'] and not self.invert_if_needed(
                            self.evaluate(rule['code'], row)):
                        errors.append(row_validation_error(rule, row))
                except Exception as e:
                    errors.append({
                        'severity': 'Error',
                        'code': rule['error_code'],
                        'message': f'{type(e).__name__}: {e.args[0]}',
                        'error_columns': []
                    })

            # errors = [
            #     row_validation_error(rule, row) for rule in self.validator
            #     if not self.invert_if_needed(self.evaluate(rule['code'], row))
            # ]
            if errors:
                table['invalid_row_count'] += 1
            else:
                table['valid_row_count'] += 1
            rows.append({
                'row_number': rn,
                'data': row,
                'errors': errors,
            })
        table['rows'] = rows

        result = {
            'tables': [
                table,
            ],
            'valid': (table['invalid_row_count'] == 0)
        }

        return result
示例#22
0
import configobj
import validate

from django.core import exceptions, urlresolvers

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Find the configuration file.
CONFIG_PATH = os.path.join(
    os.path.expanduser(os.getenv("XDG_CONFIG_PATH", "~/.config")), "swoptact",
    "config.ini")
CONFIG_PATH = os.getenv("SWOPTACT_CONFIG", CONFIG_PATH)

if not os.path.exists(CONFIG_PATH):
    raise exceptions.ImproperlyConfigured("No config file provided")

# For security purposes other users shouldn't be able to edit the file
mode = os.stat(CONFIG_PATH).st_mode
if mode & stat.S_IRWXO != 0:
    warnings.warn(
        "Check permissions on settings file, it contains sensitive data.")

# Open config file
CONFIG_SPEC = os.path.join(BASE_DIR, "swoptact", "config_spec.ini")

config = configobj.ConfigObj(CONFIG_PATH, configspec=CONFIG_SPEC)
config.validate(validate.Validator())

##
# Config settings, please alter these in the settings file not this file
示例#23
0
        else:
            txt = 'Error importing backend %s: "%s".' % (class_module, e)

        raise exceptions.ImproperlyConfigured(txt)

    try:
        clazz = getattr(mod, class_name)
    except AttributeError:
        if setting_name:
            txt = ('Backend module "%s" does not define a "%s" class. Check'
                   ' your %s setting' % (class_module, class_name,
                                         setting_name))
        else:
            txt = 'Backend module "%s" does not define a "%s" class.' % (
                class_module, class_name)
        raise exceptions.ImproperlyConfigured(txt)
    return clazz


def ajaxify_template_var(template_var):
    if isinstance(template_var, (list, tuple)):
        template_var = type(template_var)(ajaxify_template_name(name) for name in template_var)
    elif isinstance(template_var, basestring):
        template_var = ajaxify_template_name(template_var)
    return template_var


def ajaxify_template_name(name):
    if "." in name:
        name = "%s-ajax.%s" % tuple(name.rsplit('.', 1))
    else:
    This is needed to allow our crazy custom model usage.
    """
    setting_name = setting_format % model_name.upper().replace('_', '')
    class_path = getattr(settings, setting_name, None)

    if not class_path:
        return fallback_format % model_name
    elif isinstance(class_path, basestring):
        parts = class_path.split('.')
        try:
            index = parts.index('models') - 1
        except ValueError, e:
            raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR %
                                                  (setting_name, setting_name))
        app_label, model_name = parts[index], parts[-1]
    else:
        try:
            class_path, app_label = class_path
            model_name = class_path.split('.')[-1]
        except:
            raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR %
                                                  (setting_name, setting_name))

    return "%s.%s" % (app_label, model_name)


def get_category_model_string(model_name):
    return get_model_string(model_name,
                            setting_format='SHOP_CATEGORIES_%s_MODEL',
                            fallback_format='shop_categories.%s')
示例#25
0
 def get_success_url(self):
     if self.success_url:
         return self.success_url.format(**self.object.__dict__)
     else:
         raise exceptions.ImproperlyConfigured(
             "No URL to redirect to. Provide a success_url.")
                    win32con.LOGON32_LOGON_INTERACTIVE,
                    win32con.LOGON32_PROVIDER_DEFAULT)
                win32security.ImpersonateLoggedOnUser(handler)
                handler.Close()

        def terminate_impersonation(self, username):
            """undo user from impersonation
            """
            if self.file_access_user:
                win32security.RevertToSelf()

    personate_user_class = WindowsPersonateUser

if personate_user_class is None:
    raise exceptions.ImproperlyConfigured(
        "It can't setup the personate user class. "
        "If your platform is Windows, please install pywin32.")


class FTPAccountAuthorizer(personate_user_class):
    """Authorizer class by django authentication.
    """
    model = models.FTPUserAccount

    def __init__(self, file_access_user=None):
        super(FTPAccountAuthorizer, self).__init__(file_access_user)
        self.username_field = get_username_field()

    def _filter_user_by(self, username):
        return {"user__%s" % self.username_field: username}
示例#27
0
 def get_path(self):
   if 'path' in self.kwargs:
     return self.kwargs['path']
   else:
     raise exceptions.ImproperlyConfigured('Path is not set')
示例#28
0
# coding: utf-8
from __future__ import unicode_literals
from django.core import exceptions
from django.utils.importlib import import_module


def get_function_by_path(path):
    try:
        module_name, function_name = path.rsplit('.', 1)
    except ValueError:
        raise exceptions.ImproperlyConfigured(
            '%s isn\'t a function module_name' % path)
    try:
        module = import_module(module_name)
    except ImportError, e:
        raise exceptions.ImproperlyConfigured(
            'Error importing module_name %s: "%s"' % (module_name, e))
    try:
        function = getattr(module, function_name)
    except AttributeError:
        raise exceptions.ImproperlyConfigured(
            'Module "%s" does not define a "%s" function' %
            (module_name, function_name))
    return function
示例#29
0
    """
    try:
        mw_module, mw_classname = path.rsplit('.', 1)
    except ValueError:
        raise exceptions.ImproperlyConfigured('%s isn\'t a manager module' %
                                              path)
    try:
        mod = import_module(mw_module)
    except ImportError, e:
        raise exceptions.ImproperlyConfigured(
            'Error importing manager %s: "%s"' % (mw_module, e))
    try:
        manager_instance = getattr(mod, mw_classname)
    except AttributeError:
        raise exceptions.ImproperlyConfigured(
            'Manager module "%s" does not define a "%s" object' %
            (mw_module, mw_classname))
    return manager_instance


def initialize():
    from django.conf import settings

    global MIGRATION_MANAGERS, DEFAULT_MANAGER

    MANAGERS = getattr(settings, "MIGRATION_MANAGERS", None)

    if MANAGERS is not None:
        for k, v in MANAGERS:
            MIGRATION_MANAGERS[k] = load_manager(v)
示例#30
0
 def ready(self):
     for backend in settings.AUTHENTICATION_BACKENDS:
         provider = self.backend_providers.get(backend)
         if provider and not self.has_keys_needed(settings, provider):
             message = 'The OAuth keys for %s are missing' % backend
             raise exceptions.ImproperlyConfigured(message)