-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
177 lines (173 loc) · 6.85 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
$(function() {
//Customize by setting base_url to cybercom/api docker application
base_url = "/api";
//No other alterations is need to get the standard applicaiton running!
login_url = base_url + "/api-auth/login/?next=";
logout_url = base_url + "/api-auth/logout/?next=";
user_task_url = base_url + "/queue/usertasks/.json?page_size=10";
user_url = base_url + "/user/?format=json";
prevlink=null;nextlink=null;
set_auth(base_url,login_url);
$("#aprofile").click(function(){activaTab('profile')})
$("#alogout").click(function(){window.location = logout_url.concat(document.URL);})
load_task_history(user_task_url);
$('#prevlink').click(function(){load_task_history(prevlink);});
$('#nextlink').click(function(){load_task_history(nextlink);});
Handlebars.registerHelper('json_metatags', function(context) {
if (typeof context !== 'undefined') {
return JSON.stringify(context).replace(/"/g,'').replace(/\[/g,'').replace(/\]/g,'').replace(/,/g,', ');
}else{
return ""
}
});
//$('#reset_password').click(function(){$('#pass_form').toggle(!$('#pass_form').is(':visible'));});
//$('#user_form').submit(function(){var formData = JSON.parse($("#user_form").serializeArray());console.log(formData);return false;})
});//End of Document Ready
function submit_user(){
console.log(user_url)
$.post( user_url,$('#user_form').serializeObject(),function(data){
data.csrftoken = getCookie('csrftoken')
$('#profile').empty();
//source = $('#user-template').html()
//user_template = Handlebars.compile(source);
user_template = Handlebars.templates['tmpl-user']
$('#profile').append(user_template(data))
$('#user_form').hide()
$('#view_form').show()
$('#reset_password').click(function(){$('#pass_form').toggle(!$('#pass_form').is(':visible'));});
})
.fail(function(){ alert("Error Occured on User Update.")});
//$('#user_form').hide()
//$('#view_form').show()
//var formData = JSON.parse($("#user_form").serializeArray());
//console.log(formData);
return false;
}
function edit_user(){
$('#user_form').show()
$('#view_form').hide()
return false;
}
function set_password(){
pass = $('#pass_form').serializeObject()
if (pass.password !== pass.password2){
alert("Passwords were not identical")
return false;
}
$.post( user_url,$('#pass_form').serializeObject(),function(data){
$('#reset_password').click(function(){$('#pass_form').toggle(!$('#pass_form').is(':visible'));});
alert(JSON.stringify(data))
})
.fail(function(){ alert("Error Occured on Password Reset.")});
return false;
}
function set_auth(base_url,login_url){
$.getJSON( base_url + "/user/.json",function(data){
$('#user').html(data['username'].concat( ' <span class="caret"></span> '));
$("#user").append($('<img style="border-radius:80px;">').attr("src",data['gravator_url'] + "?s=40&d=mm") );
data.csrftoken = getCookie('csrftoken')
//source = $('#user-template').html()
//user_template = Handlebars.compile(source);
user_template = Handlebars.templates['tmpl-user']
$('#profile').append(user_template(data))
$('#user_form').hide()
$('#view_form').show()
$('#reset_password').click(function(){$('#pass_form').toggle(!$('#pass_form').is(':visible'));});
})
.fail(function() {
var slink = login_url.concat(document.URL);
window.location = slink
});
}
function activaTab(tab){
$('a[href="#' + tab + '"]').tab('show')
};
function load_task_history(url){
$.getJSON(url, function(data){
prevlink = data.previous;
nextlink = data.next;
if (prevlink == null){$('#li_prevlink').addClass("disabled");} else {$('#li_prevlink').removeClass("disabled");};
if (nextlink == null){$('#li_nextlink').addClass("disabled");} else {$('#li_nextlink').removeClass("disabled");};
setTaskDisplay(data);
//source = $('#tr-template').html();
//tr_template = Handlebars.compile(source);
tr_template = Handlebars.templates['tmpl-tr']
$('#result_tbody').html("")//clear table
$.each(data.results, function(i, item) {
temp=item.task_name.split('.')
item['task_name']= temp[temp.length-1]
item.timestamp = item.timestamp.substring(0,19).replace('T',' ')
$('#result_tbody').append(tr_template(item))
});
});
}
function setTaskDisplay(data){
if (data.count <= data.meta.page_size){
$('#task_count').text('Task 1 - ' + data.count + ' Total ' + data.count );
}else{
rec_start = data.meta.page_size*data.meta.page - data.meta.page_size +1;
rec_end = "";
if(data.meta.page_size*data.meta.page >= data.count){
rec_end = data.count;
}else{
rec_end = data.meta.page_size*data.meta.page;
}
$('#task_count').text('Task ' + rec_start + ' - ' + rec_end + ' Total ' + data.count )
}
}
function showResult(url){
//myModalLabel -->title
$.getJSON(url + ".json" , function(data){
json_data = JSON.stringify(data,null, 4);
$("#myModalbody").html(json_data);
$("#myModalbody").urlize();
$("#myModal").modal('show');
});
}
jQuery.fn.urlize = function() {
if (this.length > 0) {
this.each(function(i, obj){
// making links active
var x = $(obj).html();
var list = x.match( /\b(http:\/\/|www\.|http:\/\/www\.)[^ <]{2,200}\b/g );
if (list) {
for ( i = 0; i < list.length; i++ ) {
var prot = list[i].indexOf('http://') === 0 || list[i].indexOf('https://') === 0 ? '' : 'http://';
x = x.replace( list[i], "<a target='_blank' href='" + prot + list[i] + "'>"+ list[i] + "</a>" );
}
}
$(obj).html(x);
});
}
};
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}