From 58dae78a430477122c6d61aedb3fa74a9c5449cd Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Sat, 11 Mar 2017 13:46:19 +0000 Subject: [PATCH] Modified the REST API to provide group descriptions taken from the group charters if group.description is blank. - Legacy-Id: 12993 --- ietf/group/models.py | 17 +++++++++++++++++ ietf/group/resources.py | 3 ++- ietf/utils/test_data.py | 4 ++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ietf/group/models.py b/ietf/group/models.py index 8877fad75..b77d7b08f 100644 --- a/ietf/group/models.py +++ b/ietf/group/models.py @@ -2,6 +2,7 @@ import datetime import email.utils +import re from urlparse import urljoin from django.db import models @@ -168,6 +169,22 @@ class Group(GroupInfo): status_events = status_events.filter(time__gte=previous_meeting.end_date()+datetime.timedelta(days=1)) return status_events.first() + def get_description(self): + """ + Return self.description if set, otherwise the first paragraph of the + charter if any, else a short error message. Used to provide a + fallback for self.description in group.resources.GroupResource. + """ + desc = 'No description available' + if self.description: + desc = self.description + elif self.charter: + text = self.charter.text() + # split into paragraphs and grab the first non-empty one + if text: + desc = [ p for p in re.split('\r?\n\s*\r?\n\s*', text) if p.strip() ][0] + return desc + class GroupHistory(GroupInfo): group = models.ForeignKey(Group, related_name='history_set') acronym = models.CharField(max_length=40) diff --git a/ietf/group/resources.py b/ietf/group/resources.py index 70b4e062e..c2210cba3 100644 --- a/ietf/group/resources.py +++ b/ietf/group/resources.py @@ -1,7 +1,7 @@ # Autogenerated by the mkresources management command 2014-11-13 23:15 from ietf.api import ModelResource from ietf.api import ToOneField -from tastypie.fields import ToManyField +from tastypie.fields import ToManyField, CharField from tastypie.constants import ALL, ALL_WITH_RELATIONS from tastypie.cache import SimpleCache @@ -22,6 +22,7 @@ class GroupResource(ModelResource): charter = ToOneField('ietf.doc.resources.DocumentResource', 'charter', null=True) unused_states = ToManyField('ietf.doc.resources.StateResource', 'unused_states', null=True) unused_tags = ToManyField(DocTagNameResource, 'unused_tags', null=True) + description = CharField(attribute='get_description') class Meta: cache = SimpleCache() queryset = Group.objects.all() diff --git a/ietf/utils/test_data.py b/ietf/utils/test_data.py index 3a84eed3d..bea3e5ff8 100644 --- a/ietf/utils/test_data.py +++ b/ietf/utils/test_data.py @@ -133,6 +133,7 @@ def make_test_data(): group = Group.objects.create( name="Martian Special Interest Group", acronym="mars", + description="This group discusses mars issues.", state_id="active", type_id="wg", parent=area, @@ -159,6 +160,7 @@ def make_test_data(): group = Group.objects.create( name="Asteroid Mining Equipment Standardization Group", acronym="ames", + description="This group works towards standardization of asteroid mining equipment.", state_id="proposed", type_id="wg", parent=area, @@ -185,6 +187,7 @@ def make_test_data(): frfarea = Group.objects.create( name="Far Future Area Group", acronym="frfarea", + description="This group discusses future space colonization issues.", state_id="active", type_id="ag", parent=area, @@ -195,6 +198,7 @@ def make_test_data(): irg_rg = Group.objects.create( name="Internet Research Group", acronym="irg", + description="This group handles internet research.", state_id="active", type_id="rg", parent=irtf,