Skip to content

Commit e831dc3

Browse files
committed
Add generic find_history_active_at(obj, time), refactor to use it
- Legacy-Id: 3303
1 parent ba29fa9 commit e831dc3

6 files changed

Lines changed: 35 additions & 22 deletions

File tree

ietf/meeting/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ def agenda_infoREDESIGN(num=None):
141141

142142
ads = []
143143
meeting_time = datetime.datetime.combine(meeting.date, datetime.time(0, 0, 0))
144-
from redesign.group.models import Group, find_group_history_active_at
144+
from redesign.group.models import Group
145+
from ietf.utils.history import find_history_active_at
145146
for g in Group.objects.filter(type="area").order_by("acronym"):
146-
history = find_group_history_active_at(g, meeting_time)
147+
history = find_history_active_at(g, meeting_time)
147148
if history:
148149
if history.state_id == "active":
149150
ads.extend(IESGHistory().from_role(x) for x in history.rolehistory_set.filter(name="ad"))

ietf/utils/history.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def find_history_active_at(obj, time):
2+
"""Assumes obj has a corresponding history object (e.g. obj could
3+
be Person with a corresponding PersonHistory model), then returns
4+
the history object active at time, or None if the object itself
5+
was active at the time.
6+
7+
For this to work, the history model must use
8+
related_name="history_set" for the foreign key connecting to the
9+
live model, both models must have a "time" DateTimeField and a
10+
history object must be saved with a copy of the old values and
11+
time when the time field changes.
12+
"""
13+
if obj.time <= time:
14+
return None
15+
16+
histories = obj.history_set.order_by('-time')
17+
18+
for h in histories:
19+
if h.time <= time:
20+
return h
21+
22+
return None

redesign/doc/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Meta:
137137
ordering = ["document", "order"]
138138

139139
class DocHistory(DocumentInfo):
140-
doc = models.ForeignKey(Document) # ID of the Document this relates to
140+
doc = models.ForeignKey(Document, related_name="history_set")
141141
# Django 1.2 won't let us define these in the base class, so we have
142142
# to repeat them
143143
related = models.ManyToManyField('DocAlias', through=RelatedDocHistory, blank=True)

redesign/doc/proxy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def cur_state_id(self):
349349
#prev_state = models.ForeignKey(IDState, db_column='prev_state', related_name='docs_prev')
350350
@property
351351
def prev_state(self):
352-
ds = self.dochistory_set.exclude(iesg_state=self.iesg_state).order_by('-time')[:1]
352+
ds = self.history_set.exclude(iesg_state=self.iesg_state).order_by('-time')[:1]
353353
return IDState().from_object(ds[0].iesg_state) if ds else None
354354

355355
#assigned_to = models.CharField(blank=True, max_length=25) # unused
@@ -403,7 +403,7 @@ def cur_sub_state_id(self):
403403
#prev_sub_state = BrokenForeignKey(IDSubState, related_name='docs_prev', null=True, blank=True, null_values=(0, -1))
404404
@property
405405
def prev_sub_state(self):
406-
ds = self.dochistory_set.all().order_by('-time')[:1]
406+
ds = self.history_set.all().order_by('-time')[:1]
407407
substates = ds[0].tags.filter(slug__in=['extpty', 'need-rev', 'ad-f-up', 'point']) if ds else None
408408
return IDSubState().from_object(substates[0]) if substates else None
409409
@property

redesign/group/models.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def latest_event(self, *args, **filter_args):
3939
# to a group. The group acronym must be unique and is the invariant used
4040
# to select group history from this table.
4141
class GroupHistory(GroupInfo):
42-
group = models.ForeignKey('Group', related_name='group_history')
42+
group = models.ForeignKey(Group, related_name='history_set')
4343
charter = models.ForeignKey('doc.Document', related_name='chartered_group_history_set', blank=True, null=True)
4444

4545
class Meta:
@@ -91,24 +91,13 @@ def __unicode__(self):
9191

9292

9393
class RoleHistory(models.Model):
94+
# RoleHistory doesn't have a time field as it's not supposed to be
95+
# used on its own - there should always be a GroupHistory
96+
# accompanying a change in roles, so lookup the appropriate
97+
# GroupHistory instead
9498
name = models.ForeignKey(RoleName)
9599
group = models.ForeignKey(GroupHistory)
96100
email = models.ForeignKey(Email, help_text="Email address used by person for this role")
97101
auth = models.CharField(max_length=255, blank=True) # unused?
98102
def __unicode__(self):
99103
return u"%s is %s in %s" % (self.email.get_name(), self.name.name, self.group.acronym)
100-
101-
102-
def find_group_history_active_at(group, time):
103-
"""Return the GroupHistory object active at time, or None if the
104-
group itself was active at the time."""
105-
if group.time <= time:
106-
return None
107-
108-
histories = group.group_history.order_by('-time')
109-
110-
for h in histories:
111-
if h.time <= time:
112-
return h
113-
114-
return None

redesign/importing/import-roles.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from ietf.idtracker.models import IESGLogin, AreaDirector, PersonOrOrgInfo, WGChair, WGEditor, WGSecretary, WGTechAdvisor, ChairsHistory, Role as OldRole, Acronym, IRTFChair
2121
from ietf.proceedings.models import IESGHistory
22+
from ietf.utils.history import *
2223

2324

2425
# assumptions:
@@ -195,7 +196,7 @@
195196

196197
emails_for_time[key].append(email)
197198

198-
history = find_group_history_active_at(area, meeting_time)
199+
history = find_history_active_at(area, meeting_time)
199200
if (history and history.rolehistory_set.filter(email__person=email.person) or
200201
not history and area.role_set.filter(email__person=email.person)):
201202
continue

0 commit comments

Comments
 (0)