From d6d308396caa45df1fa71df57fdc128f713d3d9a Mon Sep 17 00:00:00 2001 From: Todd Dembrey <todd.dembrey@torchbox.com> Date: Fri, 5 Jan 2018 17:36:07 +0000 Subject: [PATCH] Update the contact information to be more flexible --- .../003_update_contact_information.py | 39 ++++++++++++++++ opentech/public/people/models.py | 45 ++++++++++++++++--- .../people/templates/people/person_page.html | 8 ++-- 3 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 opentech/public/people/migrations/003_update_contact_information.py diff --git a/opentech/public/people/migrations/003_update_contact_information.py b/opentech/public/people/migrations/003_update_contact_information.py new file mode 100644 index 000000000..eaf3d477c --- /dev/null +++ b/opentech/public/people/migrations/003_update_contact_information.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2018-01-05 17:34 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +import modelcluster.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('people', '0002_add_header_image'), + ] + + operations = [ + migrations.CreateModel( + name='PersonContactInfomation', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('sort_order', models.IntegerField(blank=True, editable=False, null=True)), + ('contact_method', models.CharField(blank=True, choices=[('irc', 'IRC'), ('im_jabber_xmpp', 'IM/Jabber/XMPP'), ('phone', 'Phone'), ('pgp', 'PGP fingerprint'), ('otr', 'OTR fingerprint')], max_length=255)), + ('other_method', models.CharField(blank=True, max_length=255, verbose_name='Other')), + ('contact_detail', models.CharField(max_length=255)), + ('page', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='contact_details', to='people.PersonPage')), + ], + options={ + 'ordering': ['sort_order'], + 'abstract': False, + }, + ), + migrations.RemoveField( + model_name='personpagephonenumber', + name='page', + ), + migrations.DeleteModel( + name='PersonPagePhoneNumber', + ), + ] diff --git a/opentech/public/people/models.py b/opentech/public/people/models.py index 0534a032e..f002d9e40 100644 --- a/opentech/public/people/models.py +++ b/opentech/public/people/models.py @@ -1,13 +1,16 @@ from django.db import models +from django.core.exceptions import ValidationError from django.utils.functional import cached_property from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from django.conf import settings from modelcluster.fields import ParentalKey +from wagtail.wagtailcore.models import Orderable from wagtail.wagtailcore.fields import StreamField from wagtail.wagtailadmin.edit_handlers import ( FieldPanel, + FieldRowPanel, InlinePanel, MultiFieldPanel, StreamFieldPanel @@ -25,7 +28,7 @@ class SocialMediaProfile(models.Model): ) site_titles = ( ('twitter', "Twitter"), - ('linkedin', "LinkedIn") + ('linkedin', "LinkedIn"), ) site_urls = ( ('twitter', 'https://twitter.com/'), @@ -68,14 +71,44 @@ class PersonPagePersonType(models.Model): return self.person_type.title -class PersonPagePhoneNumber(models.Model): - page = ParentalKey('PersonPage', related_name='phone_numbers') - phone_number = models.CharField(max_length=255) +class PersonContactInfomation(Orderable): + methods = ( + ('irc', 'IRC'), + ('im_jabber_xmpp', 'IM/Jabber/XMPP'), + ('phone', 'Phone'), + ('pgp', 'PGP fingerprint'), + ('otr', 'OTR fingerprint'), + ) + page = ParentalKey('PersonPage', related_name='contact_details') + contact_method = models.CharField(max_length=255, choices=methods, blank=True) + other_method = models.CharField(max_length=255, blank=True, verbose_name='Other') + contact_detail = models.CharField(max_length=255) panels = [ - FieldPanel('phone_number') + FieldRowPanel([ + FieldPanel('contact_method'), + FieldPanel('other_method'), + ]), + FieldPanel('contact_detail'), ] + @property + def method_display(self): + return self.other_method or self.get_contact_method_display() + + def clean(self): + if not (self.contact_method or self.other_method): + raise ValidationError({ + 'contact_method': 'Please select or type at least one contact method.', + 'other_method': '', + }) + + if self.contact_method and self.other_method: + raise ValidationError({ + 'contact_method': 'Please only select or type one contact method.', + 'other_method': '', + }) + class PersonPage(BasePage): subpage_types = [] @@ -107,7 +140,7 @@ class PersonPage(BasePage): FieldPanel('website'), MultiFieldPanel([ FieldPanel('email'), - InlinePanel('phone_numbers', label='Phone numbers'), + InlinePanel('contact_details', label='Other Contact Methods'), ], heading='Contact information'), InlinePanel('person_types', label='Person types'), FieldPanel('introduction'), diff --git a/opentech/public/people/templates/people/person_page.html b/opentech/public/people/templates/people/person_page.html index 8d860d7c9..d06d83f2e 100644 --- a/opentech/public/people/templates/people/person_page.html +++ b/opentech/public/people/templates/people/person_page.html @@ -23,11 +23,11 @@ <p>{{ page.email }}</p> {% endif %} - {% with phone_numbers=page.phone_numbers.all %} - {% if phone_numbers %} + {% with contact_details=page.contact_details.all %} + {% if contact_details %} - {% for related_phone_number in phone_numbers %} - <p>{{ related_phone_number.phone_number }}</p> + {% for contact in contact_details %} + <p>{{ contact.method_display }}: {{ contact.contact_detail }}</p> {% endfor %} {% endif %} -- GitLab