django-anchor


Namedjango-anchor JSON
Version 0.6.1 PyPI version JSON
download
home_pageNone
SummaryAttach files to Django models
upload_time2025-02-09 11:21:06
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseCopyright (c) 2024 Elias Hernandis 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.
keywords django files attachments
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Django Anchor

[![Test](https://github.com/knifecake/django-anchor/actions/workflows/test.yml/badge.svg)](https://github.com/knifecake/django-anchor/actions/workflows/test.yml)
[![Documentation Status](https://readthedocs.org/projects/django-anchor/badge/?version=latest)](https://django-anchor.readthedocs.io/en/latest/?badge=latest)

Django Anchor is a reusable Django app that allows you to attach files to models.

Anchor works very similarly to Django's ``FileField`` and ``ImageField`` model
fields, but adds a few extra features:

- Images can be resized, converted to another format and otherwise transformed.
- Files are served through signed URLs that can expire after a configurable
  amount of time, even when using the default file-system storage backend.

Django Anchor is essentially a port of the excellent [Active
Storage](https://edgeguides.rubyonrails.org/active_storage_overview.html) Ruby
on Rails feature, but leveraging existing Django abstractions and packages of
the Python ecosystem. Some features are not yet implemented, but the core
concepts are there two eventually be able to support them.

## Installation

Check out the [installation
guide](https://django-anchor.readthedocs.io/en/latest/installation.html) in the
documentation for more details.

Django-anchor is compatible with Django >= 4.2 and Python >= 3.11.

1. Add the `django-anchor` package to your dependencies. You can do this by
   running:

       pip install django-anchor

   or by adding `django-anchor` to your `requirements.txt` or `pyproject.toml`
   files if you have one.

2. Add  `anchor` to `settings.INSTALLED_APPS`

3. Add URL configuration to your project:

   ```python
   urlpatterns = [
       path('anchor/', include('anchor.urls')),
   ]
   ```

4. Run migrations:

   ```bash
   python manage.py migrate
   ```

In addition, if you wish to create image variants, a Pillow >= 9.5 should be
available in your system.

## Usage

💡 Check out the [demo](./demo/) Django project for inspiration and the [Getting
Started
guide](https://django-anchor.readthedocs.io/en/latest/getting_started.html) in
the documentation.

### Adding files to models

The easiest way to add a file to a model is to add a `BlobField` to it:

```python
from django.db import models
from anchor.models.fields import SingleAttachmentField


class Movie(models.Model):
    title = models.CharField(max_length=100)

    cover = SingleAttachmentField()
```

That's it! No need to run ``makemigrations`` or ``migrate`` since Django Anchor
doesn't actually need any columns added to the model.

The ``cover`` field works just like any other model field:

```python
# Create a new movie
movie = Movie.objects.create(title="My Movie")

# Attach an uploaded file
movie.cover = uploaded_file

# Get a URL to the file
movie.cover.url()

# Get a URL to a miniature version of the file
movie.cover.representation(resize_to_fit=(200, 200), format="webp").url()
```

### Using files in templates

Django anchor comes with a handy template tag to render URLs of files you've stored:

```
{% load anchor %}
<img src="{% variant_url movie.cover resize_to_limit='300x600' format='jpeg' %}">
```

The above call to `variant_url` will generate an optimized version of the
movie's cover in JPEG format which fits inside a 300x600 rectangle.

## Contributing

PRs and issues are very welcome!

Check out [CONTRIBUTING.md](./CONTRIBUTING.md) to learn how to set up the
project locally.

## License

This project is released under the MIT License. Check out
[LICENSE](./LICENSE.md) to get the full text of the license.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-anchor",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "django, files, attachments",
    "author": null,
    "author_email": "Elias Hernandis <elias@hernandis.me>",
    "download_url": "https://files.pythonhosted.org/packages/5b/65/4011d2d568a3cf11d73ed0e078195141d5e5bb1960bfc4df4442018f1cfe/django_anchor-0.6.1.tar.gz",
    "platform": null,
    "description": "# Django Anchor\n\n[![Test](https://github.com/knifecake/django-anchor/actions/workflows/test.yml/badge.svg)](https://github.com/knifecake/django-anchor/actions/workflows/test.yml)\n[![Documentation Status](https://readthedocs.org/projects/django-anchor/badge/?version=latest)](https://django-anchor.readthedocs.io/en/latest/?badge=latest)\n\nDjango Anchor is a reusable Django app that allows you to attach files to models.\n\nAnchor works very similarly to Django's ``FileField`` and ``ImageField`` model\nfields, but adds a few extra features:\n\n- Images can be resized, converted to another format and otherwise transformed.\n- Files are served through signed URLs that can expire after a configurable\n  amount of time, even when using the default file-system storage backend.\n\nDjango Anchor is essentially a port of the excellent [Active\nStorage](https://edgeguides.rubyonrails.org/active_storage_overview.html) Ruby\non Rails feature, but leveraging existing Django abstractions and packages of\nthe Python ecosystem. Some features are not yet implemented, but the core\nconcepts are there two eventually be able to support them.\n\n## Installation\n\nCheck out the [installation\nguide](https://django-anchor.readthedocs.io/en/latest/installation.html) in the\ndocumentation for more details.\n\nDjango-anchor is compatible with Django >= 4.2 and Python >= 3.11.\n\n1. Add the `django-anchor` package to your dependencies. You can do this by\n   running:\n\n       pip install django-anchor\n\n   or by adding `django-anchor` to your `requirements.txt` or `pyproject.toml`\n   files if you have one.\n\n2. Add  `anchor` to `settings.INSTALLED_APPS`\n\n3. Add URL configuration to your project:\n\n   ```python\n   urlpatterns = [\n       path('anchor/', include('anchor.urls')),\n   ]\n   ```\n\n4. Run migrations:\n\n   ```bash\n   python manage.py migrate\n   ```\n\nIn addition, if you wish to create image variants, a Pillow >= 9.5 should be\navailable in your system.\n\n## Usage\n\n\ud83d\udca1 Check out the [demo](./demo/) Django project for inspiration and the [Getting\nStarted\nguide](https://django-anchor.readthedocs.io/en/latest/getting_started.html) in\nthe documentation.\n\n### Adding files to models\n\nThe easiest way to add a file to a model is to add a `BlobField` to it:\n\n```python\nfrom django.db import models\nfrom anchor.models.fields import SingleAttachmentField\n\n\nclass Movie(models.Model):\n    title = models.CharField(max_length=100)\n\n    cover = SingleAttachmentField()\n```\n\nThat's it! No need to run ``makemigrations`` or ``migrate`` since Django Anchor\ndoesn't actually need any columns added to the model.\n\nThe ``cover`` field works just like any other model field:\n\n```python\n# Create a new movie\nmovie = Movie.objects.create(title=\"My Movie\")\n\n# Attach an uploaded file\nmovie.cover = uploaded_file\n\n# Get a URL to the file\nmovie.cover.url()\n\n# Get a URL to a miniature version of the file\nmovie.cover.representation(resize_to_fit=(200, 200), format=\"webp\").url()\n```\n\n### Using files in templates\n\nDjango anchor comes with a handy template tag to render URLs of files you've stored:\n\n```\n{% load anchor %}\n<img src=\"{% variant_url movie.cover resize_to_limit='300x600' format='jpeg' %}\">\n```\n\nThe above call to `variant_url` will generate an optimized version of the\nmovie's cover in JPEG format which fits inside a 300x600 rectangle.\n\n## Contributing\n\nPRs and issues are very welcome!\n\nCheck out [CONTRIBUTING.md](./CONTRIBUTING.md) to learn how to set up the\nproject locally.\n\n## License\n\nThis project is released under the MIT License. Check out\n[LICENSE](./LICENSE.md) to get the full text of the license.\n",
    "bugtrack_url": null,
    "license": "Copyright (c) 2024 Elias Hernandis\n        \n        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:\n        \n        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n        \n        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.\n        ",
    "summary": "Attach files to Django models",
    "version": "0.6.1",
    "project_urls": {
        "Documentation": "https://django-anchor.readthedocs.io/en/latest/",
        "Homepage": "https://github.com/knifecake/django-anchor",
        "Releases": "https://github.com/knifecake/django-anchor/releases",
        "Source": "https://github.com/knifecake/django-anchor",
        "Tracker": "https://github.com/knifecake/django-anchor/issues"
    },
    "split_keywords": [
        "django",
        " files",
        " attachments"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9ed4dd7ad64aa041a5de7c72daf14b316c5cb9d6c7ebcae4e2665eb0060f7e6b",
                "md5": "6ae28237372765e60ad4e6eaec1ffc55",
                "sha256": "887f1385b7e4e6464c71ce6573dd722a2a47e906dcf2a2738b3e12eab47f40b3"
            },
            "downloads": -1,
            "filename": "django_anchor-0.6.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6ae28237372765e60ad4e6eaec1ffc55",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 36391,
            "upload_time": "2025-02-09T11:21:02",
            "upload_time_iso_8601": "2025-02-09T11:21:02.333200Z",
            "url": "https://files.pythonhosted.org/packages/9e/d4/dd7ad64aa041a5de7c72daf14b316c5cb9d6c7ebcae4e2665eb0060f7e6b/django_anchor-0.6.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5b654011d2d568a3cf11d73ed0e078195141d5e5bb1960bfc4df4442018f1cfe",
                "md5": "42cfd1ac7ceb2cb447a2a60754a00df9",
                "sha256": "95a422528c9a481fec4275e9999702c98e1824f6124edbd6544e63ddd57c5e4f"
            },
            "downloads": -1,
            "filename": "django_anchor-0.6.1.tar.gz",
            "has_sig": false,
            "md5_digest": "42cfd1ac7ceb2cb447a2a60754a00df9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 26943,
            "upload_time": "2025-02-09T11:21:06",
            "upload_time_iso_8601": "2025-02-09T11:21:06.127077Z",
            "url": "https://files.pythonhosted.org/packages/5b/65/4011d2d568a3cf11d73ed0e078195141d5e5bb1960bfc4df4442018f1cfe/django_anchor-0.6.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-09 11:21:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "knifecake",
    "github_project": "django-anchor",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "django-anchor"
}
        
Elapsed time: 1.82474s