Download profile feature

This commit is contained in:
sosguns2002 2017-06-01 13:43:36 +03:00
parent 61227c428e
commit 42e9e3d707
3 changed files with 285 additions and 45 deletions

View File

@ -70,6 +70,9 @@ class Application(tornado.web.Application):
(r"/", madAppQueryGenerator),
(r"/importing-controller", importingControllerHandler),
(r"/importing-text-controller", importingTextsControllerHandler),
(r"/save-config-controller", profileCreationHandler),
(r"/download-config-controller", profileServeHandler),
(r"/upload-profile-controller", profileUploadHandler),
(r"/?$", madAppBarHandler),
(r"/[^/]+/?$", madAppHandler),
(r"/[^/]+/.+$", madAppDataHandler)
@ -316,6 +319,8 @@ class madAppQueryGenerator(BaseHandler):
# Get the unique user id from the coookie set
user_id = self.get_secure_cookie('madgikmining')
if user_id is None:
return
# data to be sent
data = {}
try:
@ -409,19 +414,151 @@ class madAppQueryGenerator(BaseHandler):
pass
self.write(json.dumps(data))
self.flush()
self.finish()
class saveProfileHandler(BaseHandler):
class profileCreationHandler(BaseHandler):
def post(self):
"""Controls the importing job as follows:
*** load-raw-data (reading and saving them in the DB)"""
try:
user_id = self.get_secure_cookie('madgikmining')
if 'saveprofile' in self.request.arguments:
print "asda"
if user_id is None:
return
import sys
sys.path.append(msettings.MADIS_PATH)
import madis
# get the database cursor
# profile file name
profile_file_name = "/tmp/OAMiningProfile_{0}.oamp".format(user_id)
cursor=madis.functions.Connection(profile_file_name).cursor()
# Create poswords table
cursor.execute("drop table if exists poswords", parse=False)
cursor.execute("create table poswords(c1,c2)", parse=False)
# Create negwords table
cursor.execute("drop table if exists negwords", parse=False)
cursor.execute("create table negwords(c1,c2)", parse=False)
# Create filters table
cursor.execute("drop table if exists filters", parse=False)
cursor.execute("create table filters(c1,c2)", parse=False)
# Create grants table
cursor.execute("drop table if exists grants", parse=False)
cursor.execute("create table grants(c1)", parse=False)
if 'poswords' in self.request.arguments and self.request.arguments['poswords'][0] != '{}':
# construct math string for positive words matching calculation with weights
pos_words = json.loads(self.request.arguments['poswords'][0])
cursor.executemany("insert into poswords(c1,c2) values(?,?)",
(
(key, value,) for key, value in pos_words.iteritems()
)
)
if 'negwords' in self.request.arguments and self.request.arguments['negwords'][0] != '{}':
# construct math string for negative words matching calculation with weights
neg_words = json.loads(self.request.arguments['negwords'][0])
cursor.executemany("insert into negwords(c1,c2) values(?,?)",
(
(key, value,) for key, value in neg_words.iteritems()
)
)
if 'filters' in self.request.arguments and self.request.arguments['filters'][0] != '{}':
# construct math string for negative words matching calculation with weights
filters = json.loads(self.request.arguments['filters'][0])
cursor.executemany("insert into filters(c1,c2) values(?,?)",
(
(key, value,) for key, value in filters.iteritems()
)
)
if numberOfGrantsUploaded(user_id, self.get_secure_cookie('madgikmining_grantsuploaded')) != 0:
cursor.execute("insert into grants select stripchars(c1) as c1 from (file '/tmp/p{0}.csv')".format(user_id))
cursor.close()
self.write(json.dumps({'respond': "File ploaded.<br><b>1 Code</b> loaded! <i>Please make sure that you separate each code with newline!</i>"}))
except Exception as ints:
self.write(json.dumps({'respond': "<b style=\"color: red\">SOomething went very wrong!</b>"}))
self.write(json.dumps({'respond': "<b style=\"color: red\">Mining profile couldn't be saved!</b>"}))
print ints
return
self.finish()
class profileServeHandler(BaseHandler):
def get(self):
try:
user_id = self.get_secure_cookie('madgikmining')
if user_id is None:
return
if 'saveprofile' in self.request.arguments:
print "asda"
profile_file_name = "/tmp/OAMiningProfile_{0}.oamp".format(user_id)
buf_size = 4096
self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', 'attachment; filename=' + "OAMiningProfile_{0}.oamp".format(user_id))
self.flush()
with open(profile_file_name, 'r') as f:
while True:
data = f.read(buf_size)
if not data:
break
self.write(data)
self.finish()
# TODO delete file after sending if needed
except Exception as ints:
self.write(json.dumps({'respond': "<b style=\"color: red\">Something went very wrong!</b>"}))
print ints
return
class profileUploadHandler(BaseHandler):
def post(self):
try:
user_id = self.get_secure_cookie('madgikmining')
if user_id is None:
return
# get file info and body from post data
fileinfo = self.request.files['upload'][0]
fname = fileinfo['filename']
extn = os.path.splitext(fname)[1]
# must be .pdf or .json
if extn != ".oamp":
self.write(json.dumps({'respond': "<b style=\"color: red\">File must be .oamp compatible profile</b>"}))
return
# write data to physical file
cname = "/tmp/profile{0}.oamp".format(user_id)
fh = open(cname, 'w')
fh.write(fileinfo['body'])
fh.close()
# extract data from profile file
import sys
sys.path.append(msettings.MADIS_PATH)
import madis
# get the profile database cursor
cursor=madis.functions.Connection(cname).cursor()
# data to be sent
data = {}
# Write to csv file the grants ids
if len([r for r in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='grants'")]):
cursor.execute("output '/tmp/p{0}.csv' select * from grants".format(user_id))
numberOfGrants = numberOfGrantsUploaded(user_id, "puppet_value")
self.set_secure_cookie('madgikmining_grantsuploaded', str(numberOfGrants))
data['grants'] = numberOfGrants
# write to json the poswords
if len([r for r in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='poswords'")]):
results = [r for r in cursor.execute("select c1, c2 from poswords")]
data['poswords'] = {value:key for value, key in results}
# write to json the negwords
if len([r for r in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='negwords'")]):
results = [r for r in cursor.execute("select c1, c2 from negwords")]
data['negwords'] = {value:key for value, key in results}
# write to json the filters
if len([r for r in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='filters'")]):
results = [r for r in cursor.execute("select c1, c2 from filters")]
data['filters'] = {value:key for value, key in results}
cursor.close()
self.write(json.dumps(data))
self.finish()
except Exception as ints:
self.write(json.dumps({'respond': "<b style=\"color: red\">Something went very wrong!</b>"}))
print ints
return
@ -434,7 +571,8 @@ class importingControllerHandler(BaseHandler):
try:
user_id = self.get_secure_cookie('madgikmining')
if user_id is None:
return
csv_file_name = "/tmp/p{0}.csv".format(user_id)
csv_file = open(csv_file_name, 'w')

View File

@ -1,3 +1,34 @@
function handleConfigDownload() {
var formData = new FormData();
formData.append("poswords", $('#pos-words-text').val());
formData.append("negwords", $('#neg-words-text').val());
filters_list = {};
filters_list["stopwords"] = $('#stop-words-filter').prop('checked')===true?1:0;
filters_list["lowercase"] = $('#lowercase-filter').prop('checked')===true?1:0;
filters_list["keywords"] = $('#keywords-filter').prop('checked')===true?1:0;
formData.append("filters", JSON.stringify(filters_list));
$.ajax({
url: "save-config-controller",
type: 'POST',
data: formData,
async: false,
success: function (data) {
$('#file-upload-response').html(JSON.parse(data).respond)
// if (data.indexOf('successfully!') != -1) {
// $('#file-uploaded')[0].checked = true;
// }
window.location="download-config-controller?saveprofile=1"
},
error: function (xhr, ajaxOptions, thrownError) {
$('#file-upload-response').html('<b style=\"color: red\">File Failed to Upload!</b>'+xhr.status)
// $('#file-uploaded')[0].checked = false;
},
cache: false,
contentType: false,
processData: false
});
}
function handleLoadExampleFile() {
$("#file-input-operation").val('example');
$("#file-title-text").html('');
@ -65,6 +96,58 @@ $( window ).resize(function() {
.on( 'blur', function(){ $input.removeClass( 'has-focus' ); });
});
function handleProfileUploadButton() {
$("form#profile-input-form").submit(function(){
if ($('#profile-input')[0].value === "") {
window.alert("You must specify a profile to import.");
return false;
}
// $('#user-id').val(getCookie("madgikmining"));
$("#profile-input-operation").val('normal');
var formData = new FormData($(this)[0]);
$.ajax({
url: "upload-profile-controller",
type: 'POST',
data: formData,
async: false,
success: function (data) {
$('#profile-upload-response').html(JSON.parse(data).respond);
obj = JSON && JSON.parse(data) || $.parseJSON(data);
console.log(obj);
for (var key1 in obj) {
if (obj.hasOwnProperty(key1)) {
if (key1==="poswords") {
// delete all poswords from the lists
deleteAllPosWords(0);
for (var key2 in obj[key1]) {
createWord(1, generateId(1), key2, obj[key1][key2]);
}
} else if (key1 === "negwords") {
deleteAllNegWords(0);
for (var key2 in obj[key1]) {
createWord(0, generateId(0), key2, obj[key1][key2]);
}
} else if (key1 === "filters") {
for (var key2 in obj[key1]) {
console.log(key2, obj[key1][key2]);
}
}
}
}
},
error: function (xhr, ajaxOptions, thrownError) {
$('#profile-upload-response').html('<b style=\"color: red\">File Failed to Upload!</b>'+xhr.status)
// $('#profile-uploaded')[0].checked = false;
},
cache: false,
contentType: false,
processData: false
});
return false;
});
}
function handleFileUploadButton() {
$("form#file-input-form").submit(function(){
if ($('#file-input')[0].value === "") {
@ -154,6 +237,7 @@ $( window ).resize(function() {
}
/////////// LIST FUNCTIONS
var count_pos = 0, count_neg = 0;
@ -445,46 +529,50 @@ $( window ).resize(function() {
// }
// }
// };
var deleteAllPosWords = function(warnUser = 1) {
if(!warnUser || confirm('Are you sure you want to delete all the items in the list? There is no turning back after that.')){ //remove items from DOM
var items = $('li[id ^= positive]');
items.addClass('removed-item').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
$(this).remove();
});
//look for items in localStorage that start with word- and remove them
var keys = [];
for(var key in localStorage){
if(key.indexOf('positive') === 0){
localStorage.removeItem(key);
}
}
count_pos = 0;
updateCounter(1);
}
updatetextereas();
};
var deleteAllNegWords = function(warnUser = 1) {
if(!warnUser || confirm('Are you sure you want to delete all the items in the list? There is no turning back after that.')){ //remove items from DOM
var items = $('li[id ^= negative]');
items.addClass('removed-item').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
$(this).remove();
});
//look for items in localStorage that start with word- and remove them
var keys = [];
for(var key in localStorage){
if(key.indexOf('negative') === 0){
localStorage.removeItem(key);
}
}
count_neg = 0;
updateCounter(0);
}
updatetextereas();
};
//handler for the "delete all" button
var handleDeleteButton = function(){
$('#clear-all-pos').on('click', function(){
if(confirm('Are you sure you want to delete all the items in the list? There is no turning back after that.')){ //remove items from DOM
var items = $('li[id ^= positive]');
items.addClass('removed-item').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
$(this).remove();
});
//look for items in localStorage that start with word- and remove them
var keys = [];
for(var key in localStorage){
if(key.indexOf('positive') === 0){
localStorage.removeItem(key);
}
}
count_pos = 0;
updateCounter(1);
}
updatetextereas();
});
$('#clear-all-neg').on('click', function(){
if(confirm('Are you sure you want to delete all the items in the list? There is no turning back after that.')){ //remove items from DOM
var items = $('li[id ^= negative]');
items.addClass('removed-item').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
$(this).remove();
});
//look for items in localStorage that start with word- and remove them
var keys = [];
for(var key in localStorage){
if(key.indexOf('negative') === 0){
localStorage.removeItem(key);
}
}
count_neg = 0;
updateCounter(0);
}
updatetextereas();
});
$('#clear-all-pos').on('click', deleteAllPosWords);
$('#clear-all-neg').on('click', deleteAllNegWords);
};
var init = function(){
@ -499,6 +587,7 @@ $( window ).resize(function() {
handleFileUploadButton();
handleZipFileUploadButton();
handleDocsUploadSelect();
handleProfileUploadButton();
};
//start all
init();

View File

@ -33,6 +33,19 @@
</blockquote> -->
</div>
<div id="page" style="margin:0">
<input id="downloadConfigBtn" class="btn" onclick="handleConfigDownload()" value="Download this profile" type="button">
<div class="file-upload-wrapper js">
<form id="profile-input-form" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
<header>
<input type="file" name="upload" id="profile-input" class="inputfile" />
<label for="profile-input" id="profile-input-label" ><strong><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg> Choose a file&hellip;</strong> <span id="profile-title-text"></span></label>
<button id="profile-upload-button" class="btn" style="float: right; margin-right: 5px;" >Upload profile</button>
</header>
<footer>
<span class="response" id="profile-upload-response"></span>
</footer>
</form>
</div>
<div class="file-upload-wrapper js">
<h2>Add your <b>Text File</b> of codes</h2>
<form id="file-input-form" method="post" enctype="multipart/form-data" accept-charset="UTF-8">