create_database.mako 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. import sys
  18. from desktop import conf
  19. from desktop.views import commonheader, commonfooter
  20. if sys.version_info[0] > 2:
  21. from django.utils.translation import gettext as _
  22. else:
  23. from django.utils.translation import ugettext as _
  24. %>
  25. <%namespace name="comps" file="beeswax_components.mako" />
  26. <%namespace name="layout" file="layout.mako" />
  27. ${ commonheader(_("Create database"), 'metastore', user, request) | n,unicode }
  28. <span class="notebook">
  29. ${layout.metastore_menubar()}
  30. <script src="${ static('desktop/js/hue.routie.js') }" type="text/javascript" charset="utf-8"></script>
  31. <link rel="stylesheet" href="${ static('metastore/css/metastore.css') }" type="text/css">
  32. <link rel="stylesheet" href="${ static('notebook/css/notebook.css') }">
  33. <link rel="stylesheet" href="${ static('notebook/css/notebook-layout.css') }">
  34. <style type="text/css">
  35. % if conf.CUSTOM.BANNER_TOP_HTML.get():
  36. .show-assist {
  37. top: 110px!important;
  38. }
  39. .main-content {
  40. top: 112px!important;
  41. }
  42. % endif
  43. </style>
  44. <a title="${_('Toggle Assist')}" class="pointer show-assist" data-bind="visible: !$root.isLeftPanelVisible() && $root.assistAvailable(), click: function() { $root.isLeftPanelVisible(true); }">
  45. <i class="fa fa-chevron-right"></i>
  46. </a>
  47. <div class="main-content">
  48. <div class="vertical-full container-fluid" data-bind="style: { 'padding-left' : $root.isLeftPanelVisible() ? '0' : '20px' }">
  49. <div class="vertical-full">
  50. <div class="vertical-full row-fluid panel-container">
  51. <div class="assist-container left-panel" data-bind="visible: $root.isLeftPanelVisible() && $root.assistAvailable()">
  52. <a title="${_('Toggle Assist')}" class="pointer hide-assist" data-bind="click: function() { $root.isLeftPanelVisible(false) }">
  53. <i class="fa fa-chevron-left"></i>
  54. </a>
  55. <div class="assist" data-bind="component: {
  56. name: 'assist-panel',
  57. params: {
  58. user: '${user.username}',
  59. sql : {
  60. navigationSettings: {
  61. openItem: false,
  62. showStats: true
  63. }
  64. },
  65. visibleAssistPanels: ['sql']
  66. }
  67. }"></div>
  68. </div>
  69. <div class="resizer" data-bind="visible: $root.isLeftPanelVisible() && $root.assistAvailable(), splitDraggable : { appName: 'notebook', leftPanelVisible: $root.isLeftPanelVisible }"><div class="resize-bar">&nbsp;</div></div>
  70. <div class="content-panel">
  71. <div class="metastore-main">
  72. <h3>${_('Create a new database')}</h3>
  73. <ul id="step-nav" class="nav nav-pills">
  74. <li class="active"><a href="#step/1" class="step">${_('Step 1: Name')}</a></li>
  75. <li><a href="#step/2" class="step">${_('Step 2: Location')}</a></li>
  76. </ul>
  77. <form action="${ url(app_name + ':create_database')}" method="POST" id="mainForm" class="form-horizontal">
  78. ${ csrf_token(request) | n,unicode }
  79. <div class="steps">
  80. <div id="step1" class="stepDetails">
  81. <fieldset>
  82. <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>
  83. <div class="control-group">
  84. ${comps.bootstrapLabel(database_form["name"])}
  85. <div class="controls">
  86. ${comps.field(database_form["name"], attrs=dict(
  87. placeholder=_('database_name'),
  88. )
  89. )}
  90. <span class="help-inline error-inline hide">${_('This field is required. Spaces are not allowed.')}</span>
  91. <p class="help-block">
  92. ${_('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.')}
  93. </p>
  94. </div>
  95. </div>
  96. <div class="control-group">
  97. ${comps.bootstrapLabel(database_form["comment"])}
  98. <div class="controls">
  99. ${comps.field(database_form["comment"], attrs=dict(
  100. placeholder=_('Optional'),
  101. )
  102. )}
  103. <p class="help-block">
  104. ${_("Use a database comment to describe the database. For example, note the data's provenance and any caveats users need to know.")}
  105. </p>
  106. </div>
  107. </div>
  108. </fieldset>
  109. </div>
  110. <div id="step2" class="stepDetails hide">
  111. <fieldset>
  112. <div class="alert alert-info"><h3>${_("Choose Where Your Database's Data is Stored")}</h3>
  113. </div>
  114. <div class="control-group">
  115. <label class="control-label">${_('Location')}</label>
  116. <div class="controls">
  117. <label class="checkbox">
  118. ${comps.field(database_form["use_default_location"],
  119. render_default=True
  120. )}
  121. ${_('Use default location')}
  122. </label>
  123. <span class="help-block">
  124. ${_('Store your database in the default location (controlled by Hive, and typically')} <em>/user/hive/warehouse/database_name</em>).
  125. </span>
  126. </div>
  127. </div>
  128. <div id="location" class="control-group hide">
  129. ${comps.bootstrapLabel(database_form["external_location"])}
  130. <div class="controls">
  131. ${comps.field(database_form["external_location"],
  132. placeholder="/user/user_name/data_dir",
  133. klass="pathChooser input-xxlarge",
  134. file_chooser=True,
  135. show_errors=False
  136. )}
  137. <span class="help-inline error-inline hide">${_('This field is required.')}</span>
  138. <span class="help-block">
  139. ${_("Enter the path (on HDFS) to your database's data location.")}
  140. </span>
  141. </div>
  142. </div>
  143. </fieldset>
  144. </div>
  145. </div>
  146. <div class="form-actions" style="padding-left: 10px">
  147. <button type="button" id="backBtn" class="btn hide">${_('Back')}</button>
  148. <button type="button" id="nextBtn" class="btn btn-primary">${_('Next')}</button>
  149. <input id="submit" type="submit" name="create" class="btn btn-primary hide" value="${_('Create database')}" />
  150. </div>
  151. </form>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. <style type="text/css">
  159. #filechooser {
  160. min-height: 100px;
  161. overflow-y: auto;
  162. margin-top: 10px;
  163. height: 250px;
  164. }
  165. .inputs-list {
  166. list-style: none outside none;
  167. margin-left: 0;
  168. }
  169. .remove {
  170. float: right;
  171. }
  172. .error-inline {
  173. color: #B94A48;
  174. font-weight: bold;
  175. }
  176. .steps {
  177. min-height: 350px;
  178. margin-top: 10px;
  179. }
  180. div .alert {
  181. margin-bottom: 30px;
  182. }
  183. </style>
  184. </div>
  185. <script type="text/javascript">
  186. (function () {
  187. if (ko.options) {
  188. ko.options.deferUpdates = true;
  189. }
  190. function CreateDatabaseViewModel() {
  191. var self = this;
  192. self.apiHelper = window.apiHelper;
  193. self.assistAvailable = ko.observable(true);
  194. self.isLeftPanelVisible = ko.observable();
  195. window.hueUtils.withLocalStorage('assist.assist_panel_visible', self.isLeftPanelVisible, true);
  196. self.isLeftPanelVisible.subscribe(function () {
  197. huePubSub.publish('assist.forceRender');
  198. });
  199. huePubSub.subscribe("assist.table.selected", function (entry) {
  200. location.href = '/metastore/table/' + entry.path[0] + '/' + entry.name + '?connector_id=' + entry.getConnector().id + '&namespace=' + entry.namespace.id;
  201. });
  202. huePubSub.subscribe("assist.database.selected", function (entry) {
  203. location.href = '/metastore/tables/' + entry.name + '?connector_id=' + entry.getConnector().id + '&namespace=' + entry.namespace.id;
  204. });
  205. }
  206. $(document).ready(function () {
  207. var viewModel = new CreateDatabaseViewModel();
  208. ko.applyBindings(viewModel);
  209. // Routing
  210. var step = 1;
  211. routie({
  212. 'step/1': function(node_type) {
  213. $("#step-nav").children().removeClass("active");
  214. $("#step-nav").children(":nth-child(1)").addClass("active");
  215. $('.stepDetails').hide();
  216. $('#step1').show();
  217. $("#backBtn").hide();
  218. $("#nextBtn").show();
  219. $("#submit").hide();
  220. },
  221. 'step/2': function(node_type) {
  222. $("#step-nav").children().removeClass("active");
  223. $("#step-nav").children(":nth-child(2)").addClass("active");
  224. $('.stepDetails').hide();
  225. $('#step2').show();
  226. $("#backBtn").show();
  227. $("#nextBtn").hide();
  228. $("#submit").show();
  229. }
  230. });
  231. routie('step/' + step);
  232. // events
  233. $(".fileChooserBtn").click(function (e) {
  234. e.preventDefault();
  235. var _destination = $(this).attr("data-filechooser-destination");
  236. $("#filechooser").jHueFileChooser({
  237. initialPath: $("input[name='" + _destination + "']").val(),
  238. onFolderChoose: function (filePath) {
  239. $("input[name='" + _destination + "']").val(filePath);
  240. $("#chooseFile").modal("hide");
  241. },
  242. createFolder: false,
  243. selectFolder: true,
  244. uploadFile: false
  245. });
  246. $("#chooseFile").modal("show");
  247. });
  248. $("#id_use_default_location").change(function () {
  249. if (!$(this).is(":checked")) {
  250. $("#location").slideDown();
  251. }
  252. else {
  253. $("#location").slideUp();
  254. }
  255. });
  256. $("#submit").click(function() {
  257. return validate();
  258. });
  259. $("#nextBtn").click(function () {
  260. if (validate()) {
  261. routie('step/' + ++step);
  262. }
  263. });
  264. $("#backBtn").click(function () {
  265. // To get to the current step
  266. // users will have to get through all previous steps.
  267. routie('step/' + --step);
  268. });
  269. $(".step").click(function() {
  270. return validate();
  271. });
  272. // Validation
  273. function validate() {
  274. switch(step) {
  275. case 1:
  276. var databaseNameFld = $("input[name='name']");
  277. if (!isValid($.trim(databaseNameFld.val()))) {
  278. showFieldError(databaseNameFld);
  279. return false;
  280. } else {
  281. hideFieldError(databaseNameFld);
  282. }
  283. break;
  284. case 2:
  285. var externalLocationFld = $("input[name='external_location']");
  286. if (!($("#id_use_default_location").is(":checked"))) {
  287. if (!isValid($.trim(externalLocationFld.val()))) {
  288. showFieldError(externalLocationFld);
  289. return false;
  290. }
  291. else {
  292. hideFieldError(externalLocationFld);
  293. }
  294. }
  295. break;
  296. }
  297. return true;
  298. }
  299. function isValid(str) {
  300. return (str != "" && str.indexOf(" ") == -1);
  301. }
  302. function showFieldError(field) {
  303. field.nextAll(".error-inline").not(".error-inline-bis").removeClass("hide");
  304. }
  305. function hideFieldError(field) {
  306. if (!(field.nextAll(".error-inline").hasClass("hide"))) {
  307. field.nextAll(".error-inline").addClass("hide");
  308. }
  309. }
  310. });
  311. })();
  312. </script>
  313. </span>
  314. ${ commonfooter(request, messages) | n,unicode }