Ejemplo n.º 1
0
def load_goals(user_id):
    if (request.method == 'GET'):
        goals = db.session.query(Goal).filter(Goal.user_id == user_id)
        goals_dict = {}
        for goal in goals:
            goals_dict[goal.id] = goal.to_dict()
        return {'goals': goals_dict}, 200
    elif (request.method == 'POST'):
        data = request.get_json()
        goal = Goal(description=data['description'],
                    amount=data['amount'],
                    completion_year=data['completion_year'],
                    completion_month=data['completion_month'],
                    user_id=user_id)
        db.session.add(goal)
        db.session.commit()
        goals = db.session.query(Goal).filter(Goal.user_id == user_id)
        goals_dict = {}
        for goal in goals:
            goals_dict[goal.id] = goal.to_dict()
        return {'goals': goals_dict}, 200
    elif (request.method == 'PATCH'):
        data = request.get_json()
        id = data['id']
        goal = Goal.query.get(id)
        goal.is_complete = not goal.is_complete
        db.session.add(goal)
        db.session.commit()
        goals = db.session.query(Goal).filter(Goal.user_id == user_id)
        goals_dict = {}
        for goal in goals:
            goals_dict[goal.id] = goal.to_dict()
        return {'goals': goals_dict}, 200
    elif (request.method == 'DELETE'):
        data = request.get_json()
        id = data['id']
        goal = Goal.query.get(id)
        db.session.delete(goal)
        db.session.commit()
        goals = db.session.query(Goal).filter(Goal.user_id == user_id)
        goals_dict = {}
        for goal in goals:
            goals_dict[goal.id] = goal.to_dict()
        return {'goals': goals_dict}, 200
Ejemplo n.º 2
0
def create_goal():
    form = GoalForm()
    form['csrf_token'].data = request.cookies['csrf_token']
    if form.validate_on_submit():
        data = Goal()  
        form.populate_obj(data)
        data.userId = current_user.id
        db.session.add(data)
        db.session.commit()
        return data.to_dict()
    return 'invalid info'