|
| 1 | +# views for managing group materials (slides, ...) |
| 2 | +import re |
| 3 | +import os |
| 4 | +import datetime |
| 5 | +import shutil |
| 6 | + |
| 7 | +from django import forms |
| 8 | +from django.shortcuts import render, get_object_or_404, redirect |
| 9 | +from django.http import HttpResponse, HttpResponseForbidden, Http404, HttpResponseRedirect |
| 10 | +from django.utils.html import mark_safe |
| 11 | +from django.utils.text import slugify |
| 12 | +from django.contrib.auth.decorators import login_required |
| 13 | +from django.core.urlresolvers import reverse as urlreverse |
| 14 | + |
| 15 | +import debug # pyflakes:ignore |
| 16 | + |
| 17 | +from ietf.doc.models import Document, DocAlias, DocTypeName, DocEvent, State |
| 18 | +from ietf.doc.models import NewRevisionDocEvent, save_document_in_history |
| 19 | +from ietf.doc.utils import add_state_change_event |
| 20 | +from ietf.group.models import Group |
| 21 | +from ietf.group.utils import can_manage_materials |
| 22 | +from ietf.ietfauth.utils import has_role |
| 23 | + |
| 24 | +@login_required |
| 25 | +def choose_material_type(request, acronym): |
| 26 | + group = get_object_or_404(Group, acronym=acronym) |
| 27 | + if not group.features.has_materials: |
| 28 | + raise Http404 |
| 29 | + |
| 30 | + return render(request, 'doc/material/choose_material_type.html', { |
| 31 | + 'group': group, |
| 32 | + 'material_types': DocTypeName.objects.filter(slug__in=group.features.material_types), |
| 33 | + }) |
| 34 | + |
| 35 | +def name_for_material(doc_type, group, title): |
| 36 | + return "%s-%s-%s" % (doc_type.slug, group.acronym, slugify(title)) |
| 37 | + |
| 38 | +class UploadMaterialForm(forms.Form): |
| 39 | + title = forms.CharField(max_length=Document._meta.get_field("title").max_length) |
| 40 | + state = forms.ModelChoiceField(State.objects.all(), empty_label=None) |
| 41 | + material = forms.FileField(label='File', help_text="PDF or text file (ASCII/UTF-8)") |
| 42 | + |
| 43 | + def __init__(self, doc_type, action, group, doc, *args, **kwargs): |
| 44 | + super(UploadMaterialForm, self).__init__(*args, **kwargs) |
| 45 | + |
| 46 | + self.fields["state"].queryset = self.fields["state"].queryset.filter(type=doc_type) |
| 47 | + |
| 48 | + self.doc_type = doc_type |
| 49 | + self.action = action |
| 50 | + self.group = group |
| 51 | + |
| 52 | + if action == "new": |
| 53 | + self.fields["state"].widget = forms.HiddenInput() |
| 54 | + self.fields["state"].queryset = self.fields["state"].queryset.filter(slug="active") |
| 55 | + self.fields["state"].initial = self.fields["state"].queryset[0].pk |
| 56 | + else: |
| 57 | + self.fields["title"].initial = doc.title |
| 58 | + self.fields["state"].initial = doc.get_state().pk if doc.get_state() else None |
| 59 | + if doc.get_state_slug() == "deleted": |
| 60 | + self.fields["state"].help_text = "Note: If you wish to revise this document, you may wish to change the state so it's not deleted." |
| 61 | + |
| 62 | + if action == "title": |
| 63 | + del self.fields["state"] |
| 64 | + del self.fields["material"] |
| 65 | + elif action == "state": |
| 66 | + del self.fields["title"] |
| 67 | + del self.fields["material"] |
| 68 | + |
| 69 | + def clean_title(self): |
| 70 | + title = self.cleaned_data["title"] |
| 71 | + if self.action == "new": |
| 72 | + name = name_for_material(self.doc_type, self.group, title) |
| 73 | + existing = Document.objects.filter(type=self.doc_type, name=name) |
| 74 | + if existing: |
| 75 | + url = urlreverse("material_edit", kwargs={ 'name': existing[0].name, 'action': 'revise' }) |
| 76 | + raise forms.ValidationError(mark_safe("Can't upload: %s with name %s already exists. The name is derived from the title so you must either choose another title for what you're uploading or <a href=\"%s\">revise the existing %s</a>." % (self.doc_type.name, name, url, name))) |
| 77 | + |
| 78 | + return title |
| 79 | + |
| 80 | +@login_required |
| 81 | +def edit_material(request, name=None, acronym=None, action=None, doc_type=None): |
| 82 | + # the materials process is not very developed, so at the moment we |
| 83 | + # handle everything through the same view/form |
| 84 | + |
| 85 | + if action == "new": |
| 86 | + group = get_object_or_404(Group, acronym=acronym) |
| 87 | + if not group.features.has_materials: |
| 88 | + raise Http404 |
| 89 | + |
| 90 | + doc = None |
| 91 | + document_type = get_object_or_404(DocTypeName, slug=doc_type) |
| 92 | + else: |
| 93 | + doc = get_object_or_404(Document, name=name) |
| 94 | + group = doc.group |
| 95 | + document_type = doc.type |
| 96 | + |
| 97 | + if not can_manage_materials(request.user, group): |
| 98 | + return HttpResponseForbidden("You don't have permission to access this view") |
| 99 | + |
| 100 | + if request.method == 'POST': |
| 101 | + form = UploadMaterialForm(document_type, action, group, doc, request.POST, request.FILES) |
| 102 | + |
| 103 | + if form.is_valid(): |
| 104 | + if action == "new": |
| 105 | + doc = Document() |
| 106 | + doc.type = document_type |
| 107 | + doc.group = group |
| 108 | + doc.rev = "00" |
| 109 | + doc.name = name_for_material(doc.type, doc.group, form.cleaned_data["title"]) |
| 110 | + prev_rev = None |
| 111 | + else: |
| 112 | + save_document_in_history(doc) |
| 113 | + prev_rev = doc.rev |
| 114 | + |
| 115 | + prev_title = doc.title |
| 116 | + prev_state = doc.get_state() |
| 117 | + |
| 118 | + if "title" in form.cleaned_data: |
| 119 | + doc.title = form.cleaned_data["title"] |
| 120 | + |
| 121 | + doc.time = datetime.datetime.now() |
| 122 | + |
| 123 | + if "material" in form.fields: |
| 124 | + if action != "new": |
| 125 | + doc.rev = "%02d" % (int(doc.rev) + 1) |
| 126 | + |
| 127 | + f = form.cleaned_data["material"] |
| 128 | + file_ext = os.path.splitext(f.name)[1] |
| 129 | + |
| 130 | + with open(os.path.join(doc.get_file_path(), doc.name + "-" + doc.rev + file_ext), 'wb+') as dest: |
| 131 | + for chunk in f.chunks(): |
| 132 | + dest.write(chunk) |
| 133 | + |
| 134 | + doc.save() |
| 135 | + |
| 136 | + if action == "new": |
| 137 | + DocAlias.objects.get_or_create(name=doc.name, document=doc) |
| 138 | + |
| 139 | + if prev_rev != doc.rev: |
| 140 | + e = NewRevisionDocEvent(type="new_revision", doc=doc, rev=doc.rev) |
| 141 | + e.time = doc.time |
| 142 | + e.by = request.user.person |
| 143 | + e.desc = "New version available: <b>%s-%s</b>" % (doc.name, doc.rev) |
| 144 | + e.save() |
| 145 | + |
| 146 | + if prev_title != doc.title: |
| 147 | + e = DocEvent(doc=doc, by=request.user.person, type='changed_document') |
| 148 | + e.desc = u"Changed title to <b>%s</b>" % doc.title |
| 149 | + if prev_title: |
| 150 | + e.desc += u" from %s" % prev_title |
| 151 | + e.time = doc.time |
| 152 | + e.save() |
| 153 | + |
| 154 | + if "state" in form.cleaned_data and form.cleaned_data["state"] != prev_state: |
| 155 | + doc.set_state(form.cleaned_data["state"]) |
| 156 | + add_state_change_event(doc, request.user.person, prev_state, form.cleaned_data["state"]) |
| 157 | + |
| 158 | + return redirect("doc_view", name=doc.name) |
| 159 | + else: |
| 160 | + form = UploadMaterialForm(document_type, action, group, doc) |
| 161 | + |
| 162 | + return render(request, 'doc/material/edit_material.html', { |
| 163 | + 'group': group, |
| 164 | + 'form': form, |
| 165 | + 'action': action, |
| 166 | + 'document_type': document_type, |
| 167 | + 'doc_name': doc.name if doc else "", |
| 168 | + }) |
0 commit comments