Skip to content

Commit 4b9704f

Browse files
committed
[spalenque] - #13433 * crate Tag CRUD on sangria
1 parent 8efbe35 commit 4b9704f

12 files changed

Lines changed: 614 additions & 1 deletion

File tree

openstack/_config/routes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Director:
77
'depsurvey': 'DeploymentSurveyController'
88
'profile_images': 'ProfileImageApi'
99
'api/v1/groups': 'GroupsApi'
10+
'api/v1/tags': 'TagsCrudApi'
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2017 OpenStack Foundation
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
**/
15+
class TagsCrudApi extends AbstractRestfulJsonApi
16+
{
17+
const ApiPrefix = 'api/v1/tags';
18+
19+
/**
20+
* @return bool
21+
*/
22+
public function checkOwnAjaxRequest()
23+
{
24+
$referer = @$_SERVER['HTTP_REFERER'];
25+
if (empty($referer)) {
26+
return false;
27+
}
28+
//validate
29+
if (!Director::is_ajax()) {
30+
return false;
31+
}
32+
return Director::is_site_url($referer);
33+
}
34+
35+
36+
/**
37+
* @return bool
38+
*/
39+
protected function isApiCall()
40+
{
41+
$request = $this->getRequest();
42+
if (is_null($request)) {
43+
return false;
44+
}
45+
return strpos(strtolower($request->getURL()), self::ApiPrefix) !== false;
46+
}
47+
48+
/**
49+
* @return bool
50+
*/
51+
protected function authorize()
52+
{
53+
return true;
54+
}
55+
56+
/**
57+
* @return bool
58+
*/
59+
protected function authenticate()
60+
{
61+
return Permission::check('SANGRIA_ACCESS');
62+
}
63+
64+
/**
65+
* @var array
66+
*/
67+
static $url_handlers = array(
68+
'GET ' => 'getTags',
69+
'PUT $TAG_ID!' => 'updateTag',
70+
'POST ' => 'addTag',
71+
'DELETE $TAG_ID!' => 'deleteTag',
72+
);
73+
74+
/**
75+
* @var array
76+
*/
77+
static $allowed_actions = array(
78+
'getTags',
79+
'updateTag',
80+
'addTag',
81+
'deleteTag',
82+
);
83+
84+
85+
/**
86+
* @param SS_HTTPRequest $request
87+
* @return SS_HTTPResponse
88+
*/
89+
public function getTags(SS_HTTPRequest $request){
90+
$query_string = $request->getVars();
91+
$search = (isset($query_string['search'])) ? Convert::raw2sql($query_string['search']) : '';
92+
93+
try{
94+
$tags = Tag::get()->where("Tag.Tag != ''");
95+
if (!empty($search)) {
96+
$tags = $tags->filter(['Tag:PartialMatch' => $search]);
97+
}
98+
$tags = $tags->sort('Tag','ASC');
99+
100+
$data = [];
101+
foreach ($tags as $tag) {
102+
103+
$data[] = [
104+
'id' => $tag->ID,
105+
'tag' => $tag->Tag
106+
];
107+
}
108+
109+
return $this->ok($data);
110+
}
111+
catch(Exception $ex)
112+
{
113+
SS_Log::log($ex->getMessage(), SS_Log::ERR);
114+
return $this->serverError();
115+
}
116+
}
117+
118+
/**
119+
* @param SS_HTTPRequest $request
120+
* @return SS_HTTPResponse
121+
*/
122+
public function updateTag(SS_HTTPRequest $request){
123+
$query_string = $request->getVars();
124+
$tag_val = Convert::raw2sql($query_string['tag']);
125+
$tag_id = $request->param('TAG_ID');
126+
127+
try{
128+
129+
$tag = Tag::get()->byID($tag_id);
130+
if (!$tag)
131+
throw new NotFoundEntityException('Tag');
132+
133+
if (empty($tag_val))
134+
throw new ValidationException();
135+
136+
$tag->Tag = $tag_val;
137+
$tag->write();
138+
139+
140+
return $this->getTags($request);
141+
}
142+
catch(Exception $ex)
143+
{
144+
SS_Log::log($ex->getMessage(), SS_Log::ERR);
145+
return $this->serverError();
146+
}
147+
}
148+
149+
/**
150+
* @param SS_HTTPRequest $request
151+
* @return SS_HTTPResponse
152+
*/
153+
public function addTag(SS_HTTPRequest $request){
154+
$query_string = $request->getVars();
155+
$tag_val = Convert::raw2sql($query_string['tag']);
156+
157+
try{
158+
159+
if (empty($tag_val))
160+
throw new ValidationException();
161+
162+
$tag = new Tag();
163+
$tag->Tag = $tag_val;
164+
$tag->write();
165+
166+
return $this->getTags($request);
167+
}
168+
catch(Exception $ex)
169+
{
170+
SS_Log::log($ex->getMessage(), SS_Log::ERR);
171+
return $this->serverError();
172+
}
173+
}
174+
175+
/**
176+
* @param SS_HTTPRequest $request
177+
* @return SS_HTTPResponse
178+
*/
179+
public function deleteTag(SS_HTTPRequest $request){
180+
$tag_id = $request->param('TAG_ID');
181+
182+
try{
183+
184+
$tag = Tag::get()->byID($tag_id);
185+
if (!$tag)
186+
throw new NotFoundEntityException('Tag');
187+
188+
$tag->delete();
189+
190+
return $this->getTags($request);
191+
}
192+
catch(Exception $ex)
193+
{
194+
SS_Log::log($ex->getMessage(), SS_Log::ERR);
195+
return $this->serverError();
196+
}
197+
}
198+
}

sangria/code/SangriaPage.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ final class SangriaPage_Controller extends AdminController implements Permission
3333
public static $default_end_date;
3434
public static $date_filter_query;
3535

36-
static $allowed_actions = array();
36+
static $allowed_actions = array(
37+
'ViewTagsCrud'
38+
);
3739

3840
static $url_handlers = array();
3941

@@ -370,4 +372,19 @@ public static function generateDateFilters($table_prefix = '', $date_field = 'Up
370372
return $where_query;
371373
}
372374

375+
public function ViewTagsCrud(){
376+
Requirements::clear();
377+
// css
378+
Requirements::css('marketplace/ui/source/css/sangria.css');
379+
Requirements::css("themes/openstack/css/bootstrap.min.css");
380+
Requirements::css("themes/openstack/bower_assets/fontawesome/css/font-awesome.min.css");
381+
Requirements::css('//fonts.googleapis.com/css?family=Open+Sans:300,400,700');
382+
383+
// js
384+
Requirements::javascript("themes/openstack/bower_assets/jquery/dist/jquery.min.js");
385+
Requirements::javascript("themes/openstack/javascript/bootstrap.min.js");
386+
387+
return $this->getViewer('TagsCrudPage')->process($this);
388+
}
389+
373390
}

sangria/templates/Layout/SangriaPage.ss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@
188188
<ul>
189189
<li><a href="/sangria/StandardizeOrgNames" id="stand_orgs">Standardize Organizations</a></li>
190190
</ul>
191+
192+
<h2>Misc</h2>
193+
<ul>
194+
<li><a href="/sangria/ViewTagsCrud" >Tags CRUD</a></li>
195+
</ul>
191196
</div>
192197
<br>
193198
<br>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h2>Tags CRUD</h2>
2+
<div id="tags-crud-app"></div>
3+
4+
$ModuleJS("sangria-tags-crud", false , "sangria")
5+
$ModuleCSS("sangria-tags-crud", false , "sangria")

0 commit comments

Comments
 (0)