def update_project(project_id):
    new_project = request.get_json()
    try:
        project = Project.get(Project.id == project_id)
    except models.DoesNotExist:
        return jsonify({ 'message': 'Project not found' }), 400
    if new_project['id'] != project.id:
        return jsonify({ 'message': 'Cannot update project at this url' }), 400
    try:
        if new_project['markerOptions']:
            marker_options = new_project['markerOptions']
            for marker_name in marker_options:
                try:
                    marker_option = MarkerOption.get(MarkerOption.name == marker_name)
                    ProjectMarkerOption.get(ProjectMarkerOption.project == project.id,
                                            ProjectMarkerOption.marker_option == marker_option.id)
                except models.DoesNotExist:
                    ProjectMarkerOption.create(project = project, marker_option = marker_option)
        if new_project['users']:
            users = new_project['users']
            for user in users:
                try:
                    project_user = ProjectUser.get(ProjectUser.app_user == user['id'],
                                                    ProjectUser.project == project)
                except models.DoesNotExist:
                    ProjectUser.create(app_user = user['id'], project = project)
        if new_project['stacks']:
            stacks = new_project['stacks']
            with db.atomic():
                for stack in stacks:
                    try:
                        project_stack = ProjectStack.get(ProjectStack.stack == stack['id'],
                                                        ProjectStack.project == project)
                    except models.DoesNotExist:
                        project_stack = ProjectStack.create(project=project, stack = stack['id'])
                        probe_setting = stack['probeSettings']
                        probe_position = json.loads(probe_setting['position'])
                        ProbeSetting.create(x_offset=int(probe_position['xOffset']),
                                            y_offset=int(probe_position['yOffset']),
                                            project_stack=project_stack,
                                            label=probe_position['label']
                                        )
        message = "Project updated"
    except Exception as ex:
        print ex
    project = Project.get(Project.id == project_id)
    return jsonify({ 'message': 'Project updated', 'project': project.deep_serialized() }), 200
def projects():
    req = request.get_json()
    message = ""
    try:
        page = req['page']
        page_size = req['pageSize']
    except (TypeError, KeyError):
        page = 1
        page_size = 20
    if request.method == 'POST':
        try:
            request_error = False
            title = req['title']
            comments = req['comments']
            stacks = req['stacks']
            probe_type = req['probeType']
        except KeyError:
            request_error = True
        if request_error:
            return jsonify({ 'message': 'Insufficient information to create project' }), 400
        try:
            project = Project.create(title=title, comments=comments, probe_type=probe_type)
            if req['markerOptions']:
                marker_options = req['markerOptions']
                for marker_name in marker_options:
                    try:
                        marker_option = MarkerOption.get(MarkerOption.name == marker_name)
                        ProjectMarkerOption.create(project = project, marker_option = marker_option)
                    except Exception as ex:
                        print("line 74")
                        print ex
            if req['users']:
                users = req['users']
                for user in users:
                    try:
                        ProjectUser.create(app_user = user['id'], project = project)
                    except Exception as ex:
                        print ex
            for stack in stacks:
                try:
                    project_stack = ProjectStack.create(project=project, stack = stack['stackId'])
                    probe_setting = stack['probeSettings']
                    probe_position = json.loads(probe_setting['position'])
                    ProbeSetting.create(x_offset=int(probe_position['xOffset']),
                                        y_offset=int(probe_position['yOffset']),
                                        project_stack=project_stack,
                                        label=probe_position['label']
                                    )
                except Exception as ex:
                    print ex
            message = "Project created"
        except Exception as ex:
            print ex
    projects = (Project.select()
        .order_by(Project.created_date.desc()))
        # .paginate(page, page_size))
    return jsonify({ 
        'message': message,
        'projects': [project.serialized for project in projects],
        'projectCount': SelectQuery(models.Project).count()
    }), 201