Contact

The new version of the contact app (branch 2953-ember) stores multiple email addresses, phone numbers and postal addresses for each contact.

Note

The email address in the User is not indexed by ElasticSearch. All email addresses should be stored in the ContactEmail model (migration 0006_auto_20180417_0736 handles this automatically).

Icon

<i class="fa fa-user"></i>

Management Commands

rebuild_contact_index
refresh_contact_index

Model

Define the contact model in settings/base.py (or in example_app/base.py for an app):

CONTACT_MODEL = "contact.Contact"

To get the actual contact model, create a get_contact_model function (perhaps in models.py) e.g:

from django.apps import apps
from django.conf import settings

def get_contact_model():
    return apps.get_model(settings.CONTACT_MODEL)

Templates and Mixins

Tip

All projects which use the contact app should provide a detail URL named contact.detail.

ContactDetailMixin

from braces.views import LoginRequiredMixin, StaffuserRequiredMixin
from django.views.generic import DetailView, TemplateView

from contact.views import ContactDetailMixin

class ContactDetailView(
    LoginRequiredMixin,
    StaffuserRequiredMixin,
    ContactDetailMixin,
    BaseMixin,
    DetailView,
):
    pass
from .views import ContactDetailView

re_path(
    r"^contact/(?P<pk>\d+)/$",
    view=ContactDetailView.as_view(),
    name="contact.detail",
),

test_view_perm.py:

from contact.tests.factories import UserContactFactory

@pytest.mark.django_db
def test_contact_detail(perm_check):
    user_contact = UserContactFactory()
    url = reverse("contact.detail", args=[user_contact.contact.pk])
    perm_check.staff(url)

Tip

The example_contact/templates/example/contact_detail.html example template shows an example of how to use the two contact detail views:

{% include 'contact/contact_detail.html' %}
{% include 'contact/contact_detail_multi.html' %}

ContactListMixin

from contact.views import ContactListMixin

class ContactListView(
    LoginRequiredMixin,
    StaffuserRequiredMixin,
    ContactListMixin,
    BaseMixin,
    ListView,
):
    template_name = "example/contact_list.html"

Tip

We also have a contact/templates/contact/_contact_list.html which can be included in your own templates if required:

{% include 'contact/_contact_list.html' %}
re_path(
    r"^contact/$",
    view=ContactListView.as_view(),
    name="contact.list"
),
@pytest.mark.django_db
def test_contact_list(perm_check):
    url = reverse("contact.list")
    perm_check.staff(url)

UserContactRedirectView

This is a standard view which redirects from a user pk to a contact detail view. It is used by the app-gdpr.

from django.views.generic import RedirectView

class UserContactRedirectView(RedirectView):

    permanent = False

    def get_redirect_url(self, *args, **kwargs):
        pk = kwargs["user_pk"]
        contact = Contact.objects.get(user__pk=pk)
        return reverse("contact.detail", args=[contact.pk])
from .views import UserContactRedirectView

re_path(
    r"^user/(?P<user_pk>\d+)/redirect/$",
    view=UserContactRedirectView.as_view(),
    name="user.redirect.contact",
),