create_database.mako 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. ## Licensed to Cloudera, Inc. under one
  2. ## or more contributor license agreements. See the NOTICE file
  3. ## distributed with this work for additional information
  4. ## regarding copyright ownership. Cloudera, Inc. licenses this file
  5. ## to you under the Apache License, Version 2.0 (the
  6. ## "License"); you may not use this file except in compliance
  7. ## with the License. You may obtain a copy of the License at
  8. ##
  9. ## http://www.apache.org/licenses/LICENSE-2.0
  10. ##
  11. ## Unless required by applicable law or agreed to in writing, software
  12. ## distributed under the License is distributed on an "AS IS" BASIS,
  13. ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ## See the License for the specific language governing permissions and
  15. ## limitations under the License.
  16. <%!
  17. from desktop import conf
  18. from desktop.views import commonheader, commonfooter
  19. from django.utils.translation import ugettext as _
  20. %>
  21. <%namespace name="assist" file="/assist.mako" />
  22. <%namespace name="comps" file="beeswax_components.mako" />
  23. <%namespace name="layout" file="layout.mako" />
  24. ${ commonheader(_("Create database"), 'metastore', user) | n,unicode }
  25. ${layout.metastore_menubar()}
  26. <script src="${ static('desktop/ext/js/jquery/plugins/jquery-ui-1.10.4.custom.min.js') }"></script>
  27. <script src="${ static('desktop/js/jquery.huedatatable.js') }"></script>
  28. <script src="${ static('desktop/ext/js/d3.v3.js') }" type="text/javascript" charset="utf-8"></script>
  29. <script src="${ static('desktop/ext/js/routie-0.3.0.min.js') }" type="text/javascript" charset="utf-8"></script>
  30. <script src="${ static('desktop/ext/js/knockout.min.js') }"></script>
  31. <script src="${ static('desktop/ext/js/selectize.min.js') }"></script>
  32. <script src="${ static('desktop/js/apiHelper.js') }"></script>
  33. <script src="${ static('metastore/js/metastore.ko.js') }"></script>
  34. <script src="${ static('desktop/js/ko.charts.js') }"></script>
  35. <script src="${ static('desktop/ext/js/knockout-mapping.min.js') }"></script>
  36. <script src="${ static('desktop/ext/js/knockout-sortable.min.js') }"></script>
  37. <script src="${ static('desktop/js/ko.editable.js') }"></script>
  38. <script src="${ static('desktop/js/ko.hue-bindings.js') }"></script>
  39. <script src="${ static('desktop/js/assist/assistDbEntry.js') }"></script>
  40. <script src="${ static('desktop/js/assist/assistDbSource.js') }"></script>
  41. <script src="${ static('desktop/js/assist/assistHdfsEntry.js') }"></script>
  42. <script src="${ static('desktop/js/fileBrowser/hueDocument.js') }"></script>
  43. <script src="${ static('desktop/js/fileBrowser/hueFileEntry.js') }"></script>
  44. <link rel="stylesheet" href="${ static('metastore/css/metastore.css') }" type="text/css">
  45. <link rel="stylesheet" href="${ static('notebook/css/notebook.css') }">
  46. <style type="text/css">
  47. % if conf.CUSTOM.BANNER_TOP_HTML.get():
  48. .show-assist {
  49. top: 110px!important;
  50. }
  51. .main-content {
  52. top: 112px!important;
  53. }
  54. % endif
  55. </style>
  56. ${ assist.assistPanel() }
  57. <a title="${_('Toggle Assist')}" class="pointer show-assist" data-bind="visible: !$root.isLeftPanelVisible() && $root.assistAvailable(), click: function() { $root.isLeftPanelVisible(true); }">
  58. <i class="fa fa-chevron-right"></i>
  59. </a>
  60. <div class="main-content">
  61. <div class="vertical-full container-fluid" data-bind="style: { 'padding-left' : $root.isLeftPanelVisible() ? '0' : '20px' }">
  62. <div class="vertical-full">
  63. <div class="vertical-full row-fluid panel-container">
  64. <div class="assist-container left-panel" data-bind="visible: $root.isLeftPanelVisible() && $root.assistAvailable()">
  65. <a title="${_('Toggle Assist')}" class="pointer hide-assist" data-bind="click: function() { $root.isLeftPanelVisible(false) }">
  66. <i class="fa fa-chevron-left"></i>
  67. </a>
  68. <div class="assist" data-bind="component: {
  69. name: 'assist-panel',
  70. params: {
  71. user: '${user.username}',
  72. sql : {
  73. sourceTypes: [{
  74. name: 'hive',
  75. type: 'hive'
  76. }],
  77. navigationSettings: {
  78. openItem: false,
  79. showStats: true
  80. }
  81. },
  82. visibleAssistPanels: ['sql']
  83. }
  84. }"></div>
  85. </div>
  86. <div class="resizer" data-bind="visible: $root.isLeftPanelVisible() && $root.assistAvailable(), splitDraggable : { appName: 'notebook', leftPanelVisible: $root.isLeftPanelVisible }"><div class="resize-bar">&nbsp;</div></div>
  87. <div class="right-panel">
  88. <div class="metastore-main">
  89. <h3>${_('Create a new database')}</h3>
  90. <ul id="step-nav" class="nav nav-pills">
  91. <li class="active"><a href="#step/1" class="step">${_('Step 1: Name')}</a></li>
  92. <li><a href="#step/2" class="step">${_('Step 2: Location')}</a></li>
  93. </ul>
  94. <form action="${ url(app_name + ':create_database')}" method="POST" id="mainForm" class="form-horizontal">
  95. ${ csrf_token(request) | n,unicode }
  96. <div class="steps">
  97. <div id="step1" class="stepDetails">
  98. <fieldset>
  99. <div class="alert alert-info"><h3>${_('Create a database')}</h3>${_("Let's start with a name and description for where we'll store your data.")}</div>
  100. <div class="control-group">
  101. ${comps.bootstrapLabel(database_form["name"])}
  102. <div class="controls">
  103. ${comps.field(database_form["name"], attrs=dict(
  104. placeholder=_('database_name'),
  105. )
  106. )}
  107. <span class="help-inline error-inline hide">${_('This field is required. Spaces are not allowed.')}</span>
  108. <p class="help-block">
  109. ${_('Name of the new database. Database names must be globally unique. Database names tend to correspond to the directory where the data will be stored.')}
  110. </p>
  111. </div>
  112. </div>
  113. <div class="control-group">
  114. ${comps.bootstrapLabel(database_form["comment"])}
  115. <div class="controls">
  116. ${comps.field(database_form["comment"], attrs=dict(
  117. placeholder=_('Optional'),
  118. )
  119. )}
  120. <p class="help-block">
  121. ${_("Use a database comment to describe the database. For example, note the data's provenance and any caveats users need to know.")}
  122. </p>
  123. </div>
  124. </div>
  125. </fieldset>
  126. </div>
  127. <div id="step2" class="stepDetails hide">
  128. <fieldset>
  129. <div class="alert alert-info"><h3>${_("Choose Where Your Database's Data is Stored")}</h3>
  130. </div>
  131. <div class="control-group">
  132. <label class="control-label">${_('Location')}</label>
  133. <div class="controls">
  134. <label class="checkbox">
  135. ${comps.field(database_form["use_default_location"],
  136. render_default=True
  137. )}
  138. ${_('Use default location')}
  139. </label>
  140. <span class="help-block">
  141. ${_('Store your database in the default location (controlled by Hive, and typically')} <em>/user/hive/warehouse/database_name</em>).
  142. </span>
  143. </div>
  144. </div>
  145. <div id="location" class="control-group hide">
  146. ${comps.bootstrapLabel(database_form["external_location"])}
  147. <div class="controls">
  148. ${comps.field(database_form["external_location"],
  149. placeholder="/user/user_name/data_dir",
  150. klass="pathChooser input-xxlarge",
  151. file_chooser=True,
  152. show_errors=False
  153. )}
  154. <span class="help-inline error-inline hide">${_('This field is required.')}</span>
  155. <span class="help-block">
  156. ${_("Enter the path (on HDFS) to your database's data location.")}
  157. </span>
  158. </div>
  159. </div>
  160. </fieldset>
  161. </div>
  162. </div>
  163. <div class="form-actions" style="padding-left: 10px">
  164. <button type="button" id="backBtn" class="btn hide">${_('Back')}</button>
  165. <button type="button" id="nextBtn" class="btn btn-primary">${_('Next')}</button>
  166. <input id="submit" type="submit" name="create" class="btn btn-primary hide" value="${_('Create database')}" />
  167. </div>
  168. </form>
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. </div>
  174. </div>
  175. <div id="chooseFile" class="modal hide fade">
  176. <div class="modal-header">
  177. <a href="#" class="close" data-dismiss="modal">&times;</a>
  178. <h3>${_('Choose a file')}</h3>
  179. </div>
  180. <div class="modal-body">
  181. <div id="filechooser">
  182. </div>
  183. </div>
  184. <div class="modal-footer">
  185. </div>
  186. </div>
  187. <style type="text/css">
  188. #filechooser {
  189. min-height: 100px;
  190. overflow-y: auto;
  191. margin-top: 10px;
  192. height: 250px;
  193. }
  194. .inputs-list {
  195. list-style: none outside none;
  196. margin-left: 0;
  197. }
  198. .remove {
  199. float: right;
  200. }
  201. .error-inline {
  202. color: #B94A48;
  203. font-weight: bold;
  204. }
  205. .steps {
  206. min-height: 350px;
  207. margin-top: 10px;
  208. }
  209. div .alert {
  210. margin-bottom: 30px;
  211. }
  212. </style>
  213. </div>
  214. <script type="text/javascript" charset="utf-8">
  215. (function () {
  216. ko.options.deferUpdates = true;
  217. function MetastoreViewModel(options) {
  218. var self = this;
  219. self.apiHelper = ApiHelper.getInstance(options);
  220. self.assistAvailable = ko.observable(true);
  221. self.isLeftPanelVisible = ko.observable();
  222. self.apiHelper.withTotalStorage('assist', 'assist_panel_visible', self.isLeftPanelVisible, true);
  223. huePubSub.subscribe("assist.table.selected", function (tableDef) {
  224. location.href = '/metastore/table/' + tableDef.database + '/' + tableDef.name;
  225. });
  226. huePubSub.subscribe("assist.database.selected", function (databaseDef) {
  227. location.href = '/metastore/tables/' + databaseDef.name;
  228. });
  229. }
  230. $(document).ready(function () {
  231. var options = {
  232. user: '${ user.username }',
  233. i18n: {
  234. errorLoadingDatabases: "${ _('There was a problem loading the databases') }",
  235. errorLoadingTablePreview: "${ _('There was a problem loading the table preview.') }"
  236. }
  237. };
  238. var viewModel = new MetastoreViewModel(options);
  239. ko.applyBindings(viewModel);
  240. // Routing
  241. var step = 1;
  242. routie({
  243. 'step/1': function(node_type) {
  244. $("#step-nav").children().removeClass("active");
  245. $("#step-nav").children(":nth-child(1)").addClass("active");
  246. $('.stepDetails').hide();
  247. $('#step1').show();
  248. $("#backBtn").hide();
  249. $("#nextBtn").show();
  250. $("#submit").hide();
  251. },
  252. 'step/2': function(node_type) {
  253. $("#step-nav").children().removeClass("active");
  254. $("#step-nav").children(":nth-child(2)").addClass("active");
  255. $('.stepDetails').hide();
  256. $('#step2').show();
  257. $("#backBtn").show();
  258. $("#nextBtn").hide();
  259. $("#submit").show();
  260. }
  261. });
  262. routie('step/' + step);
  263. // events
  264. $(".fileChooserBtn").click(function (e) {
  265. e.preventDefault();
  266. var _destination = $(this).attr("data-filechooser-destination");
  267. $("#filechooser").jHueFileChooser({
  268. initialPath: $("input[name='" + _destination + "']").val(),
  269. onFolderChoose: function (filePath) {
  270. $("input[name='" + _destination + "']").val(filePath);
  271. $("#chooseFile").modal("hide");
  272. },
  273. createFolder: false,
  274. selectFolder: true,
  275. uploadFile: false
  276. });
  277. $("#chooseFile").modal("show");
  278. });
  279. $("#id_use_default_location").change(function () {
  280. if (!$(this).is(":checked")) {
  281. $("#location").slideDown();
  282. }
  283. else {
  284. $("#location").slideUp();
  285. }
  286. });
  287. $("#submit").click(function() {
  288. return validate();
  289. });
  290. $("#nextBtn").click(function () {
  291. if (validate()) {
  292. routie('step/' + ++step);
  293. }
  294. });
  295. $("#backBtn").click(function () {
  296. // To get to the current step
  297. // users will have to get through all previous steps.
  298. routie('step/' + --step);
  299. });
  300. $(".step").click(function() {
  301. return validate();
  302. });
  303. // Validation
  304. function validate() {
  305. switch(step) {
  306. case 1:
  307. var databaseNameFld = $("input[name='name']");
  308. if (!isValid($.trim(databaseNameFld.val()))) {
  309. showFieldError(databaseNameFld);
  310. return false;
  311. } else {
  312. hideFieldError(databaseNameFld);
  313. }
  314. break;
  315. case 2:
  316. var externalLocationFld = $("input[name='external_location']");
  317. if (!($("#id_use_default_location").is(":checked"))) {
  318. if (!isValid($.trim(externalLocationFld.val()))) {
  319. showFieldError(externalLocationFld);
  320. return false;
  321. }
  322. else {
  323. hideFieldError(externalLocationFld);
  324. }
  325. }
  326. break;
  327. }
  328. return true;
  329. }
  330. function isValid(str) {
  331. return (str != "" && str.indexOf(" ") == -1);
  332. }
  333. function showFieldError(field) {
  334. field.nextAll(".error-inline").not(".error-inline-bis").removeClass("hide");
  335. }
  336. function hideFieldError(field) {
  337. if (!(field.nextAll(".error-inline").hasClass("hide"))) {
  338. field.nextAll(".error-inline").addClass("hide");
  339. }
  340. }
  341. });
  342. })();
  343. </script>
  344. ${ commonfooter(request, messages) | n,unicode }