On 2/26/07, Rainy <[EMAIL PROTECTED]> wrote:
>
> I can't reproduce this error right now because TG fails to access
> database.. but this was bugging me for the last few days. I have a
> form with 4 drop down menus.. When first menu is chosen, it loads the
> second through ajax. When second is chosen, it loads 3 & 4 through
> ajax. (1st is country, second is state, 3 & 4 are county and city). It
> works fine at home but on remote server only the second (state) menu
> loads, when I change 2nd menu, 3&4 do not load and I get a traceback
> from TG that goes, AssertionError: This transaction has already gone
> through ROLLBACK; begin another transaction .
>
first of all if it works ok on one env and the other we should list
the differences, if your using different db engines maybe you have hit
a bug in one of SO's backends. there is no reason for code to work
different on two hardwares with the same environment.
> The code in question is this:
>
> @expose(format="json")
> def getCounties(self, state_id, tg_errors=None):
> # print "###getCounties, counties, ",
> model.State.get(state_id).counties
> tmp = {"id": "0", "countyName": "Select county.."}
> is_id = False
> try:
> state = int(state_id)
> is_id = True
> except:
> state = state_id
> is_id = False
>
> if is_id:
> counties = [tmp] + model.State.get(state).counties
> else:
> counties = [tmp] + model.State.selectBy(stateName=state)
> [0].counties
> return dict(counties=counties)
>
just a note on the structure of the code, instead of using the same
variable to store the key param and the string param why not add 2
named params something like
@expose(format="json")
def getCounties(self,state_id=None,state_name=None,tg_errors=None):
if state_id:
state = int(state_id)
counties = model.State.get(state).counties
if state_name:
counties = model.State.selectBy(stateName=state)[0].counties
return dict(counties=counties)
play around with SO exceptions for the missing entries and also add a
check so both are not there, it can be improved a lot but you get the
idea....
also the tmp variable is not a good idea I don't see why your turning
it later into a list why not just leave that on the template and just
move the actual data in the controller, it's always going to be
"Select county" right?
> getCities is similar. Note that at home I just have try: State.get(id)
> except: State.selectBy(name=state) and it works fine. On remote server
> I tried to do this because I thought it might be related to the
> Rollback error. It didn't help of course..
>
> The reason I'm not sure if I will get an id or a name is that when the
> menu is loaded from ajax, sometimes it will return name and sometimes
> id. Menu created by me of course always returns id, as it well should.
>
ok that makes no sense at all what menu? if this is actually happening
that's a bug on your menu code.
> Again, this doesn't cause trouble at home and I didn't investigate
> this more yet. I don't think this causes the Rollback error though.
>
> >From my reading of the group it looks like there was an old bug that
> was related to this but it was fixed. It was also possible to bring
> this error about by passing around sqlobject object between
> transactions (if I understood right) and also there was one case when
> a function had an empty list as an argument and it was reused in later
> transactions causing the error.. It looks like this case is something
> different.
>
> Here's the mochikit code..:
>
> <SCRIPT SRC="/static/javascript/MochiKit.js" TYPE="text/javascript"></
> SCRIPT>
> <!-- document.write("STATES!!"+states)-->
> <script>
> var conn = MochiKit.Signal.connect;
>
> function replace_state(req) {
> var states = evalJSONRequest(req).states;
> replaceChildNodes($('location_form_state_id'),
> map(
> function(p) {
> return OPTION((p.id,p.stateName))
> },
> states
> )
> );
> }
>
> function replace_county(req) {
> var counties = evalJSONRequest(req).counties;
> replaceChildNodes($('location_form_county'),
> map(
> function(c) {
> return OPTION((c.id,c.countyName))
> },
> counties
> )
> );
> }
>
>
> function replace_city(req) {
> var cities = evalJSONRequest(req).cities;
> replaceChildNodes($('location_form_city'),
> map(
> function(city) {
> return OPTION((city.id,city.cityName))
> },
> cities
> )
> );
> }
>
> function state_id_changedb(event) {
> var state_id = event.target().value;
> var defb = doSimpleXMLHttpRequest('/getCounties', {state_id :
> state_id});
> defb.addCallback(replace_county);
> }
>
>
> function state_id_changed(event) {
> var state_id = event.target().value;
> var def = doSimpleXMLHttpRequest('/getCities', {state_id :
> state_id});
> def.addCallback(replace_city);
> }
>
> function country_id_changed(event) {
> var country_id = event.target().value;
> var def = doSimpleXMLHttpRequest('/getStates', {country_id :
> country_id});
> def.addCallback(replace_state);
> }
>
> function init_state_selector() {
> conn($('location_form_state_id'), 'onchange', state_id_changedb);
> conn($('location_form_state_id'), 'onchange', state_id_changed);
> };
>
> function init_country_selector() {
> conn($('location_form_country_id'), 'onchange', country_id_changed);
> };
>
> conn(window, 'onload', init_country_selector);
> conn(window, 'onload', init_state_selector);
>
> </script>
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---