Ejemplo n.º 1
0
    def __getattr__(self, name):
        # a model attribute was not found, so it could be an attempt to access
        # a dynamic type field. on the first time such a field is accessed,
        # we will store the values for all of the instance's dynamic fields
        # as attributes on the model instance (so that future accesses will
        # simply return the value without processing); the assumption is that
        # if one dynamic field is accessed, it is likely that another dynamic
        # field will be accessed in the lifetime of the model instance.
        if 'dynamic_type' not in name and self.dynamic_type and not name.startswith('_'):
            dfields = cache.get('dfields_%d' % self.dynamic_type_id)
            if not dfields:
                dfields = DynamicTypeField.objects.filter(dynamic_type=self.dynamic_type)
                cache.set('dfields_%d' % self.dynamic_type_id, dfields)

            if name in [field.name for field in dfields]:

                # if this is the first time an Attribute has been created,
                # then create a dummy one to force the introspection to happen
                if name not in Attribute._meta.get_all_field_names():
                    Attribute()

                attrs = cache.get('attribute_%d_%d' % (ContentType.objects.get_for_model(self).pk, self.pk))
                if not attrs:
                    attrs = Attribute.objects.get(content_type=ContentType.objects.get_for_model(self),
                                                  object_id=self.pk)
                    cache.set('attribute_%d_%d' % (ContentType.objects.get_for_model(self).pk, self.pk), attrs)

                for field in dfields:
                    setattr(self, field.name, getattr(attrs, field.column))

                # now that all the dynamic fields have been stored on the instance,
                # we just call getattr again to get the value
                return getattr(self, name)

        # we're ignoring "private" variables that Django checks for (start with
        # underscore), as well as anything else if no dynamic type is set for
        # this model instance
        raise AttributeError
Ejemplo n.º 2
0
    def process_request(self, request):
        HEADER_VARS = {}
        extensions_done = False

        rules = cache.get('enabled_header_rules')
        if not rules:
            rules = HeaderRule.objects.all().filter(Q(enabled=True) | Q(admin_override=True)).select_related()
            cache.set('enabled_header_rules', rules)

        for rule in rules:

            """
            This if statement only looks complex because it's multiline
            Basically, if the user is an admin and the admin override is enabled then it works
            Otherwise, it checks if the rule is enabled, the domain matches, and the header regex matches
            """

            if (rule.admin_override and request.user.is_superuser) \
            or ( \
            rule.enabled and \
            re.match(rule.domain, request.META.get('HTTP_HOST')) and \
            re.search(rule.regex, request.META.get("HTTP_" + rule.header.header.replace('-', '_').upper())) \
            ):

                #Only set extensions for first rule that matches
                if not extensions_done:
                    #Add extensions to request.extension, stripping any leading dots
                    request.extension = [ext.lstrip('.') for ext in rule.extension_list.split(' ')]
                    extensions_done = True

                #Set context variable if it exists
                if rule.context_var:
                    HEADER_VARS[rule.context_var] = True

        #Attach any context vars to the request object
        request.HEADER_VARS = HEADER_VARS