File: emailcolumn.py

package info (click to toggle)
django-tables 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,752 kB
  • sloc: python: 7,120; makefile: 132; sh: 74
file content (40 lines) | stat: -rw-r--r-- 1,092 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django.db import models

from .base import library
from .linkcolumn import BaseLinkColumn


@library.register
class EmailColumn(BaseLinkColumn):
    """
    Render email addresses to `mailto:`-links.

    Arguments:
        attrs (dict): HTML attributes that are added to the rendered
                      ``<a href="...">...</a>`` tag.
        text: Either static text, or a callable. If set, this will be used to
              render the text inside link instead of the value.

    Example::

        # models.py
        class Person(models.Model):
            name = models.CharField(max_length=200)
            email =  models.EmailField()

        # tables.py
        class PeopleTable(tables.Table):
            name = tables.Column()
            email = tables.EmailColumn()

        # result
        # [...]<a href="mailto:email@example.com">email@example.com</a>
    """

    def get_url(self, value):
        return f"mailto:{value}"

    @classmethod
    def from_field(cls, field, **kwargs):
        if isinstance(field, models.EmailField):
            return cls(**kwargs)