forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews_bofreq.py
More file actions
349 lines (297 loc) · 15 KB
/
views_bofreq.py
File metadata and controls
349 lines (297 loc) · 15 KB
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
# Copyright The IETF Trust 2021 All Rights Reserved
import debug # pyflakes:ignore
import io
from django import forms
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.urls import reverse as urlreverse
from django.utils.html import escape
from ietf.doc.mails import (email_bofreq_title_changed, email_bofreq_editors_changed,
email_bofreq_new_revision, email_bofreq_responsible_changed)
from ietf.doc.models import (Document, DocAlias, DocEvent, NewRevisionDocEvent,
BofreqEditorDocEvent, BofreqResponsibleDocEvent, State)
from ietf.doc.utils import add_state_change_event
from ietf.doc.utils_bofreq import bofreq_editors, bofreq_responsible
from ietf.ietfauth.utils import has_role, role_required
from ietf.person.fields import SearchablePersonsField
from ietf.utils import markdown
from ietf.utils.response import permission_denied
from ietf.utils.text import xslugify
from ietf.utils.textupload import get_cleaned_text_file_content
def bof_requests(request):
reqs = Document.objects.filter(type_id='bofreq')
for req in reqs:
req.latest_revision_event = req.latest_event(NewRevisionDocEvent)
req.responsible = bofreq_responsible(req)
req.editors = bofreq_editors(req)
sorted_reqs = sorted(sorted(reqs, key=lambda doc: doc.latest_revision_event.time, reverse=True), key=lambda doc: doc.get_state().order)
return render(request, 'doc/bofreq/bof_requests.html',dict(reqs=sorted_reqs))
class BofreqUploadForm(forms.Form):
ACTIONS = [
("enter", "Enter content directly"),
("upload", "Upload content from file"),
]
bofreq_submission = forms.ChoiceField(choices=ACTIONS, widget=forms.RadioSelect)
bofreq_file = forms.FileField(label="Markdown source file to upload", required=False)
bofreq_content = forms.CharField(widget=forms.Textarea(attrs={'rows':30}), required=False, strip=False)
def clean(self):
def require_field(f):
if not self.cleaned_data.get(f):
self.add_error(f, forms.ValidationError("You must fill in this field."))
return False
else:
return True
submission_method = self.cleaned_data.get("bofreq_submission")
content = ''
if submission_method == "enter":
if require_field("bofreq_content"):
content = self.cleaned_data["bofreq_content"].replace("\r", "")
default_content = render_to_string('doc/bofreq/bofreq_template.md',{})
if content==default_content:
raise forms.ValidationError('The example content may not be saved. Edit it as instructed to document this BOF request.')
elif submission_method == "upload":
if require_field("bofreq_file"):
content = get_cleaned_text_file_content(self.cleaned_data["bofreq_file"])
try:
_ = markdown.markdown(content)
except Exception as e:
raise forms.ValidationError(f'Markdown processing failed: {e}')
@login_required
def submit(request, name):
bofreq = get_object_or_404(Document, type="bofreq", name=name)
previous_editors = bofreq_editors(bofreq)
state_id = bofreq.get_state_slug('bofreq')
if not (has_role(request.user,('Secretariat', 'Area Director', 'IAB')) or (state_id=='proposed' and request.user.person in previous_editors)):
permission_denied(request,"You do not have permission to upload a new revision of this BOF Request")
if request.method == 'POST':
form = BofreqUploadForm(request.POST, request.FILES)
if form.is_valid():
bofreq.rev = "%02d" % (int(bofreq.rev)+1)
e = NewRevisionDocEvent.objects.create(
type="new_revision",
doc=bofreq,
by=request.user.person,
rev=bofreq.rev,
desc='New revision available',
time=bofreq.time,
)
bofreq.save_with_history([e])
bofreq_submission = form.cleaned_data['bofreq_submission']
if bofreq_submission == "upload":
content = get_cleaned_text_file_content(form.cleaned_data["bofreq_file"])
else:
content = form.cleaned_data['bofreq_content']
with io.open(bofreq.get_file_name(), 'w', encoding='utf-8') as destination:
destination.write(content)
email_bofreq_new_revision(request, bofreq)
return redirect('ietf.doc.views_doc.document_main', name=bofreq.name)
else:
init = {'bofreq_content':bofreq.text_or_error(),
'bofreq_submission':'enter',
}
form = BofreqUploadForm(initial=init)
return render(request, 'doc/bofreq/upload_content.html',
{'form':form,'doc':bofreq})
class NewBofreqForm(BofreqUploadForm):
title = forms.CharField(max_length=255)
field_order = ['title','bofreq_submission','bofreq_file','bofreq_content']
def __init__(self, requester, *args, **kwargs):
self._requester = requester
super().__init__(*args, **kwargs)
def name_from_title(self, title):
requester_slug = xslugify(self._requester.last_name())
title_slug = xslugify(title)
name = f'bofreq-{requester_slug[:64]}-{title_slug[:128]}'
return name.replace('_', '-')
def clean_title(self):
title = self.cleaned_data['title']
name = self.name_from_title(title)
if name == self.name_from_title(''):
raise forms.ValidationError('The filename derived from this title is empty. Please include a few descriptive words using ascii or numeric characters')
if Document.objects.filter(name=name).exists():
raise forms.ValidationError('This title produces a filename already used by an existing BOF request')
return title
@login_required
def new_bof_request(request):
if request.method == 'POST':
form = NewBofreqForm(request.user.person, request.POST, request.FILES)
if form.is_valid():
title = form.cleaned_data['title']
name = form.name_from_title(title)
bofreq = Document.objects.create(
type_id='bofreq',
name = name,
title = title,
abstract = '',
rev = '00',
)
bofreq.set_state(State.objects.get(type_id='bofreq',slug='proposed'))
e1 = NewRevisionDocEvent.objects.create(
type="new_revision",
doc=bofreq,
by=request.user.person,
rev=bofreq.rev,
desc='New revision available',
time=bofreq.time,
)
e2 = BofreqEditorDocEvent.objects.create(
type="changed_editors",
doc=bofreq,
rev=bofreq.rev,
by=request.user.person,
desc= f'Editors changed to {request.user.person.name}',
)
e2.editors.set([request.user.person])
bofreq.save_with_history([e1,e2])
alias = DocAlias.objects.create(name=name)
alias.docs.set([bofreq])
bofreq_submission = form.cleaned_data['bofreq_submission']
if bofreq_submission == "upload":
content = get_cleaned_text_file_content(form.cleaned_data["bofreq_file"])
else:
content = form.cleaned_data['bofreq_content']
with io.open(bofreq.get_file_name(), 'w', encoding='utf-8') as destination:
destination.write(content)
email_bofreq_new_revision(request, bofreq)
return redirect('ietf.doc.views_doc.document_main', name=bofreq.name)
else:
init = {'bofreq_content':escape(render_to_string('doc/bofreq/bofreq_template.md',{})),
'bofreq_submission':'enter',
}
form = NewBofreqForm(request.user.person, initial=init)
return render(request, 'doc/bofreq/new_bofreq.html',
{'form':form})
class ChangeEditorsForm(forms.Form):
editors = SearchablePersonsField(required=False)
@login_required
def change_editors(request, name):
bofreq = get_object_or_404(Document, type="bofreq", name=name)
previous_editors = bofreq_editors(bofreq)
state_id = bofreq.get_state_slug('bofreq')
if not (has_role(request.user,('Secretariat', 'Area Director', 'IAB')) or (state_id=='proposed' and request.user.person in previous_editors)):
permission_denied(request,"You do not have permission to change this document's editors")
if request.method == 'POST':
form = ChangeEditorsForm(request.POST)
if form.is_valid():
new_editors = form.cleaned_data['editors']
if set(new_editors) != set(previous_editors):
e = BofreqEditorDocEvent(type="changed_editors", doc=bofreq, rev=bofreq.rev, by=request.user.person)
e.desc = f'Editors changed to {", ".join([p.name for p in new_editors])}'
e.save()
e.editors.set(new_editors)
bofreq.save_with_history([e])
email_bofreq_editors_changed(request, bofreq, previous_editors)
return redirect("ietf.doc.views_doc.document_main", name=bofreq.name)
else:
init = { "editors" : previous_editors }
form = ChangeEditorsForm(initial=init)
titletext = bofreq.get_base_name()
return render(request, 'doc/bofreq/change_editors.html',
{'form': form,
'doc': bofreq,
'titletext' : titletext,
},
)
class ChangeResponsibleForm(forms.Form):
responsible = SearchablePersonsField(required=False)
def clean_responsible(self):
responsible = self.cleaned_data['responsible']
not_leadership = list()
for person in responsible:
if not has_role(person.user, ('Area Director', 'IAB')):
not_leadership.append(person)
if not_leadership:
raise forms.ValidationError('Only current IAB and IESG members are allowed. Please remove: '+', '.join([person.plain_name() for person in not_leadership]))
return responsible
@login_required
def change_responsible(request,name):
if not has_role(request.user,('Secretariat', 'Area Director', 'IAB')):
permission_denied(request,"You do not have permission to change this document's responsible leadership")
bofreq = get_object_or_404(Document, type="bofreq", name=name)
previous_responsible = bofreq_responsible(bofreq)
if request.method == 'POST':
form = ChangeResponsibleForm(request.POST)
if form.is_valid():
new_responsible = form.cleaned_data['responsible']
if set(new_responsible) != set(previous_responsible):
e = BofreqResponsibleDocEvent(type="changed_responsible", doc=bofreq, rev=bofreq.rev, by=request.user.person)
e.desc = f'Responsible leadership changed to {", ".join([p.name for p in new_responsible])}'
e.save()
e.responsible.set(new_responsible)
bofreq.save_with_history([e])
email_bofreq_responsible_changed(request, bofreq, previous_responsible)
return redirect("ietf.doc.views_doc.document_main", name=bofreq.name)
else:
init = { "responsible" : previous_responsible }
form = ChangeResponsibleForm(initial=init)
titletext = bofreq.get_base_name()
return render(request, 'doc/bofreq/change_responsible.html',
{'form': form,
'doc': bofreq,
'titletext' : titletext,
},
)
class ChangeTitleForm(forms.Form):
title = forms.CharField(max_length=255, label="Title", required=True)
@login_required
def edit_title(request, name):
bofreq = get_object_or_404(Document, type="bofreq", name=name)
editors = bofreq_editors(bofreq)
state_id = bofreq.get_state_slug('bofreq')
if not (has_role(request.user,('Secretariat', 'Area Director', 'IAB')) or (state_id=='proposed' and request.user.person in editors)):
permission_denied(request, "You do not have permission to edit this document's title")
if request.method == 'POST':
form = ChangeTitleForm(request.POST)
if form.is_valid():
bofreq.title = form.cleaned_data['title']
c = DocEvent(type="added_comment", doc=bofreq, rev=bofreq.rev, by=request.user.person)
c.desc = "Title changed to '%s'"%bofreq.title
c.save()
bofreq.save_with_history([c])
email_bofreq_title_changed(request, bofreq)
return redirect("ietf.doc.views_doc.document_main", name=bofreq.name)
else:
init = { "title" : bofreq.title }
form = ChangeTitleForm(initial=init)
titletext = bofreq.get_base_name()
return render(request, 'doc/change_title.html',
{'form': form,
'doc': bofreq,
'titletext' : titletext,
},
)
class ChangeStateForm(forms.Form):
new_state = forms.ModelChoiceField(State.objects.filter(type="bofreq", used=True), label="BOF Request State", empty_label=None, required=True)
comment = forms.CharField(widget=forms.Textarea, help_text="Optional comment for the state change history entry.", required=False, strip=False)
@role_required('Area Director', 'Secretariat', 'IAB')
def change_state(request, name, option=None):
bofreq = get_object_or_404(Document, type="bofreq", name=name)
login = request.user.person
if request.method == 'POST':
form = ChangeStateForm(request.POST)
if form.is_valid():
clean = form.cleaned_data
new_state = clean['new_state']
comment = clean['comment'].rstrip()
if comment:
c = DocEvent(type="added_comment", doc=bofreq, rev=bofreq.rev, by=login)
c.desc = comment
c.save()
prev_state = bofreq.get_state()
if new_state != prev_state:
bofreq.set_state(new_state)
events = []
events.append(add_state_change_event(bofreq, login, prev_state, new_state))
bofreq.save_with_history(events)
return redirect('ietf.doc.views_doc.document_main', name=bofreq.name)
else:
s = bofreq.get_state()
init = dict(new_state=s.pk if s else None)
form = ChangeStateForm(initial=init)
return render(request, 'doc/change_state.html',
dict(form=form,
doc=bofreq,
login=login,
help_url=urlreverse('ietf.doc.views_help.state_help', kwargs=dict(type="bofreq")),
))