Просмотр исходного кода

[core] Indent and simplify checking logic of new tag creation

Romain Rigaux 10 лет назад
Родитель
Сommit
5f9e9bec46
2 измененных файлов с 37 добавлено и 44 удалено
  1. 12 17
      desktop/core/src/desktop/api.py
  2. 25 27
      desktop/core/src/desktop/templates/home.mako

+ 12 - 17
desktop/core/src/desktop/api.py

@@ -214,29 +214,24 @@ def massage_doc_for_json(document, user, url=''):
 
 
 def valid_project(name):
 def valid_project(name):
   project_doc = DocumentTag.objects.filter(tag=name)
   project_doc = DocumentTag.objects.filter(tag=name)
-  num = len(project_doc)
-  if num > 1:
-    return -1
-  if num == 0:
-    return 0
-  return -1
-  
+  return len(project_doc) == 0
+
+
 @require_POST
 @require_POST
 def add_tag(request):
 def add_tag(request):
   response = {'status': -1, 'message': ''}
   response = {'status': -1, 'message': ''}
-  
 
 
   try:
   try:
     validstatus = valid_project(name=request.POST['name'])
     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
+    if validstatus:
+      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:
   except KeyError, e:
     response['message'] = _('Form is missing %s field') % e.message
     response['message'] = _('Form is missing %s field') % e.message
   except Exception, e:
   except Exception, e:

+ 25 - 27
desktop/core/src/desktop/templates/home.mako

@@ -296,8 +296,8 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
       </p>
       </p>
     </div>
     </div>
     <div class="modal-footer">
     <div class="modal-footer">
-	  <div id="saveProjectAlert" class="alert-message error hide" style="position: absolute; left: 78px;">
-          <span class="label label-important"></span>
+      <div id="saveProjectAlert" class="alert-message error hide" style="position: absolute; left: 78px;">
+        <span class="label label-important"></span>
       </div>
       </div>
       <a href="#" data-dismiss="modal" class="btn">${_('Cancel')}</a>
       <a href="#" data-dismiss="modal" class="btn">${_('Cancel')}</a>
       <a id="tagsNewBtn" href="#" class="btn btn-primary disable-feedback">${ _('Add') }</a>
       <a id="tagsNewBtn" href="#" class="btn btn-primary disable-feedback">${ _('Add') }</a>
@@ -381,32 +381,30 @@ ${ commonshare() | n,unicode }
 
 
     $("#tagsNewBtn").on("click", function () {
     $("#tagsNewBtn").on("click", function () {
       var tag_name = $("#tagsNew").val();
       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;
-      }
+  
+    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", {
+    $.post("/desktop/api/tag/add_tag", {
         name: tag_name
         name: tag_name
-      },function (data) {
-	  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");
-		  }
+      }, function (data) {
+        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) {
       }).fail(function (xhr, textStatus, errorThrown) {
         $(document).trigger("error", "${_("There was an error processing your action: ")}" + xhr.responseText); // reserved name, duplicate etc
         $(document).trigger("error", "${_("There was an error processing your action: ")}" + xhr.responseText); // reserved name, duplicate etc
       });
       });
@@ -423,7 +421,7 @@ ${ commonshare() | n,unicode }
   function addTag() {
   function addTag() {
     $("#tagsNew").val('');
     $("#tagsNew").val('');
     $("#addTagModal").modal("show");
     $("#addTagModal").modal("show");
-	$("#saveProjectAlert").hide();
+    $("#saveProjectAlert").hide();
   }
   }
 
 
   function removeTag() {
   function removeTag() {