Пример #1
0
    def test_basic_compliance_creation(self):
        p = Project(name='test project')
        p.save()
        c = Compliance(project=p)
        c.save()

        self.assertEqual(c.compliance_type, 'Benchmarking')
        #  test relation from compliance to project and vice versa
        self.assertEqual(c.project, p)
        self.assertTrue(p.compliance_set.exists())
        self.assertTrue(p.compliance_set.all()[0], c)
        # test repr or str
        self.assertEqual('Compliance Benchmarking for project Project test project', str(c))
        p.delete()
        c.delete()
Пример #2
0
    def test_basic_compliance_creation(self):
        p = Project(name='test project')
        p.save()
        c = Compliance(project=p)
        c.save()

        self.assertEqual(c.compliance_type, 'Benchmarking')
        #  test relation from compliance to project and vice versa
        self.assertEqual(c.project, p)
        self.assertTrue(p.compliance_set.exists())
        self.assertTrue(p.compliance_set.all()[0], c)
        # test repr or str
        self.assertEqual('Compliance Benchmarking for project Project test project', str(c))
        p.delete()
        c.delete()
Пример #3
0
    def partial_update(self, request, pk):
        """
        Updates a project. Allows partial update, i.e. only updated param s need be supplied.

        :PUT: Expects organization_id in query string.
        ---
        parameters:
            - name: organization_id
              description: ID of organization to associate new project with
              type: integer
              required: true
              paramType: query
            - name: project slug or pk
              description: The project slug identifier or primary key for this project
              required: true
              paramType: path
            - name: name
              description: name of the new project
              type: string
              required: false
            - name: is_compliance
              description: add compliance data if true
              type: bool
              required: false
            - name: compliance_type
              description: description of type of compliance
              type: string
              required: true if is_compliance else false
            - name: description
              description: description of new project
              type: string
              required: true if is_compliance else false
            - name: end_date
              description: Timestamp for when project ends
              type: string
              required: true if is_compliance else false
            - name: deadline_date
              description: Timestamp for compliance deadline
              type: string
              required: true if is_compliance else false
        Returns::
            {
                'status': 'success',
                'project': {
                        'id': project's primary key,
                        'name': project's name,
                        'slug': project's identifier,
                        'status': 'active',
                        'number_of_buildings': Count of buildings associated with project
                        'last_modified': Timestamp when project last changed
                        'last_modified_by': {
                            'first_name': first name of user that made last change,
                            'last_name': last name,
                            'email': email address,
                        },
                        'is_compliance': True if project is a compliance project,
                        'compliance_type': Description of compliance type,
                        'deadline_date': Timestamp of when compliance is due,
                        'end_date': Timestamp of end of project,
                        'property_count': number of property views associated with project,
                        'taxlot_count':  number of taxlot views associated with project,
                }
            }
        """
        error = None
        status_code = status.HTTP_200_OK
        project_data, _ = self.get_params(PROJECT_KEYS)
        project_data['last_modified_by'] = request.user
        if 'status' in project_data:
            # convert to int equivalent
            project_data['status'] = self.get_status(project_data['status'])
        is_compliance = project_data.pop('is_compliance', None)
        if is_compliance:
            compliance_data, _ = self.get_params(COMPLIANCE_KEYS)
            compliance_data = convert_dates(
                compliance_data, ['end_date', 'deadline_date']
            )
        key = self.get_key(pk)
        project = self.get_project(key, pk)
        if not project:
            error, status_code = self.get_error(
                'not found', key=key, val=pk
            )
        else:
            project = project[0]
            compliance = project.get_compliance()
            if is_compliance:
                if not compliance:
                    compliance = Compliance(project=project)
                compliance = update_model(
                    compliance, compliance_data
                )
            project = update_model(project, project_data)
            if is_compliance:
                compliance.save()
            # delete compliance if one exists
            elif is_compliance == 'False':
                compliance.delete()
            project.save()
        if error:
            result = {'status': 'error', 'message': error}
        else:
            result = {
                'status': 'success',
                'project': ProjectSerializer(project).data
            }
        return Response(result, status=status_code)
Пример #4
0
def create_project(request):
    """
    Creates a new project.
    @TODO: What's a compliance_type?

    Payload::

        {
         'organization_id': ID of org to associate new project with,
         'project': {
           'name': name of new project,
           'compliance_type': description of type of compliance,
           'description': description of new project,
           'end_date': Timestamp for when project ends,
           'deadline_date': Timestamp for compliance deadline
         }
        }

    Returns::

        {
         'status': 'success' or 'error',
         'message': 'error message, if any',
         'project_slug': Identifier of new project, if project successfully
                         created
        }

    """
    body = json.loads(request.body)
    project_json = body.get('project')

    if Project.objects.filter(
            name=project_json['name'],
            super_organization_id=body['organization_id']).exists():
        return {
            'status': 'error',
            'message': 'project already exists for user'
        }

    project, created = Project.objects.get_or_create(
        name=project_json['name'],
        owner=request.user,
        super_organization_id=body['organization_id'],
    )
    if not created:
        return {
            'status': 'error',
            'message': 'project already exists for the organization'
        }
    project.last_modified_by = request.user
    project.description = project_json.get('description')
    project.save()

    compliance_type = project_json.get('compliance_type', None)
    end_date = project_json.get('end_date', None)
    deadline_date = project_json.get('deadline_date', None)
    if ((compliance_type is not None and end_date is not None
         and deadline_date is not None)):
        c = Compliance(project=project)
        c.compliance_type = compliance_type
        c.end_date = parser.parse(project_json['end_date'])
        c.deadline_date = parser.parse(project_json['deadline_date'])
        c.save()

    return {'status': 'success', 'project_slug': project.slug}
def create_project(request):
    """
    Creates a new project.
    @TODO: What's a compliance_type?

    Payload::

        {
         'organization_id': ID of org to associate new project with,
         'project': {
           'name': name of new project,
           'compliance_type': description of type of compliance,
           'description': description of new project,
           'end_date': Timestamp for when project ends,
           'deadline_date': Timestamp for compliance deadline
         }
        }

    Returns::

        {
         'status': 'success' or 'error',
         'message': 'error message, if any',
         'project_slug': Identifier of new project, if project successfully
                         created
        }

    """
    body = json.loads(request.body)
    project_json = body.get('project')

    if Project.objects.filter(
        name=project_json['name'],
        super_organization_id=body['organization_id']
    ).exists():
        return {
            'status': 'error',
            'message': 'project already exists for user'
        }

    project, created = Project.objects.get_or_create(
        name=project_json['name'],
        owner=request.user,
        super_organization_id=body['organization_id'],
    )
    if not created:
        return {
            'status': 'error',
            'message': 'project already exists for the organization'
        }
    project.last_modified_by = request.user
    project.description = project_json.get('description')
    project.save()

    compliance_type = project_json.get('compliance_type', None)
    end_date = project_json.get('end_date', None)
    deadline_date = project_json.get('deadline_date', None)
    if ((compliance_type is not None
         and end_date is not None
         and deadline_date is not None)):
        c = Compliance(project=project)
        c.compliance_type = compliance_type
        c.end_date = parser.parse(project_json['end_date'])
        c.deadline_date = parser.parse(project_json['deadline_date'])
        c.save()

    return {'status': 'success', 'project_slug': project.slug}
Пример #6
0
    def partial_update(self, request, pk):
        """
        Updates a project. Allows partial update, i.e. only updated param s need be supplied.

        :PUT: Expects organization_id in query string.
        ---
        parameters:
            - name: organization_id
              description: ID of organization to associate new project with
              type: integer
              required: true
              paramType: query
            - name: project slug or pk
              description: The project slug identifier or primary key for this project
              required: true
              paramType: path
            - name: name
              description: name of the new project
              type: string
              required: false
            - name: is_compliance
              description: add compliance data if true
              type: bool
              required: false
            - name: compliance_type
              description: description of type of compliance
              type: string
              required: true if is_compliance else false
            - name: description
              description: description of new project
              type: string
              required: true if is_compliance else false
            - name: end_date
              description: Timestamp for when project ends
              type: string
              required: true if is_compliance else false
            - name: deadline_date
              description: Timestamp for compliance deadline
              type: string
              required: true if is_compliance else false
        Returns::
            {
                'status': 'success',
                'project': {
                        'id': project's primary key,
                        'name': project's name,
                        'slug': project's identifier,
                        'status': 'active',
                        'number_of_buildings': Count of buildings associated with project
                        'last_modified': Timestamp when project last changed
                        'last_modified_by': {
                            'first_name': first name of user that made last change,
                            'last_name': last name,
                            'email': email address,
                        },
                        'is_compliance': True if project is a compliance project,
                        'compliance_type': Description of compliance type,
                        'deadline_date': Timestamp of when compliance is due,
                        'end_date': Timestamp of end of project,
                        'property_count': number of property views associated with project,
                        'taxlot_count':  number of taxlot views associated with project,
                }
            }
        """
        error = None
        status_code = status.HTTP_200_OK
        project_data, _ = self.get_params(PROJECT_KEYS)
        project_data['last_modified_by'] = request.user
        if 'status' in project_data:
            # convert to int equivalent
            project_data['status'] = self.get_status(project_data['status'])
        is_compliance = project_data.pop('is_compliance', None)
        if is_compliance:
            compliance_data, _ = self.get_params(COMPLIANCE_KEYS)
            compliance_data = convert_dates(
                compliance_data, ['end_date', 'deadline_date']
            )
        key = self.get_key(pk)
        project = self.get_project(key, pk)
        if not project:
            error, status_code = self.get_error(
                'not found', key=key, val=pk
            )
        else:
            project = project[0]
            compliance = project.get_compliance()
            if is_compliance:
                if not compliance:
                    compliance = Compliance(project=project)
                compliance = update_model(
                    compliance, compliance_data
                )
            project = update_model(project, project_data)
            if is_compliance:
                compliance.save()
            # delete compliance if one exists
            elif is_compliance == 'False':
                compliance.delete()
            project.save()
        if error:
            result = {'status': 'error', 'message': error}
        else:
            result = {
                'status': 'success',
                'project': ProjectSerializer(project).data
            }
        return Response(result, status=status_code)