Exemple #1
0
def new():
    '''create new goal'''
    if request.method == 'POST':
        # validate
        error = []
        if 'name' not in request.form or not request.form['name']: error.append('Goal name is missing')
        if 'file' not in request.files or not request.files['file']: error.append('Reward image file is missing')

        if not error:
            # meta
            goal = {'name': request.form['name'], 'description': request.form['description'], 'status': 'active'}

            # dates
            goal['date'] = {
                'begin': utils.timestamp_new(),
                '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

            # save image
            c = CDN()
            image = c.save({ 'image': utils.file_to_mongo(request.files['file'])})
            goal['reward'] = image

            goal['variant'] = request.form['variant']
            goal['log'] = []

            # points system
            if goal['variant'] == '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')

                if not error:
                    points = {
                        'target': int(request.form['points[target]']),
                        'logged': 0,
                        'currency': [] }

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

                    goal['points'] = points

            # target value system
            else:
                # 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')

                if not error:
                    goal['values'] = {
                        'begin': float(request.form['values[begin]']),
                        'end': float(request.form['values[end]']),
                        'current': float(request.form['values[begin]'])
                    }
                    goal['points'] = {
                        'target': int(request.form['target[points]']),
                        'logged': 0,
                        'conversion': (float(request.form['values[end]']) - float(request.form['values[begin]'])) / float(request.form['target[points]']),
                        'unit': request.form['unit']
                    }

            if not error:
                # save
                g = Goals()
                id = g.save(goal)

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

    g = Goals()
    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]
    return render_template('new.html', **locals())
Exemple #2
0
def new():
    '''create new goal'''
    if request.method == 'POST':
        # validate
        error = []
        if 'name' not in request.form or not request.form['name']:
            error.append('Goal name is missing')
        if 'file' not in request.files or not request.files['file']:
            error.append('Reward image file is missing')

        if not error:
            # meta
            goal = {
                'name': request.form['name'],
                'description': request.form['description'],
                'status': 'active'
            }

            # dates
            goal['date'] = {
                'begin':
                utils.timestamp_new(),
                '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

            # save image
            c = CDN()
            image = c.save(
                {'image': utils.file_to_mongo(request.files['file'])})
            goal['reward'] = image

            goal['variant'] = request.form['variant']
            goal['log'] = []

            # points system
            if goal['variant'] == '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')

                if not error:
                    points = {
                        'target': int(request.form['points[target]']),
                        'logged': 0,
                        'currency': []
                    }

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

                    goal['points'] = points

            # target value system
            else:
                # 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')

                if not error:
                    goal['values'] = {
                        'begin': float(request.form['values[begin]']),
                        'end': float(request.form['values[end]']),
                        'current': float(request.form['values[begin]'])
                    }
                    goal['points'] = {
                        'target':
                        int(request.form['target[points]']),
                        'logged':
                        0,
                        'conversion': (float(request.form['values[end]']) -
                                       float(request.form['values[begin]'])) /
                        float(request.form['target[points]']),
                        'unit':
                        request.form['unit']
                    }

            if not error:
                # save
                g = Goals()
                id = g.save(goal)

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

    g = Goals()
    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]
    return render_template('new.html', **locals())
Exemple #3
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())
Exemple #4
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())