コード例 #1
0
ファイル: goals.py プロジェクト: radekstepan/monGoals
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())
コード例 #2
0
ファイル: goals.py プロジェクト: radekstepan/monGoals
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())
コード例 #3
0
ファイル: cdn.py プロジェクト: radekstepan/monGoals
def image(id):
    '''fetch a base64 encoded image from the db'''
    c = CDN()
    file = c.find_one(id)

    return file['image']
コード例 #4
0
ファイル: goals.py プロジェクト: radekstepan/monGoals
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())
コード例 #5
0
ファイル: goals.py プロジェクト: radekstepan/monGoals
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())
コード例 #6
0
ファイル: goals.py プロジェクト: radekstepan/monGoals
def remove(id):
    '''remove'''
    g, c = Goals(), CDN()
    c.remove(g.find_one(id)['reward'])
    g.remove(id)
    return redirect(url_for('goals.all'))
コード例 #7
0
def image(id):
    '''fetch a base64 encoded image from the db'''
    c = CDN()
    file = c.find_one(id)

    return file['image']