示例#1
0
def main(request):
    user = users.get_current_user()
    if not user:
        return direct_to_template(request, template = 'homepage.html', extra_context = {'login_url' : users.create_login_url(request.get_full_path())})

    if request.method == 'POST':
        return_msg = {}
        try:
            date = datetime.date.fromtimestamp(time.mktime(time.strptime(request.POST['date'], '%Y-%m-%d')))
            tracker = WeightTracker.all().filter('user = '******'date = ', date).get()
            if tracker:
                tracker.weight = float(request.POST['weight'])
                tracker.put()
            else:
                tracker = WeightTracker(weight = float(request.POST['weight']), date = date)
                tracker.put()
            return_msg['error'] = 0
            return_msg['msg'] = 'Data was saved succesfully'
            return_msg['weight'] = str(tracker.weight)
        except ValueError, e:
            return_msg['error'] = 1
            return_msg['msg'] = 'The date was not in the correct format'
        
        #Delete all cache data whenever a new entry is made
        memcache.delete_multi([CHART_DATA_CACHE_KEY(), BMI_CACHE_KEY()])
        if request.is_ajax():
            return HttpResponse(simplejson.dumps(return_msg), mimetype = 'application/json')
        else:
            return HttpResponseRedirect('/')
示例#2
0
def delete_data(request):
    if request.is_ajax() and request.method == 'POST':
        if 'date' in request.POST:
            date = datetime.date.fromtimestamp(time.mktime(time.strptime(request.POST['date'], '%Y-%m-%d')))
            WeightTracker.all().filter('user = '******'date = ', date).get().delete()
        else:
            from google.appengine.ext import db
            db.delete(WeightTracker.all().filter('user = '******'Delete successful')
    else:
        return HttpResponseForbidden('You are not allowed to view this URL')
示例#3
0
def handle_uploaded_file(file):
    lines = file.read().split('\n')
    for i, line in enumerate(lines):
        if line:
            date, weight = map(strip, line.split(','))
            try:
                date = datetime.date.fromtimestamp(time.mktime(time.strptime(date, '%m/%d/%Y')))
                weight = float(weight)
            except ValueError:
                return False, i + 1
            entry = WeightTracker.all().filter('user = '******'date = ', date).get()
            if entry:
                entry.weight = weight
            else:
                entry = WeightTracker(date = date, weight = weight)
            entry.put()

    return True, -1
示例#4
0
def get_chart_data(request):
    if request.is_ajax():
        data_dict_json = memcache.get(CHART_DATA_CACHE_KEY())
        if data_dict_json is None:
            all_data = WeightTracker.all().filter('user = '******'date')
            data_dict = {'data' : map(lambda entry : (str(entry.date), entry.weight), all_data), 'chart_max' : WeightTrackerSettings.all().filter('user ='******'application/json')
    else:
        return HttpResponseForbidden('You are not allowed to view this URL')
示例#5
0
                tracker.put()
            return_msg['error'] = 0
            return_msg['msg'] = 'Data was saved succesfully'
            return_msg['weight'] = str(tracker.weight)
        except ValueError, e:
            return_msg['error'] = 1
            return_msg['msg'] = 'The date was not in the correct format'
        
        #Delete all cache data whenever a new entry is made
        memcache.delete_multi([CHART_DATA_CACHE_KEY(), BMI_CACHE_KEY()])
        if request.is_ajax():
            return HttpResponse(simplejson.dumps(return_msg), mimetype = 'application/json')
        else:
            return HttpResponseRedirect('/')
    else:
        all_data = WeightTracker.all().filter('user = '******'-date')
        data_dict = {'data' : all_data, 'logout_url' : GET_LOGOUT_URL(), 'tracker_form' : TrackerForm()}
        settings = WeightTrackerSettings.all().filter('user = '******'cause that decorator is not called for this view.
        if not settings:
            return HttpResponseRedirect('/settings/?first')
        data_dict['units'] = settings.units
        data_dict['bmi'] = get_bmi(settings, all_data.get())
        data_dict['days_left'], data_dict['weight_left'], data_dict['req_rate'], data_dict['cur_rate'] = get_weight_time_lost(settings, all_data)

        #Need to put this try/except block in case no entries for 'this' user have been created
        try:
            if all_data.get().date != datetime.date.today():
                data_dict['today'] = datetime.date.today()
        except AttributeError, e:
            data_dict['today'] = datetime.date.today()