Ejemplo n.º 1
0
def update_entry(id):
    entry = g.db.Entry.find_one(ObjectId(id))
    if not entry:
        flash('entry not found', 'error')
    else:
        if request.form.get('duration'):
            entry['duration'] = int(request.form['duration'])
        stub = request.form.get('stub', '')
        if stub:
            stub = process_stub(stub)
        else:
            stub = {'tags':[], 'comments':[]}
        for tag_id in stub['tags']:
            if not g.db.Tag.find_one(tag_id):
                tag = g.db.Tag()
                tag['_id'] = tag_id
                tag.save()
        entry['tags'] = stub['tags']
        entry['comments'] = stub['comments']
        project_id = request.form.get('project')
        if project_id:
            if not g.db.Project.find_one(project_id):
                project = g.db.Project()
                project['_id'] = project_id
                project.save()
            entry['project'] = project_id
        entry.save()
        flash('entry updated', 'success')
    return redirect(url_for('list_entries'))
Ejemplo n.º 2
0
def create_entry():
    """
    save an entry in the database. If the task does not exists, it will create a
    new one on the fly.
    """
    project_id =  request.form.get('project')
    if not project_id or not request.form.get('duration'):
        abort(400)
    duration = int(request.form['duration'])
    stub = request.form.get('stub', '')
    if stub:
        stub = process_stub(stub)
    else:
        stub = {'tags':[], 'comments':[]}
    if not g.db.Project.find_one(project_id):
        project = g.db.Project()
        project['_id'] = project_id
        project.save()
    for tag_id in stub['tags']:
        if not g.db.Tag.find_one(tag_id):
            tag = g.db.Tag()
            tag['_id'] = tag_id
            tag.save()
    entry = g.db.Entry()
    entry['project'] = project_id
    entry['duration'] = duration
    entry['tags'] = stub['tags']
    entry['comments'] = stub['comments']
    entry['created_at'] = datetime.utcnow()
    entry.save()
    flash('entry created', 'success')
    return redirect(url_for('list_entries'))