Esempio n. 1
0
def projects(id=None):
    user = g.user

    if request.method == 'GET': # Fetch
        if id:
            obj = Project.get_by_id_and_owner(id, user)
            if obj:
                return obj.to_dict()
            else:
                logger.debug('No project for key %s and user %s', id, user)
                abort(404)
        else:
            return [o.to_dict() for o in user.projects]

    elif request.method == 'POST': # Create
        data = MultiDict(request.json)
        assert not id, 'No id allowed'
        form = ProjectForm(data)
        if form.validate():
            obj = Project(owner=user,
                          title=form.title.data)
            obj.put()
            return obj.to_dict()
        else:
            logger.warn('Project create errors: %s', form.errors)
            abort(400)

    elif request.method == 'PUT': # Update
        data = MultiDict(request.json)
        obj = Project.get_by_id_and_owner(id, user)
        if not obj: abort(404)
        form = ProjectForm(data, obj)
        if form.validate():
            form.populate_obj(obj)
            obj.save()
            return obj.to_dict()
        else:
            logger.warn('Project update errors: %s', form.errors)
            abort(400)

    elif request.method == 'DELETE': # Delete
        obj = Project.get_by_id_and_owner(id, user)
        if not obj: abort(404)
        obj.delete()
        return {'success': True}