1

I have an input field that is basically a comma delimited string (i.e. something like "deniscm, toms, peters"). That information is sent via AJAX to my Python handler SaveQueryPage. What I want to do is parse this information as a list and then insert each entry into my database. My code is as follows, but it doesn't work unfortunately. Any suggestions?

Python code:

class SaveQueryPage(webapp2.RequestHandler):
  def post(self):
    user = users.get_current_user()
    user_nickname = user.nickname()
    query_name = self.request.get('queryName')
    query_collab = self.request.get('queryCollab')

    query_collaborators = re.split(r'\s*[,]\s*', query_collab.strip())
    query_collaborators = query_collaborators.append(user_nickname)
    query_collaborators = filter(None, query_collaborators)

    conn = rdbms.connect(instance=_INSTANCE_NAME, database='queryInfo')
    cursor = conn.cursor()
    cursor.execute('INSERT INTO queries (userNickname, queryName) VALUES (%s, %s)', (user_nickname, query_name))
    conn.commit()

    for item in query_collaborators:
      cursor = conn.cursor()
      cursor.execute('INSERT INTO collaborators (queryName, userNickname) VALUES (%s, %s)', (query_name, item))
      conn.commit()

    conn.close()
kd1978
  • 487
  • 5
  • 21
  • 3
    What doesn't work? Do you get an error? Does it not give the expected results? – Niek de Klein May 11 '12 at 11:01
  • A query HAS collaborators, but, there is no linking, you use a RDBMS, i assume Google SQL Cloud Storage? This makes no sence. – Julius F May 11 '12 at 11:03
  • Well and ('SOME MYSQL QUERY %s') %(var) isn't quite safe too. – Julius F May 11 '12 at 11:05
  • I am indeed using Google Cloud SQL - as it is an internal application, I am not too worried at this stage yet about security. I left out a number of attributes from the queries table to make it easier to digest the info, but there is a proper relation between queries and collaborators. The error I am seeing in the logs is as follows: 'NoneType' object is not iterable Traceback (most recent call last): File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/weba – kd1978 May 11 '12 at 11:50
  • What line does that refer to? – Daniel Roseman May 11 '12 at 12:06
  • I added some logging, and it looks like the for loop works correctly, but the regular expression has turned the variable into a unicode list: Data read for query_collaborators_encode is [u'guillaumek', u'denismc'] How do I convert this into a list of str()? – kd1978 May 11 '12 at 13:22

1 Answers1

1

I finally managed to get it working. Looks like the regular expression turned the items in the list to a unicode format, which was only caught when I added some logs. I also had an error in appending a string to the list. Thanks for the pointers! The code below now works for me:

class SaveQueryPage(webapp2.RequestHandler):
  def post(self):
    user = users.get_current_user()
    user_nickname = user.nickname()
    user_email = user.email()

    query_name = self.request.get('queryName')
    query_description = self.request.get('queryDescription')

    query_collab = self.request.get('queryCollab')
    logging.info('Data read for query_collab is %s', query_collab)

    query_collab_re = re.split(r'\s*[,;]\s*', query_collab.strip())
    logging.info('Data read for query_collab_re is %s', query_collab_re)

    query_collab_decode = []
    for item in query_collab_re:
      item = str(item)
      query_collab_decode.append(item)
    logging.info('Data read for query_collab_decode is %s', query_collab_decode)

    query_collab_decode.append(user_nickname)
    logging.info('Data read for query_collab_append is %s', query_collab_decode)

    query_collab_filter = filter(None, query_collab_decode)
    logging.info('Data read for query_collab_filter is %s', query_collab_filter)

    query_value = self.request.get('queryValue') # query_value
    date_created = datetime.today()
    date_lastupdated = datetime.today()
    active_flag = "true"
    random_id = random.randint(1000000000000, 9999999999999)
    unique_query_id = user_nickname + "_" + str(random_id)

    conn = rdbms.connect(instance=_INSTANCE_NAME, database='userPrefs')
    cursor = conn.cursor()
    cursor.execute('INSERT INTO queries (userNickname, queryName, queryDescription, queryValue, dateCreated, dateLastUpdated, activeFlag, uniqueId) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', (user_nickname, query_name, query_description, query_value, date_created, date_lastupdated, active_flag, unique_query_id))
    conn.commit()

    try:
      for item in query_collab_filter:
        cursor = conn.cursor()
        cursor.execute('INSERT INTO collaborators (uniqueId, userNickname) VALUES (%s, %s)', (unique_query_id, item))
        conn.commit()
    except:
      logging.error('There was an error inserting the values into the collaborators table. query_collaborators =' + str(query_collaborators))

    conn.close()
kd1978
  • 487
  • 5
  • 21