Bläddra i källkod

[core] Prevent creating a project with an empty or already existing name

https://github.com/cloudera/hue/pull/27

home-> My documents->My project->create project: Add estimate to forbid to forbid to create a empty name project,and forbid to create the project which name is already exist.
Xhxiong 10 år sedan
förälder
incheckning
abd18d8
2 ändrade filer med 48 tillägg och 11 borttagningar
  1. 20 6
      desktop/core/src/desktop/api.py
  2. 28 5
      desktop/core/src/desktop/templates/home.mako

+ 20 - 6
desktop/core/src/desktop/api.py

@@ -212,17 +212,31 @@ def massage_doc_for_json(document, user, url=''):
   return massaged_doc
 
 
+def valid_project(name):
+  project_doc = DocumentTag.objects.filter(tag=name)
+  num = len(project_doc)
+  if num > 1:
+    return -1
+  if num == 0:
+    return 0
+  return -1
+  
 @require_POST
 def add_tag(request):
   response = {'status': -1, 'message': ''}
+  
 
   try:
-    tag = DocumentTag.objects.create_tag(request.user, request.POST['name'])
-    response['name'] = request.POST['name']
-    response['id'] = tag.id
-    response['docs'] = []
-    response['owner'] = request.user.username
-    response['status'] = 0
+    validstatus = valid_project(name=request.POST['name'])
+    if validstatus == 0:
+         tag = DocumentTag.objects.create_tag(request.user, request.POST['name'])
+         response['name'] = request.POST['name']
+         response['id'] = tag.id
+         response['docs'] = []
+         response['owner'] = request.user.username
+         response['status'] = 0
+	else:
+         response['status'] = -1
   except KeyError, e:
     response['message'] = _('Form is missing %s field') % e.message
   except Exception, e:

+ 28 - 5
desktop/core/src/desktop/templates/home.mako

@@ -296,6 +296,9 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
       </p>
     </div>
     <div class="modal-footer">
+	  <div id="saveProjectAlert" class="alert-message error hide" style="position: absolute; left: 78px;">
+          <span class="label label-important"></span>
+      </div>
       <a href="#" data-dismiss="modal" class="btn">${_('Cancel')}</a>
       <a id="tagsNewBtn" href="#" class="btn btn-primary disable-feedback">${ _('Add') }</a>
     </div>
@@ -378,14 +381,32 @@ ${ commonshare() | n,unicode }
 
     $("#tagsNewBtn").on("click", function () {
       var tag_name = $("#tagsNew").val();
+	  
+	  if ($.trim(tag_name) == "") {
+       $("#saveProjectAlert span").text("${_('File name is required.')}");
+       $("#saveProjectAlert").show();
+       $("#tagsNew").addClass("fieldError");
+       resetPrimaryButtonsStatus(); //globally available
+       return false;
+      }
+
       $.post("/desktop/api/tag/add_tag", {
         name: tag_name
       },function (data) {
-        data.name = hueUtils.htmlEncode(data.name);
-        viewModel.createTag(data);
-        $("#tagsNew").val("");
-        $(document).trigger("info", "${_('Project created')}");
-        $("#addTagModal").modal("hide");
+	  if(data.status==-1)
+          {
+            $("#saveProjectAlert span").text("${_('project name already exists')}");
+            $("#saveProjectAlert").show();
+            resetPrimaryButtonsStatus(); //globally available
+          }
+        else
+		  {
+            data.name = hueUtils.htmlEncode(data.name);
+            viewModel.createTag(data);
+            $("#tagsNew").val("");
+            $(document).trigger("info", "${_('Project created')}");
+            $("#addTagModal").modal("hide");
+		  }
       }).fail(function (xhr, textStatus, errorThrown) {
         $(document).trigger("error", "${_("There was an error processing your action: ")}" + xhr.responseText); // reserved name, duplicate etc
       });
@@ -400,7 +421,9 @@ ${ commonshare() | n,unicode }
   });
 
   function addTag() {
+    $("#tagsNew").val('');
     $("#addTagModal").modal("show");
+	$("#saveProjectAlert").hide();
   }
 
   function removeTag() {