11# views for managing group materials (slides, ...)
22import os
33import datetime
4+ import re
45
56from django import forms
67from django .shortcuts import render , get_object_or_404 , redirect
1415
1516from ietf .doc .models import Document , DocAlias , DocTypeName , DocEvent , State
1617from ietf .doc .models import NewRevisionDocEvent , save_document_in_history
17- from ietf .doc .utils import add_state_change_event
18+ from ietf .doc .utils import add_state_change_event , check_common_doc_name_rules
1819from ietf .group .models import Group
1920from ietf .group .utils import can_manage_materials
2021
@@ -34,6 +35,7 @@ def name_for_material(doc_type, group, title):
3435
3536class UploadMaterialForm (forms .Form ):
3637 title = forms .CharField (max_length = Document ._meta .get_field ("title" ).max_length )
38+ name = forms .CharField (max_length = Document ._meta .get_field ("name" ).max_length )
3739 state = forms .ModelChoiceField (State .objects .all (), empty_label = None )
3840 material = forms .FileField (label = 'File' , help_text = "PDF or text file (ASCII/UTF-8)" )
3941
@@ -50,7 +52,10 @@ def __init__(self, doc_type, action, group, doc, *args, **kwargs):
5052 self .fields ["state" ].widget = forms .HiddenInput ()
5153 self .fields ["state" ].queryset = self .fields ["state" ].queryset .filter (slug = "active" )
5254 self .fields ["state" ].initial = self .fields ["state" ].queryset [0 ].pk
55+ self .fields ["name" ].initial = u"%s-%s-" % (doc_type .slug , group .acronym )
5356 else :
57+ del self .fields ["name" ]
58+
5459 self .fields ["title" ].initial = doc .title
5560 self .fields ["state" ].initial = doc .get_state ().pk if doc .get_state () else None
5661 if doc .get_state_slug () == "deleted" :
@@ -63,16 +68,20 @@ def __init__(self, doc_type, action, group, doc, *args, **kwargs):
6368 del self .fields ["title" ]
6469 del self .fields ["material" ]
6570
66- def clean_title (self ):
67- title = self .cleaned_data ["title" ]
68- if self .action == "new" :
69- name = name_for_material (self .doc_type , self .group , title )
70- existing = Document .objects .filter (type = self .doc_type , name = name )
71- if existing :
72- url = urlreverse ("material_edit" , kwargs = { 'name' : existing [0 ].name , 'action' : 'revise' })
73- 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 )))
71+ def clean_name (self ):
72+ name = self .cleaned_data ["name" ].strip ().rstrip ("-" )
73+
74+ check_common_doc_name_rules (name )
75+
76+ if not re .search ("^%s-%s-[a-z0-9]+" % (self .doc_type .slug , self .group .acronym ), name ):
77+ raise forms .ValidationError ("The name must start with %s-%s- followed by descriptive dash-separated words." % (self .doc_type .slug , self .group .acronym ))
78+
79+ existing = Document .objects .filter (type = self .doc_type , name = name )
80+ if existing :
81+ url = urlreverse ("material_edit" , kwargs = { 'name' : existing [0 ].name , 'action' : 'revise' })
82+ 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 )))
7483
75- return title
84+ return name
7685
7786@login_required
7887def edit_material (request , name = None , acronym = None , action = None , doc_type = None ):
0 commit comments