Beispiel #1
0
def edit(id):
    '''goal detail'''
    g = Goals()
    goal = g.find_one(id)

    if request.method == 'POST':
        error = []

        # change name, description, due date and reward file
        if 'name' in request.form and request.form['name']:
            goal['name'] = request.form['name']
        if 'description' in request.form and request.form['description']:
            goal['description'] = request.form['description']
        if 'file' in request.files and request.files['file']:
            # save image
            c = CDN()
            image = c.replace(goal['reward'], { 'image': utils.file_to_mongo(request.files['file'])})

        goal['date']['end'] = utils.timestamp_new(
                year = request.form['due-date[year]'],
                month = request.form['due-date[month]'],
                day = request.form['due-date[day]'])

        # tags
        if 'tags' in request.form and request.form['tags']:
            tags = [utils.slugify(tag) for tag in request.form['tags'].split(',')]
            goal['tags'] = tags

        # two different systems
        if goal['variant'] == 'value': # target value

            # validate
            if 'values[begin]' not in request.form or not request.form['values[begin]']:
                error.append('A starting value is missing')
            if 'values[end]' not in request.form or not request.form['values[end]']:
                error.append('A target value is missing')
            if 'target[points]' not in request.form or not request.form['target[points]']:
                error.append('Target amount of points is missing')

            # change the values
            if not error:
                goal['values']['begin'] = float(request.form['values[begin]'])
                goal['values']['end'] = float(request.form['values[end]'])

                goal['points']['target'] = int(request.form['target[points]'])
                goal['points']['conversion'] = (float(request.form['values[end]']) - float(request.form['values[begin]'])) / float(request.form['target[points]'])
                goal['points']['unit'] = request.form['unit']

        else: # points

            # validate
            if 'points[target]' not in request.form or not request.form['points[target]']:
                error.append('Target amount of points is missing')
            if 'points[name-0]' not in request.form or not request.form['points[name-0]']:
                error.append('At least one currency activity is missing')

            # change the values
            if not error:
                goal['points']['target'] = int(request.form['points[target]'])

                currency = []
                for n in range(10):
                    if 'points[name-'+str(n)+']' in request.form and len(request.form['points[name-'+str(n)+']']) > 0:
                        currency.append({
                            'name': request.form['points[name-'+str(n)+']'],
                            'points': int(request.form['points[currency-'+str(n)+']']) })

                goal['points']['currency'] = currency

        if not error:
            # update
            g.replace(goal['_id'], goal)

            return redirect(url_for('goals.goal', id=id))

    goals = utils.sort_list_by_points(g.to_list(g.find_all()))
    fibonacci = [1,2,3,5,8,13,20,35,50,100,150,200,400]
    date = utils.date_list(goal['date']['end'])

    return render_template(goal['variant']+'-edit.html', **locals())
Beispiel #2
0
def edit(id):
    '''goal detail'''
    g = Goals()
    goal = g.find_one(id)

    if request.method == 'POST':
        error = []

        # change name, description, due date and reward file
        if 'name' in request.form and request.form['name']:
            goal['name'] = request.form['name']
        if 'description' in request.form and request.form['description']:
            goal['description'] = request.form['description']
        if 'file' in request.files and request.files['file']:
            # save image
            c = CDN()
            image = c.replace(
                goal['reward'],
                {'image': utils.file_to_mongo(request.files['file'])})

        goal['date']['end'] = utils.timestamp_new(
            year=request.form['due-date[year]'],
            month=request.form['due-date[month]'],
            day=request.form['due-date[day]'])

        # tags
        if 'tags' in request.form and request.form['tags']:
            tags = [
                utils.slugify(tag) for tag in request.form['tags'].split(',')
            ]
            goal['tags'] = tags

        # two different systems
        if goal['variant'] == 'value':  # target value

            # validate
            if 'values[begin]' not in request.form or not request.form[
                    'values[begin]']:
                error.append('A starting value is missing')
            if 'values[end]' not in request.form or not request.form[
                    'values[end]']:
                error.append('A target value is missing')
            if 'target[points]' not in request.form or not request.form[
                    'target[points]']:
                error.append('Target amount of points is missing')

            # change the values
            if not error:
                goal['values']['begin'] = float(request.form['values[begin]'])
                goal['values']['end'] = float(request.form['values[end]'])

                goal['points']['target'] = int(request.form['target[points]'])
                goal['points']['conversion'] = (
                    float(request.form['values[end]']) -
                    float(request.form['values[begin]'])) / float(
                        request.form['target[points]'])
                goal['points']['unit'] = request.form['unit']

        else:  # points

            # validate
            if 'points[target]' not in request.form or not request.form[
                    'points[target]']:
                error.append('Target amount of points is missing')
            if 'points[name-0]' not in request.form or not request.form[
                    'points[name-0]']:
                error.append('At least one currency activity is missing')

            # change the values
            if not error:
                goal['points']['target'] = int(request.form['points[target]'])

                currency = []
                for n in range(10):
                    if 'points[name-' + str(n) + ']' in request.form and len(
                            request.form['points[name-' + str(n) + ']']) > 0:
                        currency.append({
                            'name':
                            request.form['points[name-' + str(n) + ']'],
                            'points':
                            int(request.form['points[currency-' + str(n) +
                                             ']'])
                        })

                goal['points']['currency'] = currency

        if not error:
            # update
            g.replace(goal['_id'], goal)

            return redirect(url_for('goals.goal', id=id))

    goals = utils.sort_list_by_points(g.to_list(g.find_all()))
    fibonacci = [1, 2, 3, 5, 8, 13, 20, 35, 50, 100, 150, 200, 400]
    date = utils.date_list(goal['date']['end'])

    return render_template(goal['variant'] + '-edit.html', **locals())
Beispiel #3
0
def log(id):
    '''log progress on a goal'''
    g = Goals()
    goal = g.find_one(id)
    if goal:
        if request.method == 'POST':
            error = []
            
            # fetch the log
            log = goal['log']
            if goal['variant'] == 'value':
                # validate
                if 'value' not in request.form or not request.form['value']: error.append('New value is missing')

                if not error:
                    if not log: # difference from the beginning
                        points = (float(request.form['value']) - goal['values']['begin']) / goal['points']['conversion']
                    else: # difference from last log
                        points = (float(request.form['value']) - log[-1]['value']) / goal['points']['conversion']
                    entry = {
                        'value': round(float(request.form['value']), 1),
                        'points': round(points, 1),
                        'description': request.form['description'],
                        'date': utils.timestamp_new(
                            year = request.form['date[year]'],
                            month = request.form['date[month]'],
                            day = request.form['date[day]'])
                    }
            else:
                entry = {
                    'points': goal['points']['currency'][int(request.form['points'])]['points'],
                    'name': goal['points']['currency'][int(request.form['points'])]['name'],
                    'description': request.form['description'],
                    'date': utils.timestamp_new(
                        year = request.form['date[year]'],
                        month = request.form['date[month]'],
                        day = request.form['date[day]'])
                }

            if not error:
                # append to the log
                log.append(entry)

                # sort (yeah, not smart when already sorted)
                log = utils.sort_list(log)

                # update current value on the last entry (by date, not actual entry)
                if goal['variant'] == 'value':
                    g.update(id, 'values.current', log[-1]['value'])

                # update the entry
                g.update(id, 'log', log)
                # increment running total of logged points
                g.increment(id, 'points.logged', entry['points'])

                return redirect(url_for('goals.goal', id=id))

        date = utils.date_list(utils.timestamp_new())
        return render_template(goal['variant']+'-log.html', **locals())
    else:
        return redirect(url_for('goals.all'))
Beispiel #4
0
def log(id):
    '''log progress on a goal'''
    g = Goals()
    goal = g.find_one(id)
    if goal:
        if request.method == 'POST':
            error = []

            # fetch the log
            log = goal['log']
            if goal['variant'] == 'value':
                # validate
                if 'value' not in request.form or not request.form['value']:
                    error.append('New value is missing')

                if not error:
                    if not log:  # difference from the beginning
                        points = (float(request.form['value']) -
                                  goal['values']['begin']
                                  ) / goal['points']['conversion']
                    else:  # difference from last log
                        points = (
                            float(request.form['value']) -
                            log[-1]['value']) / goal['points']['conversion']
                    entry = {
                        'value':
                        round(float(request.form['value']), 1),
                        'points':
                        round(points, 1),
                        'description':
                        request.form['description'],
                        'date':
                        utils.timestamp_new(year=request.form['date[year]'],
                                            month=request.form['date[month]'],
                                            day=request.form['date[day]'])
                    }
            else:
                entry = {
                    'points':
                    goal['points']['currency'][int(
                        request.form['points'])]['points'],
                    'name':
                    goal['points']['currency'][int(
                        request.form['points'])]['name'],
                    'description':
                    request.form['description'],
                    'date':
                    utils.timestamp_new(year=request.form['date[year]'],
                                        month=request.form['date[month]'],
                                        day=request.form['date[day]'])
                }

            if not error:
                # append to the log
                log.append(entry)

                # sort (yeah, not smart when already sorted)
                log = utils.sort_list(log)

                # update current value on the last entry (by date, not actual entry)
                if goal['variant'] == 'value':
                    g.update(id, 'values.current', log[-1]['value'])

                # update the entry
                g.update(id, 'log', log)
                # increment running total of logged points
                g.increment(id, 'points.logged', entry['points'])

                return redirect(url_for('goals.goal', id=id))

        date = utils.date_list(utils.timestamp_new())
        return render_template(goal['variant'] + '-log.html', **locals())
    else:
        return redirect(url_for('goals.all'))