예제 #1
0
    def create_incident(self):
        '''
        Function to create an incident
        Validates the incident data
        If data passes validation an incident is created

        '''
        incident_body = request.get_json()
        user_id = get_identity()

        incident_body['createdby'] = user_id
        incident_body['status'] = 'pending'
        incident_body['createdon'] = datetime.utcnow().date()

        errors = validate_incident(incident_body)
        if errors:
            return jsonify({'status': 400, 'errors': errors}), 400

        incident_type = incident_body['type']
        images = self.get_media(incident_body, 'images')
        videos = self.get_media(incident_body, 'videos')

        incident = Incident(**incident_body)

        self.add_incident_media(incident, images, 'images')
        self.add_incident_media(incident, videos, 'videos')

        incident_id = db_services.insert_incident(incident)

        success_response = {
            'id': incident_id,
            'message': 'Created {} record'.format(incident_type)
        }

        return jsonify({'status': 201, 'data': [success_response]}), 201
예제 #2
0
 def get_user(self):
     """
     The function returns a user
     """
     user_id = get_identity()
     user = db_services.get_user_by_id(user_id)
     return jsonify({'status': 200, 'data': [user]}), 200
예제 #3
0
    def patch_incident_image(self, incident_id):
        user_id = get_identity()      
        # APP_ROOT = os.path.dirname(os.path.abspath('api/'))
        upload_folder = os.environ['UPLOAD_FOLDER']

        incident = db_services.get_incident(incident_id)
        if not incident:
            return jsonify({'status': 404, 'errors':
                            'incident not found'}), 404

        errors = is_modifiable(incident, user_id)
        if errors:
            return jsonify({'status': 403, 'error': errors}), 403

        incident_type = incident.type

        image = request.files.get('image', '')
        if image.filename == '':
            return jsonify({'status': 400, 'errors':
                        ['image name cannot be empty']}), 400

        filename = secure_filename(image.filename)
        image.save(os.path.join(upload_folder, filename))
        logging.debug(upload_folder)
        db_services.add_incident_image(incident_id, filename)
            
        success_response = {
            'id': incident_id,
            'message':
            'Image added to {} record'.format(incident_type)
            }

        return jsonify({'status': 201, 'data': [success_response]}), 201
예제 #4
0
    def put_incident(self, incident_id):
        '''
        Function to update an entire incident
        Validates the incident exists and belongs to this user
        If validation is passed the incident is updated
        and the updted incident is returned

        '''
        data = request.get_json()
        user_id = get_identity()

        data['created_by'] = user_id
        data['id'] = incident_id

        errors = validate_incident(data)
        if errors:
            return jsonify({'status': 400, 'errors': errors}), 400

        current_incident = db_services.get_incident(incident_id)
        if current_incident is None:
            return jsonify({'status': 404, 'errors':
                            'incident not found'}), 404

        error = is_modifiable(current_incident, user_id)
        if error:
            return jsonify({'status': 403, 'errors': error}), 403

        update_incident = Incident(**data)

        updated_incident = db_services.put_incident(update_incident)

        return jsonify({
            'status': 200,
            'data': [updated_incident.to_dict()]
            })
예제 #5
0
    def get_incidents(self, incident_type):
        '''
        Function to retun all incident given an incident id
        '''
        user_id = get_identity()

        incidents = db_services.get_all_incidents(user_id, incident_type)

        return jsonify({'status': 200, 'data': [incidents]})
예제 #6
0
    def get_totals(self):
        '''
        Function to retun all incident totals
        '''
        user_id = get_identity()
        is_admin = verify_is_admin()
        incident_totals = ""

        if is_admin is True:  
            incident_totals = db_services.get_all_totals()
        else:
            incident_totals = db_services.get_user_totals(user_id)

        return jsonify({'status': 200, 'data': incident_totals})
예제 #7
0
    def get_incident(self, incident_id):
        '''
        Function to retun an incident give an incident id
        Validates the incident exists and belongs to this user
        If validation is passed an incident is returned

        '''
        user_id = get_identity()        
        incident = db_services.get_incident(incident_id)

        if not incident:
            return jsonify({'status': 404, 'errors':
                            'Incident does not exist'}), 404

        error = is_modifiable(incident, user_id)
        if error:
            return jsonify({'status': 403, 'errors': error}), 403


        return jsonify({'status': 200, 'data': [incident.to_dict()]}), 200
예제 #8
0
    def patch_incident(self, incident_id, update_key):
        '''
        Function to update a property of an incident
        Validates the incident exists and belongs to this user
        If validation is passed the incidentpropert is updated
        and a success message is returned

        '''
        data = request.get_json()
        user_id = get_identity()

        data['created_by'] = user_id
        data['id'] = incident_id

        errors = validate_incident(data)
        if errors:
            return jsonify({'status': 400, 'errors': errors}), 400

        existing_incident = db_services.get_incident(incident_id)

        if not existing_incident:
            return jsonify({'status': 404, 'errors':
                            'incident not found'}), 404

        incident_type = existing_incident.type

        errors = is_modifiable(existing_incident, user_id)
        if errors:
            return jsonify({'status': 403, 'error': errors}), 403

        update_incident = Incident(**data)

        incident = db_services.patch_incident(
            update_incident, update_key)
        success_response = {
            'id': incident_id,
            'message':
            'Updated {} record’s {}'.format(incident_type, update_key)
        }

        return jsonify({'status': 200, 'data': [success_response]}), 200
예제 #9
0
    def delete_incident(self, incident_id):
        '''
        Function to delete an incident
        Validates the incident exists and belongs to this user
        or the user is an admin
        If validation is passed the incident is deleted
        and a success message is returned

        '''
        user_id = get_identity()

        user = db_services.get_user_by_id(user_id)
        if not user:
            return jsonify({'status': 401, 'errors':
                            'You are not authenticated'}), 401

        existing_incident = db_services.get_incident(incident_id)

        if existing_incident is None:
            return jsonify({'status': 404, 'error':
                            'Incident doesnt exist'}), 404

        errors = is_modifiable(existing_incident, user_id)
        if errors:
            return jsonify({'status': 403, 'error': errors}), 403

        incident_type = existing_incident.type

        deleted_id = db_services.delete_incident(existing_incident.id)
        if deleted_id is None:
            return jsonify({'status': 404, 'error':
                            'Incident doesnt exist'}), 404

        success_response = {
            'id': deleted_id,
            'message':
            '{} record has been deleted'.format(incident_type)
        }

        return jsonify({'status': 200, 'data': success_response}), 200