django.conf.settings.DEBUG

Here are the examples of the python api django.conf.settings.DEBUG taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

152 Examples 7

Example 101

Project: socorro Source File: views.py
Function: model_wrapper
@csrf_exempt
@waffle_switch('!app_api_all_disabled')
@ratelimit(
    key='ip',
    method=['GET', 'POST', 'PUT'],
    rate=utils.ratelimit_rate,
    block=True
)
@track_api_pageview
@utils.add_CORS_header  # must be before `utils.json_view`
@utils.json_view
def model_wrapper(request, model_name):
    if model_name in BLACKLIST:
        raise http.Http404("Don't know what you're talking about!")

    for source in MODELS_MODULES:
        try:
            model = getattr(source, model_name)
            break
        except AttributeError:
            pass
    else:
        raise http.Http404('no service called `%s`' % model_name)

    if not is_valid_model_class(model):
        raise http.Http404('no service called `%s`' % model_name)

    required_permissions = getattr(model(), 'API_REQUIRED_PERMISSIONS', None)
    if isinstance(required_permissions, basestring):
        required_permissions = [required_permissions]
    if (
        required_permissions and
        (
            not request.user.is_active or
            not has_permissions(request.user, required_permissions)
        )
    ):
        permission_names = []
        for permission in required_permissions:
            codename = permission.split('.', 1)[1]
            try:
                permission_names.append(
                    Permission.objects.get(
                        codename=codename
                    ).name
                )
            except Permission.DoesNotExist:
                permission_names.append(codename)
        # you're not allowed to use this model
        return http.JsonResponse({
            'error': "Use of this endpoint requires the '%s' permission" % (
                ', '.join(permission_names),
            )
        }, status=403)

    # it being set to None means it's been deliberately disabled
    if getattr(model, 'API_WHITELIST', False) is False:
        raise APIWhitelistError('No API_WHITELIST defined for %r' % model)

    instance = model()

    # Any additional headers we intend to set on the response
    headers = {}

    # Certain models need to know who the user is to be able to
    # internally use that to determine its output.
    instance.api_user = request.user

    if request.method == 'POST':
        function = instance.post
    else:
        function = instance.get
    if not function:
        return http.HttpResponseNotAllowed([request.method])

    # assume first that it won't need a binary response
    binary_response = False

    request_data = request.method == 'GET' and request.GET or request.POST
    form = FormWrapper(model, request_data)
    if form.is_valid():
        try:
            result = function(**form.cleaned_data)
        except models.BadStatusCodeError as e:
            error_code = e.status
            message = e.message
            if error_code >= 400 and error_code < 500:
                # if the error message looks like JSON,
                # carry that forward in the response
                try:
                    json.loads(message)
                    return http.HttpResponse(
                        message,
                        status=error_code,
                        content_type='application/json; charset=UTF-8'
                    )
                except ValueError:
                    # The error from the middleware was not a JSON error.
                    # Not much more we can do.
                    reason = REASON_PHRASES.get(
                        error_code,
                        'UNKNOWN STATUS CODE'
                    )
                    return http.HttpResponse(
                        reason,
                        status=error_code,
                        content_type='text/plain; charset=UTF-8'
                    )
            if error_code >= 500:
                # special case
                reason = REASON_PHRASES[424]
                return http.HttpResponse(
                    reason,
                    status=424,
                    content_type='text/plain'
                )
            raise
        except ValueError as e:
            if (
                # built in json module ValueError
                'No JSON object could be decoded' in e or
                # ujson module ValueError
                'Expected object or value' in e
            ):
                return http.HttpResponseBadRequest(
                    json.dumps({'error': 'Not a valid JSON response'}),
                    content_type='application/json; charset=UTF-8'
                )
            raise
        except NOT_FOUND_EXCEPTIONS as exception:
            return http.HttpResponseNotFound(
                json.dumps({'error': str(exception)}),
                content_type='application/json; charset=UTF-8'
            )
        except BAD_REQUEST_EXCEPTIONS as exception:
            return http.HttpResponseBadRequest(
                json.dumps({'error': str(exception)}),
                content_type='application/json; charset=UTF-8'
            )

        # Some models allows to return a binary reponse. It does so based on
        # the models `BINARY_RESPONSE` dict in which all keys and values
        # need to be in the valid query. For example, if the query is
        # `?foo=bar&other=thing&bar=baz` and the `BINARY_RESPONSE` dict is
        # exactly: {'foo': 'bar', 'bar': 'baz'} it will return a binary
        # response with content type `application/octet-stream`.
        for key, value in model.API_BINARY_RESPONSE.items():
            if form.cleaned_data.get(key) == value:
                binary_response = True
            else:
                binary_response = False
                break

        if binary_response:
            # if you don't have all required permissions, you'll get a 403
            required_permissions = model.API_BINARY_PERMISSIONS
            if isinstance(required_permissions, basestring):
                required_permissions = [required_permissions]
            if (
                required_permissions and
                not has_permissions(request.user, required_permissions)
            ):
                permission_names = []
                for permission in required_permissions:
                    codename = permission.split('.', 1)[1]
                    try:
                        permission_names.append(
                            Permission.objects.get(
                                codename=codename
                            ).name
                        )
                    except Permission.DoesNotExist:
                        permission_names.append(codename)
                # you're not allowed to get the binary response
                return http.HttpResponseForbidden(
                    "Binary response requires the '%s' permission\n" %
                    (', '.join(permission_names))
                )

        elif not request.user.has_perm('crashstats.view_pii'):
            clean_scrub = getattr(model, 'API_CLEAN_SCRUB', None)

            if isinstance(model.API_WHITELIST, models.Lazy):
                # This is necessary because in Cleaner() we're going to
                # rely on asking `isinstance(whitelist, dict)` and there's
                # no easy or convenient way to be lazy about that.
                model.API_WHITELIST = model.API_WHITELIST.materialize()

            if result and model.API_WHITELIST:
                cleaner = Cleaner(
                    model.API_WHITELIST,
                    clean_scrub=clean_scrub,
                    # if True, uses warnings.warn() to show fields
                    # not whitelisted
                    debug=settings.DEBUG,
                )
                cleaner.start(result)

    else:
        # custom override of the status code
        return {'errors': dict(form.errors)}, 400

    if binary_response:
        assert model.API_BINARY_FILENAME, 'No API_BINARY_FILENAME set on model'
        response = http.HttpResponse(
            result,
            content_type='application/octet-stream'
        )
        filename = model.API_BINARY_FILENAME % form.cleaned_data
        response['Content-Disposition'] = (
            'attachment; filename="%s"' % filename
        )
        return response

    if (
        getattr(model, 'deprecation_warning', False)
    ):
        if isinstance(result, dict):
            result['DEPRECATION_WARNING'] = model.deprecation_warning
        # If you return a tuple of two dicts, the second one becomes
        # the extra headers.
        # return result, {
        headers['DEPRECATION-WARNING'] = (
            model.deprecation_warning.replace('\n', ' ')
        )

    if model.cache_seconds:
        # We can set a Cache-Control header.
        # We say 'private' because the content can depend on the user
        # and we don't want the response to be collected in HTTP proxies
        # by mistake.
        headers['Cache-Control'] = 'private, max-age={}'.format(
            model.cache_seconds,
        )

    return result, headers

Example 102

Project: nosedjango Source File: nosedjango.py
    def begin(self):
        """
        Create the test database and schema, if needed, and switch the
        connection over to that database. Then call install() to install
        all apps listed in the loaded settings module.
        """
        for plugin in self.nose_config.plugins.plugins:
            if getattr(plugin, 'django_plugin', False):
                self.django_plugins.append(plugin)

        os.environ['DJANGO_SETTINGS_MODULE'] = self.settings_module

        if self.conf.addPaths:
            map(add_path, self.conf.where)

        try:
            __import__(self.settings_module)
            self.settings_path = self.settings_module
        except ImportError:
            # Settings module is not found in PYTHONPATH. Try to do
            # some funky backwards crawling in directory tree, ie. add
            # the working directory (and any package parents) to
            # sys.path before trying to import django modules;
            # otherwise, they won't be able to find project.settings
            # if the working dir is project/ or project/..

            self.settings_path = get_settings_path(self.settings_module)

            if not self.settings_path:
                # short circuit if no settings file can be found
                raise RuntimeError("Can't find Django settings file!")

            add_path(self.settings_path)
            sys.path.append(self.settings_path)

        from django.conf import settings

        # Some Django code paths evaluate differently
        # between DEBUG and not DEBUG.  Example of this include the url
        # dispatcher when 404's are hit.  Django's own test runner forces DEBUG
        # to be off.
        settings.DEBUG = False

        self.call_plugins_method('beforeConnectionSetup', settings)

        from django.core import management
        from django.test.utils import setup_test_environment

        if hasattr(settings, 'DATABASES'):
            self.old_db = settings.DATABASES['default']['NAME']
        else:
            self.old_db = settings.DATABASE_NAME
        from django.db import connections

        self._monkeypatch_test_classes()

        for connection in connections.all():
            self.call_plugins_method(
                'beforeTestSetup', settings, setup_test_environment,
                connection)
        setup_test_environment()
        import django
        if hasattr(django, 'setup'):
            django.setup()

        self.call_plugins_method('afterTestSetup', settings)

        management.get_commands()
        # Ensure that nothing (eg. South) steals away our syncdb command
        if self.django_version < self.DJANGO_1_7:
            management._commands['syncdb'] = 'django.core'

        for connection in connections.all():
            self.call_plugins_method(
                'beforeTestDb', settings, connection, management)
            connection.creation.create_test_db(
                verbosity=self.verbosity,
                autoclobber=True,
            )
            logger.debug("Running syncdb")
            self._num_syncdb_calls += 1
            self.call_plugins_method('afterTestDb', settings, connection)
        self.store_original_transaction_methods()

Example 103

Project: django-quick-test Source File: testrunner.py
	def run_tests(self, test_labels, extra_tests=None):
		"""
		Run the unit tests for all the test names in the provided list.

		Test names specified may be file or module names, and may optionally
		indicate the test case to run by separating the module or file name
		from the test case name with a colon. Filenames may be relative or
		absolute.  Examples:

		runner.run_tests('test.module')
		runner.run_tests('another.test:TestCase.test_method')
		runner.run_tests('a.test:TestCase')
		runner.run_tests('/path/to/test/file.py:test_function')

		Returns the number of tests that failed.
		"""

		settings.DEBUG = False

		self.setup_test_environment()
		
		
		# Switch to the test database
		old_name = settings.DATABASES['default']['NAME']

		if settings.DATABASES['test']['NAME']:
			settings.DATABASES['default']['NAME'] = settings.DATABASES['test']['NAME']
		else:
			settings.DATABASES['default']['NAME'] = creation.TEST_DATABASE_PREFIX + settings.DATABASES['default']['NAME']
		
		connection.settings_dict["DATABASE_NAME"] = settings.DATABASES['default']['NAME']
	
		try:
			settings.DATABASES['default']['SUPPORTS_TRANSACTIONS'] = connection.creation._rollback_works()	
		except: 
			pass

		nose_argv = ['nosetests', '--verbosity', str(self.verbosity)]

		if hasattr(settings, 'NOSE_ARGS'):
			nose_argv.extend(settings.NOSE_ARGS)

		# Skip over 'manage.py test' and any arguments handled by django.
		django_opts = ['--noinput']
		for opt in BaseCommand.option_list:
			django_opts.extend(opt._long_opts)
			django_opts.extend(opt._short_opts)

		nose_argv.extend(OPTION_TRANSLATION.get(opt, opt)
				for opt in sys.argv[2:]
				if not any(opt.startswith(d) for d in django_opts))

		if self.verbosity >= 1:
			print ' '.join(nose_argv)


		result = self.run_suite(nose_argv)

		#Restore the old db name.
		settings.DATABASES['default']['NAME'] = old_name
		connection.settings_dict["DATABASE_NAME"] = old_name

		self.teardown_test_environment()
		# suite_result expects the suite as the first argument.  Fake it.
		return self.suite_result({}, result)

Example 104

Project: inventory Source File: views.py
def compare_view(request, object_class, extra_context=None):
    """
    compare two versions.
    Used make_compare() to create the html diff.
    """
    form = SelectDiffForm(request.GET)
    if not form.is_valid():
        msg = "Wrong version IDs."
        if settings.DEBUG:
            msg += " (form errors: %s)" % ", ".join(form.errors)
        raise Http404(msg)

    object_id = request.GET.get('object_id', None)
    if not object_id:
        raise Http404("What object are you looking for?")

    version_id1 = form.cleaned_data["version_id1"]
    version_id2 = form.cleaned_data["version_id2"]

    object_klass = get_obj_meta(object_class).Klass
    if not object_klass:
        raise Http404("No {0} object type with id {1}".format(object_class,
                        object_id))
    obj = get_object_or_404(object_klass, pk=object_id)
    queryset = reversion.get_for_object(obj)
    version1 = get_object_or_404(queryset, pk=version_id1)
    version2 = get_object_or_404(queryset, pk=version_id2)

    if version_id1 > version_id2:
        # Compare always the newest one with the older one 
        version1, version2 = version2, version1

    compare_data, has_unfollowed_fields = compare(obj, version1, version2)

    opts = obj._meta

    context = {
        "opts": opts,
        "app_label": opts.app_label,
        "module_name": capfirst(opts.verbose_name),
        "title": _("Compare {0}".format(version1.object_repr)),
        "obj": obj,
        "compare_data": compare_data,
        "has_unfollowed_fields": has_unfollowed_fields,
        "version1": version1,
        "version2": version2,
    }
    extra_context = extra_context or {}
    context.update(extra_context)
    return render(request, 'reversion_compare/compare.html',
                    context)

Example 105

Project: fedora-software Source File: importcomponents.py
Function: handle
    def handle(self, *args, **options):

        # check arguments
        if len(args) == 1:
            xml_file = args[0]
        elif len(args) == 0:
            try:
                # try to find the file in rpm database
                import rpm
                header = rpm.TransactionSet().dbMatch('name', 'appstream-data').next()
                xml_file = filter(lambda f: f[0].endswith('.xml.gz'), header.fiFromHeader())[0][0]
            except:
                if settings.DEBUG:
                    raise
                raise CommandError(
                    'Failed to find xml file provided by appstream-data package. '\
                    'Specify path to the file as an argument.\nType {} help importcomponents'.format(
                        os.path.basename(sys.argv[0])
                    ))
        elif len(args) > 1:
            raise CommandError('Invalid number of arguments.\nType {} help importcomponents'.format(
                os.path.basename(sys.argv[0])))

        logger.info('Reading %s' % xml_file)

        try:
            tree = ElementTree.fromstring(gzip.open(xml_file,'rb').read())
        except Exception as e:
            if settings.DEBUG:
                raise
            raise CommandError('Failed to read content of {xml_file}: {e}\nType {manage} help importcomponents'.format(
                xml_file = xml_file, e = e, manage = os.path.basename(sys.argv[0])
                ))

        component_nodes_count = len(tree)
        logger.info('Parsed {} component nodes'.format(component_nodes_count))

        errors = 0
        component_ids = []
        for c_node in tree:
            c_type      = 'unknown'
            c_type_id   = 'unknown'

            try:
                with transaction.atomic():
                    c_type      = c_node.attrib['type']
                    c_type_id   = c_node.find('id').text
                    c_pkgname   = c_node.find('pkgname').text
                    try:
                        c_project_license = c_node.find('project_license').text
                    except:
                        c_project_license = None

                    logger.info('Importing component {}/{} ({}/{})'.format(
                        c_type, c_type_id, len(component_ids)+1, component_nodes_count,
                    ))

                    # create component
                    c = Component.objects.get_or_create(
                        type            = c_type,
                        type_id         = c_type_id,
                        pkgname         = c_pkgname,
                        project_license = c_project_license,
                    )[0]

                    lang_attr = '{http://www.w3.org/XML/1998/namespace}lang'

                    # create names
                    c.names.all().delete()
                    for name_node in c_node.findall('name'):
                        c.names.add(ComponentName(
                            lang = name_node.attrib.get(lang_attr),
                            name = name_node.text,
                        ))

                    # create summaries
                    c.summaries.all().delete()
                    for summary_node in c_node.findall('summary'):
                        c.summaries.add(ComponentSummary(
                            lang = summary_node.attrib.get(lang_attr),
                            summary = summary_node.text,
                        ))

                    # create descriptions
                    c.descriptions.all().delete()
                    for description_node in c_node.findall('description'):
                        c.descriptions.add(ComponentDescription(
                            lang = description_node.attrib.get(lang_attr),
                            description = ElementTree.tostring(description_node, method="html"),
                        ))

                    # create icons
                    c.icons.all().delete()
                    for icon_node in c_node.findall('icon'):
                        c.icons.add(ComponentIcon(
                            icon    = icon_node.text,
                            type    = icon_node.attrib.get('type'),
                            height  = icon_node.attrib.get('height'),
                            width   = icon_node.attrib.get('width'),
                        ))

                    # create categories
                    c.categories.all().delete()
                    categories_node = c_node.find('categories')
                    if categories_node is not None:
                        for category_node in categories_node.findall('category'):
                            c.categories.add(Category.objects.get_or_create(
                                slug        = slugify(category_node.text),
                                category    = category_node.text,
                            )[0])

                    # create keywords
                    c.keywords.all().delete()
                    keywords_node = c_node.find('keywords')
                    if keywords_node is not None:
                        for keyword_node in keywords_node.findall('keyword'):
                            c.keywords.add(Keyword.objects.get_or_create(
                                lang    = keyword_node.attrib.get(lang_attr),
                                keyword = keyword_node.text,
                            )[0])

                    # create urls
                    c.urls.all().delete()
                    for url_node in c_node.findall('url'):
                        if url_node.text is not None:
                            c.urls.add(ComponentUrl(
                                url     = url_node.text,
                                type    = url_node.attrib.get('type'),
                            ))

                    # create screenshots
                    c.screenshots.all().delete()
                    screenshots_node = c_node.find('screenshots')
                    if screenshots_node is not None:
                        for screenshot_node in screenshots_node.findall('screenshot'):
                            screenshot = ComponentScreenshot(
                                type = screenshot_node.attrib.get('type'),
                            )
                            c.screenshots.add(screenshot)
                            for image_node in screenshot_node.findall('image'):
                                screenshot.images.add(ComponentScreenshotImage(
                                    image   = image_node.text,
                                    type    = image_node.attrib.get('type'),
                                    height  = image_node.attrib.get('height'),
                                    width   = image_node.attrib.get('width'),
                                ))

                    # create releases
                    c.releases.all().delete()
                    releases_node = c_node.find('releases')
                    if releases_node is not None:
                        for release_node in releases_node.findall('release'):
                            c.releases.add(ComponentRelease(
                                version     = release_node.attrib.get('version'),
                                timestamp   = datetime.utcfromtimestamp(
                                    int(release_node.attrib.get('timestamp'))
                                ).replace(tzinfo=utc)
                            ))

                    # create languages
                    c.languages.all().delete()
                    languages_node = c_node.find('languages')
                    if languages_node is not None:
                        for lang_node in languages_node.findall('lang'):
                            c.languages.add(ComponentLanguage(
                                percentage  = lang_node.attrib.get('percentage'),
                                lang        = lang_node.text,
                            ))

                    # create metadata
                    c.metadata.all().delete()
                    metadata_node = c_node.find('metadata')
                    if metadata_node is not None:
                        for value_node in metadata_node.findall('value'):
                            c.metadata.add(ComponentMetadata(
                                key     = value_node.attrib.get('key'),
                                value   = value_node.text,
                            ))

            except Exception as e:
                logger.error('Failed to import node {}/{}: {}'.format(c_type, c_type_id, e))
                if settings.DEBUG:
                    raise
                errors += 1
            else:
                component_ids.append(c.id)

        # check errors
        if errors > 0:
            raise CommandError('Failed to import components: {} error(s)'.format(errors))
        else:
            logger.info('Successfully imported {} components'.format(len(component_ids)))

        # delete stale components
        deleted_components_count = 0
        for c in Component.objects.all():
            if c.id not in component_ids:
                logger.info('Deleting stale component {}/{}'.format(c.type, c.type_id))
                c.delete()
                deleted_components_count += 1

        if deleted_components_count > 0:
            logger.info('Successfully deleted {} stale components'.format(deleted_components_count))

Example 106

Project: evething Source File: character.py
def character_common(request, char, public=True, anonymous=False):
    """Common code for character views"""
    tt = TimerThing('character_common')

    utcnow = datetime.datetime.utcnow()

    # I don't know how this happens but hey, let's fix it here
    if char.config is None:
        char.config = CharacterConfig.objects.create(
            character=char,
        )

    # Do various visibility things here instead of in awful template code
    show = {
        'implants': not anonymous and (not public or char.config.show_implants),
        'queue': anonymous or not public or char.config.show_skill_queue,
        'standings': not anonymous and (not public or char.config.show_standings),
        'wallet': not anonymous and (not public or char.config.show_wallet),
    }

    # Retrieve skill queue
    queue = []
    training_id = None
    # training_level = None
    queue_duration = None

    if show['queue']:
        queue = list(SkillQueue.objects.select_related('skill__item', 'character__corporation', 'character__details').filter(character=char, end_time__gte=utcnow).order_by('end_time'))
        if queue:
            training_id = queue[0].skill.item.id
            # training_level = queue[0].to_level
            queue_duration = total_seconds(queue[-1].end_time - utcnow)
            queue[0].z_complete = queue[0].get_complete_percentage()

    tt.add_time('skill queue')

    # Try retrieving skill data from cache
    cache_key = 'character:skills:%s' % (char.id)
    skill_data = cache.get(cache_key)
    # Not cached, fetch from database and cache
    if skill_data is None:
        # Retrieve the list of skills and group them by market group
        skills = OrderedDict()
        cur = None

        # Fake MarketGroup for unpublished skills
        total_sp = 0

        unpub_mg = MarketGroup(id=0, name="Unpublished")
        unpub_mg.z_total_sp = 0
        skills[unpub_mg] = []

        css = CharacterSkill.objects.filter(character=char)
        css = css.select_related('skill__item__market_group')
        css = css.order_by('skill__item__market_group__name', 'skill__item__name')

        for cs in css:
            mg = cs.skill.item.market_group or unpub_mg
            if mg != cur:
                cur = mg
                cur.z_total_sp = 0
                skills[cur] = []

            cs.z_icons = []
            # level 5 skill = 5 special icons
            if cs.level == 5:
                cs.z_icons.extend(['star level5'] * 5)
                cs.z_class = "level5"
            # 0-4 = n icons
            else:
                cs.z_icons.extend(['star'] * cs.level)

            # training skill can have a training icon
            if show['queue'] and cs.skill.item.id == training_id:
                cs.z_icons.append('star training-highlight')
                cs.z_training = True
                cs.z_class = "training-highlight"

                # add partially trained SP to the total
                total_sp += int(queue[0].get_completed_sp(cs, utcnow))

            # partially trained skills get a partial icon
            elif cs.points > cs.skill.get_sp_at_level(cs.level):
                cs.z_icons.append('star-o training-highlight')

            # then fill out the rest with empty icons
            cs.z_icons.extend(['star-o'] * (5 - len(cs.z_icons)))

            skills[cur].append(cs)
            cur.z_total_sp += cs.points
            if cur is not unpub_mg:
                total_sp += cs.points

        # Move the fake MarketGroup to the end if it has any skills
        k, v = skills.popitem(False)
        if v:
            skills[k] = v

        skill_data = (total_sp, skills)
        cache.set(cache_key, skill_data, 300)

    # Data was cached
    else:
        total_sp, skills = skill_data

    tt.add_time('skill group')

    # Retrieve skillplans
    # user_ids = APIKey.objects.filter(characters__name=char.name).values_list('user_id', flat=True)

    if anonymous is False and request.user.is_authenticated():
        plans = SkillPlan.objects.filter(
            Q(user=request.user)
            | Q(visibility=SkillPlan.GLOBAL_VISIBILITY)
        )
        # |
        # (
        #     Q(user__in=user_ids)
        #     &
        #     Q(visibility=SkillPlan.PUBLIC_VISIBILITY)
        # )
    else:
        plans = SkillPlan.objects.filter(visibility=SkillPlan.GLOBAL_VISIBILITY)

    plans = plans.select_related('user')

    # Sort out the plans and apply icon states
    user_plans = []
    public_plans = []
    for sp in plans:
        if sp.visibility == SkillPlan.PRIVATE_VISIBILITY:
            sp.z_icon = 'lock fa-fw'
        elif sp.visibility == SkillPlan.PUBLIC_VISIBILITY:
            sp.z_icon = 'eye fa-fw'
        elif sp.visibility == SkillPlan.GLOBAL_VISIBILITY:
            sp.z_icon = 'globe fa-fw'

        if sp.user_id == request.user.id:
            user_plans.append(sp)
        else:
            public_plans.append(sp)

    tt.add_time('skill plans')

    if show['standings']:
        # Try retrieving standings data from cache
        cache_key = 'character:standings:%s' % (char.id)
        standings_data = cache.get(cache_key)
        # Not cached, fetch from database and cache
        if standings_data is None:
            faction_standings = list(char.factionstanding_set.select_related().all())
            corp_standings = list(char.corporationstanding_set.select_related().all())
            standings_data = (faction_standings, corp_standings)
            cache.set(cache_key, standings_data, 300)
        # Data was cached
        else:
            faction_standings, corp_standings = standings_data
    else:
        faction_standings = []
        corp_standings = []

    # Render template
    out = render_page(
        'thing/character.html',
        {
            'char': char,
            'public': public,
            'anonymous': anonymous,
            'show': show,
            'total_sp': total_sp,
            'skills': skills,
            'queue': queue,
            'queue_duration': queue_duration,
            'user_plans': user_plans,
            'public_plans': public_plans,
            'faction_standings': faction_standings,
            'corp_standings': corp_standings,
        },
        request,
    )

    tt.add_time('template')
    if settings.DEBUG:
        tt.finished()

    return out

Example 107

Project: newfies-dialer Source File: dashboard.py
    def init_with_context(self, context):

        request = context['request']

        # we want a 3 columns layout
        self.columns = 3

        self.children.append(modules.Group(
            title=_("General"),
            display="tabs",
            children=[
                modules.AppList(
                    title=_('User'),
                    models=('django.contrib.*', 'user_profile.*', 'agent.*', ),
                ),
                modules.AppList(
                    _('Task Manager'),
                    models=('djcelery.*', ),
                ),
                modules.AppList(
                    _('Dashboard stats'),
                    models=('admin_tools_stats.*', ),
                ),
                modules.RecentActions(_('Recent Actions'), 5),
            ]
        ))

        self.children.append(modules.AppList(
            _('Callcenter'),
            models=('callcenter.*', ),
        ))

        self.children.append(modules.AppList(
            _('Settings'),
            models=('dialer_settings.*', ),
        ))

        # append an app list module for "Dialer"
        self.children.append(modules.AppList(
            _('VoIP dialer'),
            models=('dialer_cdr.*', 'dialer_gateway.*', 'dialer_contact.*', 'dialer_campaign.*', ),
        ))

        # append an app list module for "Dialer"
        self.children.append(modules.AppList(
            _('Surveys'),
            models=('survey.*', ),
        ))

        self.children.append(modules.AppList(
            _('SMS Gateway'),
            models=('sms.*', ),
        ))

        # append an app list module for "SMS"
        self.children.append(modules.AppList(
            _('SMS module'),
            models=('mod_sms.*', ),
        ))

        # append an app list module for "Dialer"
        self.children.append(modules.AppList(
            _('Audio Files'),
            models=('audiofield.*', ),
        ))

        self.children.append(modules.AppList(
            _('Do Not Call'),
            models=('dnc.*', ),
        ))

        self.children.append(modules.AppList(
            _('Appointment'),
            models=('appointment.*', ),
        ))

        self.children.append(modules.AppList(
            _('Mod Mailer'),
            models=('mod_mailer.*', ),
        ))

        self.children.append(modules.AppList(
            _('Calendar Settings'),
            models=('calendar_settings.*', ),
        ))

        self.children.append(modules.LinkList(
            _('Reporting'),
            draggable=True,
            deletable=True,
            collapsible=True,
            children=[
                [_('Call Daily Report'),
                 reverse('admin:dialer_cdr_voipcall_changelist') + 'voip_daily_report/'],
            ],
        ))

        # append a link list module for "quick links"
        # site_name = get_admin_site_name(context)

        # Quick link seems to broke the admin design if too many element
        self.children.append(modules.LinkList(
            _('Quick links'),
            layout='inline',
            draggable=True,
            deletable=True,
            collapsible=True,
            children=[
                [_('Newfies-Dialer Website'), 'http://www.newfies-dialer.org/'],
                [_('Support'), 'http://www.newfies-dialer.org/about-us/contact/'],
                [_('Add-ons'), 'http://www.newfies-dialer.org/add-ons/'],
                # [_('Change password'), reverse('%s:password_change' % site_name)],
                # [_('Log out'), reverse('%s:logout' % site_name)],
            ],
        ))

        if not settings.DEBUG:
            # append a feed module
            self.children.append(modules.Feed(
                _('Latest Newfies-Dialer News'),
                feed_url='http://www.newfies-dialer.org/category/blog/feed/',
                limit=5
            ))

        # append an app list module for "Country_prefix"
        self.children.append(modules.AppList(
            _('Dashboard Stats Settings'),
            models=('admin_dashboard_stats.*', ),
        ))

        # Copy following code into your custom dashboard
        graph_list = get_active_graph()
        for i in graph_list:
            kwargs = {}
            kwargs['require_chart_jscss'] = False
            kwargs['graph_key'] = i.graph_key
            if request.POST.get('select_box_' + i.graph_key):
                kwargs['select_box_' + i.graph_key] = request.POST['select_box_' + i.graph_key]

            self.children.append(DashboardCharts(**kwargs))

Example 108

Project: django-ajax Source File: views.py
@json_response
def endpoint_loader(request, application, model, **kwargs):
    """Load an AJAX endpoint.

    This will load either an ad-hoc endpoint or it will load up a model
    endpoint depending on what it finds. It first attempts to load ``model``
    as if it were an ad-hoc endpoint. Alternatively, it will attempt to see if
    there is a ``ModelEndpoint`` for the given ``model``.
    """
    if request.method != "POST":
        raise AJAXError(400, _('Invalid HTTP method used.'))

    try:
        module = import_module('%s.endpoints' % application)
    except ImportError as e:
        if settings.DEBUG:
            raise e
        else:
            raise AJAXError(404, _('AJAX endpoint does not exist.'))

    if hasattr(module, model):
        # This is an ad-hoc endpoint
        endpoint = getattr(module, model)
    else:
        # This is a model endpoint
        method = kwargs.get('method', 'create').lower()
        try:
            del kwargs['method']
        except:
            pass

        try:
            model_endpoint = ajax.endpoint.load(model, application, method,
                **kwargs)
            if not model_endpoint.authenticate(request, application, method):
                raise AJAXError(403, _('User is not authorized.'))

            endpoint = getattr(model_endpoint, method, False)

            if not endpoint:
                raise AJAXError(404, _('Invalid method.'))
        except NotRegistered:
            raise AJAXError(500, _('Invalid model.'))

    data = endpoint(request)
    if isinstance(data, HttpResponse):
        return data

    if isinstance(data, EnvelopedResponse):
        envelope = data.metadata
        payload = data.data
    else:
        envelope = {}
        payload = data

    envelope.update({
        'success': True,
        'data': payload,
    })

    return HttpResponse(json.dumps(envelope, cls=DjangoJSONEncoder,
        separators=(',', ':')))

Example 109

Project: horizon Source File: keystone.py
Function: keystone_client
def keystoneclient(request, admin=False):
    """Returns a client connected to the Keystone backend.

    Several forms of authentication are supported:

        * Username + password -> Unscoped authentication
        * Username + password + tenant id -> Scoped authentication
        * Unscoped token -> Unscoped authentication
        * Unscoped token + tenant id -> Scoped authentication
        * Scoped token -> Scoped authentication

    Available services and data from the backend will vary depending on
    whether the authentication was scoped or unscoped.

    Lazy authentication if an ``endpoint`` parameter is provided.

    Calls requiring the admin endpoint should have ``admin=True`` passed in
    as a keyword argument.

    The client is cached so that subsequent API calls during the same
    request/response cycle don't have to be re-authenticated.
    """
    api_version = VERSIONS.get_active_version()
    user = request.user
    token_id = user.token.id

    if is_multi_domain_enabled:
        # Cloud Admin, Domain Admin or Mixed Domain Admin
        if is_domain_admin(request):
            domain_token = request.session.get('domain_token')
            if domain_token:
                token_id = getattr(domain_token, 'auth_token', None)

    if admin:
        if not policy.check((("identity", "admin_required"),), request):
            raise exceptions.NotAuthorized
        endpoint_type = 'adminURL'
    else:
        endpoint_type = getattr(settings,
                                'OPENSTACK_ENDPOINT_TYPE',
                                'internalURL')

    # Take care of client connection caching/fetching a new client.
    # Admin vs. non-admin clients are cached separately for token matching.
    cache_attr = "_keystoneclient_admin" if admin \
        else backend.KEYSTONE_CLIENT_ATTR
    if (hasattr(request, cache_attr) and
        (not user.token.id or
         getattr(request, cache_attr).auth_token == user.token.id)):
        conn = getattr(request, cache_attr)
    else:
        endpoint = _get_endpoint_url(request, endpoint_type)
        insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
        cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
        LOG.debug("Creating a new keystoneclient connection to %s." % endpoint)
        remote_addr = request.environ.get('REMOTE_ADDR', '')
        conn = api_version['client'].Client(token=token_id,
                                            endpoint=endpoint,
                                            original_ip=remote_addr,
                                            insecure=insecure,
                                            cacert=cacert,
                                            auth_url=endpoint,
                                            debug=settings.DEBUG)
        setattr(request, cache_attr, conn)
    return conn

Example 110

Project: detective.io Source File: register.py
Function: topic_models
def topic_models(path, force=False):
    """
        Auto-discover topic-related model by looking into
        a topic package for an ontology file. This will also
        create all api resources and endpoints.

        This will create the following modules:
            {path}
            {path}.models
            {path}.resources
            {path}.summary
            {path}.urls
    """
    # Clean the topic virtual instances from sys.module
    if force: clean_topic(path)
    topic_module = import_or_create(path, force=force)
    topic_name   = path.split(".")[-1]
    # Ensure that the topic's model exist
    topic = Topic.objects.get(ontology_as_mod=topic_name)
    app_label = topic.app_label()
    reload_models(app_label)
    # Add '.models to the path if needed
    models_path = path if path.endswith(".models") else '%s.models' % path
    urls_path   = "%s.urls" % path
    # Import or create virtually the models.py file
    models_module = import_or_create(models_path, force=force)
    try:
        # Generates all model using the ontology file.
        # Also overides the default app label to allow data persistance
        if topic.ontology_as_json is not None:
            # JSON ontology
            models = parser.json.parse(topic.ontology_as_json, path, app_label=app_label)
        elif topic.ontology_as_owl is not None:
            # OWL ontology
            models = parser.owl.parse(topic.ontology_as_owl, path, app_label=app_label)
        else:
            models = []
    # except TypeError as e:
    #    if settings.DEBUG: print 'TypeError:', e
    #    models = []
    except ValueError as e:
        if settings.DEBUG: print 'ValueError:', e
        models = []
    # Makes every model available through this module
    for m in models:
        # Record the model
        setattr(models_module, m, models[m])
    # Generates the API endpoints
    api = NamespacedApi(api_name='v1', urlconf_namespace=app_label)
    # Create resources root if needed
    resources = import_or_create("%s.resources" % path, force=force)
    # Creates a resource for each model
    for name in models:
        Resource = utils.create_model_resource(models[name])
        resource_name = "%sResource" % name
        # Register the virtual resource to by importa
        resource_path = "%s.resources.%s" % (path, resource_name)
        # This resource is now available everywhere:
        #  * as an attribute of `resources`
        #  * as a module
        setattr(resources, resource_name, Resource)
        # And register it into the API instance
        api.register(Resource())
    # Every app have to instance a SummaryResource class
    summary_path   = "%s.summary" % path
    summary_module = import_or_create(summary_path, force=force)
    # Take the existing summary resource
    if hasattr(summary_module, 'SummaryResource'):
        SummaryResource = summary_module.SummaryResource
    # We create one if it doesn't exist
    else:
        from app.detective.topics.common.summary import SummaryResource as CommonSummaryResource
        attrs           = dict(meta=CommonSummaryResource.Meta)
        SummaryResource = type('SummaryResource', (CommonSummaryResource,), attrs)
    # Register the summary resource
    api.register(SummaryResource())
    # Create url patterns
    urlpatterns = patterns(path, url('', include(api.urls)), )
    # Import or create virtually the url path
    urls_modules = import_or_create(urls_path, force=force)
    # Merge the two url patterns if needed
    if hasattr(urls_modules, "urlpatterns"): urlpatterns += urls_modules.urlpatterns
    # Update the current url pattern
    urls_modules.urlpatterns = urlpatterns
    # API is now up and running,
    # we need to connect its url patterns to global one
    urls = importlib.import_module("app.detective.urls")
    # Add api url pattern with the highest priority
    new_patterns = patterns(app_label,
        url(r'^{0}/{1}/'.format(topic.author, topic.slug), include(urls_path, namespace=app_label) ),
    )
    if hasattr(urls, "urlpatterns"):
        # Merge with a filtered version of the urlpattern to avoid duplicates
        new_patterns += [u for u in urls.urlpatterns if getattr(u, "namespace", None) != app_label ]
    # Then update url pattern
    urls.urlpatterns = new_patterns
    # At last, force the url resolver to reload (because we update it)
    clear_url_caches()
    reload_urlconf()
    topic_module.__name__ = path
    sys.modules[path] = topic_module

    return topic_module

Example 111

Project: airmozilla Source File: views.py
@csrf_exempt
@require_POST
def vidly_media_webhook(request):
    if not request.POST.get('xml'):
        return http.HttpResponseBadRequest("no 'xml'")

    xml_string = request.POST['xml'].strip()
    try:
        struct = xmltodict.parse(xml_string)
    except ExpatError:
        return http.HttpResponseBadRequest("Bad 'xml'")

    try:
        task = struct['Response']['Result']['Task']
        try:
            vidly_submission = VidlySubmission.objects.get(
                url=task['SourceFile'],
                tag=task['MediaShortLink']
            )
            if task['Status'] == 'Finished':
                if not vidly_submission.finished:
                    vidly_submission.finished = timezone.now()
                    vidly_submission.save()

                event = vidly_submission.event

                if (
                    task['Private'] == 'false' and
                    event.privacy != Event.PRIVACY_PUBLIC
                ):
                    # the event is private but the video is not
                    vidly.update_media_protection(
                        vidly_submission.tag,
                        True  # make it private
                    )
                    if not vidly_submission.token_protection:
                        vidly_submission.token_protection = True
                        vidly_submission.save()

                # Awesome!
                # This event now has a fully working transcoded piece of
                # media.
                if (
                    event.status == Event.STATUS_PENDING or
                    event.status == Event.STATUS_PROCESSING
                ):
                    event.status = Event.STATUS_SCHEDULED
                event.archive_time = timezone.now()
                event.save()

                # More awesome! We can start processing the transcoded media.
                # XXX move this to a background task.
                if not event.duration:
                    videoinfo.fetch_duration(
                        event,
                        save=True,
                        verbose=settings.DEBUG
                    )
                    event = Event.objects.get(id=event.id)
                if event.duration:
                    if not Picture.objects.filter(event=event):
                        videoinfo.fetch_screencapture(
                            event,
                            save=True,
                            verbose=settings.DEBUG,
                            set_first_available=True,
                        )
            elif task['Status'] == 'Error':
                if not vidly_submission.errored:
                    vidly_submission.errored = timezone.now()
                    vidly_submission.save()
        except VidlySubmission.DoesNotExist:
            # remember, we can't trust the XML since it's publicly
            # available and exposed as a webhook
            pass

    except KeyError:
        # If it doesn't have a "Result" or "Task", it was just a notification
        # that the media was added.
        pass

    return http.HttpResponse('OK\n')

Example 112

Project: mollyproject Source File: views.py
    def handle_GET(self, request, context):
        # Check whether the referer header is from the same host as the server
        # is responding as
        try:
            referer_host = request.META.get('HTTP_REFERER', '').split('/')[2]
            internal_referer = referer_host == request.META.get('HTTP_HOST')
        except IndexError:
            internal_referer = False

        # Redirects if the user is a desktop browser who hasn't been referred
        # from this site. Also extra checks for preview mode and DEBUG.
        if ("generic_web_browser" in device_parents[request.device.devid]
            and not request.session.get('home:desktop_shown', False)
            and not request.GET.get('preview') == 'true'
            and not internal_referer
            and not settings.DEBUG
            and has_app('molly.apps.desktop')
            and request.REQUEST.get('format') is None):
            return self.redirect(reverse('desktop:index'), request)
        
        messages = []
        # Add any one-off messages to be shown to this user
        if UserMessage.objects.filter(
                read=False, session_key=request.session.session_key).count():
            messages.append({
                'url': reverse('home:messages'),
                'body': _('You have a message from the developers')
            })
        
        # Warn users who use Opera devices
        if not request.session.get('home:opera_mini_warning', False) \
          and request.browser.mobile_browser == u'Opera Mini':
            messages.append(
                { 'body': _("""Please note that the "Mobile View" on Opera Mini does not display this site correctly. To ensure correct operation of this site, ensure "Mobile View" is set to Off in Opera settings""") })
            request.session['home:opera_mini_warning'] = True
        
        if has_app_by_application_name('molly.apps.weather'):
            weather_id = app_by_application_name('molly.apps.weather').location_id
            try:
                weather = Weather.objects.get(ptype='o', location_id=weather_id)
            except Weather.DoesNotExist:
                weather = None
        else:
            weather = None
        
        applications = [{
            'application_name': app.application_name,
            'local_name': app.local_name,
            'title': app.title,
            'url': reverse('%s:index' % app.local_name) \
                    if app.has_urlconf else None,
            'display_to_user': app.display_to_user,
        } for app in all_apps()]

        # Add accesskeys to the first 9 apps to be displayed to the user
        for i, app in enumerate(
                [app for app in applications if app['display_to_user']][:9]
            ):
            app['accesskey'] = i + 1

        context = {
            'applications': applications,
            'hide_feedback_link': True,
            'messages': messages,
            'favourites': get_favourites(request),
            'weather': weather,
        }
        return self.render(request, context, 'home/index',
                           expires=timedelta(minutes=10))

Example 113

Project: django-salmonella Source File: views.py
@user_passes_test(lambda u: u.is_staff)
def label_view(request, app_name, model_name, template_name="", multi=False,
               template_object_name="object"):

    # The list of to obtained objects is in GET.id. No need to resume if we
    # didnt get it.
    if not request.GET.get('id'):
        msg = 'No list of objects given'
        return HttpResponseBadRequest(settings.DEBUG and msg or '')

    # Given objects are either an integer or a comma-separted list of
    # integers. Validate them and ignore invalid values. Also strip them
    # in case the user entered values by hand, such as '1, 2,3'.
    object_list = []
    for pk in request.GET['id'].split(","):
        object_list.append(pk.strip())

    # Check if at least one value survived this cleanup.
    if len(object_list) == 0:
        msg = 'No list or only invalid ids of objects given'
        return HttpResponseBadRequest(settings.DEBUG and msg or '')

    # Make sure this model exists and the user has 'change' permission for it.
    # If he doesnt have this permission, Django would not display the
    # change_list in the popup and the user were never able to select objects.
    try:
        model = apps.get_model(app_name, model_name)
    except LookupError:
        msg = 'Model %s.%s does not exist.' % (app_name, model_name)
        return HttpResponseBadRequest(settings.DEBUG and msg or '')

    if not request.user.has_perm('%s.change_%s' % (app_name, model_name)):
        return HttpResponseForbidden()

    try:
        if multi:
            model_template = "salmonella/%s/multi_%s.html" % (app_name, model_name)
            objs = model.objects.filter(pk__in=object_list)
            objects = []
            for obj in objs:
                change_url = reverse("admin:%s_%s_change" % (app_name, model_name),
                                     args=[obj.pk])
                obj = (obj, change_url)
                objects.append(obj)
            extra_context = {
                template_object_name: objects,
            }
        else:
            model_template = "salmonella/%s/%s.html" % (app_name, model_name)
            obj = model.objects.get(pk=object_list[0])
            change_url = reverse("admin:%s_%s_change" % (app_name, model_name),
                                 args=[obj.pk])
            extra_context = {
                template_object_name: (obj, change_url),
            }
    # most likely the pk wasn't convertable
    except ValueError:
        msg = 'ValueError during lookup'
        return HttpResponseBadRequest(settings.DEBUG and msg or '')
    except model.DoesNotExist:
        msg = 'Model instance does not exist'
        return HttpResponseBadRequest(settings.DEBUG and msg or '')

    return render_to_response((model_template, template_name), extra_context)

Example 114

Project: mozilla-ignite Source File: set_fake_emails.py
Function: handle_noargs
    def handle_noargs(self, **options):
        if not settings.DEBUG:
            raise CommandError('Only available in debug mode')

        from django.contrib.auth.models import User, Group
        email = options.get('default_email', DEFAULT_FAKE_EMAIL)
        include_regexp = options.get('include_regexp', None)
        exclude_regexp = options.get('exclude_regexp', None)
        include_groups = options.get('include_groups', None)
        exclude_groups = options.get('exclude_groups', None)
        no_admin = options.get('no_admin', False)
        no_staff = options.get('no_staff', False)

        users = User.objects.all()
        if no_admin:
            users = users.exclude(is_superuser=True)
        if no_staff:
            users = users.exclude(is_staff=True)
        if exclude_groups:
            groups = Group.objects.filter(name__in=exclude_groups.split(","))
            if groups:
                users = users.exclude(groups__in=groups)
            else:
                raise CommandError("No group matches filter: %s" % exclude_groups)
        if include_groups:
            groups = Group.objects.filter(name__in=include_groups.split(","))
            if groups:
                users = users.filter(groups__in=groups)
            else:
                raise CommandError("No groups matches filter: %s" % include_groups)
        if exclude_regexp:
            users = users.exclude(username__regex=exclude_regexp)
        if include_regexp:
            users = users.filter(username__regex=include_regexp)
        for user in users:
            user.email = email % {'username': user.username,
                                  'first_name': user.first_name,
                                  'last_name': user.last_name}
            user.save()
        print 'Changed %d emails' % users.count()

Example 115

Project: django-analytics Source File: collect_reporting_stats.py
    def calculate_page_durations(self):
        # finding all page visits with no duration
        # from the last 'max_age' days
        query = models.PageVisit.objects.filter(
            duration=None,
            request_event__created__gte=datetime.now() - timedelta(
                days=self.max_age
            ),
        )
        # This looks a little messy, but makes use of python to get a distinct list
        # as opposed to the database. At least with MySQL, the 'distinct' can be
        # a performance hit.
        all_visits = list(set(
            query.values_list(
                'request_event__tracking_key', flat=True
            )
        ))
        total_visits = len(all_visits)
        start_idx = 0
        subset = 100000 if total_visits > 100000 else total_visits
        while start_idx < total_visits:
            self.stdout.write('\n')
            self.stdout.write('Processing %s to %s of %s total '
                'page visits for duration\n' % (
                    start_idx,
                    start_idx + subset
                        if start_idx + subset < total_visits else total_visits,
                    total_visits
                )
            )
            tracking_keys = all_visits[start_idx: start_idx+subset]
            start_idx += subset
            # finding sessions with more than one request event
            events = list(
                models.RequestEvent.objects.filter(
                    tracking_key__in=tracking_keys
                ).values(
                    'tracking_key'
                ).annotate(
                    Count('tracking_key')
                ).order_by().filter(
                    tracking_key__count__gt=1
                ).values_list('tracking_key', flat=True).distinct()
            )
            # limit the page visits to just those with sessions that
            # have more than one request event
            subquery = query.filter(
                request_event__tracking_key__in=events
            ).select_related('request_event')
            total_page_visits = subquery.count()
            self.stdout.write('Processing %s page visits for durations\n' % total_page_visits)
            for cnt, page_visit in enumerate(subquery):
                if cnt % 1000 == 0:
                    self.stdout.write(
                        'Processed %s of %s page visits\n' % (cnt, total_page_visits)
                    )
                    if settings.DEBUG:
                        reset_queries()
                try:
                    # get the next chronological request event
                    next_event = models.RequestEvent.objects.filter(
                        tracking_key=page_visit.request_event.tracking_key,
                        created__gt=page_visit.request_event.created
                    ).earliest()
                    elapsed_delta = next_event.created - page_visit.request_event.created
                    page_visit.duration = round(elapsed_delta.total_seconds())
                    page_visit.save()
                except models.RequestEvent.DoesNotExist:
                    # nothing newer than the current PageVisit
                    pass

Example 116

Project: theyworkforyou Source File: loaddata.py
Function: handle
    def handle(self, *fixture_labels, **options):
        from django.db.models import get_apps
        from django.core import serializers
        from django.db import connection, transaction
        from django.conf import settings

        self.style = no_style()

        verbosity = int(options.get('verbosity', 1))
        show_traceback = options.get('traceback', False)

        # commit is a stealth option - it isn't really useful as
        # a command line option, but it can be useful when invoking
        # loaddata from within another script.
        # If commit=True, loaddata will use its own transaction;
        # if commit=False, the data load SQL will become part of
        # the transaction in place when loaddata was invoked.
        commit = options.get('commit', True)

        # Keep a count of the installed objects and fixtures
        fixture_count = 0
        object_count = 0
        models = set()

        humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'

        # Get a cursor (even though we don't need one yet). This has
        # the side effect of initializing the test database (if
        # it isn't already initialized).
        cursor = connection.cursor()

        # Start transaction management. All fixtures are installed in a
        # single transaction to ensure that all references are resolved.
        if commit:
            transaction.commit_unless_managed()
            transaction.enter_transaction_management()
            transaction.managed(True)

        class SingleZipReader(zipfile.ZipFile):
            def __init__(self, *args, **kwargs):
                zipfile.ZipFile.__init__(self, *args, **kwargs)
                if settings.DEBUG:
                    assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file."
            def read(self):
                return zipfile.ZipFile.read(self, self.namelist()[0])

        compression_types = {
            None:   file,
            'gz':   gzip.GzipFile,
            'zip':  SingleZipReader
        }
        if has_bz2:
            compression_types['bz2'] = bz2.BZ2File

        app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()]
        for fixture_label in fixture_labels:
            parts = fixture_label.split('.')

            if len(parts) > 1 and parts[-1] in compression_types:
                compression_formats = [parts[-1]]
                parts = parts[:-1]
            else:
                compression_formats = compression_types.keys()

            if len(parts) == 1:
                fixture_name = parts[0]
                formats = serializers.get_public_serializer_formats()
            else:
                fixture_name, format = '.'.join(parts[:-1]), parts[-1]
                if format in serializers.get_public_serializer_formats():
                    formats = [format]
                else:
                    formats = []

            if formats:
                if verbosity > 1:
                    print "Loading '%s' fixtures..." % fixture_name
            else:
                sys.stderr.write(
                    self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
                        (fixture_name, format)))
                transaction.rollback()
                transaction.leave_transaction_management()
                return

            if os.path.isabs(fixture_name):
                fixture_dirs = [fixture_name]
            else:
                fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']

            for fixture_dir in fixture_dirs:
                if verbosity > 1:
                    print "Checking %s for fixtures..." % humanize(fixture_dir)

                label_found = False
                for format in formats:
                    for compression_format in compression_formats:
                        if compression_format: 
                            file_name = '.'.join([fixture_name, format, 
                                                  compression_format])
                        else: 
                            file_name = '.'.join([fixture_name, format])
                    
                        if verbosity > 1:
                            print "Trying %s for %s fixture '%s'..." % \
                                (humanize(fixture_dir), file_name, fixture_name)
                        full_path = os.path.join(fixture_dir, file_name)
                        open_method = compression_types[compression_format]                                
                        try: 
                            fixture = open_method(full_path, 'r')
                            if label_found:
                                fixture.close()
                                print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
                                    (fixture_name, humanize(fixture_dir)))
                                transaction.rollback()
                                transaction.leave_transaction_management()
                                return
                            else:
                                fixture_count += 1
                                objects_in_fixture = 0
                                if verbosity > 0:
                                    print "Installing %s fixture '%s' from %s." % \
                                        (format, fixture_name, humanize(fixture_dir))
                                try:
                                    objects = serializers.deserialize(format, fixture)
                                    for obj in objects:
                                        objects_in_fixture += 1
                                        models.add(obj.object.__class__)
                                        obj.save()
                                    object_count += objects_in_fixture
                                    label_found = True
                                except (SystemExit, KeyboardInterrupt):
                                    raise
                                except Exception:
                                    import traceback
                                    fixture.close()
                                    transaction.rollback()
                                    transaction.leave_transaction_management()
                                    if show_traceback:
                                        traceback.print_exc()
                                    else:
                                        sys.stderr.write(
                                            self.style.ERROR("Problem installing fixture '%s': %s\n" %
                                                 (full_path, ''.join(traceback.format_exception(sys.exc_type, 
                                                     sys.exc_value, sys.exc_traceback))))) 
                                    return
                                fixture.close()

                                # If the fixture we loaded contains 0 objects, assume that an
                                # error was encountered during fixture loading.
                                if objects_in_fixture == 0:
                                    sys.stderr.write(
                                        self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
                                            (fixture_name)))
                                    transaction.rollback()
                                    transaction.leave_transaction_management()
                                    return

                        except Exception, e:
                            if verbosity > 1:
                                print "No %s fixture '%s' in %s." % \
                                    (format, fixture_name, humanize(fixture_dir))

        # If we found even one object in a fixture, we need to reset the
        # database sequences.
        if object_count > 0:
            sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
            if sequence_sql:
                if verbosity > 1:
                    print "Resetting sequences"
                for line in sequence_sql:
                    cursor.execute(line)

        if commit:
            transaction.commit()
            transaction.leave_transaction_management()

        if object_count == 0:
            if verbosity > 1:
                print "No fixtures found."
        else:
            if verbosity > 0:
                print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)

        # Close the DB connection. This is required as a workaround for an
        # edge case in MySQL: if the same connection is used to
        # create tables, load data, and query, the query can return
        # incorrect results. See Django #7572, MySQL #37735.
        if commit:
            connection.close()

Example 117

Project: evething Source File: home.py
Function: home
@login_required
def home(request):
    """Home page"""
    tt = TimerThing('home')

    profile = request.user.profile

    tt.add_time('profile')

    # Make a set of characters to hide
    hide_characters = set(int(c) for c in profile.home_hide_characters.split(',') if c)

    # Initialise various data structures
    now = datetime.datetime.utcnow()
    total_balance = 0

    api_keys = set()
    training = set()
    chars = {}
    ship_item_ids = set()

    # Try retrieving characters from cache
    cache_key = 'home:characters:%d' % (request.user.id)
    characters = cache.get(cache_key)
    # Not cached, fetch from database and cache
    if characters is None:
        character_qs = Character.objects.filter(
            apikeys__user=request.user,
            apikeys__valid=True,
            apikeys__key_type__in=(APIKey.ACCOUNT_TYPE, APIKey.CHARACTER_TYPE),
        ).prefetch_related(
            'apikeys',
        ).select_related(
            'config',
            'details',
        ).distinct()

        # Django 1.5 workaround for the stupid change from a non-existent reverse
        # relation returning None to it raising self.related.model.DoesNotExist :(
        characters = []
        char_map = {}
        for c in character_qs:
            try:
                c.details is not None
            except:
                pass
            else:
                characters.append(c)
                char_map[c.id] = c

        tt.add_time('c1')

        # Fetch skill data now WITHOUT Unpublished SP
        cskill_qs = CharacterSkill.objects.filter(
            character__in=char_map.keys(),
            skill__item__market_group__isnull=False,
        ).values(
            'character',
        ).annotate(
            total_sp=Sum('points'),
        )
        for cskill in cskill_qs:
            char_map[cskill['character']].total_sp = cskill['total_sp']

        cache.set(cache_key, characters, 300)

        tt.add_time('c2')

    for character in characters:
        char_keys = [ak for ak in character.apikeys.all() if ak.user_id == request.user.id]
        api_keys.update(char_keys)

        chars[character.id] = character
        character.z_apikey = char_keys[0]
        character.z_training = {}

        total_balance += character.details.wallet_balance
        if character.details.ship_item_id is not None:
            ship_item_ids.add(character.details.ship_item_id)

    tt.add_time('characters')

    # Retrieve ship information
    ship_map = Item.objects.in_bulk(ship_item_ids)
    tt.add_time('ship_items')

    # Do skill training check - this can't be in the model because it
    # scales like crap doing individual queries
    skill_qs = []

    queues = SkillQueue.objects.filter(character__in=chars, end_time__gte=now)
    queues = queues.select_related('skill__item')
    for sq in queues:
        char = chars[sq.character_id]
        duration = total_seconds(sq.end_time - now)

        if 'sq' not in char.z_training:
            char.z_training['sq'] = sq
            char.z_training['skill_duration'] = duration
            char.z_training['sp_per_hour'] = int(sq.skill.get_sp_per_minute(char) * 60)
            char.z_training['complete_per'] = sq.get_complete_percentage(now, char)
            training.add(char.z_apikey)

            skill_qs.append(Q(character=char, skill=sq.skill))

        char.z_training['queue_duration'] = duration

    tt.add_time('training')

    # Retrieve training skill information
    if skill_qs:
        for cs in CharacterSkill.objects.filter(reduce(operator.ior, skill_qs)):
            chars[cs.character_id].z_tskill = cs

    tt.add_time('training skills')

    # Do total skill point aggregation
    total_sp = 0
    for char in characters:
        char.z_total_sp = getattr(char, 'total_sp', 0)
        if 'sq' in char.z_training and hasattr(char, 'z_tskill'):
            char.z_total_sp += int(char.z_training['sq'].get_completed_sp(char.z_tskill, now, char))

        total_sp += char.z_total_sp

    tt.add_time('total_sp')

    # Try retrieving total asset value from cache
    cache_key = 'home:total_assets:%d' % (request.user.id)
    total_assets = cache.get(cache_key)
    # Not cached, fetch from database and cache
    if total_assets is None:
        total_assets = AssetSummary.objects.filter(
            character__in=chars.keys(),
            corporation_id=0,
        ).aggregate(
            t=Sum('total_value'),
        )['t']
        cache.set(cache_key, total_assets, 300)

    tt.add_time('total_assets')

    # Work out who is and isn't training
    not_training = api_keys - training

    # Do notifications
    for char_id, char in chars.items():
        char.z_notifications = []

        # Game time warnings
        if char.z_apikey.paid_until:
            timediff = total_seconds(char.z_apikey.paid_until - now)

            if timediff < 0:
                char.z_notifications.append({
                    'icon': 'clock-o',
                    'text': 'Expired',
                    'tooltip': 'Game time has expired!',
                    'span_class': 'low-game-time',
                })

            elif timediff < EXPIRE_WARNING:
                char.z_notifications.append({
                    'icon': 'clock-o',
                    'text': shortduration(timediff),
                    'tooltip': 'Remaining game time is low!',
                    'span_class': 'low-game-time',
                })

        # API key warnings
        if char.z_apikey.expires:
            timediff = total_seconds(char.z_apikey.expires - now)
            if timediff < EXPIRE_WARNING:
                char.z_notifications.append({
                    'icon': 'key',
                    'text': shortduration(timediff),
                    'tooltip': 'API key is close to expiring!',
                })

        # Empty skill queue
        if char.z_apikey in not_training:
            char.z_notifications.append({
                'icon': 'list-ol',
                'text': 'Empty!',
                'tooltip': 'Skill queue is empty!',
            })

        if char.z_training:
            # Room in skill queue
            if char.z_training['queue_duration'] < ONE_DAY:
                timediff = ONE_DAY - char.z_training['queue_duration']
                char.z_notifications.append({
                    'icon': 'list-ol',
                    'text': shortduration(timediff),
                    'tooltip': 'Skill queue is not full!',
                })

            # Missing implants
            skill = char.z_training['sq'].skill
            pri_attrs = Skill.ATTRIBUTE_MAP[skill.primary_attribute]
            sec_attrs = Skill.ATTRIBUTE_MAP[skill.secondary_attribute]
            pri_bonus = getattr(char.details, pri_attrs[1])
            sec_bonus = getattr(char.details, sec_attrs[1])

            t = []
            if pri_bonus == 0:
                t.append(skill.get_primary_attribute_display())
            if sec_bonus == 0:
                t.append(skill.get_secondary_attribute_display())

            if t:
                char.z_notifications.append({
                    'icon': 'lightbulb-o',
                    'text': ', '.join(t),
                    'tooltip': 'Missing stat implants for currently training skill!',
                })

        # Sort out well classes here ugh
        classes = []
        if char.z_apikey in not_training:
            if profile.home_highlight_backgrounds:
                classes.append('background-error')
            if profile.home_highlight_borders:
                classes.append('border-error')
        elif char.z_notifications:
            if profile.home_highlight_backgrounds:
                classes.append('background-warn')
            if profile.home_highlight_borders:
                classes.append('border-warn')
        else:
            if profile.home_highlight_backgrounds:
                classes.append('background-success')
            if profile.home_highlight_borders:
                classes.append('border-success')

        if classes:
            char.z_well_class = ' %s' % (' '.join(classes))
        else:
            char.z_well_class = ''

    tt.add_time('notifications')

    # Decorate/sort based on settings, ugh
    char_list = chars.values()
    if profile.home_sort_order == 'apiname':
        temp = [(c.z_apikey.group_name or 'ZZZ', c.z_apikey.name, c.name.lower(), c) for c in char_list]
    elif profile.home_sort_order == 'charname':
        temp = [(c.z_apikey.group_name or 'ZZZ', c.name.lower(), c) for c in char_list]
    elif profile.home_sort_order == 'corpname':
        temp = [(c.z_apikey.group_name or 'ZZZ', c.corporation.name.lower(), c.name.lower(), c) for c in char_list]
    elif profile.home_sort_order == 'totalsp':
        temp = [(c.z_apikey.group_name or 'ZZZ', getattr(c, 'z_total_sp', 0), c) for c in char_list]
    elif profile.home_sort_order == 'wallet':
        temp = [(c.z_apikey.group_name or 'ZZZ', c.details and c.details.wallet_balance, c.name.lower(), c) for c in char_list]

    temp.sort()
    if profile.home_sort_descending:
        temp.reverse()

    tt.add_time('sort')

    # Now group based on group_name
    bleh = OrderedDict()
    for temp_data in temp:
        bleh.setdefault(temp_data[0], []).append(temp_data[-1])

    char_lists = []
    for char_list in bleh.values():
        first = [char for char in char_list if char.z_training and char.id not in hide_characters]
        last = [char for char in char_list if not char.z_training and char.id not in hide_characters]
        char_lists.append(first + last)

    tt.add_time('group')

    # Try retrieving corporations from cache
    cache_key = 'home:corporations:%d' % (request.user.id)
    corporations = cache.get(cache_key)
    # Not cached, fetch from database and cache
    if corporations is None:
        corp_ids = Corporation.get_ids_with_access(request.user, APIKey.CORP_ACCOUNT_BALANCE_MASK)
        corp_map = OrderedDict()
        # WARNING: Theoritically we are exposing the wallet divison name which may not be exposed
        # if you only have the BALANCE_MASK or some shit
        for corp_wallet in CorpWallet.objects.select_related().filter(corporation__in=corp_ids):
            if corp_wallet.corporation_id not in corp_map:
                corp_map[corp_wallet.corporation_id] = corp_wallet.corporation
                corp_map[corp_wallet.corporation_id].wallets = []

            corp_map[corp_wallet.corporation_id].wallets.append(corp_wallet)

        corporations = corp_map.values()
        cache.set(cache_key, corporations, 300)

    tt.add_time('corps')

    # Try retrieving total corp asset value from cache
    cache_key = 'home:corp_assets:%d' % (request.user.id)
    corp_assets = cache.get(cache_key)
    # Not cached, fetch from database and cache
    if corp_assets is None:
        corp_ids = Corporation.get_ids_with_access(request.user, APIKey.CORP_ASSET_LIST_MASK)

        corp_assets = AssetSummary.objects.filter(
            corporation_id__in=corp_ids,
        ).aggregate(
            t=Sum('total_value'),
        )['t']
        cache.set(cache_key, corp_assets, 300)

    tt.add_time('corp_assets')

    # Render template
    out = render_page(
        'thing/home.html',
        {
            'profile': profile,
            'not_training': not_training,
            'total_balance': total_balance,
            'total_sp': total_sp,
            'total_assets': total_assets,
            'corp_assets': corp_assets,
            'corporations': corporations,
            # 'characters': first + last,
            'characters': char_lists,
            'events': list(Event.objects.filter(user=request.user)[:10]),
            'ship_map': ship_map,
            # 'task_count': task_count,
        },
        request,
        chars.keys(),
        [c.id for c in corporations]
    )

    tt.add_time('template')
    if settings.DEBUG:
        tt.finished()

    return out

Example 118

Project: Nitrate Source File: filters.py
Function: wrap_exceptions
def wrap_exceptions(func):
    @wraps(func)
    def _decorator(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except django.core.exceptions.PermissionDenied as e:
            # 403 Forbidden
            fault_code = httplib.FORBIDDEN
            fault_string = str(e)
        except django.db.models.ObjectDoesNotExist as e:
            # 404 Not Found
            fault_code = httplib.NOT_FOUND
            fault_string = str(e)
        except (django.db.models.FieldDoesNotExist,
                django.core.exceptions.FieldError,
                django.core.exceptions.ValidationError,
                django.core.exceptions.MultipleObjectsReturned,
                ValueError,
                TypeError) as e:
            # 400 Bad Request
            fault_code = httplib.BAD_REQUEST
            fault_string = str(e)
        except django.db.utils.IntegrityError as e:
            # 409 Duplicate
            fault_code = httplib.CONFLICT
            fault_string = str(e)
        except NotImplementedError as e:
            fault_code = httplib.NOT_IMPLEMENTED
            fault_string = str(e)
        except Exception as e:
            # 500 Server Error
            fault_code = httplib.INTERNAL_SERVER_ERROR
            fault_string = str(e)

        if settings.DEBUG:
            stack_trace = ''.join(traceback.format_exception(*sys.exc_info()))
            fault_string = '%s\n%s' % (fault_string, stack_trace)

        raise Fault(faultCode=fault_code,
                    faultString=_format_message(fault_string))

    return _decorator

Example 119

Project: django-improved-inlines Source File: parser.py
Function: render_inline
def render_inline(inline):
    """
    Replace inline markup with template markup that matches the
    appropriate app and model.

    """
    # Look for inline model type, 'app.model'
    if inline.name == 'inline':
        model_string = inline.get('type', None)
    else:
        model_string = inline.get('data-inline-type', None)

    if not model_string:
        if settings.DEBUG:
            raise TemplateSyntaxError, "Couldn't find a model attribute in the <%s> tag." % inline.name
        else:
            return ''
    app_label, model_name = model_string.split('.')

    # Look for content type
    try:
        content_type = ContentType.objects.get(app_label=app_label, model=model_name)
        model = content_type.model_class()
    except ContentType.DoesNotExist:
        if settings.DEBUG:
            raise TemplateSyntaxError, "Inline ContentType not found for '%s.%s'." % (app_label, model_name)
        else:
            return ''

    # Check for an inline class attribute
    inline_class = smart_unicode(inline.get('class', ''))

    inline_attr = get_inline_attr(inline)

    if not inline_attr:
        return ''

    if inline_attr.get("ids", None):
        # The tag has the 'ids' attribute, process accordingly...
        id_list = [int(i) for i in inline_attr["ids"].split(',')]
        obj_list = model.objects.in_bulk(id_list)
        obj_list = list(obj_list[int(i)] for i in id_list)
        context = { 'object_list': obj_list, 'class': inline_class }
    elif inline_attr.get("id", None):
        # The tag has the 'id' attribute, process accordingly...
        try:
            obj = model.objects.get(pk=inline_attr["id"])
        except model.DoesNotExist:
            if settings.DEBUG:
                raise model.DoesNotExist, "%s with pk of '%s' does not exist" % (model_name, inline_attr["id"])
            else:
                return ''
        context = { 'content_type':"%s.%s" % (app_label, model_name), 'object': obj, 'class': inline_class, 'settings': settings }
    elif inline_attr.get("filter", None):
        # The tag has the 'filter' attribute, process accordingly...
        try:
            l = inline_attr["filter"].split(',')
            filterdict = dict()
            for item in l:
                try:
                    # This is just here to raise a ValueError if there is no '='
                    item.index('=')
                    parts = item.split('=')
                    ## This should work for text, Need to test for all sorts of values
                    # Note: Potentially dangerous use of eval
                    filterdict[str(parts[0])] = eval(parts[1])
                except ValueError:
                    pass
            obj_list = list(model.objects.filter(**filterdict))
            context = { 'object_list': obj_list, 'class': inline_class }
        except KeyError:
            if settings.DEBUG:
                raise TemplateSyntaxError, "The <inline> filter attribute is missing or invalid."
            else:
                return ''
        except ValueError:
            if settings.DEBUG:
                raise TemplateSyntaxError, inline_attr["filter"] + ' is bad, dummy.'
            else:
                return ''
    else:
        raise ValueError

    # Set Default Template
    template = list()
    if inline.get('template', None):
        template.insert(0, inline.get('template'))
    if inline.get('data-inline-template', None):
        template.insert(0, inline.get('data-inline-template'))

    template.extend(["inlines/%s_%s.html" % (app_label, model_name), "inlines/default.html"])

    rendered_inline = {'template': template, 'context': context}

    return rendered_inline

Example 120

Project: mloss Source File: safe_markup.py
def safe_markdown(value, arg=''):
    """
    Runs Markdown over a given value, optionally using various
    extensions python-markdown supports.
    
    Syntax::
    
        {{ value|markdown:"extension1_name,extension2_name..." }}
    
    To enable safe mode, which strips raw HTML and only returns HTML
    generated by actual Markdown syntax, pass "safe" as the first
    extension in the list.
    
    If the version of Markdown in use does not support extensions,
    they will be silently ignored.
    
    """
    try:
        import markdown
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError('Error in markdown filter: Markdown could not be imported.')
        else:
            # Try to salvage this; whatever we do, we shouldn't
            # return potentially unsafe HTML.
            from django.utils.html import escape, linebreaks
            return linebreaks(escape(value))
    else:
        extensions=arg.split(",")
        if len(extensions) > 0 and extensions[0] == "safe":
            extensions = extensions[1:]
            safe_mode = True
        else:
            safe_mode = False
        if len(extensions) > 0 and extensions[0].startswith("cut"):
            cutoff = int(extensions[0][extensions[0].rfind("=")+1:])
            extensions = extensions[1:]
            if len(value)>cutoff:
                p = value[:cutoff].find("\n")
                append=""
                if p == -1:
                    append=" [...]"
                    p = value[:cutoff-1].rfind(" ")
                    if p >= 0:
                        cutoff = p
                else:
                    cutoff = p

                value = value[:cutoff] + append
        return markdown.markdown(value, extensions, safe_mode=safe_mode)

Example 121

Project: tollgate Source File: __init__.py
def update_permissions_after_migration(app, **kwargs):
	"""
	Update app permission just after every migration.
	This is based on app django_extensions update_permissions management command.
	
	"""
	
	from django.conf import settings
	from django.db.models import get_app, get_models
	from django.contrib.auth.management import create_permissions, \
		_get_all_permissions
	from django.contrib.auth import models as auth_app
	from django.contrib.contenttypes.models import ContentType

	app = get_app(app)
	create_permissions(app, get_models(), 2 if settings.DEBUG else 0)
	
	# create_permissions doesn't update labels.
	# We need to do this ourself!
	app_models = get_models(app)
	ctypes, searched_perms = set(), set()
	
	# lookup custom permissions and models
	pyperms = {}
	for klass in app_models:
		ctype = ContentType.objects.get_for_model(klass)
		ctypes.add(ctype)
		for code, label in klass._meta.permissions:
			pyperms[(klass._meta.object_name.lower(), code)] = label
	
	# Iterate through existing permissions, find ones with different labels
	# and update them.
	for perm in auth_app.Permission.objects.filter(content_type__in=ctypes):
		# lookup perm in class
		if (perm.content_type.model, perm.codename) in pyperms:
			pyperm_label = pyperms[(perm.content_type.model, perm.codename)]
			
			if perm.name != pyperm_label:
				print _("Updating permission label for %s.%s...") % \
					(perm.content_type.model, perm.codename)
				perm.name = pyperm_label
				perm.save()

Example 122

Project: django-zappa Source File: handler.py
Function: lambda_handler
def lambda_handler(event, context=None, settings_name="zappa_settings"):  # NoQA
    """
    An AWS Lambda function which parses specific API Gateway input into a WSGI request.

    The request get fed it to Django, processes the Django response, and returns that
    back to the API Gateway.
    """
    time_start = datetime.datetime.now()

    # If in DEBUG mode, log all raw incoming events.
    if settings.DEBUG:
        logger.info('Zappa Event: {}'.format(event))

    # This is a normal HTTP request
    if event.get('method', None):

        # Create the environment for WSGI and handle the request
        environ = create_wsgi_request(event, script_name=settings.SCRIPT_NAME)

        # We are always on https on Lambda, so tell our wsgi app that.
        environ['HTTPS'] = 'on'
        environ['wsgi.url_scheme'] = 'https'

        wrap_me = get_wsgi_application()
        app = ZappaWSGIMiddleware(wrap_me)

        # Execute the application
        response = Response.from_app(app, environ)
        response.content = response.data

        # Prepare the special dictionary which will be returned to the API GW.
        returnme = {'Content': response.data}

        # Pack the WSGI response into our special dictionary.
        for (header_name, header_value) in response.headers:
            returnme[header_name] = header_value
        returnme['Status'] = response.status_code

        # To ensure correct status codes, we need to
        # pack the response as a deterministic B64 string and raise it
        # as an error to match our APIGW regex.
        # The DOCTYPE ensures that the page still renders in the browser.
        exception = None
        if response.status_code in ERROR_CODES:
            content = u"<!DOCTYPE html>" + unicode(response.status_code) + unicode('<meta charset="utf-8" />') + response.data.encode('utf-8')
            b64_content = base64.b64encode(content)
            exception = (b64_content)
        # Internal are changed to become relative redirects
        # so they still work for apps on raw APIGW and on a domain.
        elif 300 <= response.status_code < 400 and response.has_header('Location'):
            location = returnme['Location']
            location = '/' + location.replace("http://zappa/", "")
            exception = location

        # Calculate the total response time,
        # and log it in the Common Log format.
        time_end = datetime.datetime.now()
        delta = time_end - time_start
        response_time_ms = delta.total_seconds() * 1000
        common_log(environ, response, response_time=response_time_ms)

        # Finally, return the response to API Gateway.
        if exception:
            raise Exception(exception)
        else:
            return returnme

    # This is a management command invocation.
    elif event.get('command', None):
        from django.core import management

        # Couldn't figure out how to get the value into stdout with StringIO..
        # Read the log for now. :[]
        management.call_command(*event['command'].split(' '))
        return {}
    elif event.get('detail'):
        module, function = event['detail'].rsplit('.', 1)

        app_module = importlib.import_module(module)
        app_function = getattr(app_module, function)

        # Execute the function!
        app_function()
        return
    else:
        logger.error('Unhandled event: {}'.format(json.dumps(event)))

Example 123

Project: django-ddp Source File: websocket.py
    def process_ddp(self, data):
        """Process a single DDP message."""
        msg_id = data.get('id', None)
        try:
            msg = data.pop('msg')
        except KeyError:
            self.reply(
                'error', reason='Bad request',
                offendingMessage=data,
            )
            return
        try:
            # dispatch message
            self.dispatch(msg, data)
        except Exception as err:  # pylint: disable=broad-except
            # This should be the only protocol exception handler
            kwargs = {
                'msg': {'method': 'result'}.get(msg, 'error'),
            }
            if msg_id is not None:
                kwargs['id'] = msg_id
            if isinstance(err, MeteorError):
                error = err.as_dict()
            else:
                error = {
                    'error': 500,
                    'reason': 'Internal server error',
                }
            if kwargs['msg'] == 'error':
                kwargs.update(error)
            else:
                kwargs['error'] = error
            if not isinstance(err, MeteorError):
                # not a client error, should always be logged.
                stack, _ = safe_call(
                    self.logger.error, '%r %r', msg, data, exc_info=1,
                )
                if stack is not None:
                    # something went wrong while logging the error, revert to
                    # writing a stack trace to stderr.
                    traceback.print_exc(file=sys.stderr)
                    sys.stderr.write(
                        'Additionally, while handling the above error the '
                        'following error was encountered:\n'
                    )
                    sys.stderr.write(stack)
            elif settings.DEBUG:
                print('ERROR: %s' % err)
                dprint('msg', msg)
                dprint('data', data)
                error.setdefault('details', traceback.format_exc())
                # print stack trace for client errors when DEBUG is True.
                print(error['details'])
            self.reply(**kwargs)
            if msg_id and msg == 'method':
                self.reply('updated', methods=[msg_id])

Example 124

Project: fedora-software Source File: importfeaturedapps.py
Function: handle
    def handle(self, *args, **options):

        # check arguments
        ini_file = '/usr/share/gnome-software/featured.ini'
        if len(args) == 1:
            ini_file = args[0]
        elif len(args) > 1:
            raise CommandError('Invalid number of arguments.\nType {} help importfeaturedapps'.format(
                os.path.basename(sys.argv[0])))

        logger.info('Reading %s' % ini_file)

        try:
            featured_config = ConfigParser.ConfigParser()
            featured_config.read([ini_file])
        except Exception as e:
            if settings.DEBUG:
                raise
            raise CommandError('Failed to read content of {ini_file}: {e}\nType {manage} help importfeaturedapps'.format(
                ini_file = ini_file, e = e, manage = os.path.basename(sys.argv[0])
                ))
        else:
            featured_apps_count = len(featured_config.sections())
            logger.info('Parsed {} featured apps'.format(featured_apps_count))


        # import featured apps
        errors = 0
        featured_app_ids = []
        for section in featured_config.sections():
            logger.info('Importing feature app {} ({}/{})'.format(
                section, len(featured_app_ids)+1, featured_apps_count,
            ))
            try:
                with transaction.atomic():
                    # find corresponding component
                    try:
                        c = Component.objects.get(type='desktop', type_id=section)
                    except Component.DoesNotExist:
                        logger.warn('Component desktop/{} does not exist!'.format(section))
                        featured_apps_count -= 1
                        continue

                    # create or update featured app
                    try:
                        fa = c.featured
                    except FeaturedApp.DoesNotExist:
                        fa = FeaturedApp(component=c)
                    fa.style = '; '.join(': '.join(item) for item in featured_config.items(section))
                    fa.style = re.sub(r'/usr/share/gnome-software/', "/static/images/", fa.style)
                    fa.style = re.sub(r'\stext:', " color:", fa.style)
                    fa.save()

            except Exception as e:
                logger.error('Failed to import node {}: {}'.format(section, e))
                if settings.DEBUG:
                    raise
                errors += 1
            else:
                featured_app_ids.append(fa.id)

        # check errors
        if errors > 0:
            raise CommandError('Failed to import featured apps: {} error(s)'.format(errors))
        else:
            logger.info('Successfully imported {} featured apps'.format(len(featured_app_ids)))

        # get stale featured apps
        stale_featured_apps = FeaturedApp.objects.exclude(id__in=featured_app_ids)
        stale_featured_apps_count = stale_featured_apps.count()

        # delete stale featured apps
        if stale_featured_apps_count > 0:
            stale_featured_apps.delete()
            logger.info('Successfully deleted {} stale featured apps'.format(stale_featured_apps_count))

Example 125

Project: ANALYSE Source File: __init__.py
Function: replace_static_urls
def replace_static_urls(text, data_directory, course_id=None, static_asset_path=''):
    """
    Replace /static/$stuff urls either with their correct url as generated by collectstatic,
    (/static/$md5_hashed_stuff) or by the course-specific content static url
    /static/$course_data_dir/$stuff, or, if course_namespace is not None, by the
    correct url in the contentstore (/c4x/.. or /asset-loc:..)

    text: The source text to do the substitution in
    data_directory: The directory in which course data is stored
    course_id: The course identifier used to distinguish static content for this course in studio
    static_asset_path: Path for static assets, which overrides data_directory and course_namespace, if nonempty
    """

    def replace_static_url(match):
        original = match.group(0)
        prefix = match.group('prefix')
        quote = match.group('quote')
        rest = match.group('rest')

        # Don't mess with things that end in '?raw'
        if rest.endswith('?raw'):
            return original

        # In debug mode, if we can find the url as is,
        if settings.DEBUG and finders.find(rest, True):
            return original
        # if we're running with a MongoBacked store course_namespace is not None, then use studio style urls
        elif (not static_asset_path) \
                and course_id \
                and modulestore().get_modulestore_type(course_id) != ModuleStoreEnum.Type.xml:
            # first look in the static file pipeline and see if we are trying to reference
            # a piece of static content which is in the edx-platform repo (e.g. JS associated with an xmodule)

            exists_in_staticfiles_storage = False
            try:
                exists_in_staticfiles_storage = staticfiles_storage.exists(rest)
            except Exception as err:
                log.warning("staticfiles_storage couldn't find path {0}: {1}".format(
                    rest, str(err)))

            if exists_in_staticfiles_storage:
                url = staticfiles_storage.url(rest)
            else:
                # if not, then assume it's courseware specific content and then look in the
                # Mongo-backed database
                url = StaticContent.convert_legacy_static_url_with_course_id(rest, course_id)
        # Otherwise, look the file up in staticfiles_storage, and append the data directory if needed
        else:
            course_path = "/".join((static_asset_path or data_directory, rest))

            try:
                if staticfiles_storage.exists(rest):
                    url = staticfiles_storage.url(rest)
                else:
                    url = staticfiles_storage.url(course_path)
            # And if that fails, assume that it's course content, and add manually data directory
            except Exception as err:
                log.warning("staticfiles_storage couldn't find path {0}: {1}".format(
                    rest, str(err)))
                url = "".join([prefix, course_path])

        return "".join([quote, url, quote])

    return re.sub(
        _url_replace_regex(u'(?:{static_url}|/static/)(?!{data_dir})'.format(
            static_url=settings.STATIC_URL,
            data_dir=static_asset_path or data_directory
        )),
        replace_static_url,
        text
    )

Example 126

Project: airmozilla Source File: vidly_media.py
@superuser_required
def legacy_video_migration(request):  # pragma: no cover
    """for one off mass vid.ly submission"""

    class VideoURLError(Exception):
        pass

    def redirect_recurse(url):
        """return the URL only when it's not redirecting.
        Raise an error on all other statuses >= 400
        """
        response = requests.head(url)
        if response.status_code in (301, 302):
            return redirect_recurse(response.headers['Location'])
        elif response.status_code >= 400:
            raise VideoURLError('{} => {}'.format(
                url, response.status_code
            ))
        return url

    if request.method == 'POST':
        events = Event.objects.filter(id__in=request.POST.getlist('ids'))
        template, = Template.objects.filter(default_archive_template=True)
        for event in events:
            try:
                url = event.template_environment['url']
                url = redirect_recurse(url)
                base_url = get_base_url(request)
                webhook_url = base_url + reverse('manage:vidly_media_webhook')
                url = prepare_vidly_video_url(url)
                token_protection = event.privacy != Event.PRIVACY_PUBLIC
                shortcode, error = vidly.add_media(
                    url,
                    hd=True,
                    token_protection=token_protection,
                    notify_url=webhook_url,
                )

                VidlySubmission.objects.create(
                    event=event,
                    url=url,
                    token_protection=token_protection,
                    hd=True,
                    tag=shortcode,
                    submission_error=error
                )
                event.template_environment = {
                    'tag': shortcode,
                }
                if shortcode:
                    event.template = template
                    event.archive_time = None
                    event.status = Event.STATUS_PROCESSING
                    event.save()

                    videoinfo.fetch_duration(
                        event,
                        video_url=url,
                        save=True,
                        verbose=settings.DEBUG
                    )
                    if Event.objects.get(id=event.id).duration:
                        create_all_event_pictures.delay(
                            event.id,
                            video_url=url,
                        )
                        create_all_timestamp_pictures.delay(
                            event.id,
                            video_url=url,
                        )
            except Exception as exception:
                error = str(exception)
                messages.error(
                    request,
                    'Failed to submit "{}". Error: {}'.format(
                        event.title,
                        error,
                    )
                )

        messages.success(
            request,
            'Submitted {} events for Vid.ly processing'.format(
                events.count()
            )
        )
        return redirect('manage:legacy_video_migration')

    search = request.GET.get('search', 'http://videos.mozilla.org/')
    if search:
        events = Event.objects.filter(
            template_environment__icontains=search
        )
    else:
        events = Event.objects.none()
    context = {
        'events': events,
        'search': search,
    }
    return render(request, 'manage/legacy_video_migration.html', context)

Example 127

Project: mollyproject Source File: views.py
    def render(self, request, context, template_name, expires=None):
        """
        Given a request, a context dictionary and a template name, this renders
        the template with the given context according to the capabilities and
        requested format of the client. An optional final argument is that of
        a timedelta object, which sets additional caching headers for the
        content.
        """
        context.pop('exposes_user_data', None)

        if 'format' in request.REQUEST:
            formats = request.REQUEST['format'].split(',')
            renderers, seen_formats = [], set()
            for format in formats:
                if format in self.FORMATS and format not in seen_formats:
                    renderers.append(self.FORMATS[format])
        elif request.META.get('HTTP_ACCEPT'):
            accepts = self.parse_accept_header(request.META['HTTP_ACCEPT'])
            renderers = MediaType.resolve(accepts, self.FORMATS_BY_MIMETYPE)
        else:
            renderers = [self.FORMATS['html']]

        # Stop external sites from grabbing JSON representations of pages
        # which contain sensitive user information.
        try:
            offsite_referrer = 'HTTP_REFERER' in request.META and \
                request.META['HTTP_REFERER'].split('/')[2] != \
                                                request.META.get('HTTP_HOST')
        except IndexError:
            # Malformed referrers (i.e., those not containing a full URL) throw
            # this
            offsite_referrer = True

        for renderer in renderers:
            if renderer.format != 'html' and context.get('exposes_user_data') \
              and offsite_referrer:
                continue
            try:
                response = renderer(request, context, template_name)
            except NotImplementedError:
                continue
            else:
                if expires is not None and not settings.DEBUG and \
                  not getattr(settings, 'NO_CACHE', False):
                    response['Expires'] = formatdate(
                        mktime((datetime.now() + expires).timetuple()))
                    
                    # if expires is negative, then consider this to be no-cache
                    if expires < timedelta(seconds=0):
                        response['Cache-Control'] = 'no-cache'
                    else:
                        response['Cache-Control'] = 'max-age=%d' % \
                                (expires.seconds + expires.days * 24 * 3600)
                    
                return response
        else:
            if 'format' not in request.REQUEST:
                tried_mimetypes = list(itertools.chain(*[r.mimetypes
                                                         for r in renderers]))
                response = HttpResponse(
                  _("Your Accept header didn't contain any supported media ranges.") + \
                  "\n\n" + _("Supported ranges are:") + \
                  "\n\n * %s\n" % '\n * '.join(
                      sorted('%s (%s)' % (f[0].value, f[1].format) for f in
                      self.FORMATS_BY_MIMETYPE if not f[0] in tried_mimetypes)),
                mimetype="text/plain")
            else:
                response = HttpResponse(
                  _("Unable to render this docuement in this format.") + "\n\n" +
                  _("Supported formats are") + ":\n\n * %s\n" \
                                % '\n * '.join(self.FORMATS.keys()),
                  mimetype="text/plain")
            response.status_code = 406 # Not Acceptable
            return response

Example 128

Project: gazetteer Source File: models.py
Function: parse_file
    def parseFile(self):
        import hashlib, datetime
        from gazetteer.place import Place
        from shapely import wkt
        from shapely.geometry import mapping
        
        place_defaults = {
            "updated":datetime.datetime.utcnow().replace(second=0, microsecond=0).isoformat(),
            "is_primary": True,
            "relationships": [],
            "admin":[] 
            }
            
        CsvFile = csv.DictReader(self.batch_file.file, delimiter="\t")
        count = 0
        for row in CsvFile:
            
            name = row.get("NAME")
            if not name:
                continue

            count = count + 1
            
            if row.get("WKT"):
                shapelygeom =  wkt.loads(row.get("WKT"))
                centroid = [shapelygeom.centroid.x , shapelygeom.centroid.y]
                geometry = mapping(shapelygeom)
            else:
                centroid = []
                geometry = {}
                
            alternates = []
            if row.get("ALTERNATE"):
                alt_list = row.get("ALTERNATE").split("|")
                for alt_name in alt_list:
                    alternates.append({"name": alt_name, "lang": "en"})
                
            uris = row.get("URIS").split("|")

            action = ""
            if row.get("ID"):
                place_id = row.get("ID")
                action = "updated"
            else:
                place_id = hashlib.md5(uris[0]).hexdigest()[:16]
                action = "added"
                  
            timeframe = {}
            if row.get("START") or row.get("END"):
                timeframe = {"start": row.get("START"), "end": row.get("END"), "start_range": row.get("START_RANGE"), "end_range": row.get("END_RANGE") }

            place_dict = {
                "id": place_id, 
                "name":  name,
                "feature_code": row.get("FEATURE_CODE"),
                "timeframe": timeframe, 
                "geometry":geometry,
                "centroid": centroid,
                "alternate": alternates,
                "uris": uris 
                }

            add_optional = False
            for field in self.optional_fields:
                if row.get(field):
                    add_optional = True
            if add_optional:
                optional = {"address":{"number":row.get("NUMBER"), "street":row.get("STREET"), "city":row.get("CITY"), "state":row.get("STATE"), "postcode": row.get("POSTCODE")}}
                place_dict.update(optional)
                
            place_dict.update(place_defaults)
            
            place = Place(place_dict)
            metadata = {
                'user': self.user.username,
                'user_id': str(self.user.id),
                'comment': "Place "+ action +" via batch import id:" + str(self.id)
            }
            
            place.save(metadata=metadata)
            if settings.DEBUG:
                print "BatchImport: place " + action, place.id

        if settings.DEBUG:
                print "BatchImport done: count: " + str(count)
        self.record_count = count
        self.imported_at = datetime.datetime.utcnow()

Example 129

Project: fjord Source File: google_utils.py
def ga_track_event(params, async=True):
    """Sends event tracking information to Google.

    :arg params: Params specific to the event being tracked. Particularly
        keys ``cid``, ``ec``, ``ea``, ``el`` and ``ev``.
    :arg async: Whether to do it now or later via celery.

    >>> ga_track_event({'cid': 'xxxx', 'ec': 'sumo_suggest', 'ea': 'suggest'})

    .. Note::

       If this runs in DEBUG mode or as part of a test suite, it'll
       prepend ``test_`` to the ``ec`` parameter. This reduces
       testing/development data hosing your production data.

    """
    # FIXME: We should only have the google id in settings in
    # production and not anywhere else. Thus when not running in
    # production, this function would check that setting, notice it
    # was blank and then exit. Then we don't have to do this goofy
    # "are we testing?" test which breaks when we change our testing
    # infrastructure.
    if sys.argv and sys.argv[0].endswith('py.test'):
        return

    params.update({
        'v': '1',
        # FIXME: turn this into a setting. the complexity is that it's
        # also in fjord/base/static/js/ga.js.
        'tid': 'UA-35433268-26',
        't': 'event'
    })

    # If we're in DEBUG mode, we don't want to hose production data,
    # so we prepend test_ to the category.
    if 'ec' in params and settings.DEBUG:
        params['ec'] = 'test_' + params['ec']

    if async:
        post_event.delay(params)
    else:
        post_event(params)

Example 130

Project: django-analytics Source File: collect_reporting_stats.py
    def create_page_visits(self, start, end):
        page_pattern_cache = defaultdict(list)
        for page_pattern in models.PagePattern.objects.order_by('client'):
            page_pattern_cache[page_pattern.client.pk].append(
                namedtuple('CachedPagePattern', 'pattern,page_type')(
                    re.compile(page_pattern.pattern, flags=re.IGNORECASE),
                    page_pattern.page_type
                )
            )
        web_property_cache = {
            domain.name: domain.web_property
            for domain in models.Domain.objects.select_related()
        }
        device_cache = {}
        visitor_cache = {}
        visit_cache = {}
        page_cache = {}
        created_page_visits = []
        query_dict = {
            'pagevisit': None,
            'domain__in': web_property_cache.keys(),
        }
        if start:
            start = dt_parse(start)
            query_dict['created__gte'] = start
        if end:
            end = dt_parse(end)
            query_dict['created__lte'] = end

        query = models.RequestEvent.objects.filter(
            **query_dict
        ).select_related().order_by('tracking_user_id', 'created')
        total_events = query.count()
        query = query.iterator()
        self.stdout.write(
            'Processing %s RequestEvents - Start: %s\tEnd: %s\n' % (
                total_events,
                start.strftime('%m/%d/%Y') if start else 'None',
                end.strftime('%m/%d/%Y') if end else 'None'
            )
        )
        for cnt, request_event in enumerate(query):
            if cnt % 1000 == 0:
                self.stdout.write('Processed %s of %s\n' % (cnt, total_events))
                # in DEBUG mode, django holds on to all database queries
                # this can cause memory issues in pre-production environments
                # with large sets of data
                if settings.DEBUG:
                    reset_queries()
            if cnt % 20000 == 0:
                self.stdout.write('Clearing caches')
                visit_cache = {}
                visitor_cache = {}
                page_cache = {}
                device_cache = {}
                models.PageVisit.objects.bulk_create(
                    created_page_visits
                )
                created_page_visits = []
            user_agent_md5 = device = None
            if request_event.user_agent:
                user_agent_md5 = md5(request_event.user_agent.encode('utf-8')).hexdigest()
                device = device_cache.get(user_agent_md5)
            if user_agent_md5 and not device:
                user_agent = parse(request_event.user_agent)
                device, created = models.Device.objects.get_or_create(
                    user_agent_md5=user_agent_md5,
                    defaults={
                        'user_agent': request_event.user_agent,
                        'screen_width': request_event.screen_width,
                        'screen_height': request_event.screen_height,
                        'os': user_agent.os.family,
                        'os_version': user_agent.os.version_string,
                        'browser': user_agent.browser.family,
                        'browser_version': user_agent.browser.version_string,
                        'device': user_agent.device.family,
                        'device_type': self._get_device_type(user_agent),
                    }
                )
                device_cache[request_event.user_agent] = device
            if not device:
                continue
            visitor = visitor_cache.get(request_event.tracking_user_id)
            if not visitor:
                visitor, _created = models.Visitor.objects.get_or_create(
                    uuid=request_event.tracking_user_id,
                )
                visitor_cache[request_event.tracking_user_id] = visitor
            visitor.clients.add(request_event.client)
            page_key = (request_event.path, request_event.client)
            page = page_cache.get(page_key)
            if not page:
                page_type = None
                for page_pattern in page_pattern_cache[request_event.client.pk]:
                    if page_pattern.pattern.match(request_event.path):
                        page_type = page_pattern.page_type
                        break
                page, created = models.Page.objects.get_or_create(
                    path=request_event.path,
                    client=request_event.client,
                    defaults={"page_type": page_type}
                )
                page_cache[page_key] = page
            web_property = web_property_cache.get(request_event.domain.lower())
            if not web_property:
                self.stdout.write(
                    'No WebProperty found for domain: %s - skipping' % request_event.domain
                )
                continue
            visit = visit_cache.get(request_event.tracking_key)
            if not visit:
                visit, _created = models.Visit.objects.get_or_create(
                    uuid=request_event.tracking_key,
                    visitor=visitor,
                    defaults={
                        'first_page': page,
                        'device': device,
                        'visit_date': request_event.created.date(),
                        'web_property': web_property,
                    },
                )
                visit_cache[request_event.tracking_key] = visit

            created_page_visits.append(
                models.PageVisit(
                    page=page,
                    visit=visit,
                    request_event=request_event
                )
            )
        if created_page_visits:
            models.PageVisit.objects.bulk_create(created_page_visits)

Example 131

Project: kitsune Source File: views.py
@cache_page(24 * 60 * 60)  # 24 hours.
def redirect(request, product, version, platform, locale, topic=None):
    """Redirect in-product URLs to the right place."""
    redirects = Redirect.objects.all()

    # In order from least to most important.
    parts = ('locale', 'product', 'version', 'platform', 'topic')

    # First we remove any redirects that explicitly don't match. Do this in
    # Python to avoid an explosion of cache use.
    t = topic if topic else ''

    def f(redirect):
        matches = (
            redirect.product.lower() in (product.lower(), ''),
            redirect.version.lower() in (version.lower(), ''),
            redirect.platform.lower() in (platform.lower(), ''),
            redirect.locale.lower() in (locale.lower(), ''),
            redirect.topic.lower() == t.lower(),
        )
        return all(matches)

    redirects = filter(f, redirects)

    # Assign a ordinal (score) to each redirect based on how specific it is,
    # then order by score descending.
    #
    # Scores are weighted by powers of 2 so that specifying the most important
    # field (i.e. topic) outweighs specifying all 4 other fields. This means
    # we should find the correct match faster in most common cases.
    # For example, if you have two redirects that have topic='foo', and one
    # of them also has platform='mac', and one with platform='win', they'll
    # sort like:
    #   24, Redirect(topic='foo', platform='mac')
    #   16, Redirect(topic='foo')
    #   8,  Redirect(platform='win')
    # Macs going to 'foo' will will hit the first redirect. Everyone else
    # going to 'foo' will hit the second. (Windows users going to 'foo' would
    # never match Redirect(platform='win') but if it sorted higher, it would
    # take longer to get there.)
    def ordinal(redirect):
        score = 0
        for i, part in enumerate(parts):
            if getattr(redirect, part) != '':
                score += 1 << i
        return score, redirect

    ordered = map(ordinal, redirects)
    ordered.sort(key=lambda x: x[0], reverse=True)

    # A redirect matches if all its fields match. A field matches if it is
    # blank or matches the input.
    def matches(redirect, **kw):
        for part in parts:
            attr = getattr(redirect, part)
            if attr != '':
                v = kw[part] if kw[part] else ''
                if attr.lower() != v.lower():
                    return False
        return True

    # As soon as we've found a match, that's the best one.
    destination = None
    for score, redirect in ordered:
        if matches(redirect, product=product, version=version,
                   platform=platform, locale=locale, topic=topic):
            destination = redirect
            break

    # Oh noes! We didn't find a target.
    if not destination:
        raise Http404

    # If the target starts with HTTP, we don't add a locale or query string
    # params.
    if destination.target.startswith('http'):
        target = destination.target
    else:
        params = {
            'as': 'u',
            'utm_source': 'inproduct'}
        if hasattr(request, 'eu_build'):
            params['eu'] = 1
        target = u'/%s/%s' % (locale, destination.target.lstrip('/'))
        target = urlparams(target, **params)

        # Switch over to HTTPS if we DEBUG=False and sample is active.
        if not settings.DEBUG and waffle.sample_is_active('inproduct-https'):
            domain = Site.objects.get_current().domain
            target = 'https://%s%s' % (domain, target)

    # 302 because these can change over time.
    return HttpResponseRedirect(target)

Example 132

Project: evething Source File: assets.py
@login_required
def assets_filter(request):
    """Assets filter"""
    tt = TimerThing('assets')

    characters = Character.objects.filter(apikeys__user=request.user.id).distinct()
    character_ids = []
    character_map = {}
    for character in characters:
        character_ids.append(character.id)
        character_map[character.id] = character

    corp_ids = Corporation.get_ids_with_access(request.user, APIKey.CORP_ASSET_LIST_MASK)
    corporations = Corporation.objects.filter(pk__in=corp_ids)
    corporation_ids = []
    corporation_map = {}
    for corporation in corporations:
        corporation_ids.append(corporation.id)
        corporation_map[corporation.id] = corporation

    # apply our initial set of filters
    assets = Asset.objects.filter(
        Q(character__in=character_ids, corporation_id=0)
        |
        Q(corporation_id__in=corporation_ids)
    )
    assets = assets.prefetch_related('item__item_group__category', 'inv_flag', 'system', 'station')
    # assets = assets.distinct()

    tt.add_time('init')

    # Parse and apply filters
    filters = parse_filters(request, ASSETS_EXPECTED)

    if 'char' in filters:
        qs = []
        for fc, fv in filters['char']:
            if fc == 'eq':
                qs.append(Q(character=fv, corporation_id=0))
            elif fc == 'ne':
                qs.append(~Q(character=fv, corporation_id=0))
        assets = assets.filter(reduce(operator.ior, qs))

    if 'corp' in filters:
        qs = []
        for fc, fv in filters['corp']:
            if fc == 'eq':
                if fv == -1:
                    qs.append(Q(corporation_id__gt=0))
                else:
                    qs.append(Q(corporation_id=fv))
            elif fc == 'ne':
                if fv == -1:
                    qs.append(Q(corporation_id=0))
                else:
                    qs.append(~Q(corporation_id=fv))
        assets = assets.filter(reduce(operator.ior, qs))

    if 'invflag' in filters:
        qs = []
        for fc, fv in filters['invflag']:
            if fc == 'eq' and fv.isdigit():
                qs.append(Q(inv_flag_id=fv))
            elif fc == 'ne' and fv.isdigit():
                qs.append(~Q(inv_flag_id=fv))
            elif fc == 'in':
                qs.append(Q(inv_flag__name__icontains=fv))
        assets = assets.filter(reduce(operator.ior, qs))

    if 'item' in filters:
        qs = []
        for fc, fv in filters['item']:
            if fc == 'eq':
                qs.append(Q(item__name=fv))
            elif fc == 'ne':
                qs.append(~Q(item__name=fv))
            elif fc == 'in':
                qs.append(Q(item__name__icontains=fv))
        assets = assets.filter(reduce(operator.ior, qs))

    if 'itemcat' in filters:
        qs = []
        for fc, fv in filters['itemcat']:
            if fc == 'eq':
                if fv.isdigit():
                    qs.append(Q(item__item_group__category=fv))
                else:
                    qs.append(Q(item__item_group__category__name=fv))
            elif fc == 'ne':
                if fv.isdigit():
                    qs.append(~Q(item__item_group__category=fv))
                else:
                    qs.append(~Q(item__item_group__category__name=fv))
            elif fc == 'in':
                qs.append(Q(item__item_group__category__name__icontains=fv))
        assets = assets.filter(reduce(operator.ior, qs))

    if 'station' in filters:
        qs = []
        for fc, fv in filters['station']:
            if fc == 'eq':
                if fv.isdigit():
                    qs.append(Q(station=fv))
                else:
                    qs.append(Q(station__name=fv))
            elif fc == 'ne':
                if fv.isdigit():
                    qs.append(~Q(station=fv))
                else:
                    qs.append(~Q(station__name=fv))
            elif fc == 'in':
                qs.append(Q(station__name__icontains=fv))
        assets = assets.filter(reduce(operator.ior, qs))

    if 'system' in filters:
        qs = []
        for fc, fv in filters['system']:
            if fc == 'eq':
                if fv.isdigit():
                    qs.append(Q(system=fv))
                else:
                    qs.append(Q(system__name=fv))
            elif fc == 'ne':
                if fv.isdigit():
                    qs.append(~Q(system=fv))
                else:
                    qs.append(~Q(system__name=fv))
            elif fc == 'in':
                qs.append(Q(system__name__icontains=fv))
        assets = assets.filter(reduce(operator.ior, qs))

    tt.add_time('filters')

    asset_map = {}
    for asset in assets:
        asset_map[asset.asset_id] = asset

    tt.add_time('asset map')

    # do parent checks now, ugh
    recurse_you_fool = True
    recurse_assets = assets
    while recurse_you_fool:
        parents = set()
        for asset in recurse_assets:
            if asset.parent not in asset_map:
                parents.add(asset.parent)

        # found some orphan children, better go fetch some more assets
        if parents:
            recurse_assets = Asset.objects.filter(
                asset_id__in=parents,
            ).prefetch_related(
                'item__item_group__category',
                'inv_flag',
                'system',
                'station',
            )

            for asset in recurse_assets:
                asset.z_muted = True
                asset_map[asset.asset_id] = asset

        # No more orphans, escape
        else:
            recurse_you_fool = False

    # initialise data structures
    asset_lookup = {}
    loc_totals = {}
    systems = {}
    last_count = 999999999999999999

    while True:
        assets_now = asset_map.values()
        assets_len = len(assets_now)
        if assets_len == 0:
            break
        if assets_len == last_count:
            print 'infinite loop in assets?! %s' % (assets_len)
            break
        last_count = assets_len

        for asset in assets_now:
            # need to recurse this one later
            if asset.parent and asset_lookup.get(asset.parent) is None:
                continue

            asset.z_contents = []
            asset_lookup[asset.asset_id] = asset
            del asset_map[asset.asset_id]

            # skip missing character ids
            if asset.character_id not in character_map:
                continue

            # character and corporation
            asset.z_character = character_map.get(asset.character_id)
            asset.z_corporation = corporation_map.get(asset.corporation_id)

            # zz blueprints
            if asset.item.item_group.category.name == 'Blueprint':
                asset.z_blueprint = min(-1, asset.raw_quantity)
            else:
                asset.z_blueprint = 0

            # total value of this asset stack
            if asset.z_blueprint >= 0:
                # capital ships!
                if asset.item.item_group.name in ('Capital Industrial Ship', 'Carrier', 'Dreadnought', 'Supercarrier', 'Titan'):
                    asset.z_capital = True
                asset.z_price = asset.item.sell_price
            # BPOs use the base (NPC) price
            elif asset.z_blueprint == -1:
                asset.z_price = asset.item.base_price
            # BPCs count as 0 value for now
            else:
                asset.z_price = 0

            asset.z_total = asset.quantity * asset.z_price
            asset.z_volume = (asset.quantity * asset.item.volume).quantize(Decimal('0.01'))

            # work out if this is a system or station asset
            asset.z_k = asset.system_or_station()
            if asset.z_k not in systems:
                loc_totals[asset.z_k] = 0
                systems[asset.z_k] = []

            # base asset, always add
            if asset.parent == 0:
                asset.z_indent = 0

                loc_totals[asset.z_k] += asset.z_total
                systems[asset.z_k].append(asset)

            # asset is inside something, assign it to parent
            else:
                # parent doesn't exist yet
                parent = asset_lookup.get(asset.parent)
                if parent is None:
                    continue

                # add to parent contents
                parent.z_contents.append(asset)

                # add this to the parent entry in loc_totals
                loc_totals[asset.z_k] += asset.z_total

                # add the total value to every parent of this asset
                p = parent
                while p is not None:
                    p.z_total += asset.z_total
                    p = asset_lookup.get(p.parent)

                # guess at what indent level this should be
                asset.z_indent = getattr(parent, 'z_indent', 0) + 1

                # Celestials (containers) need some special casing
                if parent.item.item_group.category.name == 'Celestial':
                    asset.z_locked = (asset.inv_flag.name == 'Locked')

                    asset.z_type = asset.item.item_group.category.name

                else:
                    # inventory group
                    asset.z_slot = asset.inv_flag.nice_name()
                    # corporation hangar
                    if asset.z_corporation is not None and asset.z_slot.startswith('CorpSAG'):
                        asset.z_slot = getattr(asset.z_corporation, 'division%s' % (asset.z_slot[-1]))

    tt.add_time('main loop')

    # get a total asset value
    total_value = sum(loc_totals.values())

    # decorate/sort/undecorate for our strange sort requirements :(
    for system_name in systems:
        temp = [(asset.z_character.name.lower(), len(asset.z_contents) == 0, asset.item.name, asset.name, asset) for asset in systems[system_name]]
        temp.sort()
        systems[system_name] = [s[-1] for s in temp]

    sorted_systems = sorted(systems.items())

    tt.add_time('sort root')

    # recursively sort asset.z_contents
    for asset_set in systems.values():
        for asset in asset_set:
            _content_sort(asset)

    tt.add_time('sort contents')

    # Render template
    out = render_page(
        'thing/assets_filter.html',
        {
            'json_data': _json_data(characters, corporations, filters),
            'characters': characters,
            'corporations': corporations,
            'total_value': total_value,
            'systems': sorted_systems,
            'loc_totals': loc_totals,
        },
        request,
        character_ids,
        corporation_ids,
    )

    tt.add_time('template')
    if settings.DEBUG:
        tt.finished()

    return out

Example 133

Project: moztrap Source File: test_run.py
    def test_query_count_on_activate(self):
        """
        Count number of queries needed for activation of complex run.

        Queries explained:
        ------------------

        Query 1: Get the environment ids from this run

            "SELECT `environments_environment`.`id` FROM
            `environments_environment` INNER JOIN
            `execution_run_environments` ON (`environments_environment`.`id`
             = `execution_run_environments`.`environment_id`) WHERE (
             `environments_environment`.`deleted_on` IS NULL AND
             `execution_run_environments`.`run_id` = 1 )",

        Query 2: Get the caseversion ids that SHOULD be included in this run,
            in order

            "SELECT DISTINCT cv.id as id
            FROM execution_run as r
                INNER JOIN execution_runsuite as rs
                    ON rs.run_id = r.id
                INNER JOIN library_suitecase as sc
                    ON rs.suite_id = sc.suite_id
                INNER JOIN library_suite as s
                    ON sc.suite_id = s.id
                INNER JOIN library_caseversion as cv
                    ON cv.case_id = sc.case_id
                    AND cv.productversion_id = r.productversion_id
                INNER JOIN library_caseversion_environments as cve
                    ON cv.id = cve.caseversion_id
            WHERE cv.status = 'active'
                AND s.status = 'active'
                AND rs.run_id = 1
                AND cve.environment_id IN (1,2,3,4)
            ORDER BY rs.order, sc.order
            ",

        Query 3-6: Get all the runcaseversions that are not in the result of
         Query 2
            to be used for delete. and then delete them.

            "SELECT `execution_runcaseversion`.`id`,
            `execution_runcaseversion`.`created_on`,
            `execution_runcaseversion`.`created_by_id`,
            `execution_runcaseversion`.`modified_on`,
            `execution_runcaseversion`.`modified_by_id`,
            `execution_runcaseversion`.`deleted_on`,
            `execution_runcaseversion`.`deleted_by_id`,
            `execution_runcaseversion`.`cc_version`,
            `execution_runcaseversion`.`run_id`, `execution_runcaseversion`
            .`caseversion_id`, `execution_runcaseversion`.`order` FROM
            `execution_runcaseversion` WHERE (`execution_runcaseversion`
            .`deleted_on` IS NULL AND `execution_runcaseversion`.`run_id` =
            1  AND NOT (`execution_runcaseversion`.`caseversion_id` IN (2,
            3, 4, 5, 6, 7))) ORDER BY `execution_runcaseversion`.`order` ASC",

            "SELECT `execution_result`.`id`, `execution_result`
            .`created_on`, `execution_result`.`created_by_id`,
            `execution_result`.`modified_on`,
            `execution_result`.`modified_by_id`,
            `execution_result`.`deleted_on`, `execution_result`
            .`deleted_by_id`, `execution_result`.`cc_version`,
            `execution_result`.`tester_id`, `execution_result`
            .`runcaseversion_id`, `execution_result`.`environment_id`,
            `execution_result`.`status`, `execution_result`.`comment`,
            `execution_result`.`is_latest`, `execution_result`.`review`,
            `execution_result`.`reviewed_by_id` FROM `execution_result`
            WHERE `execution_result`.`runcaseversion_id` IN (1)",

            "DELETE FROM `execution_runcaseversion_environments` WHERE
            `runcaseversion_id` IN (1)",

            "DELETE FROM `execution_runcaseversion` WHERE `id` IN (1)",

        Query 7: find duplicates in the existing rcvs, if they exist

            SELECT `execution_runcaseversion`.`caseversion_id`,
            COUNT(`execution_runcaseversion`.`caseversion_id`) AS
            `num_records` FROM `execution_runcaseversion` WHERE (
            `execution_runcaseversion`.`deleted_on` IS NULL AND
            `execution_runcaseversion`.`run_id` = 8 ) GROUP BY
            `execution_runcaseversion`.`caseversion_id` HAVING COUNT(
            `execution_runcaseversion`.`caseversion_id`) > 1  ORDER BY
            `execution_runcaseversion`.`order` ASC

        Query 8: Get existing runcaseversions with the caseversion ids so we
         can use
            Them to build the new RunCaseVersion objects we will only be
            updated
            with order in the bulk create.

            "SELECT `execution_runcaseversion`.`id`,
            `execution_runcaseversion`.`caseversion_id` FROM
            `execution_runcaseversion` WHERE (`execution_runcaseversion`
            .`deleted_on` IS NULL AND `execution_runcaseversion`.`run_id` =
            1 ) ORDER BY `execution_runcaseversion`.`order` ASC",

        Query 9: update order on existing rcvs

            "UPDATE `execution_runcaseversion` SET `modified_on` =
            '2013-03-15 01:00:08', `modified_by_id` = NULL, `order` = 4,
            `cc_version` = `execution_runcaseversion`.`cc_version` + 1 WHERE
             (`execution_runcaseversion`.`deleted_on` IS NULL AND
             `execution_runcaseversion`.`run_id` = 8  AND
             `execution_runcaseversion`.`caseversion_id` = 16 )",

        Query 10: bulk insert for RunCaseVersions

            "INSERT INTO `execution_runcaseversion` (`created_on`,
            `created_by_id`, `modified_on`, `modified_by_id`, `deleted_on`,
            `deleted_by_id`, `cc_version`, `run_id`, `caseversion_id`,
            `order`) VALUES ('2013-03-15 01:00:08', NULL,
            '2013-03-15 01:00:08', NULL, NULL, NULL, 0, 8, 13, 1),
            ('2013-03-15 01:00:08', NULL, '2013-03-15 01:00:08', NULL, NULL,
             NULL, 0, 8, 14, 2), ('2013-03-15 01:00:08', NULL,
             '2013-03-15 01:00:08', NULL, NULL, NULL, 0, 8, 15, 3),
             ('2013-03-15 01:00:08', NULL, '2013-03-15 01:00:08', NULL, NULL,
             NULL, 0, 8, 17, 5), ('2013-03-15 01:00:08', NULL,
             '2013-03-15 01:00:08', NULL, NULL, NULL, 0, 8, 18, 6)"

        Query 11: In order to add the runcaseversion_environment records,
            we need to have all the relevant runcaseversions and prefetch the
            environments for the caseversions

            "SELECT `execution_runcaseversion`.`id`,
            `execution_runcaseversion`.`created_on`,
            `execution_runcaseversion`.`created_by_id`,
            `execution_runcaseversion`.`modified_on`,
            `execution_runcaseversion`.`modified_by_id`,
            `execution_runcaseversion`.`deleted_on`,
            `execution_runcaseversion`.`deleted_by_id`,
            `execution_runcaseversion`.`cc_version`,
            `execution_runcaseversion`.`run_id`, `execution_runcaseversion`
            .`caseversion_id`, `execution_runcaseversion`.`order`,
            `library_caseversion`.`id`, `library_caseversion`.`created_on`,
            `library_caseversion`.`created_by_id`, `library_caseversion`
            .`modified_on`, `library_caseversion`.`modified_by_id`,
            `library_caseversion`.`deleted_on`, `library_caseversion`
            .`deleted_by_id`, `library_caseversion`.`cc_version`,
            `library_caseversion`.`status`, `library_caseversion`
            .`productversion_id`, `library_caseversion`.`case_id`,
            `library_caseversion`.`name`, `library_caseversion`
            .`description`, `library_caseversion`.`latest`,
            `library_caseversion`.`envs_narrowed` FROM
            `execution_runcaseversion` INNER JOIN `library_caseversion` ON (
            `execution_runcaseversion`.`caseversion_id` =
            `library_caseversion`.`id`) WHERE (`execution_runcaseversion`
            .`deleted_on` IS NULL AND `execution_runcaseversion`.`run_id` =
            1 ) ORDER BY `execution_runcaseversion`.`order` ASC",

        Query 12: This is the prefetch_related query used with Query 9.  Django
            makes a separate query and links them in-memory.

            "SELECT (`library_caseversion_environments`.`caseversion_id`) AS
             `_prefetch_related_val`, `environments_environment`.`id`,
             `environments_environment`.`created_on`,
             `environments_environment`.`created_by_id`,
             `environments_environment`.`modified_on`,
             `environments_environment`.`modified_by_id`,
             `environments_environment`.`deleted_on`,
             `environments_environment`.`deleted_by_id`,
             `environments_environment`.`cc_version`,
             `environments_environment`.`profile_id` FROM
             `environments_environment` INNER JOIN
             `library_caseversion_environments` ON (
             `environments_environment`.`id` =
             `library_caseversion_environments`.`environment_id`) WHERE (
             `environments_environment`.`deleted_on` IS NULL AND
             `library_caseversion_environments`.`caseversion_id` IN (2, 3,
             4, 5, 6, 7))",

        Query 13: runcaseversion_environments that already existed that
        pertain to
            the runcaseversions that are still relevant.

            "SELECT `execution_runcaseversion_environments`
            .`runcaseversion_id`, `execution_runcaseversion_environments`
            .`environment_id` FROM `execution_runcaseversion_environments`
            WHERE `execution_runcaseversion_environments`
            .`runcaseversion_id` IN (3, 4, 5, 2, 6, 7)",

        Query 14: Get the environments for this run so we can find the
        intersection
            with the caseversions.

            "SELECT `environments_environment`.`id` FROM
            `environments_environment` INNER JOIN
            `execution_run_environments` ON (`environments_environment`.`id`
             = `execution_run_environments`.`environment_id`) WHERE (
             `environments_environment`.`deleted_on` IS NULL AND
             `execution_run_environments`.`run_id` = 1 )",

        Query 15: Find the runcaseversion_environments that are no longer
        relevant.

            "SELECT `execution_runcaseversion_environments`.`id`,
            `execution_runcaseversion_environments`.`runcaseversion_id`,
            `execution_runcaseversion_environments`.`environment_id` FROM
            `execution_runcaseversion_environments`
            WHERE ((`execution_runcaseversion_environments`.`runcaseversion_id`
            = 2  AND `execution_runcaseversion_environments`.`environment_id`
            = 5 ))",

        Query 16: Delete the runcaseversion_environments that pertained to the
            caseversion that are no longer relevant.

            "DELETE FROM `execution_runcaseversion_environments` WHERE `id`
            IN (9)",

        Query 17: Bulk insert of runcaseversion_environment mappings.

            "INSERT INTO `execution_runcaseversion_environments`
            (`runcaseversion_id`, `environment_id`) VALUES (7, 3), (5, 4),
            (3, 1), (3, 3), (6, 4), (7, 4), (5, 2), (6, 1), (4, 4), (3, 2),
            (7, 1), (6, 3), (6, 2), (4, 3), (4, 2), (3, 4), (5, 1), (4, 1),
            (7, 2), (5, 3)",

        Query 18: Update the test run to make it active.

            "UPDATE `execution_run` SET `created_on` = '2012-11-20 00:11:25',
            `created_by_id` = NULL, `modified_on` = '2012-11-20 00:11:25',
            `modified_by_id` = NULL, `deleted_on` = NULL, `deleted_by_id` =
            NULL, `cc_version` = 1, `has_team` = 0, `status` = 'active',
            `productversion_id` = 1, `name` = 'Test Run', `description` = '',
            `start` = '2012-11-19', `end` = NULL, `build` = NULL,
            `is_series` = 0, `series_id` = NULL
            WHERE (`execution_run`.`deleted_on` IS NULL
            AND `execution_run`.`id` = 1
            AND `execution_run`.`cc_version` = 0 )"

        """

        r = self.F.RunFactory.create(productversion=self.pv8)

        # one that should get deleted because it's not in the suite
        old_cv = self.F.CaseVersionFactory.create(
            name="I shouldn't be here",
            productversion=self.pv8,
            status="active",
            )
        self.F.RunCaseVersionFactory(run=r, caseversion=old_cv)

        # test suite add to run
        ts = self.F.SuiteFactory.create(product=self.p, status="active")
        self.F.RunSuiteFactory.create(suite=ts, run=r)

        # cases that belong to the suite
        cv_needed = []
        for num in range(6):
            cv = self.F.CaseVersionFactory.create(
                name="casey" + str(num),
                productversion=self.pv8,
                status="active",
                )
            self.F.SuiteCaseFactory.create(suite=ts, case=cv.case)
            cv_needed.append(cv)

        # existing one that we should keep
        existing_rcv = self.F.RunCaseVersionFactory(run=r,
            caseversion=cv_needed[3],
            order=0,
            )
        # existing env that should be removed in removal phase
        old_env = self.F.EnvironmentFactory.create_set(
            ["OS", "Browser"],
            ["Atari", "RS-232"],
            )[0]
        self.F.model.RunCaseVersion.environments.through(
            runcaseversion=existing_rcv,
            environment=old_env,
            ).save()

        from django.conf import settings
        from django.db import connection

        settings.DEBUG = True
        connection.queries = []

        try:
            with self.assertNumQueries(17):
                r.activate()

            # to debug, uncomment these lines:
            # import json
            # r.activate()
            # print(json.dumps([x["sql"] for x in connection.queries], indent=4))
            # print("NumQueries={0}".format(len(connection.queries)))

            selects = [x["sql"] for x in connection.queries if x["sql"].startswith("SELECT")]
            inserts = [x["sql"] for x in connection.queries if x["sql"].startswith("INSERT")]
            updates = [x["sql"] for x in connection.queries if x["sql"].startswith("UPDATE")]
            deletes = [x["sql"] for x in connection.queries if x["sql"].startswith("DELETE")]

            self.assertEqual(len(selects), 10)
            self.assertEqual(len(inserts), 2)
            self.assertEqual(len(updates), 2)
            self.assertEqual(len(deletes), 3)
        except AssertionError as e:
            raise e
        finally:
            settings.DEBUG = False

        self.refresh(r)

        self.assertEqual(r.runcaseversions.count(), 6)
        self.assertEqual(
            self.F.model.RunCaseVersion.environments.through.objects.count(),
            24,
            )
        self.assertEqual(
            self.F.model.RunCaseVersion.objects.filter(
            run=r,
            caseversion=old_cv,
            ).count(), 0)

Example 134

Project: django-oauthost Source File: tests.py
Function: test_scope
    def test_scope(self):

        settings.DEBUG = True

        user_1 = User(username='Fred')
        user_1.set_password('12345')
        user_1.save()

        client_1 = Client(user=user_1, title='OClient1', identifier='OClient', password='cl012345')
        client_1.save()

        # Scope is missing.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'password', 'username': 'Fred', 'password': '12345'},
                                Authorization='Basic T0NsaWVudDpjbDAxMjM0NQ==')
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_scope')

        # No scope supported by server.
        resp = self.client.post(
            URL_TOKEN, {'grant_type': 'password', 'username': 'Fred', 'password': '12345', 'scope': 'my scope'},
            Authorization='Basic T0NsaWVudDpjbDAxMjM0NQ==')
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_scope')

        scope1 = Scope(identifier='scope1')
        scope1.save()
        scope2 = Scope(identifier='scope2')
        scope2.save()
        scope3 = Scope(identifier='scope3', status=Scope.STATUS_DISABLED)
        scope3.save()

        client_2 = Client(user=user_1, title='OClien2', identifier='OClient2', password='cl012345')
        client_2.save()
        client_2.scopes.add(scope2)

        # Unsupported (or disabled) client scope request.
        resp = self.client.post(
            URL_TOKEN, {'grant_type': 'password', 'username': 'Fred', 'password': '12345', 'scope': 'scope1 scope2'},
            Authorization='Basic T0NsaWVudDI6Y2wwMTIzNDU=')
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_scope')

        # Unsupported (or disabled) server scope request.
        resp = self.client.post(
            URL_TOKEN, {'grant_type': 'password', 'username': 'Fred', 'password': '12345', 'scope': 'scope1 scope3'},
            Authorization='Basic T0NsaWVudDpjbDAxMjM0NQ==')
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_scope')

        # Unsupported scope request.
        resp = self.client.post(
            URL_TOKEN, {'grant_type': 'password', 'username': 'Fred', 'password': '12345', 'scope': 'scope1'},
            Authorization='Basic T0NsaWVudDpjbDAxMjM0NQ==')
        # print('cuem' * 20)
        # print(resp.content_json['error_description'])
        # print('****' * 20)
        self.assertEqual(resp.status_code, 200)
        self.assertTrue('access_token' in resp.content_json)
        self.assertTrue('refresh_token' in resp.content_json)
        self.assertTrue('token_type' in resp.content_json)
        self.assertEqual(resp.content_json['scope'], 'scope1')

Example 135

Project: zamboni Source File: monitors.py
Function: memcache
def memcache():
    memcache = getattr(settings, 'CACHES', {}).get('default')
    memcache_results = []
    status = ''
    if memcache and 'memcache' in memcache['BACKEND']:
        hosts = memcache['LOCATION']
        using_twemproxy = False
        if not isinstance(hosts, (tuple, list)):
            hosts = [hosts]
        for host in hosts:
            ip, port = host.split(':')

            if ip == '127.0.0.1':
                using_twemproxy = True

            try:
                s = socket.socket()
                s.connect((ip, int(port)))
            except Exception, e:
                result = False
                status = 'Failed to connect to memcached (%s): %s' % (host, e)
                monitor_log.critical(status)
            else:
                result = True
            finally:
                s.close()

            memcache_results.append((ip, port, result))
        if (not using_twemproxy and len(hosts) > 1 and
                len(memcache_results) < 2):
            # If the number of requested hosts is greater than 1, but less
            # than 2 replied, raise an error.
            status = ('2+ memcache servers are required.'
                      '%s available') % len(memcache_results)
            monitor_log.warning(status)

    # If we are in debug mode, don't worry about checking for memcache.
    elif settings.DEBUG:
        return status, []

    if not memcache_results:
        status = 'Memcache is not configured'
        monitor_log.info(status)

    return status, memcache_results

Example 136

Project: evething Source File: character.py
def character_skillplan_common(request, character, skillplan, public=True, anonymous=False):
    tt = TimerThing('skillplan_common')

    utcnow = datetime.datetime.utcnow()

    implants_visible = not public

    # Check our GET variables
    implants = request.GET.get('implants', '')
    if implants.isdigit() and 0 <= int(implants) <= 5:
        implants = int(implants)
    elif implants_visible is True:
        implants = 0
    else:
        implants = 3

    show_trained = ('show_trained' in request.GET)

    tt.add_time('init')

    # Try retrieving learned data from cache
    cache_key = 'character_skillplan:learned:%s' % (character.id)
    learned = cache.get(cache_key)
    # Not cached, fetch from database and cache
    if learned is None:
        learned = {}
        for cs in CharacterSkill.objects.filter(character=character).select_related('skill__item'):
            learned[cs.skill.item.id] = cs
        cache.set(cache_key, learned, 300)

    tt.add_time('char skills')

    # Possibly get training information
    training_skill = None
    if anonymous is True or public is False or character.config.show_skill_queue is True:
        sqs = list(SkillQueue.objects.select_related('skill__item').filter(character=character, end_time__gte=utcnow))
        if sqs:
            training_skill = sqs[0]

    tt.add_time('training')

    # Initialise stat stuff
    if character.details:
        remap_stats = dict(
            int_attribute=character.details.int_attribute,
            mem_attribute=character.details.mem_attribute,
            per_attribute=character.details.per_attribute,
            wil_attribute=character.details.wil_attribute,
            cha_attribute=character.details.cha_attribute,
        )
    else:
        remap_stats = dict(
            int_attribute=0,
            mem_attribute=0,
            per_attribute=0,
            wil_attribute=0,
            cha_attribute=0,
        )

    implant_stats = {}
    for stat in ('int', 'mem', 'per', 'wil', 'cha'):
        k = '%s_bonus' % (stat)
        if implants == 0 and implants_visible is True:
            implant_stats[k] = getattr(character.details, k, 0)
        else:
            implant_stats[k] = implants

    # Iterate over all entries in this skill plan
    entries = []
    total_remaining = 0.0
    for entry in skillplan.entries.select_related('sp_remap', 'sp_skill__skill__item__item_group'):
        # It's a remap entry
        if entry.sp_remap is not None:
            # Delete the previous remap if it's two in a row, that makes no sense
            if entries and entries[-1].sp_remap is not None:
                entries.pop()

            remap_stats['int_attribute'] = entry.sp_remap.int_stat
            remap_stats['mem_attribute'] = entry.sp_remap.mem_stat
            remap_stats['per_attribute'] = entry.sp_remap.per_stat
            remap_stats['wil_attribute'] = entry.sp_remap.wil_stat
            remap_stats['cha_attribute'] = entry.sp_remap.cha_stat

        # It's a skill entry
        if entry.sp_skill is not None:
            skill = entry.sp_skill.skill

            # If this skill is already learned
            cs = learned.get(skill.item.id, None)
            if cs is not None:
                # Mark it as injected if level 0
                if cs.level == 0:
                    entry.z_injected = True
                # It might already be trained
                elif cs.level >= entry.sp_skill.level:
                    # If we don't care about trained skills, skip this skill entirely
                    if not show_trained:
                        continue

                    entry.z_trained = True
                # check if current skill SP > level SP AND planned skill lvl - 1 = learned skill level
                elif cs.points > cs.skill.get_sp_at_level(cs.level) and entry.sp_skill.level - 1 == cs.level:
                    required_sp = cs.skill.get_sp_at_level(cs.level + 1) - cs.skill.get_sp_at_level(cs.level)
                    sp_done = cs.points - cs.skill.get_sp_at_level(cs.level)
                    entry.z_sp_done = sp_done
                    entry.z_percent_trained = round(sp_done / float(required_sp) * 100, 1)
                    entry.z_partial_trained = True

            # Not learned, need to buy it
            else:
                entry.z_buy = True

            # Calculate SP/hr
            if remap_stats:
                entry.z_sppm = skill.get_sppm_stats(remap_stats, implant_stats)
            else:
                if public is True or anonymous is True:
                    entry.z_sppm = skill.get_sp_per_minute(character, implants=implant_stats)
                else:
                    entry.z_sppm = skill.get_sp_per_minute(character)

            # 0 sppm is bad
            entry.z_sppm = max(1, entry.z_sppm)
            entry.z_spph = int(entry.z_sppm * 60)

            # Calculate time remaining
            if training_skill is not None and training_skill.skill_id == entry.sp_skill.skill_id and training_skill.to_level == entry.sp_skill.level:
                entry.z_remaining = total_seconds(training_skill.end_time - utcnow)
                entry.z_training = True
                entry.z_percent_trained = training_skill.get_complete_percentage()
            elif hasattr(entry, 'z_partial_trained'):
                remaining_sp = skill.get_sp_at_level(entry.sp_skill.level) - skill.get_sp_at_level(entry.sp_skill.level - 1)
                entry.z_remaining = (remaining_sp - entry.z_sp_done) / entry.z_sppm * 60
                entry.z_total_time = remaining_sp / entry.z_sppm * 60
            else:
                entry.z_remaining = (skill.get_sp_at_level(entry.sp_skill.level) - skill.get_sp_at_level(entry.sp_skill.level - 1)) / entry.z_sppm * 60

            # Add time remaining to total
            if not hasattr(entry, 'z_trained'):
                total_remaining += entry.z_remaining

        entries.append(entry)

    tt.add_time('skillplan loop')

    out = render_page(
        'thing/character_skillplan.html',
        {
            'show_trained': show_trained,
            'implants': implants,
            'implants_visible': implants_visible,
            'anonymous': anonymous,
            'char': character,
            'skillplan': skillplan,
            'entries': entries,
            'total_remaining': total_remaining,
        },
        request,
    )

    tt.add_time('template')
    if settings.DEBUG:
        tt.finished()

    return out

Example 137

Project: vosae-app Source File: resources.py
Function: wrap_view
    def wrap_view(self, view):
        """
        Wraps methods so they can be called in a more functional way as well
        as handling exceptions better.

        Note that if ``BadRequest`` or an exception with a ``response`` attr
        are seen, there is special handling to either present a message back
        to the user or return the response traveling with the exception.
        """
        @csrf_exempt
        def wrapper(request, *args, **kwargs):
            try:
                self.handle_tenant_access(request)  # Override
                callback = getattr(self, view)
                response = callback(request, *args, **kwargs)

                # Our response can vary based on a number of factors, use
                # the cache class to determine what we should ``Vary`` on so
                # caches won't return the wrong (cached) version.
                varies = getattr(self._meta.cache, "varies", [])

                if varies:
                    patch_vary_headers(response, varies)

                if self._meta.cache.cacheable(request, response):
                    if self._meta.cache.cache_control():
                        # If the request is cacheable and we have a
                        # ``Cache-Control`` available then patch the header.
                        patch_cache_control(response, **self._meta.cache.cache_control())

                if request.is_ajax() and not response.has_header("Cache-Control"):
                    # IE excessively caches XMLHttpRequests, so we're disabling
                    # the browser cache here.
                    # See http://www.enhanceie.com/ie/bugs.asp for details.
                    patch_cache_control(response, no_cache=True)

                return response
            except (BadRequest, tastypie_fields.ApiFieldError) as e:
                data = {"error": e.args[0] if getattr(e, 'args') else ''}
                return self.error_response(request, data, response_class=http.HttpBadRequest)
            except ValidationError as e:
                data = {"error": e.messages}
                return self.error_response(request, data, response_class=http.HttpBadRequest)
            except Exception as e:
                if hasattr(e, 'response'):
                    return e.response

                # A real, non-expected exception.
                # Handle the case where the full traceback is more helpful
                # than the serialized error.
                if settings.DEBUG and getattr(settings, 'TASTYPIE_FULL_DEBUG', False):
                    raise

                # Re-raise the error to get a proper traceback when the error
                # happend during a test case
                if request.META.get('SERVER_NAME') == 'testserver':
                    raise

                # Rather than re-raising, we're going to things similar to
                # what Django does. The difference is returning a serialized
                # error message.
                return self._handle_500(request, e)

        return wrapper

Example 138

Project: django-analytics Source File: collect_reporting_stats.py
    def collect_visit_data(self):
        # Visit data
        page_pattern_cache = {}
        for pattern in models.PagePattern.objects.select_related().filter(
            page_type__code__in=(
                models.PageType.CONVERSION,
                models.PageType.FUNNEL
            )
        ).order_by('client','page_type').distinct():
            if not pattern.client.pk in page_pattern_cache:
                page_pattern_cache[pattern.client.pk] = defaultdict(list)
            page_pattern_cache[pattern.client.pk][pattern.page_type.code].append(
                re.compile(pattern.pattern, flags=re.IGNORECASE),
            )
        query = models.Visit.objects.filter(
            visit_date__gte=(datetime.now() - timedelta(days=self.max_age))
        ).distinct().annotate(
            calc_duration=Sum('pagevisit__duration')
        ).prefetch_related(
            'pagevisit_set__page__page_type', 'pagevisit_set__page__client',
            'pagevisit_set__request_event'
        )
        total_visits = query.count()
        start_idx = 0
        subset = 100000 if total_visits > 100000 else total_visits
        while start_idx < total_visits:
            self.stdout.write('\n')
            self.stdout.write('Processing %s to %s of %s total '
                'page visits for other data\n' % (
                    start_idx,
                    start_idx + subset
                        if start_idx + subset < total_visits else total_visits,
                    total_visits
                )
            )
            visit_subset = query[start_idx: start_idx+subset]
            start_idx += subset

            for cnt, visit in enumerate(visit_subset):
                if cnt % 1000 == 0:
                    self.stdout.write(
                        'Processed %s of %s visits\n' % (cnt, len(visit_subset))
                    )
                    if settings.DEBUG:
                        reset_queries()
                update_fields = []
                pagevisits = sorted(
                    visit.pagevisit_set.all(),
                    cmp=lambda x, y: cmp(x.request_event.created, y.request_event.created)
                )
                if not visit.last_page or visit.last_page.pk != pagevisits[-1].pk:
                    visit.last_page = pagevisits[-1].page
                    update_fields.append('last_page')
                order_ids = []
                conversion_count = 0
                funnel_found = False
                for page_visit in pagevisits:
                    page_patterns = page_pattern_cache.get(page_visit.page.client.pk, {})
                    # in order to count a conversion, the visitor has to have hit
                    # at least one funnel page
                    for pattern in page_patterns.get(models.PageType.FUNNEL, []):
                        if pattern.match(page_visit.page.path):
                            funnel_found = True
                            break
                    for pattern in page_patterns.get(models.PageType.CONVERSION, []):
                        m = pattern.match(page_visit.page.path)
                        if m and len(m.groups()) > 0 and funnel_found:
                            order_ids.append(m.group(1))
                            funnel_found = False
                            break
                conversion_count = len(set(order_ids))
                if visit.conversion_count != conversion_count:
                    visit.conversion_count = conversion_count
                    update_fields.append('conversion_count')
                if visit.duration != visit.calc_duration:
                    visit.duration = visit.calc_duration
                    update_fields.append('duration')
                if update_fields:
                    visit.save(update_fields=update_fields)

Example 139

Project: weblate Source File: admin_views.py
@staff_member_required
def performance(request):
    """
    Shows performance tuning tips.
    """
    checks = []
    # Check for debug mode
    checks.append((
        _('Debug mode'),
        not settings.DEBUG,
        'production-debug',
        settings.DEBUG,
    ))
    # Check for domain configuration
    domain = Site.objects.get_current().domain
    checks.append((
        _('Site domain'),
        check_domain(domain),
        'production-site',
        domain,
    ))
    # Check database being used
    checks.append((
        _('Database backend'),
        "sqlite" not in settings.DATABASES['default']['ENGINE'],
        'production-database',
        settings.DATABASES['default']['ENGINE'],
    ))
    # Check configured admins
    checks.append((
        _('Site administrator'),
        len(settings.ADMINS) > 0 or
        '[email protected]' in [x[1] for x in settings.ADMINS],
        'production-admins',
        ', '.join([x[1] for x in settings.ADMINS]),
    ))
    # Check offloading indexing
    checks.append((
        # Translators: Indexing is postponed to cron job
        _('Indexing offloading'),
        appsettings.OFFLOAD_INDEXING,
        'production-indexing',
        appsettings.OFFLOAD_INDEXING
    ))
    if appsettings.OFFLOAD_INDEXING:
        if IndexUpdate.objects.count() < 20:
            index_updates = True
        elif IndexUpdate.objects.count() < 200:
            index_updates = None
        else:
            index_updates = False

        checks.append((
            # Translators: Indexing is postponed to cron job
            _('Indexing offloading processing'),
            index_updates,
            'production-indexing',
            IndexUpdate.objects.count(),
        ))
    # Check for sane caching
    caches = settings.CACHES['default']['BACKEND'].split('.')[-1]
    if caches in ['MemcachedCache', 'PyLibMCCache', 'DatabaseCache']:
        # We consider these good
        caches = True
    elif caches in ['DummyCache']:
        # This one is definitely bad
        caches = False
    else:
        # These might not be that bad
        caches = None
    checks.append((
        _('Django caching'),
        caches,
        'production-cache',
        settings.CACHES['default']['BACKEND'],
    ))
    # Avatar caching
    checks.append((
        _('Avatar caching'),
        'avatar' in settings.CACHES,
        'production-cache-avatar',
        settings.CACHES['avatar']['BACKEND']
        if 'avatar' in settings.CACHES else '',
    ))
    # Check email setup
    default_mails = (
        'root@localhost',
        'webmaster@localhost',
        '[email protected]'
    )
    checks.append((
        _('Email addresses'),
        (
            settings.SERVER_EMAIL not in default_mails and
            settings.DEFAULT_FROM_EMAIL not in default_mails
        ),
        'production-email',
        ', '.join((settings.SERVER_EMAIL, settings.DEFAULT_FROM_EMAIL)),
    ))
    # libravatar library
    checks.append((
        _('Federated avatar support'),
        HAS_LIBRAVATAR,
        'production-avatar',
        HAS_LIBRAVATAR,
    ))
    # pyuca library
    checks.append((
        _('pyuca library'),
        HAS_PYUCA,
        'production-pyuca',
        HAS_PYUCA,
    ))
    # Cookie signing key
    checks.append((
        _('Secret key'),
        settings.SECRET_KEY != settings_example.SECRET_KEY,
        'production-secret',
        settings.SECRET_KEY,
    ))
    # Allowed hosts
    checks.append((
        _('Allowed hosts'),
        len(settings.ALLOWED_HOSTS) > 0,
        'production-hosts',
        ', '.join(settings.ALLOWED_HOSTS),
    ))

    loader = get_first_loader()
    # Cached template loader
    checks.append((
        _('Cached template loader'),
        'cached.Loader' in loader,
        'production-templates',
        loader,
    ))

    # Check for serving static files
    checks.append((
        _('Admin static files'),
        os.path.exists(
            os.path.join(settings.STATIC_ROOT, 'admin', 'js', 'core.js')
        ),
        'production-admin-files',
        settings.STATIC_ROOT,
    ))

    context = admin_context(request)
    context['checks'] = checks
    context['errors'] = get_configuration_errors()

    return render(
        request,
        "admin/performance.html",
        context,
    )

Example 140

Project: evething Source File: transactions.py
Function: transactions
@login_required
def transactions(request):
    """Transaction list"""
    tt = TimerThing('transactions')

    # Get profile
    profile = request.user.profile

    characters = Character.objects.filter(
        apikeys__user=request.user,
        apikeys__valid=True,
        apikeys__key_type__in=[APIKey.ACCOUNT_TYPE, APIKey.CHARACTER_TYPE]
    ).distinct()
    character_ids = [c.id for c in characters]

    corporation_ids = Corporation.get_ids_with_access(request.user, APIKey.CORP_ASSET_LIST_MASK)
    corporations = Corporation.objects.filter(
        pk__in=corporation_ids
    )

    tt.add_time('init')

    # Get a QuerySet of transactions by this user
    transaction_ids = Transaction.objects.filter(
        (
            Q(character__in=character_ids)
            &
            Q(corp_wallet__isnull=True)
        )
        |
        Q(corp_wallet__corporation__in=corporation_ids)
    )
    transaction_ids = transaction_ids.order_by('-date')

    # Get a QuerySet of transactions IDs by this user
    # characters = list(Character.objects.filter(apikeys__user=request.user.id).values_list('id', flat=True))
    # transaction_ids = Transaction.objects.filter(character_id__in=characters)
    # transaction_ids = transaction_ids.order_by('-date')

    # Get only the ids, at this point joining the rest is unnecessary
    transaction_ids = transaction_ids.values_list('pk', flat=True)

    tt.add_time('transaction ids')

    # Parse and apply filters
    filters = parse_filters(request, FILTER_EXPECTED)

    if 'char' in filters:
        qs = []
        for fc, fv in filters['char']:
            if fc == 'eq':
                qs.append(Q(character=fv))
            elif fc == 'ne':
                qs.append(~Q(character=fv))
        transaction_ids = transaction_ids.filter(reduce(q_reduce_or, qs))

    if 'corp' in filters:
        qs = []
        for fc, fv in filters['corp']:
            if fc == 'eq':
                qs.append(Q(corp_wallet__corporation=fv))
            elif fc == 'ne':
                qs.append(~Q(corp_wallet__corporation=fv))
        transaction_ids = transaction_ids.filter(reduce(q_reduce_or, qs))

    # Client is a special case that requires some extra queries
    if 'client' in filters:
        qs = []
        for fc, fv in filters['client']:
            if fc == 'eq':
                qs.append(Q(name=fv))
            elif fc == 'ne':
                qs.append(~Q(name=fv))
            elif fc == 'in':
                qs.append(Q(name__icontains=fv))

        qs_reduced = reduce(q_reduce_or, qs)

        char_ids = list(Character.objects.filter(qs_reduced).values_list('id', flat=True))
        corp_ids = list(Corporation.objects.filter(qs_reduced).values_list('id', flat=True))

        transaction_ids = transaction_ids.filter(
            Q(other_char_id__in=char_ids)
            |
            Q(other_corp_id__in=corp_ids)
        )

    if 'date' in filters:
        qs = []
        for fc, fv in filters['date']:
            if fc == 'eq':
                try:
                    start = datetime.datetime.strptime(fv, '%Y-%m-%d')
                    end = datetime.datetime.strptime('%s 23:59:59' % (fv), '%Y-%m-%d %H:%M:%S')
                    qs.append(Q(date__range=(start, end)))
                except ValueError:
                    pass
            elif fc == 'bt':
                parts = fv.split(',')
                if len(parts) == 2:
                    try:
                        start = datetime.datetime.strptime(parts[0], '%Y-%m-%d')
                        end = datetime.datetime.strptime('%s 23:59:59' % (parts[1]), '%Y-%m-%d %H:%M:%S')
                        if start < end:
                            qs.append(Q(date__range=(start, end)))
                    except ValueError:
                        pass
        if qs:
            transaction_ids = transaction_ids.filter(reduce(q_reduce_or, qs))

    if 'item' in filters:
        qs = []
        for fc, fv in filters['item']:
            if fc == 'eq':
                qs.append(Q(item__name=fv))
            elif fc == 'ne':
                qs.append(~Q(item__name=fv))
            elif fc == 'in':
                qs.append(Q(item__name__icontains=fv))
        transaction_ids = transaction_ids.filter(reduce(q_reduce_or, qs))

    if 'total' in filters:
        qs = []
        for fc, fv in filters['total']:
            if fc == 'eq':
                if fv < 0:
                    qs.append(Q(buy_transaction=True, total_price=abs(fv)))
                else:
                    qs.append(Q(buy_transaction=False, total_price=fv))

            elif fc == 'ne':
                qs.append(~Q(total_price=fv))

            elif fc == 'gt':
                if fv > 0:
                    qs.append(Q(buy_transaction=False, total_price__gt=fv))
                else:
                    qs.append(
                        Q(buy_transaction=False, total_price__gt=abs(fv))
                        |
                        Q(buy_transaction=True, total_price__lt=abs(fv))
                    )

            elif fc == 'gte':
                if fv >= 0:
                    qs.append(Q(buy_transaction=False, total_price__gte=fv))
                else:
                    qs.append(
                        Q(buy_transaction=False, total_price__gte=abs(fv))
                        |
                        Q(buy_transaction=True, total_price__lte=abs(fv))
                    )

            elif fc == 'lt':
                if fv > 0:
                    qs.append(
                        Q(buy_transaction=False, total_price__lt=fv)
                        |
                        Q(buy_transaction=True, total_price__gt=0)
                    )
                else:
                    qs.append(Q(buy_transaction=True, total_price__gt=abs(fv)))

            elif fc == 'lte':
                if fv >= 0:
                    qs.append(
                        Q(buy_transaction=False, total_price__lte=fv)
                        |
                        Q(buy_transaction=True, total_price__gte=0)
                    )
                else:
                    qs.append(Q(buy_transaction=True, total_price__gte=abs(fv)))

        transaction_ids = transaction_ids.filter(reduce(q_reduce_or, qs))

    tt.add_time('filters')

    # Create a new paginator
    paginator = Paginator(transaction_ids, profile.entries_per_page)

    # If page request is out of range, deliver last page of results
    try:
        paginated = paginator.page(request.GET.get('page'))
    except PageNotAnInteger:
        # Page is not an integer, use first page
        paginated = paginator.page(1)
    except EmptyPage:
        # Page is out of range, deliver last page
        paginated = paginator.page(paginator.num_pages)

    tt.add_time('paginator')

    # Do page number things
    hp = paginated.has_previous()
    hn = paginated.has_next()
    prev = []
    next = []

    if hp:
        # prev and next, use 1 of each
        if hn:
            prev.append(paginated.previous_page_number())
            next.append(paginated.next_page_number())
        # no next, add up to 2 previous links
        else:
            for i in range(paginated.number - 1, 0, -1)[:2]:
                prev.insert(0, i)
    else:
        # no prev, add up to 2 next links
        for i in range(paginated.number + 1, paginator.num_pages)[:2]:
            next.append(i)

    # Build the transaction queryset now to avoid nasty subqueries
    transactions = Transaction.objects.filter(pk__in=paginated)
    transactions = transactions.select_related('corp_wallet__corporation', 'item', 'station', 'character', 'other_char', 'other_corp')
    transactions = transactions.order_by('-date')
    transactions = list(transactions)

    tt.add_time('transactions')

    # Build filter links, urgh
    for transaction in transactions:
        transaction.z_client_filter = build_filter(filters, 'client', 'eq', transaction.other_char or transaction.other_corp)
        transaction.z_item_filter = build_filter(filters, 'item', 'eq', transaction.item.name)

    tt.add_time('build links')

    # Ready template things
    values = {
        'chars': characters,
        'corps': corporations,
    }

    tt.add_time('template bits')

    # Render template
    out = render_page(
        'thing/transactions.html',
        {
            'json_data': _json_data(characters, corporations, filters),
            'transactions': transactions,
            'show_item_icons': request.user.profile.show_item_icons,
            'paginated': paginated,
            'next': next,
            'prev': prev,
            'values': values,
        },
        request,
        character_ids,
        corporation_ids,
    )

    tt.add_time('template')
    if settings.DEBUG:
        tt.finished()

    return out

Example 141

Project: django-jinja Source File: backend.py
    def __init__(self, params):
        params = params.copy()
        options = params.pop("OPTIONS", {}).copy()

        self.app_dirname = options.pop("app_dirname", "templates")
        super(Jinja2, self).__init__(params)

        newstyle_gettext = options.pop("newstyle_gettext", True)
        context_processors = options.pop("context_processors", [])
        match_extension = options.pop("match_extension", ".jinja")
        match_regex = options.pop("match_regex", None)
        environment_clspath = options.pop("environment", "jinja2.Environment")
        extra_filters = options.pop("filters", {})
        extra_tests = options.pop("tests", {})
        extra_globals = options.pop("globals", {})
        extra_constants = options.pop("constants", {})
        translation_engine = options.pop("translation_engine", "django.utils.translation")

        tmpl_debug = options.pop("debug", settings.DEBUG)
        bytecode_cache = options.pop("bytecode_cache", {})
        bytecode_cache.setdefault("name", "default")
        bytecode_cache.setdefault("enabled", False)
        bytecode_cache.setdefault("backend", "django_jinja.cache.BytecodeCache")

        undefined = options.pop("undefined", None)
        if undefined is not None:
            if isinstance(undefined, six.string_types):
                options["undefined"] = utils.load_class(undefined)
            else:
                options["undefined"] = undefined

        if settings.DEBUG:
            options.setdefault("undefined", jinja2.DebugUndefined)
        else:
            options.setdefault("undefined", jinja2.Undefined)

        environment_cls = import_string(environment_clspath)

        if isinstance(options.get("loader"), six.string_types):
            # Allow to specify a loader as string
            loader_cls = import_string(options.pop("loader"))
        else:
            # Backward compatible default
            loader_cls = jinja2.FileSystemLoader

        options.setdefault("loader", loader_cls(self.template_dirs))
        options.setdefault("extensions", builtins.DEFAULT_EXTENSIONS)
        options.setdefault("auto_reload", settings.DEBUG)
        options.setdefault("autoescape", True)

        self.env = environment_cls(**options)

        # Initialize i18n support
        if settings.USE_I18N:
            translation = import_module(translation_engine)
            self.env.install_gettext_translations(translation, newstyle=newstyle_gettext)
        else:
            self.env.install_null_translations(newstyle=newstyle_gettext)

        self._context_processors = context_processors
        self._match_regex = match_regex
        self._match_extension = match_extension
        self._tmpl_debug = tmpl_debug
        self._bytecode_cache = bytecode_cache

        self._initialize_builtins(filters=extra_filters,
                                  tests=extra_tests,
                                  globals=extra_globals,
                                  constants=extra_constants)

        self._initialize_thirdparty()
        self._initialize_bytecode_cache()

Example 142

Project: django-oauthost Source File: tests.py
    def test_grant_authorization_code(self):

        # Secure connection check
        settings.DEBUG = False
        resp = self.client.get(URL_TOKEN, {})
        self.assertEqual(resp.status_code, 403)
        settings.DEBUG = True

        resp = self.client.post(URL_TOKEN, {'grant_type': 'a'})
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'unsupported_grant_type')

        user_1 = User(username='Fred')
        user_1.set_password('12345')
        user_1.save()

        client_1 = Client(user=user_1, title='OClient')
        client_1.save()

        redirect_1 = RedirectionEndpoint(client=client_1, uri='http://redirect-test.com')
        redirect_1.save()

        code_1 = AuthorizationCode(user=user_1, client=client_1, uri=redirect_1.uri)
        code_1.save()

        Scope(identifier='scope1').save()

        # Missing client authentication data.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'authorization_code', 'scope': 'scope1'})
        self.assertEqual(resp.status_code, 401)
        self.assertEqual(resp.content_json['error'], 'invalid_client')

        # Missing all required params.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'authorization_code', 'scope': 'scope1',
                                            'client_id': client_1.identifier, 'client_secret': client_1.password})
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_request')

        # Missing redirect URI.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'authorization_code', 'scope': 'scope1', 'code': 'wrong_code',
                                            'client_id': client_1.identifier, 'client_secret': client_1.password})
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_request')

        # Missing code.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'authorization_code', 'scope': 'scope1',
                                            'redirect_uri': 'http://wrong-url.com', 'client_id': client_1.identifier,
                                            'client_secret': client_1.password})
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_request')

        # Wrong code.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'authorization_code', 'scope': 'scope1', 'code': 'invalid',
                                            'redirect_uri': 'http://localhost:8000/abc/',
                                            'client_id': client_1.identifier, 'client_secret': client_1.password})
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_grant')

        # Wrong URI.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'authorization_code', 'scope': 'scope1', 'code': code_1.code,
                                            'redirect_uri': 'http://wrong-url.com/', 'client_id': client_1.identifier,
                                            'client_secret': client_1.password})
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_grant')

        # Valid call for a token.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'authorization_code', 'scope': 'scope1', 'code': code_1.code,
                                            'redirect_uri': redirect_1.uri, 'client_id': client_1.identifier,
                                            'client_secret': client_1.password})
        self.assertEqual(resp.status_code, 200)
        self.assertTrue('access_token' in resp.content_json)
        self.assertTrue('refresh_token' in resp.content_json)
        self.assertTrue('token_type' in resp.content_json)

        # An additional call for code issues token and code invalidation.
        resp = self.client.post(URL_TOKEN, {'grant_type': 'authorization_code', 'scope': 'scope1', 'code': '1234567',
                                            'redirect_uri': 'http://localhost:8000/abc/',
                                            'client_id': client_1.identifier, 'client_secret': client_1.password})
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_json['error'], 'invalid_grant')

Example 143

Project: opbeat_python Source File: opbeat.py
    def handle_check(self, command, **options):
        """Check your settings for common misconfigurations"""
        self.write(LOGO, cyan)
        passed = True
        config = get_client_config()
        client_class = get_client_class()
        client = client_class(**config)
        # check if org/app and token are set:
        is_set = lambda x: x and x != 'None'
        values = [client.organization_id, client.app_id, client.secret_token]
        if all(map(is_set, values)):
            self.write(
                'Organization, app and secret token are set, good job!',
                green
            )
        else:
            passed = False
            self.write(
                'Configuration errors detected!', red, ending='\n\n'
            )
            if not is_set(client.organization_id):
                self.write(
                    "  * ORGANIZATION_ID not set! ", red, ending='\n'
                )
            if not is_set(client.app_id):
                self.write("  * APP_ID not set! ", red, ending='\n')
            if not is_set(client.secret_token):
                self.write("  * SECRET_TOKEN not set!", red, ending='\n')
            self.write(CONFIG_EXAMPLE)
        self.write('')

        # check if we're disabled due to DEBUG:
        if settings.DEBUG:
            if getattr(settings, 'OPBEAT', {}).get('DEBUG'):
                self.write(
                    'Note: even though you are running in DEBUG mode, we will '
                    'send data to Opbeat, because you set OPBEAT["DEBUG"] to '
                    'True. You can disable Opbeat while in DEBUG mode like this'
                    '\n\n',
                    yellow
                )
                self.write(
                    '   OPBEAT = {\n'
                    '       "DEBUG": False,\n'
                    '       # your other OPBEAT settings\n'
                    '   }'

                )
            else:
                self.write(
                    'Looks like you\'re running in DEBUG mode. Opbeat will NOT '
                    'gather any data while DEBUG is set to True.\n\n',
                    red,
                )
                self.write(
                    'If you want to test Opbeat while DEBUG is set to True, you'
                    ' can force Opbeat to gather data by setting'
                    ' OPBEAT["DEBUG"] to True, like this\n\n'
                    '   OPBEAT = {\n'
                    '       "DEBUG": True,\n'
                    '       # your other OPBEAT settings\n'
                    '   }'
                )
                passed = False
        else:
            self.write(
                'DEBUG mode is disabled! Looking good!',
                green
            )
        self.write('')

        # check if middleware is set, and if it is at the first position
        middleware = list(settings.MIDDLEWARE_CLASSES)
        try:
            pos = middleware.index(
                'opbeat.contrib.django.middleware.OpbeatAPMMiddleware'
            )
            if pos == 0:
                self.write(
                    'Opbeat APM middleware is set! Awesome!',
                    green
                )
            else:
                self.write(
                    'Opbeat APM middleware is set, but not at the first '
                    'position\n',
                    yellow
                )
                self.write(
                    'Opbeat APM works best if you add it at the top of your '
                    'MIDDLEWARE_CLASSES'
                )
        except ValueError:
            self.write(
                'Opbeat APM middleware not set!', red
            )
            self.write(
                '\n'
                'Add it to your MIDDLEWARE_CLASSES like this:\n\n'
                '    MIDDLEWARE_CLASSES = (\n'
                '        "opbeat.contrib.django.middleware.OpbeatAPMMiddleware",\n'
                '        # your other middleware classes\n'
                '    )\n'
            )
        self.write('')
        if passed:
            self.write('Looks like everything should be ready!', green)
        else:
            self.write(
                'Please fix the above errors. If you have any questions, write '
                'us at [email protected]!',
                red
            )
        self.write('')
        return passed

Example 144

Project: django-ca Source File: fabfile.py
@task
def init_demo():
    # setup environment
    os.chdir('ca')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ca.settings")
    sys.path.insert(0, os.getcwd())

    # setup django
    import django
    django.setup()

    # finally - imports!
    from django.conf import settings
    from django.contrib.auth import get_user_model
    from django.core.management import call_command as manage
    from django_ca import ca_settings
    from django_ca.models import Certificate
    from django_ca.models import CertificateAuthority
    from django_ca.models import Watcher
    User = get_user_model()

    if settings.DEBUG is not True:
        abort(red('Refusing to run if settings.DEBUG != True.'))

    if os.path.exists(os.path.join('ca', 'db.sqlite3')):
        abort(red('CA already set up.'))

    print(green('Creating database...'))
    manage('migrate', verbosity=0)
    print(green('Initiating CA...'))
    manage('init_ca', 'Root CA', '/C=AT/ST=Vienna/L=Vienna/O=example/OU=example/CN=ca.example.com',
           pathlen=1, ocsp_url='http://ocsp.ca.example.com', crl_url=['http://ca.example.com/crl'],
           issuer_url='http://ca.example.com/ca.crt', issuer_alt_name='https://ca.example.com'
          )
    root_ca = CertificateAuthority.objects.get(name='Root CA')

    print(green('Initiating Child CA...'))
    manage('init_ca', 'Child CA',
           '/C=AT/ST=Vienna/L=Vienna/O=example/OU=example/CN=sub.ca.example.com', parent=root_ca)
    child_ca = CertificateAuthority.objects.get(name='Child CA')

    # generate OCSP certificate
    print(green('Generate OCSP certificate...'))
    ocsp_key, ocsp_csr, ocsp_pem = create_cert('localhost', alt=['localhost'], profile='ocsp')

    # Create some client certificates (always trust localhost to ease testing)
    for i in range(1, 10):
        hostname = 'host%s.example.com' % i
        print(green('Generate certificate for %s...' % hostname))
        create_cert(hostname, cn=hostname, alt=['localhost'])

    # create stunnel.pem
    key_path = os.path.join(ca_settings.CA_DIR, 'host1.example.com.key')
    pem_path = os.path.join(ca_settings.CA_DIR, 'host1.example.com.pem')
    stunnel_path = os.path.join(ca_settings.CA_DIR, 'stunnel.pem')
    with open(key_path) as key, open(pem_path) as pem, open(stunnel_path, 'w') as stunnel:
        stunnel.write(key.read())
        stunnel.write(pem.read())

    print(green('Creating client certificate...'))
    create_cert('client', cn='First Last', cn_in_san=False, alt=['[email protected]'], ca=child_ca)

    # Revoke host1 and host2
    print(green('Revoke host1.example.com and host2.example.com...'))
    cert = Certificate.objects.get(cn='host1.example.com')
    cert.revoke()
    cert.save()

    cert = Certificate.objects.get(cn='host2.example.com')
    cert.revoke('keyCompromise')
    cert.save()


    print(green('Create CRL and OCSP index...'))
    crl_path = os.path.join(ca_settings.CA_DIR, 'crl.pem')
    ocsp_index = os.path.join(ca_settings.CA_DIR, 'ocsp_index.txt')
    manage('dump_crl', crl_path)
    manage('dump_ocsp_index', ocsp_index, ca=root_ca)

    ca_crl_path = os.path.join(ca_settings.CA_DIR, 'ca_crl.pem')

    # Concat the CA certificate and the CRL, this is required by "openssl verify"
    with open(crl_path) as crl, open(ca_crl_path, 'w') as ca_crl:
        ca_crl.write(root_ca.pub)
        ca_crl.write(crl.read())

    # create a few watchers
    Watcher.from_addr('First Last <[email protected]>')
    Watcher.from_addr('Second Last <[email protected]>')

    # create admin user for login
    User.objects.create_superuser('user', '[email protected]', 'nopass')

    # write public ca cert so it can be used by demo commands below
    ca_crt = os.path.join(ca_settings.CA_DIR, '%s.pem' % root_ca.serial)
    with open(ca_crt, 'w') as outstream:
        outstream.write(root_ca.pub)
    ca_crt = os.path.join(ca_settings.CA_DIR, '%s.pem' % child_ca.serial)
    with open(ca_crt, 'w') as outstream:
        outstream.write(child_ca.pub)

    os.chdir('../')
    cwd = os.getcwd()
    rel = lambda p: os.path.relpath(p, cwd)
    ca_crt = rel(ca_crt)
    host1_pem = rel(os.path.join(ca_settings.CA_DIR, 'host1.example.com.pem'))
    print("")
    print(green('* All certificates are in %s' % rel(ca_settings.CA_DIR)))
    print(green('* Verify with CRL:'))
    print('\topenssl verify -CAfile %s -crl_check %s' % (rel(ca_crl_path), rel(host1_pem)))
    print(green('* Run OCSP responder:'))
    print('\topenssl ocsp -index %s -port 8888 -rsigner %s -rkey %s -CA %s -text' % (rel(ocsp_index), rel(ocsp_pem), rel(ocsp_key), ca_crt))
    print(green('* Verify certificate with OCSP:'))
    print('\topenssl ocsp -CAfile %s -issuer %s -cert %s -url http://localhost:8888 -resp_text' % (ca_crt, ca_crt, host1_pem))
    print(green('* Start webserver on http://localhost:8000 (user: user, password: nopass) with:'))
    print('\tpython ca/manage.py runserver')

Example 145

Project: private-readthedocs.org Source File: middleware.py
Function: process_request
    def process_request(self, request):
        if settings.DEBUG:
            return None
        host = request.get_host()
        if ':' in host:
            host = host.split(':')[0]
        domain_parts = host.split('.')
        #Google was finding crazy www.blah.readthedocs.org domains.
        if len(domain_parts) > 3:
            if not settings.DEBUG:
                raise Http404('Invalid hostname')
        if len(domain_parts) == 3:
            subdomain = domain_parts[0]
            if not (subdomain.lower() == 'www') and 'readthedocs.org' in host:
                request.subdomain = True
                request.slug = subdomain
                request.urlconf = 'core.subdomain_urls'
                return None
        if len(domain_parts) == 3:
            subdomain = domain_parts[0]
            if not (subdomain.lower() == 'www') and 'rtfd.org' in host:
                request.slug = subdomain
                request.urlconf = 'core.djangome_urls'
                return None
        if 'readthedocs.org' not in host \
            and 'localhost' not in host \
            and 'testserver' not in host:
            request.cname = True
            try:
                slug = cache.get(host)
                if not slug:
                    redis_conn = redis.Redis(**settings.REDIS)
                    from dns import resolver
                    answer = [ans for ans in resolver.query(host, 'CNAME')][0]
                    domain = answer.target.to_unicode()
                    slug = domain.split('.')[0]
                    cache.set(host, slug, 60*60)
                    #Cache the slug -> host mapping permanently.
                    redis_conn.sadd("rtd_slug:v1:%s" % slug, host)
                request.slug = slug
                request.urlconf = 'core.subdomain_urls'
            except:
                #Some crazy person is CNAMEing to us. 404.
                if not settings.DEBUG:
                    raise Http404('Invalid Host Name.')
        #Normal request.
        return None

Example 146

Project: ideascube Source File: dummydata.py
Function: handle
    def handle(self, *args, **options):
        if not settings.DEBUG:
            msg = ('This does not seem to be a dev project. Aborting. You need'
                   ' to be in DEBUG=True')
            raise CommandError(msg)

        # Create some users.
        staff = UserFactory(short_name='Amelia', serial='123456',
                            password='password', is_staff=True)
        UserFactory(short_name='Amy', password='password')
        UserFactory(short_name='Touria', password='password')

        # Create some blog content.
        text = ('The last voice transmission received on Howland Island from '
                'Earhart indicated she and Noonan were flying along a line of '
                'position (taken from a "sun line" running on 157-337 degrees)'
                ' which Noonan would have calculated and drawn on a chart as '
                'passing through Howland. After all contact was lost with '
                'Howland Island, attempts were made to reach the flyers with '
                'both voice and Morse code transmissions. Operators across the'
                ' Pacific and the United States may have heard signals from '
                'the downed Electra but these were unintelligible or weak')
        ContentFactory(title='1937 world flight', text=text, summary=text,
                       status=Content.PUBLISHED, author=staff,
                       tags=['plane', 'aviation'],
                       image__from_path='ideascube/tests/data/amelia-earhart.jpg')  # noqa
        ContentFactory(title='This is another article with a longer title',
                       text=text, summary=text, status=Content.PUBLISHED,
                       author=staff, image=None)
        title = ('The Untold Story of Thirteen American Women and the Dream '
                 'of Space Flight')
        ContentFactory(title=title, text=text, summary=text,
                       status=Content.PUBLISHED, author=staff,
                       tags=['plane', 'aviation', 'record'],
                       image__from_path='ideascube/tests/data/plane.jpg')
        ContentFactory(title='This is a draft content', text=text,
                       status=Content.DRAFT, author=staff, image=None)
        ContentFactory(title='This is a deleted content', text=text,
                       status=Content.DELETED, author=staff, image=None)

        # Create some books.
        summary = ("If one chanced to examine the catalogues of Kingsbridge "
                   "College for the past hundred years it would be found that "
                   "in most of them is recorded the name of some dead and "
                   "gone Deering-a name famous in the annals of the South-who "
                   "came up from Louisiana, 'marched through the four long "
                   "happy years of college,' as the old song has it, with an "
                   "arts degree to his credit; or, perchance, marched out at "
                   "the end of one or two of them with nothing to his credit "
                   "at all. Kingsbridge was a tradition in the Deering "
                   "family, southern though it was-a tradition that was "
                   "hardly broken, even when in 1861 Victor Deering and a "
                   "hundred other chivalrous youths threw their text-books "
                   "out of the windows and enlisted in the armies of the "
                   "Confederacy. Victor's father, Basil, too, was in the "
                   "war, and laid down his arms at Appomattox as a "
                   "brigadier-general-brevetted for gallantry on the field of "
                   "action. For a while it seemed that no Deerings would go "
                   "to Kingsbridge, but time at length healed the old "
                   "antagonisms, and when it became a question where young "
                   "Anthony, Victor's boy, should go to[2] college, there was "
                   "no longer any question that Kingsbridge should be the "
                   "place.")
        path = 'ideascube/tests/data/deering-of-deal.jpg'
        book = BookFactory(name='Deering of Deal', description=summary,
                           subtitle='The Spirit of the School',
                           authors=u'Latta Griswold', lang='en',
                           cover__from_path=path, tags=['plane', 'aviation'])
        BookSpecimenFactory(item=book, barcode="1234567")
        summary = (u"Le roman raconte les aventures d'un Gascon impécunieux "
                   u"de 18 ans, d'Artagnan, venu à Paris pour faire carrière "
                   u"dans le corps des mousquetaires. Il se lie d'amitié avec "
                   u"Athos, Porthos et Aramis, mousquetaires du roi Louis "
                   u"XIII. Ces quatre hommes vont s'opposer au premier "
                   u"ministre, le cardinal de Richelieu et à ses agents, dont "
                   u"le comte de Rochefort et la belle et mystérieuse Milady "
                   u"de Winter, pour sauver l'honneur de la reine de France "
                   u"Anne d'Autriche.")
        path = 'ideascube/tests/data/les-trois-mousquetaires.jpg'
        book = BookFactory(name='Les Trois Mousquetaires', description=summary,
                           authors=u'Alexandre Dumas', lang='fr',
                           cover__from_path=path, tags=['roman', 'aventure'])
        BookSpecimenFactory(item=book, barcode="98765479")
        summary = ("With the title of Sense and Sensibility is connected one "
                   "of those minor problems which delight the cuemmin-splitters"
                   " of criticism. In the Cecilia of Madame D'Arblay-the "
                   "forerunner, if not the model, of Miss Austen-is a "
                   "sentence which at first sight suggests some relationship "
                   "to the name of the book which, in the present series, "
                   "inaugurated Miss Austen's novels. 'The whole of this "
                   "unfortunate business'-says a certain didactic Dr. Lyster, "
                   "talking in capitals, towards the end of volume three of "
                   "Cecilia-'has been the result of Pride and Prejudice,' and "
                   "looking to the admitted familiarity of Miss Austen with "
                   "Madame D'Arblay's work, it has been concluded that Miss "
                   "Austen borrowed from Cecilia, the title of her second "
                   "novel.")
        path = 'ideascube/tests/data/sense-and-sensibility.jpg'
        book = BookFactory(name='Sense and Sensibility', description=summary,
                           authors=u'Jane Austen', lang='en',
                           cover__from_path=path, tags=['19th-century'])
        BookSpecimenFactory(item=book, barcode="32657324")
        summary = (u"النبي (1923) أشهر كتب جبران كتبه بالإنجليزية وترجم إلى "
                   u"أكثر من خمسين لغة، وهو يعتبر بحق رائعة جبران العالمية، "
                   u"مضمونه اجتماعي، مثالي وتأملي فلسفي، وهو يحوي خلاصة "
                   u"الاراء الجبرانية في الحب والزواج والأولاد والبيوت "
                   u"والثياب والبيع والشراء والحرية والثانون والرحمة والعقاب "
                   u"والدين والأخلاق والحياة والموت واللذة والجمال والكرم "
                   u"والشرائع وغيرها، وقد وردت على لسان نبي سمي المصطفى "
                   u"ورسالة النبي رسالة المتصوف المؤمن بوحدة الوجود، وبأن "
                   u"الروح تتعطش للعودة إلى مصدرها، وبأن الحب جوهر الحياة. "
                   u"وفي كتاب النبي يعبر جبران عن آرائه في الحياة عن طريق "
                   u"معالجته للعلاقات الإنسانية التي تربط الإنسان بالإنسان.")
        path = 'ideascube/tests/data/the-prophet.jpg'
        book = BookFactory(name=u'النبي (كتاب)', description=summary,
                           authors=u'جبران خليل جبران', lang='ar',
                           cover__from_path=path)
        BookSpecimenFactory(item=book, barcode="3213542")
        title = (u"Déclaration des droits de l'homme et du citoyen "
                 u"du 26 août 1789")
        summary = (u"Les Représentants du Peuple Français, constitués en "
                   u"Assemblée Nationale, considérant que l'ignorance, "
                   u"l'oubli ou le mépris des droits de l'Homme sont les "
                   u"seules causes des malheurs publics et de la corruption "
                   u"des Gouvernements, ont résolu d'exposer, dans une "
                   u"Déclaration solennelle, les droits naturels, "
                   u"inaliénables et sacrés de l'Homme, afin que cette "
                   u"Déclaration, constamment présente à tous les Membres du "
                   u"corps social, leur rappelle sans cesse leurs droits et "
                   u"leurs devoirs ; afin que leurs actes du pouvoir "
                   u"législatif, et ceux du pouvoir exécutif, pouvant être à "
                   u"chaque instant comparés avec le but de toute institution "
                   u"politique, en soient plus respectés ; afin que les "
                   u"réclamations des citoyens, fondées désormais sur des "
                   u"principes simples et incontestables, tournent toujours "
                   u"au maintien de la Constitution et au bonheur de tous.")
        path = 'ideascube/tests/data/declaration-1789.pdf'
        DocuementFactory(title=title, original__from_path=path, summary=summary,
                        tags=['history', 'France'])
        path = 'ideascube/tests/data/amelia-earhart.jpg'
        DocuementFactory(title="Picture of Amelia Earhart",
                        original__from_path=path, tags=['plane', 'aviation'])
        self.stdout.write('Done.')

Example 147

Project: django-oauthost Source File: tests.py
Function: test_auth
    def test_auth(self):

        # User is not logged in.
        resp = self.client.get(URL_AUTHORIZE, {'client_id': '100'})
        self.assertEqual(resp.status_code, 302)

        user_1 = User(username='Fred')
        user_1.set_password('12345')
        user_1.save()

        # Logging the user in.
        self.client.login(username='Fred', password='12345')

        # Secure connection check
        settings.DEBUG = False
        resp = self.client.get(URL_AUTHORIZE, {})
        self.assertEqual(resp.status_code, 403)
        settings.DEBUG = True

        client_1 = Client(user=user_1, title='OClient', identifier='cl012345')
        client_1.save()

        client_2 = Client(user=user_1, title='OGOClient')
        client_2.save()

        redirect_1 = RedirectionEndpoint(client=client_1, uri='http://redirect-test.com')
        redirect_1.save()

        redirect_2 = RedirectionEndpoint(client=client_2, uri='http://redirect-test1.com')
        redirect_2.save()

        redirect_3 = RedirectionEndpoint(client=client_2, uri='http://redirect-test2.com')
        redirect_3.save()

        Scope(identifier='scope1').save()

        # Missing client id.
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'response_type': 'code', 'scope': 'scope1'})
        self.assertEqual(resp.status_code, 400)

        # Invalid client id.
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'response_type': 'code', 'scope': 'scope1', 'client_id': 'invalid'})
        self.assertEqual(resp.status_code, 400)

        # Client 2 - No redirect URI in request.
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'response_type': 'code', 'scope': 'scope1',
                                               'client_id': client_2.identifier})
        self.assertEqual(resp.status_code, 400)

        # Client 2 - Unknown URI in request.
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'response_type': 'code', 'scope': 'scope1',
                                               'redirect_uri': 'http://noitisnot.com',
                                               'client_id': client_2.identifier})
        self.assertEqual(resp.status_code, 400)

        # Missing response type.
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'client_id': client_1.identifier, 'state': 'abc', 'scope': 'scope1'})
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(parse_location_header(resp)['error'], 'unsupported_response_type')
        self.assertEqual(parse_location_header(resp)['state'], 'abc')

        # Wrong response type
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'client_id': client_1.identifier, 'response_type': 'habrahabr',
                                               'state': 'abc', 'scope': 'scope1'})
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(parse_location_header(resp)['error'], 'unsupported_response_type')
        self.assertEqual(parse_location_header(resp)['state'], 'abc')

        # Valid code request.
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'response_type': 'code', 'scope': 'scope1', 'state': 'somestate',
                                               'client_id': client_1.identifier})
        self.assertEqual(resp.status_code, 200)

        # User declines auth.
        resp = self.client.post(URL_AUTHORIZE, {'auth_decision': 'is_made'})
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(parse_location_header(resp)['error'], 'access_denied')

        # Again Valid code request.
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'response_type': 'code', 'scope': 'scope1', 'state': 'somestatetwo',
                                               'client_id': client_1.identifier})
        self.assertEqual(resp.status_code, 200)

        # User confirms auth.
        resp = self.client.post(URL_AUTHORIZE, {'auth_decision': 'is_made', 'confirmed': 'yes'})
        self.assertEqual(resp.status_code, 302)
        self.assertIn('code', parse_location_header(resp))
        self.assertEqual(parse_location_header(resp)['state'], 'somestatetwo')

        # ============= Implicit grant tests.

        # Valid token request.
        self.client.login(username='Fred', password='12345')
        resp = self.client.get(URL_AUTHORIZE, {'response_type': 'token', 'scope': 'scope1',
                                               'state': 'some_state_three', 'client_id': client_1.identifier})
        self.assertEqual(resp.status_code, 200)

        # User confirms token grant.
        resp = self.client.post(URL_AUTHORIZE, {'auth_decision': 'is_made', 'confirmed': 'yes'})
        self.assertEqual(resp.status_code, 302)
        params = parse_location_header(resp, True)
        self.assertIn('access_token', params)
        self.assertIn('token_type', params)
        self.assertEqual(params['state'], 'some_state_three')

Example 148

Project: test-utils Source File: fixture_tables.py
Function: tables_used_by_fixtures
def tables_used_by_fixtures(fixture_labels, using=DEFAULT_DB_ALIAS):
    """Act like Django's stock loaddata command, but, instead of loading data,
    return an iterable of the names of the tables into which data would be
    loaded."""
    # Keep a count of the installed objects and fixtures
    fixture_count = 0
    loaded_object_count = 0
    fixture_object_count = 0
    tables = set()

    class SingleZipReader(zipfile.ZipFile):
        def __init__(self, *args, **kwargs):
            zipfile.ZipFile.__init__(self, *args, **kwargs)
            if settings.DEBUG:
                assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file."
        def read(self):
            return zipfile.ZipFile.read(self, self.namelist()[0])

    compression_types = {
        None: file,
        'gz': gzip.GzipFile,
        'zip': SingleZipReader
    }
    if has_bz2:
        compression_types['bz2'] = bz2.BZ2File

    app_module_paths = []
    for app in get_apps():
        if hasattr(app, '__path__'):
            # It's a 'models/' subpackage
            for path in app.__path__:
                app_module_paths.append(path)
        else:
            # It's a models.py module
            app_module_paths.append(app.__file__)

    app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]
    for fixture_label in fixture_labels:
        parts = fixture_label.split('.')

        if len(parts) > 1 and parts[-1] in compression_types:
            compression_formats = [parts[-1]]
            parts = parts[:-1]
        else:
            compression_formats = compression_types.keys()

        if len(parts) == 1:
            fixture_name = parts[0]
            formats = serializers.get_public_serializer_formats()
        else:
            fixture_name, format = '.'.join(parts[:-1]), parts[-1]
            if format in serializers.get_public_serializer_formats():
                formats = [format]
            else:
                formats = []

        if not formats:
            # stderr.write(style.ERROR("Problem installing fixture '%s': %s is
            # not a known serialization format.\n" % (fixture_name, format)))
            return set()

        if os.path.isabs(fixture_name):
            fixture_dirs = [fixture_name]
        else:
            fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']

        for fixture_dir in fixture_dirs:
            # stdout.write("Checking %s for fixtures...\n" %
            # humanize(fixture_dir))

            label_found = False
            for combo in product([using, None], formats, compression_formats):
                database, format, compression_format = combo
                file_name = '.'.join(
                    p for p in [
                        fixture_name, database, format, compression_format
                    ]
                    if p
                )

                # stdout.write("Trying %s for %s fixture '%s'...\n" % \
                # (humanize(fixture_dir), file_name, fixture_name))
                full_path = os.path.join(fixture_dir, file_name)
                open_method = compression_types[compression_format]
                try:
                    fixture = open_method(full_path, 'r')
                    if label_found:
                        fixture.close()
                        # stderr.write(style.ERROR("Multiple fixtures named
                        # '%s' in %s. Aborting.\n" % (fixture_name,
                        # humanize(fixture_dir))))
                        return set()
                    else:
                        fixture_count += 1
                        objects_in_fixture = 0
                        loaded_objects_in_fixture = 0
                        # stdout.write("Installing %s fixture '%s' from %s.\n"
                        # % (format, fixture_name, humanize(fixture_dir)))
                        try:
                            objects = serializers.deserialize(format, fixture, using=using)
                            for obj in objects:
                                objects_in_fixture += 1
                                if router.allow_syncdb(using, obj.object.__class__):
                                    loaded_objects_in_fixture += 1
                                    tables.add(
                                        obj.object.__class__._meta.db_table)
                            loaded_object_count += loaded_objects_in_fixture
                            fixture_object_count += objects_in_fixture
                            label_found = True
                        except (SystemExit, KeyboardInterrupt):
                            raise
                        except Exception:
                            fixture.close()
                            # stderr.write( style.ERROR("Problem installing
                            # fixture '%s': %s\n" % (full_path, ''.join(tra
                            # ceback.format_exception(sys.exc_type,
                            # sys.exc_value, sys.exc_traceback)))))
                            return set()
                        fixture.close()

                        # If the fixture we loaded contains 0 objects, assume that an
                        # error was encountered during fixture loading.
                        if objects_in_fixture == 0:
                            # stderr.write( style.ERROR("No fixture data found
                            # for '%s'. (File format may be invalid.)\n" %
                            # (fixture_name)))
                            return set()

                except Exception:
                    # stdout.write("No %s fixture '%s' in %s.\n" % \ (format,
                    # fixture_name, humanize(fixture_dir)))
                    pass

    return tables

Example 149

Project: intranet Source File: spidertests.py
Function: new
    def __new__(mcs, class_name, bases, namespace):
        urls_file = namespace.get('urls_file', None)
        if urls_file is not None:
            config = ConfigParser.ConfigParser()
            config.read(urls_file)
            maxqueries = None
            if config.has_option('GENERAL', 'maxqueries'):
                maxqueries = int(config.get('GENERAL', 'maxqueries'))

            login = None
            if config.has_option('GENERAL', 'login'):
                login = config.get('GENERAL', 'login')

            def method_factory(url, http_method, data, login, maxqueries):

                def test_method(self):
                    return self._test_url(url, http_method, data, login, maxqueries)
                return test_method

            for method in ('GET', 'POST'):
                if not config.has_section(method):
                    continue

                urls = [option for option in config.options(method)
                        if option.startswith('url')]
                urls.sort()
                for url_name in urls:
                    url = config.get(method, url_name)
                    index = url_name[3:]

                    data = {}
                    if config.has_option(method, 'data%s' % index):
                        value = config.get(method, 'data%s' % index)
                        data = read_data(value)

                    login_local = None
                    if config.has_option(method, 'login%s' % index):
                        login_local = config.get(method, 'login%s' % index)
                    else:
                        login_local = login

                    maxqueries_local = None
                    if config.has_option(method, 'maxqueries%s' % index):
                        maxqueries_local = config.get(method, 'maxqueries%s' % index)
                    else:
                        maxqueries_local = maxqueries

                    if maxqueries_local >= 0:
                        settings.DEBUG = True
                    else:
                        settings.DEBUG = False

                    func = method_factory(url, method, data, login_local, maxqueries_local)
                    name = 'test_%s_%s' % (method.lower(), url)
                    func.__name__ = name
                    while name in namespace:
                        name += '_'
                    namespace[name] = func

        return type.__new__(mcs, class_name, bases, namespace)

Example 150

Project: addons-server Source File: middleware.py
    def process_request(self, request):
        # Find locale, app
        prefixer = urlresolvers.Prefixer(request)
        if settings.DEBUG:
            redirect_type = HttpResponseRedirect
        else:
            redirect_type = HttpResponsePermanentRedirect
        urlresolvers.set_url_prefix(prefixer)
        full_path = prefixer.fix(prefixer.shortened_path)

        if (prefixer.app == amo.MOBILE.short and
                request.path.rstrip('/').endswith('/' + amo.MOBILE.short)):
            # TODO: Eventually put MOBILE in RETIRED_APPS, but not yet.
            return redirect_type(request.path.replace('/mobile', '/android'))

        if ('lang' in request.GET and not prefixer.shortened_path.startswith(
                settings.SUPPORTED_NONAPPS_NONLOCALES_PREFIX)):
            # Blank out the locale so that we can set a new one.  Remove lang
            # from query params so we don't have an infinite loop.
            prefixer.locale = ''
            new_path = prefixer.fix(prefixer.shortened_path)
            query = dict((force_bytes(k), request.GET[k]) for k in request.GET)
            query.pop('lang')
            return redirect_type(urlparams(new_path, **query))

        if full_path != request.path:
            query_string = request.META.get('QUERY_STRING', '')
            full_path = urllib.quote(full_path.encode('utf-8'))

            if query_string:
                query_string = query_string.decode('utf-8', 'ignore')
                full_path = u'%s?%s' % (full_path, query_string)

            response = redirect_type(full_path)
            # Cache the redirect for a year.
            if not settings.DEBUG:
                patch_cache_control(response, max_age=60 * 60 * 24 * 365)

            # Vary on Accept-Language or User-Agent if we changed the locale or
            # app.
            old_app = prefixer.app
            old_locale = prefixer.locale
            new_locale, new_app, _ = prefixer.split_path(full_path)

            if old_locale != new_locale:
                patch_vary_headers(response, ['Accept-Language'])
            if old_app != new_app:
                patch_vary_headers(response, ['User-Agent'])
            return response

        request.path_info = '/' + prefixer.shortened_path
        request.LANG = prefixer.locale or prefixer.get_language()
        activate(request.LANG)
        request.APP = amo.APPS.get(prefixer.app, amo.FIREFOX)
See More Examples - Go to Next Page
Page 1 Page 2 Page 3 Selected Page 4