Commit f1551eed authored by Tom Mortimer-Jones's avatar Tom Mortimer-Jones
Browse files

[1599] Autotheme package when creating/editing it

parent f682839e
......@@ -8,11 +8,14 @@ Themes are not currently stored in the database (they're in schema.py for now
but should the need arise to add more
"""
import sqlalchemy
import pylons
from pylons import request, response
from pylons.controllers.util import abort
import os
import logging
import json
from ckan.lib.base import (BaseController, abort)
from ckanext.dgu.plugins_toolkit import (c, render, get_action)
from ckanext.dgu.lib.theme import categorize_package
from ckan import model
log = logging.getLogger(__name__)
......@@ -23,6 +26,25 @@ class ThemeController(BaseController):
def index(self):
return render('themed/index.html')
def categorize(self):
response.headers['Content-type'] = 'application/json'
if 'pkg' not in request.params:
print "pkg parameter missing"
abort(400, "pkg parameter missing")
try:
data = json.loads(request.params.get('pkg'))
except ValueError:
print "Error decoding pkg JSON"
abort(400, "Error decoding pkg JSON")
for key in ['name', 'title', 'notes', 'tags', 'extras']:
if key not in data:
print "Missing key '%s' in pkg JSON" % key
abort(400, "Missing key '%s' in pkg JSON" % key)
return json.dumps(categorize_package(data))
def named_theme(self, name):
"""
Shows the theme home page for a specified theme containing information relevant
......
......@@ -194,6 +194,7 @@ class ThemePlugin(p.SingletonPlugin):
theme_controller = 'ckanext.dgu.controllers.theme:ThemeController'
map.connect('/data/themes', controller=theme_controller, action='index')
map.connect('/data/themes/categorize', controller=theme_controller, action='categorize')
map.connect('/data/themes/{name}', controller=theme_controller, action='named_theme')
# For test usage when Drupal is not running
......
......@@ -552,17 +552,18 @@ Passed in:
<fieldset class="tab-pane boxed whitebox fade" id="section-themes-field">
<div style="width: 45%; float: left; margin-right: 4%;">
<h3 style="font-weight: bold;">Primary Theme</h3>
<p>1. Please choose a primary theme which this data record falls under from the options below.</p>
{{m.primary_theme_input(data, errors)}}
</div>
<div style="width: 45%; float: left; padding-left: 4%; border-left: 1px solid #aaa;">
<h3>Secondary Theme</h3>
<p>2. If there are other themes in this data, select them here:</p>
<div class="well">
{{m.secondary_theme_input(data, errors)}}
</div>
</div>
<div class="clearfix">&nbsp;</div>
<p>These themes are automatically chosen based on the details provided in the name, description, tags etc. If the dataset has not yet received a theme or you believe it has been given the wrong theme then you must add more details to the dataset.</p>
<button id="recategorize">Recategorize</button>
<hr/>
<h3>Tags</h3>
......@@ -989,6 +990,10 @@ $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
$("#schema-list select").chosen({allow_single_deselect: true});
$("#codelist-list select").chosen({allow_single_deselect: true});
}
if(/#section-themes-field$/.test(e.target)) {
categorize();
}
});
$(function() {
......@@ -1000,7 +1005,41 @@ $(function() {
$("#core-dataset-hidden").prop("disabled", true);
}
});
$('#section-themes-field input').on('click', function(){return false});
});
function categorize() {
var pkg_data = {
name: $("#name").val(),
title: $("#title").val(),
notes: $("#notes").val(),
tags: $("#tag_string").val().split(","),
extras: {}
};
$.ajax({
type: "POST",
url: "/data/themes/categorize",
data: {pkg: JSON.stringify(pkg_data)},
dataType: 'json',
success: function(data, text, xhr) {
$('#section-themes-field input[type=radio]').prop('checked', false)
$('#section-themes-field input[type=checkbox]').prop('checked', false)
$(data).each(function(i, el) {
if(i == 0) {
$('#section-themes-field input[type=radio][value="' + el + '"]').prop('checked', true)
} else {
$('#section-themes-field input[type=checkbox][value="' + el + '"]').prop('checked', true)
}
});
},
error: function(d) { console.log("error " + d + ".") }
});
return false;
}
$('#recategorize').click(categorize);
</script>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment