def upload_tracks_ajax(request): if request.method == 'POST': form = UploadTrackForm(request.POST, request.FILES) if form.is_valid(): tracks = upload_tracks_from_stream(request.user, form.cleaned_data['track_data'], form.cleaned_data['only_newer']) result = map(lambda (track, action): {'action': action, 'track': track.summary()}, tracks) return HttpResponse(jsonencoder.dumps({'success': True, 'tracks': result}), mimetype='application/json'); else: return HttpResponse(jsonencoder.dumps({'success': false, 'code': 'INVALID_FORM'}), mimetype='application/json'); else: return HttpResponse(jsonencoder.dumps({'success': false, 'code': 'REQUIRES_POST'}), mimetype='application/json');
def track_history(request): try: offset = int(request.GET.get("offset", 0)) except ValueError: offset = 0 tracks = Track.objects.order_by("date").reverse()[offset : offset + 20] return HttpResponse(jsonencoder.dumps(map(lambda t: t.summary(), tracks)), mimetype="application/json")
def login(request): if request.is_ajax(): if request.method == "POST": data = jsonencoder.loads(request.raw_post_data) user = django.contrib.auth.authenticate(username=data["username"], password=data["password"]) del data["password"] if user is not None: if user.is_active: django.contrib.auth.login(request, user) data["success"] = True data["id"] = uuid.uuid1().hex else: data["success"] = False data["code"] = "USER_DISABLED" else: data["success"] = False data["code"] = "WRONG_USER_PASSWORD" return HttpResponse(jsonencoder.dumps(data), mimetype="application/json") elif request.method == "DELETE": django.contrib.auth.logout(request) return HttpResponse(jsonencoder.dumps({"success": True}), mimetype="application/json")
def login(request): if request.is_ajax(): if request.method == 'POST': data = jsonencoder.loads(request.raw_post_data) user = django.contrib.auth.authenticate(username=data['username'], password=data['password']) del data['password'] if user is not None: if user.is_active: django.contrib.auth.login(request, user) data['success'] = True data['id'] = uuid.uuid1().hex else: data['success'] = False data['code'] = 'USER_DISABLED' else: data['success'] = False data['code'] = 'WRONG_USER_PASSWORD' return HttpResponse(jsonencoder.dumps(data), mimetype='application/json') elif request.method == 'DELETE': django.contrib.auth.logout(request) return HttpResponse(jsonencoder.dumps({'success': True}), mimetype='application/json')
def upload_tracks_ws(request): username = request.REQUEST['username'] password = request.REQUEST['password'] user = authenticate(username = username, password = password) if user is not None and user.is_active: only_newer = bool(request.REQUEST['only_newer']) stream = StringIO(request.REQUEST['data']) try: tracks = upload_tracks_from_stream(user, stream, only_newer) return HttpResponse(jsonencoder.dumps({'number_found_tracks': len(tracks), 'number_saved_tracks': len(filter(lambda t: t[1] == 'save', tracks))}), mimetype='application/json') finally: stream.close() else: return HttpResponse(status=403)
def get_track_data(request, username, year, month, day, number): user = User.objects.get(username__exact=username) if user == None: print 'No user called "%s".' % username return HttpResponse(status=404) track = get_track_by_date(user, int(year), int(month), int(day), int(number)) if track != None: positions = track.positions if len(positions) > 0: date = positions[0].time.strftime("%Y-%m-%d") start_time = positions[0].time.strftime("%H:%M") end_time = positions[len(positions) - 1].time.strftime("%H:%M") markers = create_markers(track) else: date = "Unknown" start_time = "" end_time = "" markers = {} data = { "name": track.name, "distance": track.distance, "date": date, "start_time": start_time, "end_time": end_time, "duration": track.get_duration_string(), "pace": track.get_pace_string(), "created_time": track.created_time.strftime("%Y-%m-%d %H:%M:%S"), "elevation_chart_url": track.get_elevation_chart_url(300, 145), "pace_chart_url": track.get_pace_chart_url(300, 145), "is_open": False, "positions": positions, "info_points": markers, } return HttpResponse(jsonencoder.dumps(data), mimetype="application/json") else: return HttpResponse(status=404)
def get_track_data(request, username, year, month, day, number): user = User.objects.get(username__exact = username) if user == None: print 'No user called "%s".' % username return HttpResponse(status = 404) track = get_track_by_date(user, int(year), int(month), int(day), int(number)) if track != None: positions = track.positions if len(positions) > 0: date = positions[0].time.strftime('%Y-%m-%d') start_time = positions[0].time.strftime('%H:%M') end_time = positions[len(positions) - 1].time.strftime('%H:%M') markers = create_markers(track) else: date = 'Unknown' start_time = '' end_time = '' markers = {} data = {'name': track.name, 'distance': track.distance, 'date': date, 'start_time': start_time, 'end_time': end_time, 'duration': track.time, 'duration_string': track.get_duration_string(), 'pace': track.get_pace_string(), 'created_time': track.created_time.strftime('%Y-%m-%d %H:%M:%S'), 'elevation_chart_url': track.get_elevation_chart_url(300, 145), 'pace_chart_url': track.get_pace_chart_url(300, 145), 'is_open': False, 'positions': positions, 'info_points': markers} return HttpResponse(jsonencoder.dumps(data), mimetype='application/json') else: return HttpResponse(status = 404)