Store license information alongside your Django data models
Project description
Django Content License
A Django app that allows you to associate content licenses with model instances and display appropriate attribution in your HTML templates. Perfect for academic datasets, research publications, creative content, and any application where proper licensing and attribution are important.
Features
- License Management: Store and manage various content licenses (MIT, GPL, Creative Commons, etc.)
- Easy Integration: Simple
LicenseFieldthat can be added to any Django model - Automatic Attribution: Generate proper HTML attribution snippets automatically
- Template Integration: Built-in template tags for displaying license information
- Admin Interface: Full Django admin integration for license management
- Validation: Built-in validation for license consistency and requirements
- Internationalization: Full i18n support with translations
- Performance: Optimized database queries and indexing
Quick Start
Installation
pip install django-content-license
Settings
Add licensing to your INSTALLED_APPS:
INSTALLED_APPS = [
# ... your other apps
'licensing',
]
Run migrations:
python manage.py migrate licensing
Basic Usage
1. Add License Field to Your Model
from django.db import models
from licensing.fields import LicenseField
class Dataset(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
license = LicenseField() # This field links to a License
def __str__(self):
return self.name
def get_absolute_url(self):
return f"/datasets/{self.pk}/"
2. Create Licenses
from licensing.models import License
# Create licenses through Django admin or programmatically
mit_license = License.objects.create(
name="MIT License",
canonical_url="https://opensource.org/licenses/MIT",
description="A permissive license that allows commercial use",
text="Permission is hereby granted, free of charge..."
)
3. Display Attribution in Templates
<!-- In your template -->
<div class="dataset">
<h2>{{ dataset.name }}</h2>
<p>{{ dataset.description }}</p>
<!-- Automatic attribution display -->
<div class="license-attribution">
{{ dataset.get_license_display }}
</div>
</div>
This will output properly formatted HTML like:
<a href="/datasets/1/">My Dataset</a> is licensed under
<a href="https://opensource.org/licenses/MIT" target="_blank" rel="noopener">MIT License</a>
Advanced Usage
Custom License Field Options
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
# Custom license field with specific options
license = LicenseField(
verbose_name="Content License",
help_text="Choose the license for this article",
on_delete=models.PROTECT, # Prevent license deletion if referenced
limit_choices_to={'is_active': True}, # Only show active licenses
)
Working with Creators/Authors
If your model has creator information, the attribution will automatically include it:
class Research(models.Model):
title = models.CharField(max_length=200)
license = LicenseField()
# The template will automatically look for these fields
creators = models.CharField(max_length=200) # Or ForeignKey to User/Author model
def get_absolute_url(self):
return f"/research/{self.pk}/"
# In template, this will generate:
# "Research Title by John Doe is licensed under MIT License"
License Model API
from licensing.models import License
# Get all active/recommended licenses
active_licenses = License.get_recommended_licenses()
# Check license properties
license = License.objects.get(name="MIT License")
print(license.status_display) # "Active" or "Deprecated"
print(license.short_description) # Truncated description
print(license.full_name) # Full license name
# Validation
license.clean() # Validates license consistency
# Future compatibility checking (placeholder)
compatibility = license.get_compatibility_with(other_license)
Template Customization
You can override the default attribution template by creating your own licensing/snippet.html:
<!-- templates/licensing/snippet.html -->
{% load i18n %}
<div class="license-info">
{% if object.creators %}
<span class="creators">By {{ object.creators }}</span>
{% endif %}
<span class="license-link">
Licensed under <a href="{{ license.canonical_url }}" target="_blank" rel="noopener">
{{ license.name }}
</a>
</span>
</div>
Available License Fields
LicenseField Parameters
verbose_name: Display name for the field (default: "license")help_text: Help text for admin formson_delete: What to do when license is deleted (default:models.PROTECT)limit_choices_to: Limit available license choicesnull/blank: Whether field can be empty
Model Validation
The License model includes built-in validation:
# Deprecated licenses must have a deprecated_date
license = License(name="Old License", is_active=False)
license.clean() # Raises ValidationError
# Active licenses shouldn't have deprecated_date
license = License(
name="Active License",
is_active=True,
deprecated_date=timezone.now().date()
)
license.clean() # Raises ValidationError
Testing
The package includes comprehensive test coverage using both Django's TestCase and pytest.
Running Tests
# Run all tests with Django's test runner
python manage.py test
# Run with pytest (recommended)
pytest
# Run with coverage
pytest --cov=licensing --cov-report=html
# Run specific test categories
pytest -m unit # Unit tests only
pytest -m integration # Integration tests only
pytest -m "not slow" # Skip slow tests
Test Organization
tests/test_models.py- Model functionality (Django TestCase)tests/test_fields.py- Field functionality (Django TestCase)tests/test_pytest_models.py- Model tests (pytest)tests/test_pytest_fields.py- Field tests (pytest)tests/test_integration.py- Integration tests (pytest)
Writing Your Own Tests
import pytest
from licensing.models import License
from licensing.fields import LicenseField
@pytest.mark.django_db
def test_my_model_with_license():
license_obj = License.objects.create(
name="Test License",
canonical_url="https://example.com/license",
text="License text"
)
# Test your model here
instance = MyModel.objects.create(license=license_obj)
assert instance.license == license_obj
Admin Integration
The package provides a complete Django admin interface:
License Admin Features
- List view with license name, status, and description
- Filtering by active status and creation date
- Search by name and description
- Bulk actions for activating/deactivating licenses
- Form validation with helpful error messages
Custom Admin
You can customize the admin interface:
# admin.py
from django.contrib import admin
from licensing.models import License
@admin.register(License)
class CustomLicenseAdmin(admin.ModelAdmin):
list_display = ['name', 'status_display', 'canonical_url']
list_filter = ['is_active', 'created_at']
search_fields = ['name', 'description']
readonly_fields = ['created_at', 'updated_at', 'slug']
Performance Considerations
Database Optimization
- Indexes are automatically created on
is_activeandslugfields - License lookups are optimized using
select_related - Slug generation uses efficient bulk queries
Caching
For high-traffic sites, consider caching license information:
from django.core.cache import cache
from django.db.models.signals import post_save
def invalidate_license_cache(sender, instance, **kwargs):
cache.delete(f'license_{instance.pk}')
post_save.connect(invalidate_license_cache, sender=License)
Migration from Other Apps
If you're migrating from another licensing solution:
# Create a data migration
from django.db import migrations
def migrate_licenses(apps, schema_editor):
OldLicense = apps.get_model('old_app', 'License')
NewLicense = apps.get_model('licensing', 'License')
for old_license in OldLicense.objects.all():
NewLicense.objects.create(
name=old_license.name,
canonical_url=old_license.url,
text=old_license.text,
# Map other fields as needed
)
class Migration(migrations.Migration):
dependencies = [
('licensing', '0001_initial'),
('old_app', '0001_initial'),
]
operations = [
migrations.RunPython(migrate_licenses),
]
Common Use Cases
Academic Research
class ResearchPaper(models.Model):
title = models.CharField(max_length=200)
authors = models.ManyToManyField(User)
license = LicenseField(
help_text="Choose appropriate license for your research data"
)
# For dataset licensing
dataset_license = LicenseField(
verbose_name="Dataset License",
related_name="research_datasets"
)
Creative Content
class Artwork(models.Model):
title = models.CharField(max_length=200)
artist = models.ForeignKey(User, on_delete=models.CASCADE)
license = LicenseField(
limit_choices_to={'name__icontains': 'Creative Commons'}
)
Software Projects
class SoftwareProject(models.Model):
name = models.CharField(max_length=200)
license = LicenseField(
limit_choices_to={'name__in': ['MIT', 'GPL', 'Apache']}
)
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
# Clone the repository
git clone https://github.com/SamuelJennings/django-content-license.git
cd django-content-license
# Install dependencies
poetry install
# Run tests
poetry run pytest
# Run linting
poetry run black .
poetry run pylint licensing/
Code Quality
We maintain high code quality standards:
- 100% test coverage target
- Type hints for all public APIs
- Comprehensive documentation
- Regular security audits
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- Documentation: Full documentation available at Read the Docs
- Issues: Report bugs at GitHub Issues
- Discussion: Join the discussion at GitHub Discussions
Changelog
See HISTORY.md for a complete changelog.
Related Projects
- django-licenses - Alternative license management
- Creative Commons API - Official CC license data
Made with ❤️ by the Django community
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_content_license-0.3.0.tar.gz.
File metadata
- Download URL: django_content_license-0.3.0.tar.gz
- Upload date:
- Size: 28.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47526e23759a87b859a478aa9d4bec5387ca6c0d80b2e1c152ae27892b7f3025
|
|
| MD5 |
a0bf85f1a01e93db314d3840d895cfe8
|
|
| BLAKE2b-256 |
83dc1350d1f5e3c54d33ab434d9d6924afabd55fa11c619874358078ec7ae52d
|
Provenance
The following attestation bundles were made for django_content_license-0.3.0.tar.gz:
Publisher:
on-release-main.yml on SamuelJennings/django-content-license
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_content_license-0.3.0.tar.gz -
Subject digest:
47526e23759a87b859a478aa9d4bec5387ca6c0d80b2e1c152ae27892b7f3025 - Sigstore transparency entry: 929891580
- Sigstore integration time:
-
Permalink:
SamuelJennings/django-content-license@8e1cce77704e65a5e6b599341afa86f251697fef -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/SamuelJennings
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
on-release-main.yml@8e1cce77704e65a5e6b599341afa86f251697fef -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_content_license-0.3.0-py3-none-any.whl.
File metadata
- Download URL: django_content_license-0.3.0-py3-none-any.whl
- Upload date:
- Size: 25.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1169348a3c2f32d7d890243ea6896a8d32a4240c70eb47a17389be1c29c4b8c2
|
|
| MD5 |
a6434ffe1b799fc97679d6af98013ec3
|
|
| BLAKE2b-256 |
2aef243a3b664d4f499d60b3282e8404d7609251c222f014472caf1d699cbbaf
|
Provenance
The following attestation bundles were made for django_content_license-0.3.0-py3-none-any.whl:
Publisher:
on-release-main.yml on SamuelJennings/django-content-license
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_content_license-0.3.0-py3-none-any.whl -
Subject digest:
1169348a3c2f32d7d890243ea6896a8d32a4240c70eb47a17389be1c29c4b8c2 - Sigstore transparency entry: 929891582
- Sigstore integration time:
-
Permalink:
SamuelJennings/django-content-license@8e1cce77704e65a5e6b599341afa86f251697fef -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/SamuelJennings
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
on-release-main.yml@8e1cce77704e65a5e6b599341afa86f251697fef -
Trigger Event:
push
-
Statement type: