def main(): uid = get_uid_from_cookie() uid_dir = os.path.join(app_dir, 'storage', 'users', uid['uid']) form = cgi.FieldStorage() fileId = int(form.getvalue('fileId')) if form.has_key('settings'): settings = form.getvalue('settings') userconfig = set_user_config_from_json(uid, settings) else: userconfig = read_user_config(uid) if len(userconfig['tp_files']) >= fileId: os.remove(os.path.join(uid_dir, 'turnpoints_' + str(fileId) + '.cup')) userconfig['tp_files'].pop(fileId-1) for f_id in range(fileId+1, (len(userconfig['tp_files'])+2)): os.rename(os.path.join(uid_dir, 'turnpoints_' + str(f_id) + '.cup'), os.path.join(uid_dir, 'turnpoints_' + str(f_id-1) + '.cup')) write_user_config(uid, userconfig) print "Content-type: text/html" print # print '{"success":true}' print get_user_config_as_json(uid)
def reply_success(uid, taskname): print "Content-type: text/html" print print '{"success":true,"settings":' print get_user_config_as_json(uid) print ',"download":{' print '"xcsoar":{"url":"tasks/'+uid['uid']+'/temp/'+taskname+'/xcsoar",' print '"qrcode":"tasks/'+uid['uid']+'/temp/'+taskname+'/xcsoar/qr",' print '"name":"XCSoar (*.tsk)"},' print '"seeyou":{"url":"tasks/'+uid['uid']+'/temp/'+taskname+'/seeyou",' print '"qrcode":"tasks/'+uid['uid']+'/temp/'+taskname+'/seeyou/qr",' print '"name":"SeeYou (*.cup)"}' print '}}'
def delete(taskname): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config['USERS_FOLDER'], uid['uid']) userconfig = read_user_config(uid) success = False for key, d in enumerate(userconfig['task_files']): if d.get('name') == taskname: taskfile = os.path.join(uid_dir, 'task_' + str(d.get('id')) + '.tsk') os.unlink(taskfile) userconfig['task_files'].pop(key) success = True break for key, d in enumerate(userconfig['task_files']): if (key + 1) != int(d.get('id')): taskfile_old = os.path.join(uid_dir, 'task_' + str(d.get('id')) + '.tsk') taskfile_new = os.path.join(uid_dir, 'task_' + str(key + 1) + '.tsk') os.rename(taskfile_old, taskfile_new) d['id'] = str(key + 1) write_user_config(uid, userconfig) return jsonify({ 'success': success, 'settings': get_user_config_as_json(uid, encoded=False), })
def delete(taskname): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config["USERS_FOLDER"], uid["uid"]) userconfig = read_user_config(uid) success = False for key, d in enumerate(userconfig["task_files"]): if d.get("name") == taskname: taskfile = os.path.join(uid_dir, "task_" + str(d.get("id")) + ".tsk") os.unlink(taskfile) userconfig["task_files"].pop(key) success = True break for key, d in enumerate(userconfig["task_files"]): if (key + 1) != int(d.get("id")): taskfile_old = os.path.join(uid_dir, "task_" + str(d.get("id")) + ".tsk") taskfile_new = os.path.join(uid_dir, "task_" + str(key + 1) + ".tsk") os.rename(taskfile_old, taskfile_new) d["id"] = str(key + 1) write_user_config(uid, userconfig) return jsonify({"success": success, "settings": get_user_config_as_json(uid, encoded=False)})
def upload(): uid = get_uid_from_cookie() if 'settings' in request.values: settings = request.values['settings'] write_user_config(uid, set_user_config_from_json(uid, settings)) uploaded_file = save_uploaded_file() if not uploaded_file: return jsonify({'success': False}) process = subprocess.Popen([ os.path.join(current_app.config['APP_FOLDER'], 'bin', 'private', 'add_waypoint_file_to_user'), '-u', uid['uid'], '-k', uid['key'], '-f', uploaded_file["tempname"], '-n', uploaded_file["filename"] ], stdout=subprocess.PIPE) stdout, stderr = process.communicate() os.remove(uploaded_file["tempname"]) if stdout: return jsonify({'success': False}) return jsonify({ 'success': True, 'settings': get_user_config_as_json(uid, encoded=False) })
def delete(taskname): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config['USERS_FOLDER'], uid['uid']) userconfig = read_user_config(uid) success = False for key, d in enumerate(userconfig['task_files']): if d.get('name') == taskname: taskfile = os.path.join( uid_dir, 'task_' + str(d.get('id')) + '.tsk') os.unlink(taskfile) userconfig['task_files'].pop(key) success = True break for key, d in enumerate(userconfig['task_files']): if (key + 1) != int(d.get('id')): taskfile_old = os.path.join( uid_dir, 'task_' + str(d.get('id')) + '.tsk') taskfile_new = os.path.join( uid_dir, 'task_' + str(key + 1) + '.tsk') os.rename(taskfile_old, taskfile_new) d['id'] = str(key + 1) write_user_config(uid, userconfig) return jsonify({ 'success': success, 'settings': get_user_config_as_json(uid, encoded=False), })
def load(type="json"): uid = get_uid_from_cookie() type = request.values.get("as", type) if type == "js": match = geolite2.lookup(request.remote_addr) settings = "var initialSettings = " + get_user_config_as_json(uid) + ";" if match and match.location: location = ( "var initialLocation = {lon: " + str(match.location[1]) + ", lat: " + str(match.location[0]) + "};" ) else: location = "var initialLocation = {lon: 10, lat: 50};" return settings + location elif type == "json": return jsonify(get_user_config_as_json(uid), encoded=False)
def main(): uid = get_uid_from_cookie() form = cgi.FieldStorage(); if form.getvalue('as') == 'js': geoip = GeoIP.open(os.path.join(app_dir, 'storage', 'GeoLiteCity.dat'), GeoIP.GEOIP_STANDARD) location = geoip.record_by_addr(os.environ['REMOTE_ADDR']) print "Content-type: text/html" print print 'var initialSettings = '+get_user_config_as_json(uid)+';' if location: print 'var initialLocation = {lon: '+str(location['longitude'])+', lat: '+str(location['latitude'])+'};' else: print 'var initialLocation = {lon: 10, lat: 50};' elif form.getvalue('as') == 'json': print "Content-type: text/html" print print get_user_config_as_json(uid)
def load(type='json'): uid = get_uid_from_cookie() type = request.values.get('as', type) if type == 'js': match = geolite2.lookup(request.remote_addr) settings = \ 'var initialSettings = ' + get_user_config_as_json(uid) + ';' if match and match.location: location = 'var initialLocation = {lon: ' + \ str(match.location[1]) + ', lat: ' + \ str(match.location[0]) + '};' else: location = 'var initialLocation = {lon: 10, lat: 50};' return settings + location elif type == 'json': return jsonify(get_user_config_as_json(uid), encoded=False)
def save_temp(): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config["USERS_FOLDER"], uid["uid"]) if "task" in request.values: taskstring = request.values["task"] task = parse_json_task(taskstring) else: return jsonify({"success": False, "reason": "No task."}) userconfig = read_user_config(uid) if not os.path.exists(uid_dir): write_user_config(uid, userconfig) d = datetime.now() taskname = d.strftime("%w") + str(d.hour * 3600 + d.minute * 60 + d.second) filename = "tasktemp_" + taskname + ".tsk" with open(os.path.join(uid_dir, filename), "w") as f: f.write(create_xcsoar_task(task)) base_url = "tasks/" + uid["uid"] + "/temp/" + taskname return jsonify( { "success": True, "settings": get_user_config_as_json(uid, encoded=False), "download": { "xcsoar": { "name": "XCSoar (*.tsk)", "url": base_url + "/xcsoar", "qrcode": base_url + "/xcsoar/qr", }, "seeyou": { "name": "SeeYou (*.cup)", "url": base_url + "/seeyou", "qrcode": base_url + "/seeyou/qr", }, }, } ) return jsonify({"success": False, "reason": "Unknown failure."})
def save_temp(): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config['USERS_FOLDER'], uid['uid']) if 'task' in request.values: taskstring = request.values['task'] task = parse_json_task(taskstring) else: return jsonify({'success': False, 'reason': 'No task.'}) userconfig = read_user_config(uid) if not os.path.exists(uid_dir): write_user_config(uid, userconfig) d = datetime.now() taskname = d.strftime('%w') + str(d.hour * 3600 + d.minute * 60 + d.second) filename = 'tasktemp_' + taskname + '.tsk' with open(os.path.join(uid_dir, filename), 'w') as f: f.write(create_xcsoar_task(task)) base_url = 'tasks/' + uid['uid'] + '/temp/' + taskname return jsonify({ 'success': True, 'settings': get_user_config_as_json(uid, encoded=False), 'download': { 'xcsoar': { 'name': 'XCSoar (*.tsk)', 'url': base_url + '/xcsoar', 'qrcode': base_url + '/xcsoar/qr', }, 'seeyou': { 'name': 'SeeYou (*.cup)', 'url': base_url + '/seeyou', 'qrcode': base_url + '/seeyou/qr', }, } }) return jsonify({'success': False, 'reason': 'Unknown failure.'})
def main(): uid = get_uid_from_cookie() uid_dir = os.path.join(app_dir, 'storage', 'users', uid['uid']) form = cgi.FieldStorage() m = re.compile('([^&+/;]*)').match(form.getvalue('task_name')) taskname = m.group(1) userconfig = read_user_config(uid) success = False for key, d in enumerate(userconfig['task_files']): if d.get('name') == taskname: taskfile = os.path.join(uid_dir, 'task_' + str(d.get('id')) + '.tsk') os.unlink(taskfile) userconfig['task_files'].pop(key) success = True break for key, d in enumerate(userconfig['task_files']): if (key+1) != int(d.get('id')): taskfile_old = os.path.join(uid_dir, 'task_' + str(d.get('id')) + '.tsk') taskfile_new = os.path.join(uid_dir, 'task_' + str(key+1) + '.tsk') os.rename(taskfile_old, taskfile_new) d['id'] = str(key+1) write_user_config(uid, userconfig) print "Content-type: text/html" print print '{' if success: print '"success":true,' else: print '"success":false,' print '"settings":' + get_user_config_as_json(uid) print '}'
def save_temp(): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config['USERS_FOLDER'], uid['uid']) if 'task' not in request.values: return jsonify({'success': False, 'reason': 'No task.'}) taskstring = request.values['task'] task = parse_json_task(taskstring) userconfig = read_user_config(uid) if not os.path.exists(uid_dir): write_user_config(uid, userconfig) d = datetime.now() taskname = d.strftime('%w') + str(d.hour * 3600 + d.minute * 60 + d.second) filename = 'tasktemp_' + taskname + '.tsk' with open(os.path.join(uid_dir, filename), 'w') as f: write_xcsoar_task(f, task) base_url = 'tasks/' + uid['uid'] + '/temp/' + taskname return jsonify({ 'success': True, 'settings': get_user_config_as_json(uid, encoded=False), 'download': { 'xcsoar': { 'name': 'XCSoar (*.tsk)', 'url': base_url + '/xcsoar', 'qrcode': base_url + '/xcsoar/qr', }, 'seeyou': { 'name': 'SeeYou (*.cup)', 'url': base_url + '/seeyou', 'qrcode': base_url + '/seeyou/qr', }, } })
def remove(fileId): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config['USERS_FOLDER'], uid['uid']) if 'settings' in request.values: settings = request.values['settings'] userconfig = set_user_config_from_json(uid, settings) else: userconfig = read_user_config(uid) if len(userconfig['tp_files']) >= fileId: os.remove(os.path.join(uid_dir, 'turnpoints_' + str(fileId) + '.cup')) userconfig['tp_files'].pop(fileId - 1) for f_id in range(fileId + 1, (len(userconfig['tp_files']) + 2)): os.rename( os.path.join(uid_dir, 'turnpoints_' + str(f_id) + '.cup'), os.path.join(uid_dir, 'turnpoints_' + str(f_id - 1) + '.cup') ) write_user_config(uid, userconfig) return jsonify(get_user_config_as_json(uid, encoded=False))
def reply_success(uid): print "Content-type: text/html" print print '{"success":true,"settings":' print get_user_config_as_json(uid) print '}'
def save(task_name): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config['USERS_FOLDER'], uid['uid']) if 'task' not in request.values: return jsonify({'success': False, 'reason': 'No task.'}) taskstring = request.values['task'] task = parse_json_task(taskstring) m = re.compile('([^&+/;]*)').match(task_name) task_name = m.group(1) if task_name == '': return jsonify({'success': False, 'reason': 'Invalid task name.'}) userconfig = read_user_config(uid) if not os.path.exists(uid_dir): write_user_config(uid, userconfig) replace = False taskid = len(userconfig['task_files']) for key, value in enumerate(userconfig['task_files']): if value['name'] == task_name: replace = True taskid = key break if taskid >= 20: return jsonify({ 'success': False, 'reason': 'Too much tasks saved already (maximum of 20 reached).' }) filename = 'task_' + str(taskid + 1) + '.tsk' d = datetime.today() with open(os.path.join(uid_dir, filename), 'w') as f: write_xcsoar_task(f, task) taskinfo = { 'id': taskid + 1, 'name': task_name, 'distance': task.distance, 'type': task.type, 'turnpoints': len(task), 'date': d.isoformat() } if not replace: userconfig['task_files'].append(taskinfo) else: userconfig['task_files'][taskid] = taskinfo write_user_config(uid, userconfig) return jsonify({ 'success': True, 'settings': get_user_config_as_json(uid, encoded=False) })
def save(task_name): uid = get_uid_from_cookie() uid_dir = os.path.join(current_app.config["USERS_FOLDER"], uid["uid"]) if "task" in request.values: taskstring = request.values["task"] task = parse_json_task(taskstring) else: return jsonify({"success": False, "reason": "No task."}) m = re.compile("([^&+/;]*)").match(task_name) task_name = m.group(1) if task_name == "": return jsonify({"success": False, "reason": "Invalid task name."}) userconfig = read_user_config(uid) if not os.path.exists(uid_dir): write_user_config(uid, userconfig) replace = False taskid = len(userconfig["task_files"]) for key, value in enumerate(userconfig["task_files"]): if value["name"] == task_name: replace = True taskid = key break if taskid >= 20: return jsonify({"success": False, "reason": "Too much tasks saved already (maximum of 20 reached)."}) filename = "task_" + str(taskid + 1) + ".tsk" d = datetime.today() with open(os.path.join(uid_dir, filename), "w") as f: f.write(create_xcsoar_task(task)) if not replace: userconfig["task_files"].append( { "id": taskid + 1, "name": task_name, "distance": task.distance, "type": task.type, "turnpoints": len(task), "date": d.isoformat(), } ) else: userconfig["task_files"][taskid] = { "id": taskid + 1, "name": task_name, "distance": task.distance, "type": task.type, "turnpoints": len(task), "date": d.isoformat(), } write_user_config(uid, userconfig) return jsonify({"success": True, "settings": get_user_config_as_json(uid, encoded=False)}) return jsonify({"success": False, "reason": "Unknown failure."})