Skip to content

Commit c8dcda0

Browse files
refactor: Do not use canonical_name() for charters (ietf-tools#5818)
* fix: Enforce naming of charter docs in submit() * style: Reformat submit() with Black * refactor: Remove redundant check of charter name * style: Reformat charter_with_milestones_txt with Black * refactor: Drop canonical_name, use Path in charter_with_milestones_txt * style: Reformat review_announcement_text() with Black * style: Reformat action_announcement_text() with Black * refactor: Change uses of charter.canonical_name() to charter.name * refactor: Skip docialias when retrieving charter * refactor: Change canonical_name() to name in utils_charter.py * refactor: Use Path in read_charter_text() * refactor: Drop canonical_name, minor refactor of tests_charter.py * refactor: charter.name instead of canonical_name in milestones.py * refactor: charter.name instead of canonical_name in tests_info.py * refactor: Remove unused functions in ietf/secr/utils/groups.py * refactor: charter.canonical_name -> charter.name in templates * refactor: Remove charter handling from canonical_name Temporarily raise an exception for testing * refactor: Refactor get_charter_text() without canonical_name * refactor: Remove raise when canonical_name called on a charter * fix: Add back missing ".txt" extension * test: Test rejection of invalid charter names
1 parent 4eaaa26 commit c8dcda0

12 files changed

Lines changed: 258 additions & 170 deletions

File tree

ietf/doc/models.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -856,12 +856,6 @@ def canonical_name(self):
856856
a = self.docalias.filter(name__startswith="rfc").order_by('-name').first()
857857
if a:
858858
name = a.name
859-
elif self.type_id == "charter":
860-
from ietf.doc.utils_charter import charter_name_for_group # Imported locally to avoid circular imports
861-
try:
862-
name = charter_name_for_group(self.chartered_group)
863-
except Group.DoesNotExist:
864-
pass
865859
self._canonical_name = name
866860
return self._canonical_name
867861

ietf/doc/tests_charter.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ class EditCharterTests(TestCase):
8888
settings_temp_path_overrides = TestCase.settings_temp_path_overrides + ['CHARTER_PATH']
8989

9090
def write_charter_file(self, charter):
91-
with (Path(settings.CHARTER_PATH) /
92-
("%s-%s.txt" % (charter.canonical_name(), charter.rev))
93-
).open("w") as f:
94-
f.write("This is a charter.")
91+
(Path(settings.CHARTER_PATH) / f"{charter.name}-{charter.rev}.txt").write_text("This is a charter.")
9592

9693
def test_startstop_process(self):
9794
CharterFactory(group__acronym='mars')
@@ -509,8 +506,13 @@ def test_submit_charter(self):
509506
self.assertEqual(charter.rev, next_revision(prev_rev))
510507
self.assertTrue("new_revision" in charter.latest_event().type)
511508

512-
with (Path(settings.CHARTER_PATH) / (charter.canonical_name() + "-" + charter.rev + ".txt")).open(encoding='utf-8') as f:
513-
self.assertEqual(f.read(), "Windows line\nMac line\nUnix line\n" + utf_8_snippet.decode('utf-8'))
509+
file_contents = (
510+
Path(settings.CHARTER_PATH) / (charter.name + "-" + charter.rev + ".txt")
511+
).read_text("utf-8")
512+
self.assertEqual(
513+
file_contents,
514+
"Windows line\nMac line\nUnix line\n" + utf_8_snippet.decode("utf-8"),
515+
)
514516

515517
def test_submit_initial_charter(self):
516518
group = GroupFactory(type_id='wg',acronym='mars',list_email='mars-wg@ietf.org')
@@ -538,6 +540,24 @@ def test_submit_initial_charter(self):
538540
group = Group.objects.get(pk=group.pk)
539541
self.assertEqual(group.charter, charter)
540542

543+
def test_submit_charter_with_invalid_name(self):
544+
self.client.login(username="secretary", password="secretary+password")
545+
ietf_group = GroupFactory(type_id="wg")
546+
for bad_name in ("charter-irtf-{}", "charter-randomjunk-{}", "charter-ietf-thisisnotagroup"):
547+
url = urlreverse("ietf.doc.views_charter.submit", kwargs={"name": bad_name.format(ietf_group.acronym)})
548+
r = self.client.get(url)
549+
self.assertEqual(r.status_code, 404, f"GET of charter named {bad_name} should 404")
550+
r = self.client.post(url, {})
551+
self.assertEqual(r.status_code, 404, f"POST of charter named {bad_name} should 404")
552+
553+
irtf_group = GroupFactory(type_id="rg")
554+
for bad_name in ("charter-ietf-{}", "charter-whatisthis-{}", "charter-irtf-thisisnotagroup"):
555+
url = urlreverse("ietf.doc.views_charter.submit", kwargs={"name": bad_name.format(irtf_group.acronym)})
556+
r = self.client.get(url)
557+
self.assertEqual(r.status_code, 404, f"GET of charter named {bad_name} should 404")
558+
r = self.client.post(url, {})
559+
self.assertEqual(r.status_code, 404, f"POST of charter named {bad_name} should 404")
560+
541561
def test_edit_review_announcement_text(self):
542562
area = GroupFactory(type_id='area')
543563
RoleFactory(name_id='ad',group=area,person=Person.objects.get(user__username='ad'))

ietf/doc/utils_charter.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44

55
import datetime
6-
import io
76
import os
87
import re
98
import shutil
109

10+
from pathlib import Path
11+
1112
from django.conf import settings
1213
from django.urls import reverse as urlreverse
1314
from django.template.loader import render_to_string
@@ -62,10 +63,9 @@ def next_approved_revision(rev):
6263
return "%#02d" % (int(m.group('major')) + 1)
6364

6465
def read_charter_text(doc):
65-
filename = os.path.join(settings.CHARTER_PATH, '%s-%s.txt' % (doc.canonical_name(), doc.rev))
66+
filename = Path(settings.CHARTER_PATH) / f"{doc.name}-{doc.rev}.txt"
6667
try:
67-
with io.open(filename, 'r') as f:
68-
return f.read()
68+
return filename.read_text()
6969
except IOError:
7070
return "Error: couldn't read charter text"
7171

@@ -92,16 +92,16 @@ def change_group_state_after_charter_approval(group, by):
9292
def fix_charter_revision_after_approval(charter, by):
9393
# according to spec, 00-02 becomes 01, so copy file and record new revision
9494
try:
95-
old = os.path.join(charter.get_file_path(), '%s-%s.txt' % (charter.canonical_name(), charter.rev))
96-
new = os.path.join(charter.get_file_path(), '%s-%s.txt' % (charter.canonical_name(), next_approved_revision(charter.rev)))
95+
old = os.path.join(charter.get_file_path(), '%s-%s.txt' % (charter.name, charter.rev))
96+
new = os.path.join(charter.get_file_path(), '%s-%s.txt' % (charter.name, next_approved_revision(charter.rev)))
9797
shutil.copy(old, new)
9898
except IOError:
9999
log("There was an error copying %s to %s" % (old, new))
100100

101101
events = []
102102
e = NewRevisionDocEvent(doc=charter, by=by, type="new_revision")
103103
e.rev = next_approved_revision(charter.rev)
104-
e.desc = "New version available: <b>%s-%s.txt</b>" % (charter.canonical_name(), e.rev)
104+
e.desc = "New version available: <b>%s-%s.txt</b>" % (charter.name, e.rev)
105105
e.save()
106106
events.append(e)
107107

0 commit comments

Comments
 (0)