|
9 | 9 | from django.shortcuts import render, get_object_or_404, redirect |
10 | 10 | from django.http import HttpResponseForbidden, Http404 |
11 | 11 | from django.utils.html import mark_safe |
| 12 | +from django.utils.text import slugify |
12 | 13 | from django.http import Http404, HttpResponse |
13 | 14 | from django.contrib.auth.decorators import login_required |
14 | 15 |
|
15 | 16 | import debug # pyflakes:ignore |
16 | 17 |
|
17 | | -from ietf.doc.models import DocAlias, DocTagName, Document, State, save_document_in_history |
| 18 | +from ietf.doc.models import Document, DocAlias, DocTagName, DocTypeName, DocEvent, State, save_document_in_history |
18 | 19 | from ietf.doc.utils import get_tags_for_stream_id |
19 | 20 | from ietf.group.models import ( Group, Role, GroupEvent, GroupHistory, GroupStateName, |
20 | 21 | GroupStateTransitions, GroupTypeName, GroupURL, ChangeStateGroupEvent ) |
@@ -456,18 +457,95 @@ def customize_workflow(request, group_type, acronym): |
456 | 457 | 'tags': tags, |
457 | 458 | }) |
458 | 459 |
|
| 460 | +class UploadMaterialForm(forms.Form): |
| 461 | + title = forms.CharField(max_length=Document._meta.get_field("title").max_length) |
| 462 | + state = forms.ModelChoiceField(State.objects.filter(type="material"), empty_label=None) |
| 463 | + material = forms.FileField(label='File', help_text="PDF or text file (ASCII/UTF-8)") |
| 464 | + |
| 465 | + def __init__(self, *args, **kwargs): |
| 466 | + action = kwargs.pop("action") |
| 467 | + doc = kwargs.pop("doc", None) |
| 468 | + |
| 469 | + super(UploadMaterialForm, self).__init__(*args, **kwargs) |
| 470 | + |
| 471 | + if action == "new": |
| 472 | + self.fields["state"].widget = forms.HiddenInput() |
| 473 | + self.fields["state"].queryset = self.fields["state"].queryset.filter(slug="active") |
| 474 | + else: |
| 475 | + self.fields["title"].initial = doc.title |
| 476 | + self.fields["state"].initial = doc.get_state().pk if doc.get_state() else None |
| 477 | + |
| 478 | + if action == "edit": |
| 479 | + del self.fields["material"] |
| 480 | + |
| 481 | + |
459 | 482 | @login_required |
460 | | -def upload_materials(request, acronym, group_type=None): |
| 483 | +def edit_material(request, acronym, action="new", name=None, group_type=None): |
461 | 484 | group = get_group_or_404(acronym, group_type) |
462 | 485 | if not group.features.has_materials: |
463 | 486 | raise Http404 |
464 | 487 |
|
465 | 488 | if not can_manage_materials(request.user, group): |
466 | 489 | return HttpResponseForbidden("You don't have permission to access this view") |
467 | 490 |
|
468 | | - # FIXME: fill in |
| 491 | + existing = None |
| 492 | + if name and action != "new": |
| 493 | + existing = get_object_or_404(Document, type="material", name=name) |
| 494 | + |
| 495 | + if request.method == 'POST': |
| 496 | + form = UploadMaterialForm(request.POST, request.FILES, action=action, doc=existing) |
| 497 | + |
| 498 | + if form.is_valid(): |
| 499 | + if action == "new": |
| 500 | + d = Document() |
| 501 | + d.type = DocTypeName.objects.get(slug="material") |
| 502 | + d.group = group |
| 503 | + d.rev = "01" |
| 504 | + else: |
| 505 | + d = existing |
| 506 | + |
| 507 | + d.title = form.cleaned_data["title"] |
| 508 | + d.time = datetime.datetime.now() |
| 509 | + |
| 510 | + if not d.name: |
| 511 | + d.name = "material-%s-%s" % (d.group.acronym, slugify(d.title)) |
| 512 | + i = 2 |
| 513 | + while True: |
| 514 | + if not Document.objects.filter(name=d.name).exists(): |
| 515 | + break |
| 516 | + d.name = "material-%s-%s-%s" % (d.group.acronym, slugify(d.title), i) |
| 517 | + i += 1 |
| 518 | + |
| 519 | + if "material" in form.fields: |
| 520 | + if action != "new": |
| 521 | + d.rev = "%02d" % (int(d.rev) + 1) |
| 522 | + |
| 523 | + f = form.cleaned_data["material"] |
| 524 | + file_ext = os.path.splitext(f.name)[1] |
| 525 | + |
| 526 | + with open(os.path.join(d.get_file_path(), d.name + "-" + d.rev + file_ext), 'wb+') as dest: |
| 527 | + for chunk in f.chunks(): |
| 528 | + dest.write(chunk) |
| 529 | + |
| 530 | + d.save() |
| 531 | + |
| 532 | + # FIXME: missing edit title event |
| 533 | + |
| 534 | + # FIXME: missing changed state event |
| 535 | + d.set_state(form.cleaned_data["state"]) |
| 536 | + |
| 537 | + # FIXME: wrong, should be new revision event |
| 538 | + DocEvent.objects.create(doc=d, |
| 539 | + by=request.user.person, |
| 540 | + type='uploaded', |
| 541 | + desc="Uploaded material") |
| 542 | + |
| 543 | + return redirect("group_materials", acronym=group.acronym) |
| 544 | + else: |
| 545 | + form = UploadMaterialForm(action=action, doc=existing) |
469 | 546 |
|
470 | | - return render(request, 'group/materials.html', |
471 | | - construct_group_menu_context(request, group, "materials", group_type, { |
472 | | - "materials": materials, |
473 | | - })) |
| 547 | + return render(request, 'group/edit_material.html', { |
| 548 | + 'group': group, |
| 549 | + 'form': form, |
| 550 | + 'action': action, |
| 551 | + }) |
0 commit comments