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).

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 contact.views import ContactDetailMixin

class ContactDetailView(
        LoginRequiredMixin, ContactDetailMixin, BaseMixin, DetailView):
    pass
url(
    regex=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

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' %}

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 .views import UserContactRedirectView

url(
    regex=r"^user/(?P<pk>\d+)/redirect/$",
    view=UserContactRedirectView.as_view(),
    name="user.redirect.contact",
),
from django.views.generic import RedirectView

class UserContactRedirectView(RedirectView):

    permanent = False

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