Ejemplo n.º 1
0
def update_project(project_id):
    """Updates the project matching the id project_id.
    Only the parameters to update or to add should be passed in the request body.
    """
    project = Project.objects(id=project_id).get()
    patched = Project(**dict(chain(project.to_dict().items(), request.get_json().items())))
    patched.save()

    return jsonify(patched.to_dict())
Ejemplo n.º 2
0
Archivo: api.py Proyecto: giovamo/Sar
    def create_project(self):
        if 'project_name' in self.request.POST:
            project_name = self.request.get('project_name').strip()
            hourly_rate = self.request.get('hourly_rate')

            if not project_name:
                self.response.set_status(400)
                self.response.write(json.dumps({'code': 12, 'error': 'Project name can\'t be only spaces'}))
                return

            if not hourly_rate:
                hourly_rate = '0'
            try:
                if not re.match("^[a-zA-Z0-9_\-\s]+$", project_name):
                    self.response.set_status(400)
                    self.response.write(json.dumps({'code': 13, 'error': 'Project name contains invalid characters'}))
                    return

                if not re.match(r"(\d+(\.\d*)?|\.\d+)", hourly_rate):
                    self.response.set_status(400)
                    self.response.write(json.dumps({'code': 14, 'error': 'Hourly rate is not a positive number or 0'}))
                    return
            except Exception as e:
                self.response.set_status(500)
                self.response.write(json.dumps({'code': 15, 'error': e.message}))
                return
            if len(project_name) > 25:
                self.response.set_status(400)
                self.response.write(json.dumps({'code': 16, 'error': 'Project name must be under 25 chars'}))
                return
            try:
                project = Project(parent=self.user.key)
                project = ProjectService.save_project(project, project_name, hourly_rate, self.user)
                proj = project.to_dict()
                proj['id'] = project.key.urlsafe()
                self.response.write(json.dumps({'project': proj}))

            except Exception as e:
                self.response.set_status(500)
                self.response.write(json.dumps({'code': 17, 'error': e.message}))
        else:
            self.response.set_status(400)
            self.response.write(json.dumps({'code': 18, 'error': 'Request doesn\'t contain a project_name'}))
        return
Ejemplo n.º 3
0
def projects():
    """Retrieves a list of projects from the database or create a new project.
    When retrieving a list of projects, the URL can contain pagination parameters
    page and per_page which default respectively to 1 and 10 if omitted.
    """
    if request.method == 'GET':
        return paginate(resource_name='projects', endpoint='projects', objects=Project.objects)

    json = request.get_json()

    author_id = json.get('author_id')
    if author_id is not None:
        assert_author_exists(json.get('author_id'))

    project = Project(**json)
    project.save()

    # Add the project in the list of the author projects.
    Author.objects(id=project.author_id).update_one(push__projects=project.id)

    return jsonify(project.to_dict()), 201