def post(self, request): result = "" obj_fields = {} post_env = request.copy() post_env["QUERY_STRING"] = "" post = cgi.FieldStorage(fp=request["wsgi.input"], environ=post_env, keep_blank_values=True) task_name = post["task_name"].value if task_name: obj_fields["task_name"] = task_name due_date = post["due_date"].value if due_date: try: due_time = datetime.datetime.strptime(due_date, "%Y-%m-%d %H:%M") if due_time > datetime.datetime.now(): obj_fields["due_date"] = due_date except Exception as e: result = u"<p style='color:red;'>Date format you specified don1t supported" if post.has_key("finished"): finished = "True" obj_fields["finished"] = finished tags = post["tags"].value if post.has_key("priority"): obj_fields["priority"] = post["priority"].value conn = db_connect() cur = conn.cursor() query = insert("to_do", obj_fields) cur.execute(query) conn.commit() query = select("to_do", ("id",), "WHERE ID = (SELECT MAX(ID) FROM to_do)") task_id = cur.execute(query).fetchone()[0] if tags: tag_dict = {} tags = tags.split(",") for tag in tags: tag_dict["name"] = tag.strip() query = insert("tag", tag_dict) cur.execute(query) conn.commit() query = select("tag", ("id",), "WHERE ID = (SELECT MAX(ID) FROM tag)") tag_id = cur.execute(query).fetchone()[0] todo_tag = {} todo_tag["task_id"] = task_id todo_tag["tag_id"] = tag_id query = insert("todo_tags", todo_tag) cur.execute(query) conn.commit() cur.close() conn.close() result += u"<p style='color:green;'>Success, you task is stored!</p>" else: result = u"<p style='color:red;'>Please, specify task description</p>" return {"method": result}
def process_csv(cur): with open( os.path.join(ROOT_PATH, 'data/input.csv'), 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=';') for row in reader: if len(row): to_do_dict = {} tag_dict = {} task_name = row[0] # Because task name is required if task_name: to_do_dict['task_name'] = task_name #unicode(task_name.decode("utf-8")) if row[2]: to_do_dict['priority'] = row[2] if row[1]: try: date = datetime.datetime.strptime(row[1], "%Y-%m-%d %H:%M") to_do_dict['due_date'] = row[1] except : pass query = insert("to_do", to_do_dict) try: res = cur.execute(query) except Exception as e: print "ERROR: %s" % e if row[3]: to_do_tags_dict = {} # Receive last task id query = select('to_do', ('id',), 'WHERE ID = (SELECT MAX(ID) FROM to_do)') to_do_tags_dict['task_id'] = cur.execute(query).fetchone()[0] query_tag_id = select('tag', ('id',), 'WHERE ID = (SELECT MAX(ID) FROM tag)') # Tags can be separated with '\t' tags_list = row[3].split("\t") for tag in tags_list: tag_dict['name'] = tag query = insert('tag', tag_dict) # Save tag cur.execute(query) # Receive tag id to_do_tags_dict['tag_id'] = cur.execute(query_tag_id).fetchone()[0] query = insert('todo_tags', to_do_tags_dict) # Save ids of task-tag cur.execute(query) else: continue