Make it possible to adjust the name of new materials upon upload, also fix a couple of warnings from PyFlakes

- Legacy-Id: 7914
This commit is contained in:
Ole Laursen 2014-06-13 12:30:42 +00:00
parent 602feff7c9
commit f9ee750afd
8 changed files with 57 additions and 20 deletions

View file

@ -1,6 +1,6 @@
# Copyright The IETF Trust 2011, All Rights Reserved
import os, shutil, datetime
import os, shutil
from StringIO import StringIO
from pyquery import PyQuery
@ -10,7 +10,6 @@ from django.core.urlresolvers import reverse as urlreverse
from ietf.doc.models import Document, State, DocAlias
from ietf.group.models import Group
from ietf.utils.test_utils import TestCase, login_testing_unauthorized
from ietf.utils.test_data import make_test_data
class GroupMaterialTests(TestCase):
def setUp(self):
@ -57,7 +56,7 @@ class GroupMaterialTests(TestCase):
test_file.name = "unnamed.pdf"
# faulty post
r = self.client.post(url, dict(title="", state="", material=test_file))
r = self.client.post(url, dict(title="", name="", state="", material=test_file))
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
@ -67,6 +66,7 @@ class GroupMaterialTests(TestCase):
# post
r = self.client.post(url, dict(title="Test File",
name="slides-%s-test-file" % group.acronym,
state=State.objects.get(type="slides", slug="active").pk,
material=test_file))
self.assertEqual(r.status_code, 302)
@ -79,10 +79,11 @@ class GroupMaterialTests(TestCase):
with open(os.path.join(self.materials_dir, "slides", doc.name + "-" + doc.rev + ".pdf")) as f:
self.assertEqual(f.read(), content)
# check that posting same title is prevented
# check that posting same name is prevented
test_file.seek(0)
r = self.client.post(url, dict(title="Test File",
name=doc.name,
state=State.objects.get(type="slides", slug="active").pk,
material=test_file))
self.assertEqual(r.status_code, 200)

View file

@ -4,6 +4,7 @@ import urllib
import math
from django.conf import settings
from django.forms import ValidationError
from ietf.utils import markup_txt
from ietf.doc.models import DocAlias, RelatedDocument, BallotType, DocReminder
@ -377,3 +378,18 @@ def rebuild_reference_relations(doc):
ret['unfound']=list(unfound)
return ret
def check_common_doc_name_rules(name):
"""Check common rules for document names for use in forms, throws
ValidationError in case there's a problem."""
errors = []
if re.search("[^a-z0-9-]", name):
errors.append("The name may only contain digits, lowercase letters and dashes.")
if re.search("--", name):
errors.append("Please do not put more than one hyphen between any two words in the name.")
if re.search("-[0-9]{2}$", name):
errors.append("This name looks like ends in a version number. -00 will be added automatically. Please adjust the end of the name.")
if errors:
raise ValidationError(errors)

View file

@ -1,6 +1,7 @@
# views for managing group materials (slides, ...)
import os
import datetime
import re
from django import forms
from django.shortcuts import render, get_object_or_404, redirect
@ -14,7 +15,7 @@ import debug # pyflakes:ignore
from ietf.doc.models import Document, DocAlias, DocTypeName, DocEvent, State
from ietf.doc.models import NewRevisionDocEvent, save_document_in_history
from ietf.doc.utils import add_state_change_event
from ietf.doc.utils import add_state_change_event, check_common_doc_name_rules
from ietf.group.models import Group
from ietf.group.utils import can_manage_materials
@ -34,6 +35,7 @@ def name_for_material(doc_type, group, title):
class UploadMaterialForm(forms.Form):
title = forms.CharField(max_length=Document._meta.get_field("title").max_length)
name = forms.CharField(max_length=Document._meta.get_field("name").max_length)
state = forms.ModelChoiceField(State.objects.all(), empty_label=None)
material = forms.FileField(label='File', help_text="PDF or text file (ASCII/UTF-8)")
@ -50,7 +52,10 @@ class UploadMaterialForm(forms.Form):
self.fields["state"].widget = forms.HiddenInput()
self.fields["state"].queryset = self.fields["state"].queryset.filter(slug="active")
self.fields["state"].initial = self.fields["state"].queryset[0].pk
self.fields["name"].initial = u"%s-%s-" % (doc_type.slug, group.acronym)
else:
del self.fields["name"]
self.fields["title"].initial = doc.title
self.fields["state"].initial = doc.get_state().pk if doc.get_state() else None
if doc.get_state_slug() == "deleted":
@ -63,16 +68,20 @@ class UploadMaterialForm(forms.Form):
del self.fields["title"]
del self.fields["material"]
def clean_title(self):
title = self.cleaned_data["title"]
if self.action == "new":
name = name_for_material(self.doc_type, self.group, title)
existing = Document.objects.filter(type=self.doc_type, name=name)
if existing:
url = urlreverse("material_edit", kwargs={ 'name': existing[0].name, 'action': 'revise' })
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)))
def clean_name(self):
name = self.cleaned_data["name"].strip().rstrip("-")
return title
check_common_doc_name_rules(name)
if not re.search("^%s-%s-[a-z0-9]+" % (self.doc_type.slug, self.group.acronym), name):
raise forms.ValidationError("The name must start with %s-%s- followed by descriptive dash-separated words." % (self.doc_type.slug, self.group.acronym))
existing = Document.objects.filter(type=self.doc_type, name=name)
if existing:
url = urlreverse("material_edit", kwargs={ 'name': existing[0].name, 'action': 'revise' })
raise forms.ValidationError(mark_safe("Can't upload: %s with name %s already exists. Choose another title and name for what you're uploading or <a href=\"%s\">revise the existing %s</a>." % (self.doc_type.name, name, url, name)))
return name
@login_required
def edit_material(request, name=None, acronym=None, action=None, doc_type=None):

View file

@ -193,7 +193,7 @@ class GroupPagesTests(TestCase):
self.assertTrue(milestone.docs.all()[0].name in r.content)
def test_group_about(self):
draft = make_test_data()
make_test_data()
group = Group.objects.create(
type_id="team",
acronym="testteam",
@ -210,7 +210,7 @@ class GroupPagesTests(TestCase):
self.assertTrue(group.description in r.content)
def test_materials(self):
draft = make_test_data()
make_test_data()
group = Group.objects.create(type_id="team", acronym="testteam", name="Test Team", state_id="active")
doc = Document.objects.create(

View file

@ -1,6 +1,5 @@
import os
from django.conf import settings
from django.shortcuts import get_object_or_404
from ietf.group.models import Group, RoleHistory

View file

@ -61,7 +61,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<div id="ietf-login" class="noprint">{% if user.is_authenticated %}
{{ user }}
{% else %}
<a href="https://{{ request.get_host }}/accounts/login/?next={{request.get_full_path|urlencode}}" rel="nofollow">Sign In</a>
<a href="//{{ request.get_host }}/accounts/login/?next={{request.get_full_path|urlencode}}" rel="nofollow">Sign In</a>
{% endif %}</div>
{% endif %}

View file

@ -5,7 +5,7 @@
{% block morecss %}
{{ block.super }}
form.upload-material td { padding-bottom: 0.6em; }
form.upload-material #id_title { width: 30em; }
form.upload-material #id_title, form.upload-material #id_name { width: 30em; }
form.upload-material .submit-row td { padding-top: 1em; text-align: right; }
{% endblock %}
@ -31,7 +31,7 @@ form.upload-material .submit-row td { padding-top: 1em; text-align: right; }
<h3>Upload New Revision</h3>
{% endif %}
<form class="upload-material" method="post" enctype="multipart/form-data">{% csrf_token %}
<form class="upload-material" method="post" enctype="multipart/form-data" data-nameprefix="{{ document_type.slug }}-{{ group.acronym }}-">{% csrf_token %}
<table>
{{ form.as_table }}
@ -45,3 +45,14 @@ form.upload-material .submit-row td { padding-top: 1em; text-align: right; }
</form>
{% endblock content %}
{% block scripts %}
jQuery(document).ready(function () {
jQuery("form.upload-material input#id_title").on("change keyup", function () {
var v = jQuery(this).val();
var slug = jQuery(this).parents("form").data("nameprefix");
slug += v.toLowerCase().replace(/ /g,'-').replace(/[-]+/g, '-').replace(/[^a-z-]+/g,'');
jQuery(this).parents("form").find("input#id_name").val(slug);
});
});
{% endblock %}

View file

@ -11,6 +11,7 @@ from ietf.utils.test_utils import TestCase
class PyFlakesTestCase(TestCase):
def test_pyflakes(self):
self.maxDiff = None
path = os.path.join(settings.BASE_DIR)
warnings = []
warnings = pyflakes.checkPaths([path], verbosity=0)