-
Notifications
You must be signed in to change notification settings - Fork 347
/
models.py
1523 lines (1272 loc) · 62.8 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright The IETF Trust 2010-2023, All Rights Reserved
# -*- coding: utf-8 -*-
import datetime
import logging
import io
import os
import django.db
import rfc2html
from pathlib import Path
from lxml import etree
from typing import Optional, TYPE_CHECKING
from weasyprint import HTML as wpHTML
from weasyprint.text.fonts import FontConfiguration
from django.db import models
from django.core import checks
from django.core.cache import caches
from django.core.validators import URLValidator, RegexValidator
from django.urls import reverse as urlreverse
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.utils import timezone
from django.utils.encoding import force_str
from django.utils.html import mark_safe # type:ignore
from django.contrib.staticfiles import finders
import debug # pyflakes:ignore
from ietf.group.models import Group
from ietf.name.models import ( DocTypeName, DocTagName, StreamName, IntendedStdLevelName, StdLevelName,
DocRelationshipName, DocReminderTypeName, BallotPositionName, ReviewRequestStateName, ReviewAssignmentStateName, FormalLanguageName,
DocUrlTagName, ExtResourceName)
from ietf.person.models import Email, Person
from ietf.person.utils import get_active_balloters
from ietf.utils import log
from ietf.utils.decorators import memoize
from ietf.utils.validators import validate_no_control_chars
from ietf.utils.mail import formataddr
from ietf.utils.models import ForeignKey
from ietf.utils.timezone import date_today, RPC_TZINFO, DEADLINE_TZINFO
if TYPE_CHECKING:
# importing other than for type checking causes errors due to cyclic imports
from ietf.meeting.models import ProceedingsMaterial, Session
logger = logging.getLogger('django')
class StateType(models.Model):
slug = models.CharField(primary_key=True, max_length=30) # draft, draft-iesg, charter, ...
label = models.CharField(max_length=255, help_text="Label that should be used (e.g. in admin) for state drop-down for this type of state") # State, IESG state, WG state, ...
def __str__(self):
return self.slug
@checks.register('db-consistency')
def check_statetype_slugs(app_configs, **kwargs):
errors = []
try:
state_type_slugs = [ t.slug for t in StateType.objects.all() ]
except django.db.ProgrammingError:
# When running initial migrations on an empty DB, attempting to retrieve StateType will raise a
# ProgrammingError. Until Django 3, there is no option to skip the checks.
return []
else:
for type in DocTypeName.objects.all():
if not type.slug in state_type_slugs:
errors.append(checks.Error(
"The document type '%s (%s)' does not have a corresponding entry in the doc.StateType table" % (type.name, type.slug),
hint="You should add a doc.StateType entry with a slug '%s' to match the DocTypeName slug."%(type.slug),
obj=type,
id='datatracker.doc.E0015',
))
return errors
class State(models.Model):
type = ForeignKey(StateType)
slug = models.SlugField()
name = models.CharField(max_length=255)
used = models.BooleanField(default=True)
desc = models.TextField(blank=True)
order = models.IntegerField(default=0)
next_states = models.ManyToManyField('doc.State', related_name="previous_states", blank=True)
def __str__(self):
return self.name
class Meta:
ordering = ["type", "order"]
IESG_BALLOT_ACTIVE_STATES = ("lc", "writeupw", "goaheadw", "iesg-eva", "defer")
IESG_CHARTER_ACTIVE_STATES = ("intrev", "extrev", "iesgrev")
IESG_STATCHG_CONFLREV_ACTIVE_STATES = ("iesgeval", "defer")
IESG_SUBSTATE_TAGS = ('ad-f-up', 'need-rev', 'extpty')
class DocumentInfo(models.Model):
"""Any kind of document. Draft, RFC, Charter, IPR Statement, Liaison Statement"""
time = models.DateTimeField(default=timezone.now) # should probably have auto_now=True
type = ForeignKey(DocTypeName, blank=True, null=True) # Draft, Agenda, Minutes, Charter, Discuss, Guideline, Email, Review, Issue, Wiki, External ...
title = models.CharField(max_length=255, validators=[validate_no_control_chars, ])
states = models.ManyToManyField(State, blank=True) # plain state (Active/Expired/...), IESG state, stream state
tags = models.ManyToManyField(DocTagName, blank=True) # Revised ID Needed, ExternalParty, AD Followup, ...
stream = ForeignKey(StreamName, blank=True, null=True) # IETF, IAB, IRTF, Independent Submission, Editorial
group = ForeignKey(Group, blank=True, null=True) # WG, RG, IAB, IESG, Edu, Tools
abstract = models.TextField(blank=True)
rev = models.CharField(verbose_name="revision", max_length=16, blank=True)
pages = models.IntegerField(blank=True, null=True)
words = models.IntegerField(blank=True, null=True)
formal_languages = models.ManyToManyField(FormalLanguageName, blank=True, help_text="Formal languages used in document")
intended_std_level = ForeignKey(IntendedStdLevelName, verbose_name="Intended standardization level", blank=True, null=True)
std_level = ForeignKey(StdLevelName, verbose_name="Standardization level", blank=True, null=True)
ad = ForeignKey(Person, verbose_name="area director", related_name='ad_%(class)s_set', blank=True, null=True)
shepherd = ForeignKey(Email, related_name='shepherd_%(class)s_set', blank=True, null=True)
expires = models.DateTimeField(blank=True, null=True)
notify = models.TextField(max_length=1023, blank=True)
external_url = models.URLField(blank=True)
uploaded_filename = models.TextField(blank=True)
note = models.TextField(blank=True)
rfc_number = models.PositiveIntegerField(blank=True, null=True) # only valid for type="rfc"
def file_extension(self):
if not hasattr(self, '_cached_extension'):
if self.uploaded_filename:
_, ext= os.path.splitext(self.uploaded_filename)
self._cached_extension = ext.lstrip(".").lower()
else:
self._cached_extension = "txt"
return self._cached_extension
def get_file_path(self):
if not hasattr(self, '_cached_file_path'):
if self.type_id == "rfc":
self._cached_file_path = settings.RFC_PATH
elif self.type_id == "draft":
if self.is_dochistory():
self._cached_file_path = settings.INTERNET_ALL_DRAFTS_ARCHIVE_DIR
else:
# This could be simplified since anything in INTERNET_DRAFT_PATH is also already in INTERNET_ALL_DRAFTS_ARCHIVE_DIR
draft_state = self.get_state('draft')
if draft_state and draft_state.slug == 'active':
self._cached_file_path = settings.INTERNET_DRAFT_PATH
else:
self._cached_file_path = settings.INTERNET_ALL_DRAFTS_ARCHIVE_DIR
elif self.meeting_related() and self.type_id in (
"agenda", "minutes", "narrativeminutes", "slides", "bluesheets", "procmaterials", "chatlog", "polls"
):
meeting = self.get_related_meeting()
if meeting is not None:
self._cached_file_path = os.path.join(meeting.get_materials_path(), self.type_id) + "/"
else:
self._cached_file_path = ""
elif self.type_id == "charter":
self._cached_file_path = settings.CHARTER_PATH
elif self.type_id == "conflrev":
self._cached_file_path = settings.CONFLICT_REVIEW_PATH
elif self.type_id == "statchg":
self._cached_file_path = settings.STATUS_CHANGE_PATH
elif self.type_id == "bofreq": # TODO: This is probably unneeded, as is the separate path setting
self._cached_file_path = settings.BOFREQ_PATH
else:
self._cached_file_path = settings.DOCUMENT_PATH_PATTERN.format(doc=self)
return self._cached_file_path
def get_base_name(self):
if not hasattr(self, '_cached_base_name'):
if self.uploaded_filename:
self._cached_base_name = self.uploaded_filename
elif self.type_id == 'rfc':
self._cached_base_name = "%s.txt" % self.name
elif self.type_id == 'draft':
if self.is_dochistory():
self._cached_base_name = "%s-%s.txt" % (self.doc.name, self.rev)
else:
self._cached_base_name = "%s-%s.txt" % (self.name, self.rev)
elif self.type_id in ["slides", "agenda", "minutes", "bluesheets", "procmaterials", ] and self.meeting_related():
ext = 'pdf' if self.type_id == 'procmaterials' else 'txt'
self._cached_base_name = f'{self.name}-{self.rev}.{ext}'
elif self.type_id == 'review':
# TODO: This will be wrong if a review is updated on the same day it was created (or updated more than once on the same day)
self._cached_base_name = "%s.txt" % self.name
elif self.type_id in ['bofreq', 'statement']:
self._cached_base_name = "%s-%s.md" % (self.name, self.rev)
else:
if self.rev:
self._cached_base_name = "%s-%s.txt" % (self.name, self.rev)
else:
self._cached_base_name = "%s.txt" % (self.name, )
return self._cached_base_name
def get_file_name(self):
if not hasattr(self, '_cached_file_name'):
self._cached_file_name = os.path.join(self.get_file_path(), self.get_base_name())
return self._cached_file_name
def revisions_by_dochistory(self):
revisions = []
if self.type_id != "rfc":
for h in self.history_set.order_by("time", "id"):
if h.rev and not h.rev in revisions:
revisions.append(h.rev)
if not self.rev in revisions:
revisions.append(self.rev)
return revisions
def revisions_by_newrevisionevent(self):
revisions = []
if self.type_id != "rfc":
doc = self.doc if isinstance(self, DocHistory) else self
for e in doc.docevent_set.filter(type='new_revision').distinct():
if e.rev and not e.rev in revisions:
revisions.append(e.rev)
if not doc.rev in revisions:
revisions.append(doc.rev)
revisions.sort()
return revisions
def get_href(self, meeting=None):
return self._get_ref(meeting=meeting,meeting_doc_refs=settings.MEETING_DOC_HREFS)
def get_versionless_href(self, meeting=None):
return self._get_ref(meeting=meeting,meeting_doc_refs=settings.MEETING_DOC_GREFS)
def _get_ref(self, meeting=None, meeting_doc_refs=settings.MEETING_DOC_HREFS):
"""
Returns an url to the document text. This differs from .get_absolute_url(),
which returns an url to the datatracker page for the document.
"""
# If self.external_url truly is an url, use it. This is a change from
# the earlier resolution order, but there's at the moment one single
# instance which matches this (with correct results), so we won't
# break things all over the place.
if not hasattr(self, '_cached_href'):
validator = URLValidator()
if self.external_url and self.external_url.split(':')[0] in validator.schemes:
validator(self.external_url)
return self.external_url
if self.type_id in settings.DOC_HREFS and self.type_id in meeting_doc_refs:
if self.meeting_related():
self.is_meeting_related = True
format = meeting_doc_refs[self.type_id]
else:
self.is_meeting_related = False
format = settings.DOC_HREFS[self.type_id]
elif self.type_id in settings.DOC_HREFS:
self.is_meeting_related = False
if self.type_id == "rfc":
format = settings.DOC_HREFS['rfc']
else:
format = settings.DOC_HREFS[self.type_id]
elif self.type_id in meeting_doc_refs:
self.is_meeting_related = True
else:
return None
if self.is_meeting_related:
if not meeting:
meeting = self.get_related_meeting()
if meeting is None:
return ''
# After IETF 96, meeting materials acquired revision
# handling, and the document naming changed.
if meeting.proceedings_format_version == 1:
format = settings.MEETING_DOC_OLD_HREFS[self.type_id]
else:
# This branch includes interims
format = meeting_doc_refs[self.type_id]
info = dict(doc=self, meeting=meeting)
else:
info = dict(doc=self)
href = format.format(**info)
# For slides that are not meeting-related, we need to know the file extension.
# Assume we have access to the same files as settings.DOC_HREFS["slides"] and
# see what extension is available
if self.type_id == "slides" and not self.meeting_related() and not href.endswith("/"):
filepath = Path(self.get_file_path()) / self.get_base_name() # start with this
if not filepath.exists():
# Look for other extensions - grab the first one, sorted for stability
for existing in sorted(filepath.parent.glob(f"{filepath.stem}.*")):
filepath = filepath.with_suffix(existing.suffix)
break
href += filepath.suffix # tack on the extension
if href.startswith('/'):
href = settings.IDTRACKER_BASE_URL + href
self._cached_href = href
return self._cached_href
def set_state(self, state):
"""Switch state type implicit in state to state. This just
sets the state, doesn't log the change."""
already_set = self.states.filter(type=state.type)
others = [s for s in already_set if s != state]
if others:
self.states.remove(*others)
if state not in already_set:
self.states.add(state)
if state.type and state.type.slug == 'draft-iesg':
iesg_state = self.states.get(type_id="draft-iesg") # pyflakes:ignore
log.assertion('iesg_state', note="A document's 'draft-iesg' state should never be unset'. Failed for %s"%self.name)
self.state_cache = None # invalidate cache
self._cached_state_slug = {}
def unset_state(self, state_type):
"""Unset state of type so no state of that type is any longer set."""
log.assertion('state_type != "draft-iesg"')
self.states.remove(*self.states.filter(type=state_type))
self.state_cache = None # invalidate cache
self._cached_state_slug = {}
def get_state(self, state_type=None):
"""Get state of type, or default state for document type if
not specified. Uses a local cache to speed multiple state
reads up."""
if self.pk == None: # states is many-to-many so not in database implies no state
return None
if state_type == None:
state_type = self.type_id
if not hasattr(self, "state_cache") or self.state_cache == None:
state_cache = {}
for s in self.states.all():
state_cache[s.type_id] = s
self.state_cache = state_cache
return self.state_cache.get(state_type, None)
def get_state_slug(self, state_type=None):
"""Get state of type, or default if not specified, returning
the slug of the state or None. This frees the caller of having
to check against None before accessing the slug for a
comparison."""
if not hasattr(self, '_cached_state_slug'):
self._cached_state_slug = {}
if not state_type in self._cached_state_slug:
s = self.get_state(state_type)
self._cached_state_slug[state_type] = s.slug if s else None
return self._cached_state_slug[state_type]
def friendly_state(self):
""" Return a concise text description of the document's current state."""
state = self.get_state()
if not state:
return "Unknown state"
if self.type_id == "rfc":
return f"RFC {self.rfc_number} ({self.std_level})"
elif self.type_id == 'draft':
iesg_state = self.get_state("draft-iesg")
iesg_state_summary = None
if iesg_state:
iesg_substate = [t for t in self.tags.all() if t.slug in IESG_SUBSTATE_TAGS]
# There really shouldn't be more than one tag in iesg_substate, but this will do something sort-of-sensible if there is
iesg_state_summary = iesg_state.name
if iesg_substate:
iesg_state_summary = iesg_state_summary + "::"+"::".join(tag.name for tag in iesg_substate)
rfc = self.became_rfc()
if rfc:
return f"Became RFC {rfc.rfc_number} ({rfc.std_level})"
elif state.slug == "repl":
rs = self.related_that("replaces")
if rs:
return mark_safe("Replaced by " + ", ".join("<a href=\"%s\">%s</a>" % (urlreverse('ietf.doc.views_doc.document_main', kwargs=dict(name=related.name)), related) for related in rs))
else:
return "Replaced"
elif state.slug == "active":
if iesg_state:
if iesg_state.slug == "dead":
# Many drafts in the draft-iesg "Dead" state are not dead
# in other state machines; they're just not currently under
# IESG processing. Show them as "I-D Exists (IESG: Dead)" instead...
return "I-D Exists (IESG: %s)" % iesg_state_summary
elif iesg_state.slug == "lc":
e = self.latest_event(LastCallDocEvent, type="sent_last_call")
if e:
return iesg_state_summary + " (ends %s)" % e.expires.astimezone(DEADLINE_TZINFO).date().isoformat()
return iesg_state_summary
else:
return "I-D Exists"
else:
if iesg_state and iesg_state.slug == "dead":
return state.name + " (IESG: %s)" % iesg_state_summary
# Expired/Withdrawn by Submitter/IETF
return state.name
else:
return state.name
def author_list(self):
best_addresses = []
for author in self.documentauthor_set.all():
if author.email:
if author.email.active or not author.email.person:
best_addresses.append(author.email.address)
else:
best_addresses.append(author.email.person.email_address())
return ", ".join(best_addresses)
def authors(self):
return [ a.person for a in self.documentauthor_set.all() ]
# This, and several other ballot related functions here, assume that there is only one active ballot for a document at any point in time.
# If that assumption is violated, they will only expose the most recently created ballot
def ballot_open(self, ballot_type_slug):
e = self.latest_event(BallotDocEvent, ballot_type__slug=ballot_type_slug)
return e if e and not e.type == "closed_ballot" else None
def latest_ballot(self):
"""Returns the most recently created ballot"""
ballot = self.latest_event(BallotDocEvent, type__in=("created_ballot", "closed_ballot"))
return ballot
def active_ballot(self):
"""Returns the most recently created ballot if it isn't closed."""
ballot = self.latest_ballot()
if ballot and ballot.type == "created_ballot":
return ballot
else:
return None
def has_rfc_editor_note(self):
e = self.latest_event(WriteupDocEvent, type="changed_rfc_editor_note_text")
return e != None and (e.text != "")
def meeting_related(self):
if self.type_id in ("agenda","minutes", "narrativeminutes", "bluesheets","slides","recording","procmaterials","chatlog","polls"):
return self.type_id != "slides" or self.get_state_slug('reuse_policy')=='single'
return False
def get_related_session(self) -> Optional['Session']:
"""Get the meeting session related to this document
Return None if there is no related session.
Must define this in DocumentInfo subclasses.
"""
raise NotImplementedError(f'Class {self.__class__} must define get_related_session()')
def get_related_proceedings_material(self) -> Optional['ProceedingsMaterial']:
"""Get the proceedings material related to this document
Return None if there is no related proceedings material.
Must define this in DocumentInfo subclasses.
"""
raise NotImplementedError(f'Class {self.__class__} must define get_related_proceedings_material()')
def get_related_meeting(self):
"""Get the meeting this document relates to"""
if not self.meeting_related():
return None # no related meeting if not meeting_related!
# get an item that links this doc to a meeting
item = self.get_related_session() or self.get_related_proceedings_material()
return getattr(item, 'meeting', None)
def relations_that(self, relationship):
"""Return the related-document objects that describe a given relationship targeting self."""
if isinstance(relationship, str):
relationship = ( relationship, )
if not isinstance(relationship, tuple):
raise TypeError("Expected a string or tuple, received %s" % type(relationship))
if isinstance(self, Document):
return RelatedDocument.objects.filter(target=self, relationship__in=relationship).select_related('source')
elif isinstance(self, DocHistory):
return RelatedDocHistory.objects.filter(target=self.doc, relationship__in=relationship).select_related('source')
else:
raise TypeError("Expected method called on Document or DocHistory")
def all_relations_that(self, relationship, related=None):
if not related:
related = tuple([])
rels = self.relations_that(relationship)
for r in rels:
if not r in related:
related += ( r, )
related = r.source.all_relations_that(relationship, related)
return related
def relations_that_doc(self, relationship):
"""Return the related-document objects that describe a given relationship from self to other documents."""
if isinstance(relationship, str):
relationship = ( relationship, )
if not isinstance(relationship, tuple):
raise TypeError("Expected a string or tuple, received %s" % type(relationship))
if isinstance(self, Document):
return RelatedDocument.objects.filter(source=self, relationship__in=relationship).select_related('target')
elif isinstance(self, DocHistory):
return RelatedDocHistory.objects.filter(source=self, relationship__in=relationship).select_related('target')
else:
raise TypeError("Expected method called on Document or DocHistory")
def all_relations_that_doc(self, relationship, related=None):
if not related:
related = tuple([])
rels = self.relations_that_doc(relationship)
for r in rels:
if not r in related:
related += ( r, )
related = r.target.all_relations_that_doc(relationship, related)
return related
def related_that(self, relationship):
return list(set([x.source for x in self.relations_that(relationship)]))
def all_related_that(self, relationship, related=None):
return list(set([x.source for x in self.all_relations_that(relationship)]))
def related_that_doc(self, relationship):
return list(set([x.target for x in self.relations_that_doc(relationship)]))
def all_related_that_doc(self, relationship, related=None):
return list(set([x.target for x in self.all_relations_that_doc(relationship)]))
def replaces(self):
return self.related_that_doc("replaces")
def replaced_by(self):
return set([ r.document for r in self.related_that("replaces") ])
def text(self):
path = self.get_file_name()
root, ext = os.path.splitext(path)
txtpath = root+'.txt'
if ext != '.txt' and os.path.exists(txtpath):
path = txtpath
try:
with io.open(path, 'rb') as file:
raw = file.read()
except IOError:
return None
try:
text = raw.decode('utf-8')
except UnicodeDecodeError:
text = raw.decode('latin-1')
#
return text
def text_or_error(self):
return self.text() or "Error; cannot read '%s'"%self.get_base_name()
def html_body(self, classes=""):
if self.type_id == "rfc":
try:
html = Path(
os.path.join(settings.RFC_PATH, self.name + ".html")
).read_text()
except (IOError, UnicodeDecodeError):
return None
else:
try:
html = Path(
os.path.join(
settings.INTERNET_ALL_DRAFTS_ARCHIVE_DIR,
self.name + "-" + self.rev + ".html",
)
).read_text()
except (IOError, UnicodeDecodeError):
return None
# If HTML was generated by rfc2html, do not return it. Caller
# will use htmlize() to use a more current rfc2html to
# generate an HTMLized version. TODO: There should be a
# better way to determine how an HTML format was generated.
if html.startswith("<pre>"):
return None
# get body
etree_html = etree.HTML(html)
if etree_html is None:
return None
body = etree_html.xpath("//body")[0]
body.tag = "div"
if classes:
body.attrib["class"] = classes
# remove things
for tag in ["script"]:
for t in body.xpath(f"//{tag}"):
t.getparent().remove(t)
html = etree.tostring(body, encoding=str, method="html")
return html
def htmlized(self):
name = self.get_base_name()
text = self.text()
if name.endswith('.html'):
return text
if not name.endswith('.txt'):
return None
html = ""
if text:
cache = caches['htmlized']
cache_key = name.split('.')[0]
try:
html = cache.get(cache_key)
except EOFError:
html = None
if not html:
# The path here has to match the urlpattern for htmlized
# documents in order to produce correct intra-document links
html = rfc2html.markup(text, path=settings.HTMLIZER_URL_PREFIX)
html = f'<div class="rfcmarkup">{html}</div>'
if html:
cache.set(cache_key, html, settings.HTMLIZER_CACHE_TIME)
return html
def pdfized(self):
name = self.get_base_name()
text = self.html_body(classes="rfchtml")
stylesheets = [finders.find("ietf/css/document_html_referenced.css")]
if text:
stylesheets.append(finders.find("ietf/css/document_html_txt.css"))
else:
text = self.htmlized()
stylesheets.append(f'{settings.STATIC_IETF_ORG_INTERNAL}/fonts/noto-sans-mono/import.css')
cache = caches["pdfized"]
cache_key = name.split(".")[0]
try:
pdf = cache.get(cache_key)
except EOFError:
pdf = None
if not pdf:
try:
font_config = FontConfiguration()
pdf = wpHTML(
string=text, base_url=settings.IDTRACKER_BASE_URL
).write_pdf(
stylesheets=stylesheets,
font_config=font_config,
presentational_hints=True,
optimize_images=True,
)
except AssertionError:
pdf = None
except Exception as e:
log.log('weasyprint failed:'+str(e))
raise
if pdf:
cache.set(cache_key, pdf, settings.PDFIZER_CACHE_TIME)
return pdf
def references(self):
return self.relations_that_doc(('refnorm','refinfo','refunk','refold'))
def referenced_by(self):
return self.relations_that(("refnorm", "refinfo", "refunk", "refold")).filter(
models.Q(
source__type__slug="draft",
source__states__type__slug="draft",
source__states__slug="active",
)
| models.Q(source__type__slug="rfc")
).distinct()
def referenced_by_rfcs(self):
"""Get refs to this doc from RFCs"""
return self.relations_that(("refnorm", "refinfo", "refunk", "refold")).filter(
source__type__slug="rfc"
)
def became_rfc(self):
if not hasattr(self, "_cached_became_rfc"):
doc = self if isinstance(self, Document) else self.doc
self._cached_became_rfc = next(iter(doc.related_that_doc("became_rfc")), None)
return self._cached_became_rfc
def came_from_draft(self):
if not hasattr(self, "_cached_came_from_draft"):
doc = self if isinstance(self, Document) else self.doc
self._cached_came_from_draft = next(iter(doc.related_that("became_rfc")), None)
return self._cached_came_from_draft
def contains(self):
return self.related_that_doc("contains")
def part_of(self):
return self.related_that("contains")
def referenced_by_rfcs_as_rfc_or_draft(self):
"""Get refs to this doc, or a draft/rfc it came from, from an RFC"""
refs_to = self.referenced_by_rfcs()
if self.type_id == "rfc" and self.came_from_draft():
refs_to |= self.came_from_draft().referenced_by_rfcs()
return refs_to
class Meta:
abstract = True
STATUSCHANGE_RELATIONS = ('tops','tois','tohist','toinf','tobcp','toexp')
class RelatedDocument(models.Model):
source = ForeignKey('Document')
target = ForeignKey('Document', related_name='targets_related')
relationship = ForeignKey(DocRelationshipName)
originaltargetaliasname = models.CharField(max_length=255, null=True, blank=True)
def action(self):
return self.relationship.name
def __str__(self):
return u"%s %s %s" % (self.source.name, self.relationship.name.lower(), self.target.name)
def is_downref(self):
if self.source.type_id not in ["draft","rfc"] or self.relationship.slug not in [
"refnorm",
"refold",
"refunk",
]:
return None
if self.source.type_id == "rfc":
source_lvl = self.source.std_level_id
elif self.source.type_id in ["bcp","std"]:
source_lvl = self.source.type_id
else:
source_lvl = self.source.intended_std_level_id
if source_lvl not in ["bcp", "ps", "ds", "std", "unkn"]:
return None
if self.target.type_id == 'rfc':
if not self.target.std_level:
target_lvl = 'unkn'
else:
target_lvl = self.target.std_level_id
elif self.target.type_id in ["bcp", "std"]:
target_lvl = self.target.type_id
else:
if not self.target.intended_std_level:
target_lvl = 'unkn'
else:
target_lvl = self.target.intended_std_level_id
if self.relationship.slug not in ["refnorm", "refunk"]:
return None
if source_lvl in ["inf", "exp"]:
return None
pos_downref = (
"Downref" if self.relationship_id != "refunk" else "Possible Downref"
)
if source_lvl in ["bcp", "ps", "ds", "std"] and target_lvl in ["inf", "exp"]:
return pos_downref
if source_lvl == "ds" and target_lvl == "ps":
return pos_downref
if source_lvl == "std" and target_lvl in ["ps", "ds"]:
return pos_downref
if source_lvl not in ["inf", "exp"] and target_lvl == "unkn":
return "Possible Downref"
if source_lvl == "unkn" and target_lvl in ["ps", "ds"]:
return "Possible Downref"
return None
def is_approved_downref(self):
if self.target.type_id == 'rfc':
if RelatedDocument.objects.filter(relationship_id='downref-approval', target=self.target).exists():
return "Approved Downref"
return False
class DocumentAuthorInfo(models.Model):
person = ForeignKey(Person)
# email should only be null for some historic documents
email = ForeignKey(Email, help_text="Email address used by author for submission", blank=True, null=True)
affiliation = models.CharField(max_length=100, blank=True, help_text="Organization/company used by author for submission")
country = models.CharField(max_length=255, blank=True, help_text="Country used by author for submission")
order = models.IntegerField(default=1)
def formatted_email(self):
if self.email:
return formataddr((self.person.plain_ascii(), self.email.address))
else:
return ""
class Meta:
abstract = True
ordering = ["document", "order"]
indexes = [
models.Index(fields=['document', 'order']),
]
class DocumentAuthor(DocumentAuthorInfo):
document = ForeignKey('Document')
def __str__(self):
return u"%s %s (%s)" % (self.document.name, self.person, self.order)
class DocumentActionHolder(models.Model):
"""Action holder for a document"""
document = ForeignKey('Document')
person = ForeignKey(Person)
time_added = models.DateTimeField(default=timezone.now)
CLEAR_ACTION_HOLDERS_STATES = ['approved', 'ann', 'rfcqueue', 'pub', 'dead'] # draft-iesg state slugs
GROUP_ROLES_OF_INTEREST = ['chair', 'techadv', 'editor', 'secr']
def __str__(self):
return str(self.person)
class Meta:
constraints = [
models.UniqueConstraint(fields=['document', 'person'], name='unique_action_holder')
]
def role_for_doc(self):
"""Brief string description of this person's relationship to the doc"""
roles = []
if self.person in self.document.authors():
roles.append('Author')
if self.person == self.document.ad:
roles.append('Responsible AD')
if self.document.shepherd and self.person == self.document.shepherd.person:
roles.append('Shepherd')
if self.document.group:
roles.extend([
'Group %s' % role.name.name
for role in self.document.group.role_set.filter(
name__in=self.GROUP_ROLES_OF_INTEREST,
person=self.person,
)
])
if not roles:
roles.append('Action Holder')
return ', '.join(roles)
validate_docname = RegexValidator(
r'^[-a-z0-9]+$',
"Provide a valid document name consisting of lowercase letters, numbers and hyphens.",
'invalid'
)
class Document(DocumentInfo):
name = models.CharField(max_length=255, validators=[validate_docname,], unique=True) # immutable
action_holders = models.ManyToManyField(Person, through=DocumentActionHolder, blank=True)
def __str__(self):
return self.name
def get_absolute_url(self):
"""
Returns an url to the document view. This differs from .get_href(),
which returns an url to the document content.
"""
if not hasattr(self, '_cached_absolute_url'):
name = self.name
url = None
if self.type_id == "draft" and self.get_state_slug() == "rfc":
name = self.name
url = urlreverse('ietf.doc.views_doc.document_main', kwargs={ 'name': name }, urlconf="ietf.urls")
elif self.type_id in ('slides','bluesheets','recording'):
session = self.session_set.first()
if session:
meeting = session.meeting
if self.type_id == 'recording':
url = self.external_url
else:
filename = self.uploaded_filename
url = '%sproceedings/%s/%s/%s' % (settings.IETF_HOST_URL,meeting.number,self.type_id,filename)
else:
url = urlreverse('ietf.doc.views_doc.document_main', kwargs={ 'name': name }, urlconf="ietf.urls")
self._cached_absolute_url = url
return self._cached_absolute_url
def get_related_session(self):
sessions = self.session_set.all()
return sessions.first()
def get_related_proceedings_material(self):
return self.proceedingsmaterial_set.first()
def file_tag(self):
return "<%s>" % self.filename_with_rev()
def filename_with_rev(self):
return "%s-%s.txt" % (self.name, self.rev)
def latest_event(self, *args, **filter_args):
"""Get latest event of optional Python type and with filter
arguments, e.g. d.latest_event(type="xyz") returns a DocEvent
while d.latest_event(WriteupDocEvent, type="xyz") returns a
WriteupDocEvent event."""
model = args[0] if args else DocEvent
e = model.objects.filter(doc=self).filter(**filter_args).order_by('-time', '-id').first()
return e
def display_name(self):
name = self.name
if name.startswith('rfc'):
name = name.upper()
return name
def save_with_history(self, events):
"""Save document and put a snapshot in the history models where they
can be retrieved later. You must pass in at least one event
with a description of what happened."""
assert events, "You must always add at least one event to describe the changes in the history log"
self.time = max(self.time, events[0].time)
self._has_an_event_so_saving_is_allowed = True
self.save()
del self._has_an_event_so_saving_is_allowed
from ietf.doc.utils import save_document_in_history
save_document_in_history(self)
def save(self, *args, **kwargs):
# if there's no primary key yet, we can allow the save to go
# through to break the cycle between the document and any
# events
assert kwargs.get("force_insert", False) or getattr(self, "_has_an_event_so_saving_is_allowed", None), "Use .save_with_history to save documents"
super(Document, self).save(*args, **kwargs)
def telechat_date(self, e=None):
if not e:
e = self.latest_event(TelechatDocEvent, type="scheduled_for_telechat")
return e.telechat_date if e and e.telechat_date and e.telechat_date >= date_today(settings.TIME_ZONE) else None
def past_telechat_date(self):
"Return the latest telechat date if it isn't in the future; else None"
e = self.latest_event(TelechatDocEvent, type="scheduled_for_telechat")
return e.telechat_date if e and e.telechat_date and e.telechat_date < date_today(settings.TIME_ZONE) else None
def previous_telechat_date(self):
"Return the most recent telechat date in the past, if any (even if there's another in the future)"
e = self.latest_event(
TelechatDocEvent,
type="scheduled_for_telechat",
telechat_date__lt=date_today(settings.TIME_ZONE),
)
return e.telechat_date if e else None
def request_closed_time(self, review_req):
e = self.latest_event(ReviewRequestDocEvent, type="closed_review_request", review_request=review_req)
return e.time if e and e.time else None
def area_acronym(self):
g = self.group
if g:
if g.type_id == "area":
return g.acronym
elif g.type_id != "individ" and g.parent:
return g.parent.acronym
else:
return None
def group_acronym(self):
g = self.group
if g and g.type_id != "area":
return g.acronym
else:
return "none"
@memoize
def returning_item(self):
e = self.latest_event(TelechatDocEvent, type="scheduled_for_telechat")
return e.returning_item if e else None
# This is brittle. Resist the temptation to make it more brittle by combining the search against those description
# strings to one command. It is coincidence that those states have the same description - one might change.
# Also, this needs further review - is it really the case that there would be no other changed_document events
# between when the state was changed to defer and when some bit of code wants to know if we are deferred? Why
# isn't this just returning whether the state is currently a defer state for that document type?
def active_defer_event(self):
if self.type_id == "draft" and self.get_state_slug("draft-iesg") == "defer":
return self.latest_event(type="changed_state", desc__icontains="State changed to <b>IESG Evaluation - Defer</b>")
elif self.type_id == "conflrev" and self.get_state_slug("conflrev") == "defer":
return self.latest_event(type="changed_state", desc__icontains="State changed to <b>IESG Evaluation - Defer</b>")
elif self.type_id == "statchg" and self.get_state_slug("statchg") == "defer":
return self.latest_event(type="changed_state", desc__icontains="State changed to <b>IESG Evaluation - Defer</b>")
return None
def most_recent_ietflc(self):
"""Returns the most recent IETF LastCallDocEvent for this document"""
return self.latest_event(LastCallDocEvent,type="sent_last_call")