shaw1ea
unread,Sep 14, 2009, 11:25:02 AM9/14/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to TurboGears
I am using the jquery post function in my template to call a function
in my controller. I am sending some data to the controller. The data
comes into the controller with a u' ' around each data item. I do
not want the u' 's. Is there an easy way to get rid of them. I do
not want to have to write a bunch of parsing code to remove them if I
do not have to.
Here is the code in the html template file:
function get_selected_issues() {
var data_ids1 = $('#sortable1').sortable('toArray');
var num_elements1 = data_ids1.length;
var data_ids2 = $('#sortable2').sortable('toArray');
var num_elements2 = data_ids2.length;
// Match any number of characters followed by an _
// then 0 or 1 digits followed by 1 digit at the
// end of the string
var regExp = /_\d?\d$/;
var pos = 0
var index = ""
// Extract the '_num' from the data_ids1
for (i=0; i < num_elements1; i++)
{
var id_string1 = data_ids1[i];
pos = id_string1.search(regExp);
index = id_string1.substr(pos+1); // Add one to momve past the
underscore
//id_string1 = id_string1.replace(regExp, "");
//data_ids1[i] = id_string1;
data_ids1[i] = index;
}
pos = 0
index = ""
// Extract the '_num' from the data_ids1
for (i=0; i < num_elements2; i++)
{
var id_string2 = data_ids2[i];
pos = id_string2.search(regExp);
index = id_string2.substr(pos+1); // Add one to momve past the
underscore
//id_string2 = id_string2.replace(regExp, "");
//data_ids2[i] = id_string2;
data_ids2[i] = index;
}
$.post("SaveSelectedIssues",{ issues1 : data_ids1, issues2 :
data_ids2 },
function(data2) {
// Redirect to the Review/Submit page
location = data2; // Data contains the url of the Rivew/
Submit page
}); // Adding "json" does not work. It juse casuse the app to
hang. No idea why.
}
Here is the code in the controller:
@expose()
def SaveSelectedIssues(self, *args, **kw):
message = 'All is well that ends well'
try:
available_issues = str(kw['issues1']) # From the form
length1 = len(available_issues)
if length1 > 0:
first_char1 = available_issues[0] # Get the first char
in the string
last_char1 = available_issues[-1] # Get the last char
in the string
working_data1 = ''
if ((first_char1 == '[') and (last_char1 == ']')):
working_data1 = available_issues[1:-1]
elif ((first_char1 == '[') and (last_char1 != ']')):
working_data1 = available_issues[1:]
elif ((first_char1 != '[') and (last_char1 == ']')):
working_data1 = available_issues[:-1]
else:
working_data1 = available_issues
# This does not work
#available_issues_list_json = json.JSONDecoder().decode
(available_issues)
available_issues_list = working_data1.split(',')
# Remove the u, the first ', and the last ' from each
item in the list
# non_unicode_working_data1 = []
# issue = ''
# for available_issue in available_issues_list:
# issue = available_issue
# if (available_issue[0] == 'u'):
# issue = available_issue[2:-1]
# non_unicode_working_data1.append(str(issue))
AppInfo.AvailableIssues = available_issues_list
# AppInfo.AvailableIssues = non_unicode_working_data1
selected_issues = str(kw['issues2']) # From the form
length2 = len(selected_issues) # Length must be greater
thatn 0
first_char2 = selected_issues[0] # Get the first char in
the string
last_char2 = selected_issues[-1] # Get the last char in
the string
working_data2 = ''
if ((first_char2 == '[') and (last_char2 == ']')):
working_data2 = selected_issues[1:-1]
elif ((first_char2 == '[') and (last_char2 != ']')):
working_data2 = selected_issues[1:]
elif ((first_char2 != '[') and (last_char2 == ']')):
working_data2 = selected_issues[:-1]
else:
working_data2 = selected_issues
selected_issue_list = working_data2.split(',')
# Remove the u, the first ', and the last ' from each
item in the list
# non_unicode_working_data2 = []
# issue = ''
# for selected_issue in selected_issue_list:
# issue = selected_issue.strip()
# if (issue[0] == 'u' and issue[1] == "'" and issue[-1]
== "'"):
# issue = selected_issue[2:-1]
# non_unicode_working_data2.append(str(issue))
# AppInfo.SelectedIssues = non_unicode_working_data2
AppInfo.SelectedIssues = selected_issue_list
output = open('issues_temp.txt', 'a')
output.write('kw: %s\n\n' % kw)
output.write('available_issues : %s\n' % available_issues)
output.write('working_data1 : %s\n' % working_data1)
output.write('split Available issues : %s\n\n' %
available_issues_list)
output.write('selected_issues : %s\n' % selected_issues)
output.write('working_data2 : %s\n' % working_data2)
output.write('split selected issues : %s\n\n' %
selected_issue_list)
output.write('AppInfo.AvailableIssues : %s\n\n' %
AppInfo.AvailableIssues)
output.write('AppInfo.SelectedIssues : %s\n\n\n' %
AppInfo.SelectedIssues)
output.write('Available Issues:\n')
for (i, available_issue) in enumerate
(AppInfo.AvailableIssues):
output.write('----------------------\n')
output.write("issue %s = %s\n" % (i+1, str
(available_issue)))
output.write
('------------------------------------------------\n\n')
output.write('Selected Issues:\n')
for (i, selected_issue) in enumerate
(AppInfo.SelectedIssues):
output.write('----------------------\n')
output.write("issue %s = %s\n" % (i+1, str
(selected_issue)))
output.write
('------------------------------------------------\n')
output.close()
#message = '%s' % issue_list
except:
message = 'Something bad happened!'
return('Review')
Here is the output: Notice all of the u' 's!
kw: {'issues2': [u'6', u'2', u'8', u'1'], 'issues1': [u'3', u'4',
u'5', u'7', u'9', u'10', u'11']}
available_issues : [u'3', u'4', u'5', u'7', u'9', u'10', u'11']
working_data1 : u'3', u'4', u'5', u'7', u'9', u'10', u'11'
split Available issues : ["u'3'", " u'4'", " u'5'", " u'7'", " u'9'",
" u'10'", " u'11'"]
selected_issues : [u'6', u'2', u'8', u'1']
working_data2 : u'6', u'2', u'8', u'1'
split selected issues : ["u'6'", " u'2'", " u'8'", " u'1'"]
AppInfo.AvailableIssues : ["u'3'", " u'4'", " u'5'", " u'7'", " u'9'",
" u'10'", " u'11'"]
AppInfo.SelectedIssues : ["u'6'", " u'2'", " u'8'", " u'1'"]
Available Issues:
----------------------
issue 1 = u'3'
----------------------
issue 2 = u'4'
----------------------
issue 3 = u'5'
----------------------
issue 4 = u'7'
----------------------
issue 5 = u'9'
----------------------
issue 6 = u'10'
----------------------
issue 7 = u'11'
------------------------------------------------
Selected Issues:
----------------------
issue 1 = u'6'
----------------------
issue 2 = u'2'
----------------------
issue 3 = u'8'
----------------------
issue 4 = u'1'