Code Monkey home page Code Monkey logo

django-app-helper's Introduction

Django App helper

Join the Gitter chat Latest PyPI version Python versions Latest CI build status Test coverage Code Climate License

Warning

Starting from 3.3 django-app-helper only supports Django 3.2+ and django CMS 3.11. If you need support for older (unsupported) versions, use django-app-helper<3.3

Starting from 3 django-app-helper only supports Django 2.2+ and django CMS 3.7+. If you need support for older (unsupported) versions, use django-app-helper 2.

Helper for django applications development

django-app-helper is a set of commands and helper methods to make developing and testing reusable Django applications easier.

Being born in the django CMS ecosystem, it provides a lot of utility functions to develop, run and test django CMS applications.

It's a modified version of django CMS's own develop.py script, modified to handle generic application development process.

It supports both tests writted using Django TestCase and pytest ones (see pytest support).

Supported versions

Python: 3.8, 3.9, 3.10, 3.11, 3.12

Django: 3.2, 4.0, 4.1, 4.2, 5.0

django CMS: 3.11

Newer versions might work but are not tested yet.

Common options

  • --cms: Loads configuration to properly run a django CMS-based application;
  • --extra-settings: Path to a helper file to set extra settings; see Project settings with Django App Helper for details;

Usage

The command must be executed in the main plugin directory (i.e. in the same directory as the setup.py file) as it needs to import files relative to the current directory.

The basic command structure is:

django-app-helper <application> <command> [options ...]

where <application> is the django application name and <command> is one of the available commands. Options vary for each command.

Base test class

A base test class is available to provide helpers and methods that implements repetitive tasks during development or compatibility shims (especially for django CMS).

Bootstrapping

To bootstrap a project using django-app-helper you may want to have a look at cookiecutter-djangopackage-helper, a cookiecutter template for django-app-helper.

To use it follows usage

Runner

By using the integrated runned in the settings file you'll be able to run the commands without invoking django-app-helper: see Integrated runner for reference.

ASGI / Channels

ASGI / Channels are supported by installing the project with django-app-helper[async].

With Daphne / Channels installed you can run django-app-helper server --use-daphne|--use-channels to run the project on ASGI.

See ASGI / Channels support

Pure ASGI support is available only for Django 3.0+.

Installation

Installing from pip:

pip install django-app-helper

Installing from source:

pip install git+https://github.com/nephila/django-app-helper#egg=django-app-helper

Requirements

  • django CMS optional; required only to work with --cms option
  • docopt
  • tox
  • dj-database-url

Documentation

Documentation is available on readthedocs.

Authors

django-app-helper was written by Iacopo Spalletti with help from other contributors.

Thanks

The general logic and part of the code of the whole application is heavily taken from django CMS's own develop.py so all the contributors deserve a huge thanks for their work.

django-app-helper's People

Contributors

astagi avatar chronossc avatar dependabot-preview[bot] avatar eraldo avatar evildmp avatar jrief avatar pre-commit-ci[bot] avatar protoroto avatar yakky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

django-app-helper's Issues

Updates required for new python/django

Description

Updates to the app are required for new python/django so that apps which depend on this application can continue to develop.

I've made a start on this, but require an issue to reference in my branch.

Proposed solution

Update tox/github workflows & then take it from there.

Django 1.7 Support

I'm not sure if something else is wrong with my setup. But with Django 1.7 I get an errror in main.py:

https://github.com/nephila/djangocms-helper/blob/ad3b187774c00744f4f6daba166e33e1dc5fcd99/djangocms_helper/main.py#L227

Creating test database for alias 'default'...
Traceback (most recent call last):
  File "/path/to/my/app/.tox/py27-dj17/bin/djangocms-helper", line 11, in <module>
    sys.exit(main())
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/djangocms_helper/main.py", line 327, in main
    core(args=args, application=application)
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/djangocms_helper/main.py", line 293, in core
    args['--runner-options'])
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/djangocms_helper/main.py", line 88, in test
    return _test_run_worker(test_labels, test_runner, failfast, runner_options)
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/djangocms_helper/main.py", line 72, in _test_run_worker
    failures = test_runner.run_tests(test_labels)
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/test/runner.py", line 147, in run_tests
    old_config = self.setup_databases()
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/test/runner.py", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/test/runner.py", line 299, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/db/backends/creation.py", line 377, in create_test_db
    test_flush=True,
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/core/management/__init__.py", line 93, in call_command
    app_name = get_commands()[name]
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/core/management/__init__.py", line 73, in get_commands
    for app_config in reversed(list(apps.get_app_configs())):
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/path/to/my/app/.tox/py27-dj17/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
AppRegistryNotReady: Apps aren't loaded yet.

cms settings + extra settings with empty installed apps => no installed apps

Hey @yakky

Just discovered the following:

If you're running the tool with the following command:

djangocms-helper` aldryn_snippet test --cms --extra-settings=test_settings

with this content in the file test_settings.py:

HELPER_SETTINGS = {
    'INSTALLED_APPS': [],
}

you will end up with this exception: ImproperlyConfigured: django CMS requires django-mptt package.

When I remove the INSTALLED_APPS section from the HELPER_SETTINGS it's working fine. I'm not quite sure if this is intentional, but anyway kind of misleading in my opinion :)

Adopt pre-commit

Description

Add support for pre-commit to ease code formatting

Use cases

Time spent formatting code manually is wasted time

Proposed solution

Adopt a very strict pre-commit configuration to speed up development

Add explicit channels / ASGI support

While minimal channels support is already included, is not advertised or supported by declared requirements

Improve this by:

  • add async extra requirements
  • document how to run server command with channels

invoking the server command with --boilerplate option doesn't configures boilerplates

Example of a failure https://travis-ci.org/aldryn/aldryn-newsblog/jobs/77465895#L733
the configuration of that particular test https://github.com/aldryn/aldryn-boilerplates/pull/14/files#diff-2b5bde3dd5ae5bc227010549100ba6a8R27

The issue is related to the fact that django runserver command spwans a new process/thread which gets non unicoded arguments in sys.argv.
Also if some of the arguments was passed twice (or was duplicated because of that spawn) docopt wasn't able to map arguments correctly.

Fix pytest 7.1+ support

Integrated pytest runner does not work for pytest 7.1+

That might be related to some pytest internal changes about conftest.py

Incorrect TEMPLATE_CONTEXT_PROCESSORS on Django1.8

On Django==1.8 + djangoCMS 3.1.1+, django.conf.settings.TEMPLATES['context_processors'] returns a tuple with template processors that correspond to previous versions of django. For eg: django.core.context_processors.request instead of django.template.context_processors.request.
How do we maintain compatibility over different django versions.
Should we perform a string replace of django.core to django.template here.

        default_settings['TEMPLATES'] = [
            {
                'NAME': 'django',
                'BACKEND': 'django.template.backends.django.DjangoTemplates',
                'APP_DIRS': True,
                'OPTIONS': {
                    'context_processors': [
                        template_processor.replace('django.core', 'django.template')
                        for template_processor in default_settings.pop('TEMPLATE_CONTEXT_PROCESSORS')
                    ]
                }
            }
        ]

Also, django.conf.settings.TEMPLATE_CONTEXT_PROCESSORS returns a tuple with no CMS or sekizai context processors included with the same incorrect context processors as noted above.

move to django-app-manage

Use django-app-manage to implement support for any command.
djangocms-helper thus can focus on providing a rich environment to help testing djangocms applications and plugins

Move away from docopt

Description

docopt is no longer maintained and we need to move away from it

Successor not picked, yet.

Alternatives:

  • click
  • argparse
  • ... ?

PYTEST_ARGS does not support space separated arguments

Description

A command line like PYTEST_ARGS="-m 'not nopy27' -p no:warnings" ./cms_helper.py test --failfast is currently not supported

Versions

Since introduction of PYTEST_ARGS

Expected behaviour

arguments including quoted spaces should not be splitted.

i.e.: -m 'not nopy27' -p no:warnings should become `["-m", "'not nopy27'", "-p", "no:warnings"]

Actual behaviour

-m 'not nopy27' -p no:warnings is parsed as `["-m", "'not", "nopy27'", "-p", "no:warnings"]

Support for tagged tests

It seems tagging tests is not supported yet. Is this a planned/desired feature or outside the scope of this helper? I've tried passing --exclude-tag=slow via --runner-options to no avail.

Bug: django-app-helper test does not process --runner-options properly

We use django-app-helper to run our tests. Some of them are not properly working within the django-app-helper environment so I tried to exclude them by passing --exclude-tag ... to --runner-options.

Unfortunately, it seems like django-app-helper generally does not pass --runner-options to the test runner properly because running django-app-helper <APP> test --runner-options="foo" results in the following output:

usage: django-app-helper [-h] [-t TOP_LEVEL] [-p PATTERN] [--keepdb] [-r] [--debug-mode] [-d] [--parallel [N]] [--tag TAGS] [--exclude-tag EXCLUDE_TAGS] [--pdb] [-b]
[--no-faulthandler] [--timing] [-k TEST_NAME_PATTERNS]
django-app-helper: error: unrecognized arguments: f o o

Same goes for --runner-options="--exclude-tag someTag"
django-app-helper: error: unrecognized arguments: - - e x c l u d e - t a g s o m e T a g

We are using django-app-helper 3.2.0.

If --boilerplate option is used boilerplate name from helper settings is overiden with default

if you use --boilerplate option get_boilerplate_settings will return the default value for ALDRYN_BOILERPLATE_NAME which would be 'bootstrap3', and settings are updated with default value even if helper settings contains other boilerplate name.

Hotfix for me was to add a check if default_settings at that point already has a boilerplate name - remove the default value (as it is done with other default values) :

# Do not override helper settings with defaults.
if 'ALDRYN_BOILERPLATE_NAME' in default_settings.keys():
    del boilerplate_settings['ALDRYN_BOILERPLATE_NAME']

The code was inserted here https://github.com/nephila/djangocms-helper/blob/develop/djangocms_helper/utils.py#L266

Will do a PR with this fix.

error when running server command with channels 2.0

Description

running server command in a channels-enabled project exits with error

## Steps to reproduce

run python app_helper.py server in a project with channels

Versions

2.2

Expected behaviour

server command runs normally

Actual behaviour

server command exits with error

Tweak linting

Description

Update linting configuration by:

  • sync pre-commit / tox flake8 configuration
  • add experimental informative only interrogate support
  • add dependabot configuration
  • update towncrier check

Reorganize test outside main package

Description

Move tests and test applications outside app_helper package; we don't need them in distributed package and they add too much unnecessary code

Replace mock package with unittest.mock

From what I can see, this package does not support any version of python that does not come with mock in it's standardlib. Is there any reason not use that?

My main problem is that this lists mock as a requirement, which combined with pytest-dev/pytest-mock#54 now messes with my tests :).

Add towncrier file check to pre-commit / actions

Description

Add a check to ensure a towncrier file is added on each PR

Use cases

Whatever we can automate will make life easier for contributors and maintainers alike

Proposed solution

Add an invoke task and add it to tox and pre-commit

Runner does not load extra settings when not in default file

If the extra settings file is not called cms_helper.py when using the runner, the extra settings are not loaded.
They must be injected in the runner discovering the settings file name in some way (easy if the settings is called directly, not so easy if configured as test_suite and run with python setup.py test)

Add Django 4.0/4.1/4.2 support

To provide full Django 4.0/4.1 support we need django CMS to support them

We can add preliminary support for tasks not involving django CMS, though

create_filer_image_object() bug

I am using djangocms-helper for running tests in my project and it works very well.
The only problem is, that I have to set 'FILE_UPLOAD_TEMP_DIR' to '/tmp' (or '/tmp/') in the settings.py or the additional helper-settings. The django-doc itself says, that if FILE_UPLOAD_TEMP_DIR is set to 'None' (which is the default), it takes the default temp folder of the system (/tmp on *nix).
If you dont override this value, you get an error from the BaseTestCase in base_test.py. There, the 'create_filer_image_object()' looks at the FILE_UPLOAD_TEMP_DIR from django.conf.settings and joins it with an image_name.
If it is set to 'None', I get the following error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/djangocms_helper/base_test.py", line 435, in create_filer_image_object
    file_obj = self.create_django_image_object()
  File "/usr/local/lib/python3.6/site-packages/djangocms_helper/base_test.py", line 415, in create_django_image_object
    self.filename = os.path.join(settings.FILE_UPLOAD_TEMP_DIR, self.image_name)
  File "/usr/local/lib/python3.6/posixpath.py", line 78, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType

In my opinion, the method behaves different than django does itself. It would be really great if you can find a good solution for this problem.

Fix isort 5 support

Description

isort 5 brings incompatible changes that needs fixes in the codebase

Push releases via CI

By allowing release creation via CI we will lower the barrier for releasing new versions

Rename to django-app-helper

This is much more widely useful than just django CMS applications
Change name to reflect this

Backward compatibility will be ensured for the time being

Drop Python 2

Description

Drop Python 2 / Django 1.11 and its related code

Adopt pre-commit to ease the transition

Use cases

Python 2 / Django 1.11 are no longer supported

Improve compatibility with pytest

Description

By setting TEST_RUNNER="app_helper.pytest_runner.PytestTestRunner" you can run pytest tests with django-app-helper by executing helper file.

It would be great to be able to run pytest with standard pytest command line and still benefit from django-app-helper settings

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.