[ckan-changes] [okfn/ckan] b562d3: [2204] First attempt at a JS func for creating rel...

GitHub noreply at github.com
Thu Apr 19 12:45:57 UTC 2012


  Branch: refs/heads/feature-2204-related
  Home:   https://github.com/okfn/ckan
  Commit: b562d3c626d70a2d6f2c7c2425e72cf089b63237
      https://github.com/okfn/ckan/commit/b562d3c626d70a2d6f2c7c2425e72cf089b63237
  Author: Ross Jones <rossdjones at gmail.com>
  Date:   2012-04-19 (Thu, 19 Apr 2012)

  Changed paths:
    M ckan/logic/action/create.py
    M ckan/public/scripts/application.js
    M ckan/templates/_snippet/add-related.html

  Log Message:
  -----------
  [2204] First attempt at a JS func for creating related items, and a change to the logic layer to add the related item if the dataset_id is provided


diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py
index a42d2b4..aee73a3 100644
--- a/ckan/logic/action/create.py
+++ b/ckan/logic/action/create.py
@@ -130,6 +130,11 @@ def related_create(context, data_dict):
     if not context.get('defer_commit'):
         model.repo.commit_and_remove()
 
+    if 'dataset_id' in data_dict:
+        dataset = model.Package.get(data_dict['dataset_id'])
+        dataset.related.append( related )
+        model.repo.commit_and_remove()
+
     context["related"] = related
     context["id"] = related.id
     log.debug('Created object %s' % str(related.title))
diff --git a/ckan/public/scripts/application.js b/ckan/public/scripts/application.js
index 8d74c8e..5ddbae9 100644
--- a/ckan/public/scripts/application.js
+++ b/ckan/public/scripts/application.js
@@ -9,6 +9,7 @@ CKAN.Utils = CKAN.Utils || {};
 /* ================================= */
 (function ($) {
   $(document).ready(function () {
+    CKAN.Utils.relatedSetup($("#add-related-submit"));
     CKAN.Utils.setupUserAutocomplete($('input.autocomplete-user'));
     CKAN.Utils.setupOrganizationUserAutocomplete($('input.autocomplete-organization-user'));
     CKAN.Utils.setupGroupAutocomplete($('input.autocomplete-group'));
@@ -692,7 +693,7 @@ CKAN.View.ResourceAddUpload = Backbone.View.extend({
   setupFileUpload: function() {
     var self = this;
     this.el.find('.fileupload').fileupload({
-      // needed because we are posting to remote url 
+      // needed because we are posting to remote url
       forceIframeTransport: true,
       replaceFileInput: false,
       autoUpload: false,
@@ -725,7 +726,7 @@ CKAN.View.ResourceAddUpload = Backbone.View.extend({
   },
 
   // Create an upload key/label for this file.
-  // 
+  //
   // Form: {current-date}/file-name. Do not just use the file name as this
   // would lead to collisions.
   // (Could add userid/username and/or a small random string to reduce
@@ -790,7 +791,7 @@ CKAN.View.ResourceAddUpload = Backbone.View.extend({
         newResource.set({
             url: data._location
             , name: name
-            , size: data._content_length 
+            , size: data._content_length
             , last_modified: lastmod
             , format: data._format
             , mimetype: data._format
@@ -873,7 +874,7 @@ CKAN.View.ResourceAddUrl = Backbone.View.extend({
            self.resetForm();
          }
        });
-     } 
+     }
      else {
        newResource.set({url: urlVal, resource_type: this.options.mode});
        if (newResource.get('resource_type')=='file') {
@@ -1101,6 +1102,40 @@ CKAN.Utils = function($, my) {
     });
   };
 
+
+  my.relatedSetup = function(okBtn) {
+      $(okBtn).click(function(){
+          // Validate the form
+          var data = {
+              title: $("#related-title").val(),
+              type:  $("#related-type").val(),
+              description: $("#related-description").val(),
+              url: $("#related-url").val(),
+              image_url: $("#related-image-url").val(),
+              dataset_id: $("#related-dataset").val(),
+          };
+
+          if ( ! data.title ) {
+              // TODO: Fix this
+              alert( "You must specify the title");
+              return true;
+          }
+
+          $.ajax({
+              type: 'POST',
+              url: CKAN.SITE_URL + '/api/3/action/related_create',
+              data: JSON.stringify(data),
+              success: function(data){
+                  window.location.href=window.location.href;
+              },
+              error: function(err, txt,w) {
+                  console.log(w);
+              }
+          });
+      });
+      return false;
+  };
+
   // Attach authz group autocompletion to provided elements
   //
   // Requires: jquery-ui autocomplete
diff --git a/ckan/templates/_snippet/add-related.html b/ckan/templates/_snippet/add-related.html
index 9e90998..d8d4f26 100644
--- a/ckan/templates/_snippet/add-related.html
+++ b/ckan/templates/_snippet/add-related.html
@@ -16,32 +16,33 @@
 </div>
 <div class="modal-body">
     <form class="well">
+      <input id="related-dataset" type="hidden" value="${c.pkg.id}"/>
+
       <label>Title</label>
-      <input type="text" class="span6" placeholder="Please add the title for the item"/>
+      <input id="related-title" type="text" class="span6" placeholder="Please add the title for the item"/>
 
       <label>Type of item</label>
-      <select class="span6 chzn-select">
+      <select id="related-type" class="span6 chzn-select">
           <option value="app">Application</option>
           <option value="idea">Idea</option>
           <option value="visualization">Visualization</option>
       </select>
 
       <label>Description</label>
-      <textarea class="input-xlarge span6" rows="4" placeholder="Please describe the item"></textarea>
+      <textarea id="related-description" class="input-xlarge span6" rows="4" placeholder="Please describe the item"></textarea>
 
       <label>URL</label>
-      <input type="text" class="span6" placeholder="Please add a url"/>
+      <input id="related-url" type="text" class="span6" placeholder="Please add a url"/>
 
       <label>Image URL</label>
-      <input type="text" class="span6" placeholder="Please add a link to the image"/>
+      <input id="related-image-url" type="text" class="span6" placeholder="Please add a link to the image"/>
 
       <div>
-          <button type="submit" class="btn btn-primary">Submit</button>
+          <button id='add-related-submit' type="submit" class="btn btn-primary">Submit</button>
           <button class="btn">Cancel</button>
       </div>
     </form>
 
 </div>
 </div>
-
 </html>


================================================================



More information about the ckan-changes mailing list