changeset 2259:d92720cc23c6

Added UI for Editing names.
author Ashutosh Singla <ashu1461@gmail.com>
date Wed, 11 Sep 2013 10:13:14 +0530
parents 904c649f2746
children c800c819efc4
files MoinMoin/forms.py MoinMoin/items/__init__.py MoinMoin/templates/modify_meta.html
diffstat 3 files changed, 40 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/forms.py	Wed Sep 11 10:07:52 2013 +0530
+++ b/MoinMoin/forms.py	Wed Sep 11 10:13:14 2013 +0530
@@ -78,20 +78,16 @@
     """
 
 
-def validate_name(meta, fqname, itemid):
+def validate_name(meta, itemid):
     """
     Check whether the names are valid.
     Will just return, if they are valid, will raise a NameNotValidError if not.
     """
     names = meta.get(NAME)
     current_namespace = meta.get(NAMESPACE)
-
     if current_namespace is None:
         raise NameNotValidError(L_("No namespace field in the meta."))
-
     namespaces = [namespace.rstrip('/') for namespace, _ in app.cfg.namespace_mapping]
-    if current_namespace not in namespaces:  # current_namespace must be an existing namespace.
-        raise NameNotValidError(L_("%(_namespace)s is not a valid namespace.", _namespace=current_namespace))
 
     if len(names) != len(set(names)):
         raise NameNotValidError(L_("The names in the name list must be unique."))
@@ -116,12 +112,30 @@
             raise NameNotValidError(L_("Item(s) named %(duplicate_names)s already exist.", duplicate_names=", ".join(duplicate_names)))
 
 
+class ValidName(Validator):
+    """Validator for Name
+    """
+    invalid_name_msg = ""
+
+    def validate(self, element, state):
+        # Make sure that the other meta is valid before validating the name.
+        # TODO Change/Make sure that the below statement holds good.
+        if not element.parent.parent['extra_meta_text'].valid:
+            return False
+        try:
+            validate_name(state['meta'], state[ITEMID])
+        except NameNotValidError as e:
+            self.invalid_name_msg = _(e)
+            return self.note_error(element, state, 'invalid_name_msg')
+        return True
+
+
 class ValidJSON(Validator):
     """Validator for JSON
     """
     invalid_json_msg = L_('Invalid JSON.')
-    invalid_name_msg = ""
     invalid_itemid_msg = L_('Itemid not a proper UUID')
+    invalid_namespace_msg = ''
 
     def validitemid(self, itemid):
         if not itemid:
@@ -129,16 +143,23 @@
             return False
         return uuid_validator(String(itemid), None)
 
+    def validnamespace(self, current_namespace):
+        if current_namespace is None:
+            self.invalid_namespace_msg = L_("No namespace field in the meta.")
+            return False
+        namespaces = [namespace.rstrip('/') for namespace, _ in app.cfg.namespace_mapping]
+        if current_namespace not in namespaces:  # current_namespace must be an existing namespace.
+            self.invalid_namespace_msg = L_("%(_namespace)s is not a valid namespace.", _namespace=current_namespace)
+            return False
+        return True
+
     def validate(self, element, state):
         try:
             meta = json.loads(element.value)
         except:  # catch ANY exception that happens due to unserializing
             return self.note_error(element, state, 'invalid_json_msg')
-        try:
-            validate_name(meta, state[FQNAME], state[ITEMID])
-        except NameNotValidError as e:
-            self.invalid_name_msg = _(e)
-            return self.note_error(element, state, 'invalid_name_msg')
+        if not self.validnamespace(meta.get(NAMESPACE)):
+            return self.note_error(element, state, 'invalid_namespace_msg')
         if state[FQNAME].field == ITEMID:
             if not self.validitemid(meta.get(ITEMID, state[FQNAME].value)):
                 return self.note_error(element, state, 'invalid_itemid_msg')
@@ -195,7 +216,7 @@
     label=L_('Tags'), optional=True, separator=', ', separator_regex=re.compile(r'\s*,\s*'))
 
 Names = MyJoinedString.of(String).with_properties(widget=WIDGET_TEXT).using(
-    label=L_('Names'), optional=True, separator=', ', separator_regex=re.compile(r'\s*,\s*'))
+    label=L_('Names'), optional=True, separator=', ', separator_regex=re.compile(r'\s*,\s*')).validated_by(ValidName())
 
 Search = Text.using(default=u'', optional=True).with_properties(widget=WIDGET_SEARCH, placeholder=L_("Search Query"))
 
--- a/MoinMoin/items/__init__.py	Wed Sep 11 10:07:52 2013 +0530
+++ b/MoinMoin/items/__init__.py	Wed Sep 11 10:13:14 2013 +0530
@@ -44,7 +44,7 @@
 from MoinMoin.util.interwiki import url_for_item, split_fqname, get_fqname, CompositeName
 from MoinMoin.util.registry import RegistryBase
 from MoinMoin.util.clock import timed
-from MoinMoin.forms import RequiredText, OptionalText, JSON, Tags
+from MoinMoin.forms import RequiredText, OptionalText, JSON, Tags, Names
 from MoinMoin.constants.keys import (
     NAME, NAME_OLD, NAME_EXACT, WIKINAME, MTIME, ITEMTYPE,
     CONTENTTYPE, SIZE, ACTION, ADDRESS, HOSTNAME, USERID, COMMENT,
@@ -193,6 +193,7 @@
     # value, while an emtpy acl and no acl have different semantics
     #acl = OptionalText.using(label=L_('ACL')).with_properties(placeholder=L_("Access Control List"))
     summary = OptionalText.using(label=L_("Summary")).with_properties(placeholder=L_("One-line summary of the item"))
+    name = Names
     tags = Tags
 
 
@@ -363,7 +364,8 @@
     def meta_filter(self, meta):
         """ kill metadata entries that we set automatically when saving """
         kill_keys = [  # shall not get copied from old rev to new rev
-            NAME_OLD,
+            # As we have a special field for NAME we don't want NAME to appear in JSON meta.
+            NAME, NAME_OLD,
             # are automatically implanted when saving
             REVID, DATAID,
             HASH_ALGORITHM,
@@ -798,9 +800,9 @@
                     # break them
                     return "OK"
             form = self.ModifyForm.from_request(request)
-            state = dict(fqname=self.fqname, itemid=self.meta.get(ITEMID))
+            meta, data, contenttype_guessed, comment = form._dump(self)
+            state = dict(fqname=self.fqname, itemid=meta.get(ITEMID), meta=meta)
             if form.validate(state):
-                meta, data, contenttype_guessed, comment = form._dump(self)
                 contenttype_qs = request.values.get('contenttype')
                 try:
                     self.modify(meta, data, comment, contenttype_guessed, **{CONTENTTYPE: contenttype_qs})
--- a/MoinMoin/templates/modify_meta.html	Wed Sep 11 10:07:52 2013 +0530
+++ b/MoinMoin/templates/modify_meta.html	Wed Sep 11 10:13:14 2013 +0530
@@ -8,6 +8,7 @@
             'contenttype',
             'summary',
             'tags',
+            'name',
             ] %}
             {{ forms.render(form[e]) }}
         {% endfor %}