Code Monkey home page Code Monkey logo

django-attachments's Introduction

image

image

Codacy Badge

image

django-attachments

django-attachments is a generic set of template tags to attach any kind of files to models.

Installation:

  1. Put attachments to your INSTALLED_APPS in your settings.py within your django project:

    INSTALLED_APPS = (
        ...
        'attachments',
    )
  2. Add the attachments urlpattern to your urls.py:

    url(r'^attachments/', include('attachments.urls', namespace='attachments')),
  3. Migrate your database:

    ./manage.py migrate
  4. Grant the user some permissions:
    • For adding attachments grant the user (or group) the permission attachments.add_attachment.
    • For deleting attachments grant the user (or group) the permission attachments.delete_attachment. This allows the user to delete their attachments only.
    • For deleting foreign attachments (attachments by other users) grant the user the permission attachments.delete_foreign_attachments.
  5. Set DELETE_ATTACHMENTS_FROM_DISK to True if you want to remove files from disk when Attachment objects are removed!
  6. Configure FILE_UPLOAD_MAX_SIZE (optional). This is the maximum size in bytes before raising form validation errors. If not set there is no restriction on file size.

Mind that you serve files!

django-attachments stores the files in your site_media directory and does not modify them. For example, if an user uploads a .html file your webserver will probably display it in HTML. It's a good idea to serve such files as plain text. In a Apache2 configuration this would look like:

<Location /site_media/attachments>
    AddType text/plain .html .htm .shtml .php .php5 .php4 .pl .cgi
</Location>

House-keeping

django-attachments provides the delete_stale_sttachments management command. It will remove all attachments for which the related objects don't exist anymore! Sys-admins could then:

./manage.py delete_stale_attachments

You may also want to execute this via cron.

Tests

Run the testsuite in your local environment using pipenv:

$ cd django-attachments/
$ pipenv install --dev
$ pipenv run pytest attachments/

Or use tox to test against various Django and Python versions:

$ tox -r

You can also invoke the test suite or other 'manage.py' commands by calling the django-admin tool with the test app settings:

$ cd django-attachments/
$ pipenv install --dev
$ pipenv run test
$ pipenv run django-admin.py runserver

Usage:

In contrib.admin:

django-attachments provides a inline object to add a list of attachments to any kind of model in your admin app.

Simply add AttachmentInlines to the admin options of your model. Example:

from django.contrib import admin
from attachments.admin import AttachmentInlines

class MyEntryOptions(admin.ModelAdmin):
    inlines = (AttachmentInlines,)

image

In your frontend templates:

First of all, load the attachments_tags in every template you want to use it:

{% load attachments_tags %}

django-attachments comes with some templatetags to add or delete attachments for your model objects in your frontend.

  1. get_attachments_for [object]: Fetches the attachments for the given model instance. You can optionally define a variable name in which the attachment list is stored in the template context (this is required in Django 1.8). If you do not define a variable name, the result is printed instead.

    {% get_attachments_for entry as "attachments_list" %}

  2. attachments_count [object]: Counts the attachments for the given model instance and returns an int:

    {% attachments_count entry %}
  3. attachment_form: Renders a upload form to add attachments for the given model instance. Example:

    {% attachment_form [object] %}

    It returns an empty string if the current user is not logged in.

  4. attachment_delete_link: Renders a link to the delete view for the given attachment. Example:

    {% for att in attachments_list %}
        {{ att }} {% attachment_delete_link att %}
    {% endfor %}

    This tag automatically checks for permission. It returns only a html link if the give n attachment's creator is the current logged in user or the user has the delete_foreign_attachments permission.

Quick Example:

{% load attachments_tags %}
{% get_attachments_for entry as my_entry_attachments %}

<span>Object has {% attachments_count entry %} attachments</span>
{% if my_entry_attachments %}
<ul>
{% for attachment in my_entry_attachments %}
    <li>
        <a href="{{ attachment.attachment_file.url }}">{{ attachment.filename }}</a>
        {% attachment_delete_link attachment %}
    </li>
{% endfor %}
</ul>
{% endif %}

{% attachment_form entry %}

{% if messages %}
<ul class="messages">
{% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
        {{ message }}
    </li>
{% endfor %}
</ul>
{% endif %}

django-attachments's People

Contributors

aert avatar aleksihakli avatar anentropic avatar atodorov avatar barttc avatar bashu avatar chathaway-codes avatar darkpixel avatar dependabot[bot] avatar flimm avatar gilsondev avatar gonzalobustos avatar jangeador avatar jezdez avatar maltalk avatar mattburlage avatar morlandi avatar reb00ter avatar vitan avatar yesnik avatar zulupro avatar

Watchers

 avatar  avatar  avatar  avatar

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.