pax_global_header00006660000000000000000000000064146572010150014513gustar00rootroot0000000000000052 comment=b688ac9b056c778d20d66e2970238cea8dde5706 django-swappable-models-1.4.0/000077500000000000000000000000001465720101500162145ustar00rootroot00000000000000django-swappable-models-1.4.0/.github/000077500000000000000000000000001465720101500175545ustar00rootroot00000000000000django-swappable-models-1.4.0/.github/ISSUE_TEMPLATE/000077500000000000000000000000001465720101500217375ustar00rootroot00000000000000django-swappable-models-1.4.0/.github/ISSUE_TEMPLATE/bug_report.md000066400000000000000000000011441465720101500244310ustar00rootroot00000000000000--- name: Bug report about: Open a bug report title: "[bug] " labels: bug assignees: '' --- **Describe the bug** A clear and concise description of the bug or unexpected behavior. **Steps To Reproduce** Steps to reproduce the behavior: 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' 4. See error **Expected behavior** A clear and concise description of what you expected to happen. **Screenshots** If applicable, add screenshots to help explain your problem. **System Informatioon:** - OS: [e.g. Ubuntu 24.04 LTS] - Python Version: [e.g. Python 3.11.2] - Django Version: [e.g. Django 4.2.5] django-swappable-models-1.4.0/.github/ISSUE_TEMPLATE/feature_request.md000066400000000000000000000011461465720101500254660ustar00rootroot00000000000000--- name: Feature request about: Suggest an idea for this project title: "[feature] " labels: enhancement assignees: '' --- **Is your feature request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] **Describe the solution you'd like** A clear and concise description of what you want to happen. **Describe alternatives you've considered** A clear and concise description of any alternative solutions or features you've considered. **Additional context** Add any other context or screenshots about the feature request here. django-swappable-models-1.4.0/.github/ISSUE_TEMPLATE/question.md000066400000000000000000000005171465720101500241330ustar00rootroot00000000000000--- name: Question about: Please use the Discussion Forum to ask questions title: "[question] " labels: question assignees: '' --- Please use the [Discussion Forum](https://github.com/openwisp/django-swappable-models/discussions) to ask questions. We will take care of moving the discussion to a more relevant repository if needed. django-swappable-models-1.4.0/.github/dependabot.yml000066400000000000000000000014741465720101500224120ustar00rootroot00000000000000# To get started with Dependabot version updates, you'll need to specify which # package ecosystems to update and where the package manifests are located. # Please see the documentation for all configuration options: # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file version: 2 updates: - package-ecosystem: "pip" # See documentation for possible values directory: "/" # Location of package manifests schedule: interval: "monthly" commit-message: prefix: "[deps] " - package-ecosystem: "github-actions" # Check for GitHub Actions updates directory: "/" # The root directory where the Ansible role is located schedule: interval: "monthly" # Check for updates weekly commit-message: prefix: "[ci] " django-swappable-models-1.4.0/.github/pull_request_template.md000066400000000000000000000012171465720101500245160ustar00rootroot00000000000000## Checklist - [ ] I have read the [OpenWISP Contributing Guidelines](http://openwisp.io/docs/developer/contributing.html). - [ ] I have manually tested the changes proposed in this pull request. - [ ] I have written new test cases for new code and/or updated existing tests for changes to existing code. - [ ] I have updated the documentation. ## Reference to Existing Issue Closes #. Please [open a new issue](https://github.com/openwisp/django-swappable-models/issues/new/choose) if there isn't an existing issue yet. ## Description of Changes Please describe these changes. ## Screenshot Please include any relevant screenshots. django-swappable-models-1.4.0/.github/workflows/000077500000000000000000000000001465720101500216115ustar00rootroot00000000000000django-swappable-models-1.4.0/.github/workflows/ci.yml000066400000000000000000000017771465720101500227430ustar00rootroot00000000000000name: Django Swappable Models Build on: push: branches: - master pull_request: branches: - master jobs: build: name: Python==${{ matrix.python-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: python-version: - "3.8" - "3.9" - "3.10" - "3.11" steps: - uses: actions/checkout@v4 with: ref: ${{ github.event.pull_request.head.sha }} - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies id: deps run: | pip install -U pip wheel setuptools pip install tox tox-gh-actions pip install openwisp-utils[qa]@https://github.com/openwisp/openwisp-utils/tarball/master - name: QA checks run: ./run-qa-checks - name: Test if: ${{ !cancelled() && steps.deps.conclusion == 'success' }} run: tox django-swappable-models-1.4.0/.github/workflows/pypi.yml000066400000000000000000000013131465720101500233130ustar00rootroot00000000000000name: Publish Python Package to Pypi.org on: release: types: [published] permissions: id-token: write jobs: pypi-publish: name: Release Python Package on Pypi.org runs-on: ubuntu-latest environment: name: pypi url: https://pypi.org/p/swapper permissions: id-token: write steps: - uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.10' - name: Install dependencies run: | pip install -U pip pip install build - name: Build package run: python -m build - name: Publish package distributions to PyPI uses: pypa/gh-action-pypi-publish@v1.9.0 django-swappable-models-1.4.0/.gitignore000066400000000000000000000001021465720101500201750ustar00rootroot00000000000000*.pyc *.egg-info *.egg *.sw? build dist README.rst /.eggs/ /.tox/ django-swappable-models-1.4.0/CHANGES.rst000066400000000000000000000051721465720101500200230ustar00rootroot00000000000000Changelog ========= Version 1.4.0 [2024-08-14] -------------------------- Changes ~~~~~~~ Dependencies ++++++++++++ - Added support for Django ``4.2`` and ``5.0``. - Dropped support for Django ``Django 4.0a1``. - Added support for Python ``3.10`` and ``3.11``. - Dropped support for Python ``3.7``. Verson 1.3.0 [2021-11-29] ------------------------- - [change] Allow possibility to point swappable dependency to specific migration number (instead of only to ``__latest__``) Version 1.2.0 [2021-11-12] -------------------------- - [feature] Add possibility to point swappable dependency to ``__latest__`` - [change] Added support for Python 3.9 - [change] Added support for Django 3.2 and Django 4.0a1 - [change] Dropped support for old Django versions (<2.2) - [change] Dropped support for old Python versions (<3.7) - [feature] Added optional ``require_ready`` argument to ``load_model`` function Version 1.1.2 [2020-01-15] -------------------------- - [deps] Verified support for python 3.8 - [deps] Added support for Django 3.0 and Django Rest Framework 3.11 Version 1.1.1 [2019-07-23] -------------------------- - [deps] Drop python<3.3 support - [deps] Added support for python 3.7 - [deps] Django 2 support added Version 1.1.0 [2017-05-11] -------------------------- - [test] Added tests for swapper.split - `#13 `_ [fix] Handle contrib apps and apps with dot in app_label. Version 1.0.0 [2016-08-26] -------------------------- - [docs] Improved usuability docs - `86e238 `_: [deps] Compatibility with django 1.10 added Version 0.3.0 [2015-11-17] -------------------------- - `#9 `_ [deps] Added support for django 1.9 Version 0.2.2 [2015-06-16] -------------------------- - [deps] Added support for django~=1.6.0 - [deps] Added support for Python 3.3 - [docs] Fix model reference in README - [docs] Notes for load_model initialization (`for more info see #2 `_) Version 0.2.1 [2014-11-18] -------------------------- - [docs] Added examples for migration scripts - [docs] Documented use of Functions - [fix] Fixed Lookup Error in load_model Version 0.2.0 [2014-09-13] -------------------------- - [deps] Added support for Django 1.7 - [feature] Added `swapper.dependency` function. - [tests] Added tests Version 0.1.1 [2014-01-09] -------------------------- - [docs] Added References Version 0.1.0 [2014-01-09] -------------------------- - Added base functions for swapping models django-swappable-models-1.4.0/LICENSE000066400000000000000000000021301465720101500172150ustar00rootroot00000000000000Copyright (c) 2021, OpenWISP Copyright (c) 2014-2017, S. Andrew Sheppard, http://wq.io/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. django-swappable-models-1.4.0/README.md000066400000000000000000000265741465720101500175110ustar00rootroot00000000000000Swapper ======= [![Dependency monitoring](https://img.shields.io/librariesio/release/github/openwisp/django-swappable-models)](https://libraries.io/github/openwisp/django-swappable-models) #### Django Swappable Models - No longer only for auth.User! Swapper is an unofficial API for the [undocumented] but very powerful Django feature: swappable models. Swapper facilitates implementing arbitrary swappable models in your own reusable apps. [![Latest PyPI Release](https://img.shields.io/pypi/v/swapper.svg)](https://pypi.org/project/swapper) [![Release Notes](https://img.shields.io/github/release/openwisp/django-swappable-models.svg )](https://github.com/wq/django-swappable-models/releases) [![License](https://img.shields.io/pypi/l/swapper.svg)](https://github.com/openwisp/django-swappable-models/blob/master/LICENSE) [![GitHub Stars](https://img.shields.io/github/stars/openwisp/django-swappable-models.svg)](https://github.com/openwisp/django-swappable-models/stargazers) [![GitHub Forks](https://img.shields.io/github/forks/openwisp/django-swappable-models.svg)](https://github.com/openwisp/django-swappable-models/network) [![GitHub Issues](https://img.shields.io/github/issues/openwisp/django-swappable-models.svg)](https://github.com/openwisp/django-swappable-models/issues) [![Build Status](https://github.com/openwisp/django-swappable-models/actions/workflows/ci.yml/badge.svg)](https://github.com/openwisp/django-swappable-models/actions/workflows/ci.yml) [![Python Support](https://img.shields.io/pypi/pyversions/swapper.svg)](https://pypi.org/project/swapper) [![Django Support](https://img.shields.io/pypi/djversions/swapper.svg)](https://pypi.org/project/swapper) ## Motivation Suppose your reusable app has two related tables: ```python from django.db import models class Parent(models.Model): name = models.TextField() class Child(models.Model): name = models.TextField() parent = models.ForeignKey(Parent) ``` Suppose further that you want to allow the user to subclass either or both of these models and supplement them with their own additional fields. You could use Abstract classes (e.g. `BaseParent` and `BaseChild`) for this, but then you would either need to: 1. Avoid putting the foreign key on `BaseChild` and tell the user they need to do it. 2. Put the foreign key on `BaseChild`, but make `Parent` a concrete model that can't be swapped 3. Use swappable models, together with `ForeignKeys` that read the swappable settings. This third approach is taken by Django to facilitate [swapping the auth.User model]. The `auth.User` swappable code was implemented in a generic way that allows it to be used for any model. Although this capability is currently [undocumented] while any remaining issues are being sorted out, it has proven to be very stable and useful in our experience. Swapper is essentially a simple API wrapper around this existing functionality. Note that Swapper is primarily a tool for library authors; users of your reusable app generally should not need to know about Swapper in order to use it. (See the notes on [End User Documentation](#end-user-documentation) below.) ### Real-World Examples Swapper is used extensively in several OpenWISP packages to facilitate customization and extension. Notable examples include: * [openwisp-users] * [openwisp-controller] * [openwisp-radius] The use of swapper in these packages promotes [Software Reusability][reusability], one of the core values of the OpenWISP project. ## Creating a Reusable App First, make sure you have `swapper` installed. If you are publishing your reusable app as a Python package, be sure to add `swapper` to your project's dependencies (e.g. `setup.py`) to ensure that users of your app don't have errors integrating it. ```bash pip3 install swapper ``` Extending the above example, you might create two abstract base classes and corresponding default implementations: ```python # reusableapp/models.py from django.db import models import swapper class BaseParent(models.Model): # minimal base implementation ... class Meta: abstract = True class Parent(BaseParent): # default (swappable) implementation ... class Meta: swappable = swapper.swappable_setting('reusableapp', 'Parent') class BaseChild(models.Model): parent = models.ForeignKey(swapper.get_model_name('reusableapp', 'Parent')) # minimal base implementation ... class Meta: abstract = True class Child(BaseChild): # default (swappable) implementation ... class Meta: swappable = swapper.swappable_setting('reusableapp', 'Child') ``` ### Loading Swapped Models In your reusable views and other functions, always use the swapper instead of importing swappable models directly. This is because you might not know whether the user of your app is using your default implementation or their own version. ```python # reusableapp/views.py # Might work, might not # from .models import Parent import swapper Parent = swapper.load_model("reusableapp", "Parent") Child = swapper.load_model("reusableapp", "Child") def view(request, *args, **kwargs): qs = Parent.objects.all() # ... ``` > Note: `swapper.load_model()` is the general equivalent of [get_user_model()] and subject to the same constraints: e.g. it should not be used until after the model system has fully initialized. ### Migration Scripts Swapper can also be used in migration scripts to facilitate dependency ordering and foreign key references. To use this feature in your library, generate a migration script with `makemigrations` and make the following changes. In general, users of your library should not need to make any similar changes to their own migration scripts. The one exception is if you have multiple levels of swappable models with foreign keys pointing to each other. ```diff # reusableapp/migrations/0001_initial.py from django.db import models, migrations < from django.conf import settings > import swapper class Migration(migrations.Migration): dependencies = [ < migrations.swappable_dependency(settings.REUSABLEAPP_PARENT_MODEL), > swapper.dependency('reusableapp', 'Parent') ] operations = [ migrations.CreateModel( name='Child', fields=[ ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')), ], options={ < 'swappable': 'REUSABLEAPP_CHILD_MODEL', > 'swappable': swapper.swappable_setting('reusableapp', 'Child'), }, bases=(models.Model,), ), migrations.CreateModel( name='Parent', fields=[ ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')), ], options={ < 'swappable': 'REUSABLEAPP_PARENT_MODEL', > 'swappable': swapper.swappable_setting('reusableapp', 'Parent'), }, bases=(models.Model,), ), migrations.AddField( model_name='child', name='parent', < field=models.ForeignKey(to=settings.REUSABLEAPP_PARENT_MODEL), > field=models.ForeignKey(to=swapper.get_model_name('reusableapp', 'Parent')), preserve_default=True, ), ] ``` ## End User Documentation With the above setup, the user of your app can override one or both models in their own app. You might provide them with an example like this: ```python # myapp/models.py from reusableapp.models import BaseParent class Parent(BaseParent): # custom implementation ... ``` Then, tell your users to update their settings to trigger the swap. ```python # myproject/settings.py REUSABLEAPP_PARENT_MODEL = "myapp.Parent" ``` The goal is to make this process just as easy for your end user as [swapping the auth.User model] is. As with `auth.User`, there are some important caveats that you may want to inform your users about. The biggest issue is that your users will probably need to define the swapped model settings **before creating any migrations** for their implementation of `myapp`. Due to key assumptions made within Django's migration infrastructure, it is difficult to start out with a default (non-swapped) model and then later to switch to a swapped implementation without doing some migration hacking. This is somewhat awkward - as your users will most likely want to try out your default implementation before deciding to customize it. Unfortunately, there isn't an easy workaround due to how the swappable setting is currently implemented in Django core. This will likely be addressed in future Django versions (see [#10] and [Django ticket #25313]). ## API Documentation Here is the full API for `swapper`, which you may find useful in creating your reusable app code. End users of your library should generally not need to reference this API. function | purpose ---------|-------- `swappable_setting(app_label, model)` | Generates a swappable setting name for the provided model (e.g. `"REUSABLEAPP_PARENT_MODEL"`) `is_swapped(app_label, model)` | Determines whether or not a given model has been swapped. (Returns the model name if swapped, otherwise `False`) `get_model_name(app_label, model)` | Gets the name of the model the swappable model has been swapped for (or the name of the original model if not swapped.) `get_model_names(app_label, models)` | Match a list of model names to their swapped versions. All of the models should be from the same app (though their swapped versions need not be). `load_model(app_label, model, required=True)` | Load the swapped model class for a swappable model (or the original model if it hasn't been swapped). If your code can function without the specified model, set `required = False`. `dependency(app_label, model, version=None)` | Generate a dependency tuple for use in migrations. Use `version` only when depending on the first migration of the target dependency doesn't work (eg: when a specific migration needs to be depended upon), we recommend avoid using `version='__latest__'` because it can have serious [drawbacks] when new migrations are added to the module which is being depended upon. `set_app_prefix(app_label, prefix)` | Set a custom prefix for swappable settings (the default is the upper case `app_label`). This can be useful if the app has a long name or is part of a larger framework. This should be set at the top of your models.py. `join(app_label, model)`, `split(model)` | Utilities for splitting and joining `"app.Model"` strings and `("app", "Model")` tuples. [undocumented]: https://code.djangoproject.com/ticket/19103 [swapping the auth.User model]: https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#auth-custom-user [openwisp-users]: https://github.com/openwisp/openwisp-users#extend-openwisp-users [openwisp-controller]: https://github.com/openwisp/openwisp-controller#extending-openwisp-controller [openwisp-radius]: https://openwisp-radius.readthedocs.io/en/latest/developer/how_to_extend.html [reusability]: https://openwisp.io/docs/general/values.html#software-reusability-means-long-term-sustainability [get_user_model()]: https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#referencing-the-user-model [#10]: https://github.com/openwisp/django-swappable-models/issues/10 [Django ticket #25313]: https://code.djangoproject.com/ticket/25313 [drawbacks]: https://code.djangoproject.com/ticket/23071 django-swappable-models-1.4.0/run-qa-checks000077500000000000000000000000771465720101500206070ustar00rootroot00000000000000#!/bin/bash set -e openwisp-qa-check \ --skip-checkmigrations django-swappable-models-1.4.0/setup.cfg000066400000000000000000000004721465720101500200400ustar00rootroot00000000000000[bdist_wheel] universal=1 [flake8] max-line-length = 110 # W503: line break before or after operator # W504: line break after or after operator # W605: invalid escape sequence ignore = W605, W503, W504 [isort] multi_line_output=3 use_parentheses=True include_trailing_comma=True force_grid_wrap=0 line_length=88 django-swappable-models-1.4.0/setup.py000066400000000000000000000030541465720101500177300ustar00rootroot00000000000000from setuptools import setup LONG_DESCRIPTION = """ The unofficial Django swappable models API. """ def readme(): try: readme = open('README.md') except IOError: return LONG_DESCRIPTION else: return readme.read() setup( name='swapper', use_scm_version=True, author='S. Andrew Sheppard', author_email='andrew@wq.io', url='https://github.com/openwisp/django-swappable-models', license='MIT', packages=['swapper'], description=LONG_DESCRIPTION.strip(), long_description=readme(), long_description_content_type="text/markdown", classifiers=[ 'Development Status :: 5 - Production/Stable', 'Environment :: Web Environment', 'License :: OSI Approved :: MIT License', 'Natural Language :: English', 'Operating System :: OS Independent', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', 'Programming Language :: Python :: 3.11', 'Framework :: Django', 'Framework :: Django :: 2.0', 'Framework :: Django :: 2.1', 'Framework :: Django :: 2.2', 'Framework :: Django :: 3.0', 'Framework :: Django :: 3.1', 'Framework :: Django :: 3.2', 'Framework :: Django :: 4.0', 'Framework :: Django :: 4.2', 'Framework :: Django :: 5.0', ], tests_require=['django>=2.0'], test_suite='tests', setup_requires=['setuptools_scm'], ) django-swappable-models-1.4.0/swapper/000077500000000000000000000000001465720101500176755ustar00rootroot00000000000000django-swappable-models-1.4.0/swapper/__init__.py000066400000000000000000000055041465720101500220120ustar00rootroot00000000000000from django.apps import apps from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.db.migrations import swappable_dependency _prefixes = {} def swappable_setting(app_label, model): """Returns the setting name to use for the given model Returns the setting name to use for the given model (i.e. AUTH_USER_MODEL) """ prefix = _prefixes.get(app_label, app_label) setting = "{prefix}_{model}_MODEL".format( prefix=prefix.upper(), model=model.upper() ) # Ensure this attribute exists to avoid migration issues in Django 1.7 if not hasattr(settings, setting): setattr(settings, setting, join(app_label, model)) return setting def is_swapped(app_label, model): """Returns the value of the swapped setting. Returns the value of the swapped setting, or False if the model hasn't been swapped. """ default_model = join(app_label, model) setting = swappable_setting(app_label, model) value = getattr(settings, setting, default_model) if value != default_model: return value else: return False def get_model_name(app_label, model): """Returns [app_label.model]. Returns [app_label.model] unless the model has been swapped, in which case returns the swappable setting value. """ return is_swapped(app_label, model) or join(app_label, model) def dependency(app_label, model, version=None): """Returns a Django 1.7+ style dependency tuple Returns a Django 1.7+ style dependency tuple for inclusion in migration.dependencies[] """ dependencies = swappable_dependency(get_model_name(app_label, model)) if not version: return dependencies return dependencies[0], version def get_model_names(app_label, models): """Map model names to their swapped equivalents for the given app""" return dict((model, get_model_name(app_label, model)) for model in models) def load_model(app_label, model, required=True, require_ready=True): """Load the specified model class, or the class it was swapped out for.""" swapped = is_swapped(app_label, model) if swapped: app_label, model = split(swapped) try: cls = apps.get_model(app_label, model, require_ready=require_ready) except LookupError: cls = None if cls is None and required: raise ImproperlyConfigured( "Could not find {name}!".format(name=join(app_label, model)) ) return cls def set_app_prefix(app_label, prefix): """Set a custom prefix to use for the given app (e.g. WQ)""" _prefixes[app_label] = prefix def join(app_label, model): return "{app_label}.{model}".format( app_label=app_label, model=model, ) def split(model): app_label, _, model = model.rpartition(".") return app_label, model django-swappable-models-1.4.0/tests/000077500000000000000000000000001465720101500173565ustar00rootroot00000000000000django-swappable-models-1.4.0/tests/__init__.py000066400000000000000000000011411465720101500214640ustar00rootroot00000000000000import os import django from django.core.management import call_command from django.test.utils import setup_test_environment os.environ.setdefault('DJANGO_SETTINGS_MODULE', "tests.settings") setup_test_environment() if hasattr(django, 'setup'): # Django 1.7+ django.setup() call_command('makemigrations', 'default_app', interactive=False) if os.environ["DJANGO_SETTINGS_MODULE"] == "tests.swap_settings": call_command('makemigrations', 'alt_app', interactive=False) call_command('migrate', interactive=False) else: # Django 1.6 call_command('syncdb', interactive=False) django-swappable-models-1.4.0/tests/alt_app/000077500000000000000000000000001465720101500207765ustar00rootroot00000000000000django-swappable-models-1.4.0/tests/alt_app/__init__.py000066400000000000000000000000001465720101500230750ustar00rootroot00000000000000django-swappable-models-1.4.0/tests/alt_app/models.py000066400000000000000000000002611465720101500226320ustar00rootroot00000000000000from django.db import models from tests.default_app.models import BaseType class Type(BaseType): code = models.SlugField() class Meta: app_label = 'alt_app' django-swappable-models-1.4.0/tests/default_app/000077500000000000000000000000001465720101500216425ustar00rootroot00000000000000django-swappable-models-1.4.0/tests/default_app/__init__.py000066400000000000000000000000001465720101500237410ustar00rootroot00000000000000django-swappable-models-1.4.0/tests/default_app/models.py000066400000000000000000000007601465720101500235020ustar00rootroot00000000000000from django.db import models import swapper class BaseType(models.Model): name = models.CharField(max_length=255) class Meta: abstract = True class Type(BaseType): class Meta: swappable = swapper.swappable_setting("default_app", "Type") class Item(models.Model): type = models.ForeignKey( swapper.get_model_name('default_app', "Type"), on_delete=models.CASCADE ) name = models.CharField(max_length=255) description = models.TextField() django-swappable-models-1.4.0/tests/settings.py000066400000000000000000000003031465720101500215640ustar00rootroot00000000000000SECRET_KEY = '1234' INSTALLED_APPS = ('tests.default_app',) MIDDLEWARE_CLASSES = tuple() DATABASES = { 'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}, } SWAP = False django-swappable-models-1.4.0/tests/swap_settings.py000066400000000000000000000004131465720101500226200ustar00rootroot00000000000000from . import settings DEFAULT_APP_TYPE_MODEL = "alt_app.Type" MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES DATABASES = settings.DATABASES INSTALLED_APPS = settings.INSTALLED_APPS INSTALLED_APPS += ('tests.alt_app',) SECRET_KEY = settings.SECRET_KEY SWAP = True django-swappable-models-1.4.0/tests/test_swapper.py000066400000000000000000000074421465720101500224570ustar00rootroot00000000000000import unittest from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.test import TestCase import swapper try: from django.db import migrations # noqa except ImportError: DJ17 = False else: DJ17 = True class SwapperTestCase(TestCase): # Tests that should work whether or not default_app.Type is swapped def test_fields(self): Type = swapper.load_model('default_app', 'Type') fields = dict((field.name, field) for field in Type._meta.fields) self.assertIn('name', fields) def test_create(self): Type = swapper.load_model('default_app', 'Type') Item = swapper.load_model('default_app', 'Item') Item.objects.create( type=Type.objects.create(name="Type 1"), name="Item 1", ) self.assertEqual(Item.objects.count(), 1) item = Item.objects.all()[0] self.assertEqual(item.type.name, "Type 1") def test_not_installed(self): Invalid = swapper.load_model("invalid_app", "Invalid", required=False) self.assertIsNone(Invalid) with self.assertRaises(ImproperlyConfigured): swapper.load_model("invalid_app", "Invalid", required=True) def test_non_contrib_app_split(self): self.assertEqual(swapper.split('alt_app.Type'), ('alt_app', 'Type')) def test_contrib_app_split(self): self.assertEqual( swapper.split('alt_app.contrib.named_things.NamedThing'), ('alt_app.contrib.named_things', 'NamedThing'), ) # Tests that only work if default_app.Type is swapped @unittest.skipUnless(settings.SWAP, "requires swapped models") def test_swap_setting(self): self.assertTrue(swapper.is_swapped("default_app", "Type")) self.assertEqual(swapper.get_model_name("default_app", "Type"), "alt_app.Type") @unittest.skipUnless(settings.SWAP, "requires swapped models") def test_swap_fields(self): Type = swapper.load_model('default_app', 'Type') fields = dict((field.name, field) for field in Type._meta.fields) self.assertIn('code', fields) @unittest.skipUnless(settings.SWAP, "requires swapped models") def test_swap_create(self): Type = swapper.load_model('default_app', 'Type') Item = swapper.load_model('default_app', 'Item') Item.objects.create( type=Type.objects.create( name="Type 1", code="type-1", ), name="Item 1", ) self.assertEqual(Item.objects.count(), 1) item = Item.objects.all()[0] self.assertEqual(item.type.code, "type-1") @unittest.skipUnless(settings.SWAP and DJ17, "requires swapped models & Django 1.7") def test_swap_dependency(self): self.assertEqual( swapper.dependency("default_app", "Type"), ("alt_app", "__first__") ) self.assertEqual( swapper.dependency("default_app", "Type", "__latest__"), ("alt_app", "__latest__"), ) self.assertEqual( swapper.dependency("default_app", "Type", "0001_custom_migration"), ("alt_app", "0001_custom_migration"), ) # Tests that only work if default_app.Type is *not* swapped @unittest.skipIf(settings.SWAP, "requires non-swapped models") def test_default_setting(self): self.assertFalse(swapper.is_swapped("default_app", "Type")) self.assertEqual( swapper.get_model_name("default_app", "Type"), "default_app.Type" ) @unittest.skipUnless( not settings.SWAP and DJ17, "requires non-swapped models & Django 1.7" ) def test_default_dependency(self): self.assertEqual( swapper.dependency("default_app", "Type"), ("default_app", "__first__"), ) django-swappable-models-1.4.0/tox.ini000066400000000000000000000017731465720101500175370ustar00rootroot00000000000000[tox] envlist = py{38}-django22-{noswap,swap} py{38}-django30-{noswap,swap} py{38,39}-django31-{noswap,swap} py{38,39,310}-django32-{noswap,swap} py{38,39,310}-django40-{noswap, swap} py{38,39,310,311}-django42-{noswap, swap} py{310,311}-django50-{noswap, swap} lint [gh-actions] python = 3.8: py38, lint 3.9: py39, lint 3.10: py310, lint 3.11: py311, lint [testenv] commands = rm -rf tests/default_app/migrations/ tests/alt_app/migrations/ python setup.py test deps = django22: django~=2.2.0 django30: django~=3.0.0 django31: django~=3.1.0 django32: django~=3.2.0 django40: django~=4.0.0 django42: django~=4.2.0 django50: django~=5.0.0 setenv = noswap: DJANGO_SETTINGS_MODULE=tests.settings swap: DJANGO_SETTINGS_MODULE=tests.swap_settings allowlist_externals = rm [testenv:lint] commands = rm -rf tests/default_app/migrations/ tests/alt_app/migrations/ flake8 swapper tests allowlist_externals = rm flake8