Beispiel #1
0
    def post(self):
        '''Cria uma nova denúncia'''
        session = Session()

        # get report data provided in the request
        reportdata = request.get_json(force=True)

        # create database model
        new_report = Report(state_abbr=reportdata['state_abbr'],
                            city_name=reportdata['city_name'],
                            area=reportdata['area'],
                            geolatitude=reportdata['geolatitude'],
                            geolongitude=reportdata['geolongitude'],
                            description=reportdata['description'])

        # check if given user exists
        user_id = reportdata['user']
        given_user = session.query(User).filter_by(id=user_id)

        if given_user.scalar() is None:
            response = {
                "status": 404,
                "message": "Not Found",
                "error": True,
                "response": "User not found"
            }
            return jsonify(response)

        # create and attach attachments
        for attachment in reportdata['attachments']:
            attach = Attachment(attachment_addr=attachment)
            attach.report = new_report
            attach.user = given_user.one()
            session.add(attach)

        # attach given user
        new_report.user = given_user.one()

        # add new report to database
        session.add(new_report)
        session.commit()

        attachs = []

        for item in new_report.attachments:
            attachs.append(dictionarize(item))

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": {
                "report": dictionarize(new_report),
                "attachments": attachs
            }
        }
        return jsonify(response)
Beispiel #2
0
    def put(self, id):
        '''Atualiza os dados de uma flag'''
        session = Session()

        # look up given flag
        given_flag = session.query(Flag).filter_by(identifier=id)

        # check if given flag exists
        if given_flag.scalar() is None:
            namespace.abort(404)

        # update flag entry with given values
        flag = given_flag.one()

        for datafield in request.args:
            if datafield.lower() != 'privileges':
                setattr(flag, datafield, request.args[datafield])

        flag = session.query(Flag).filter_by(identifier=id).one()

        granted = []

        if 'privileges' in request.args:
            # get list of privileges
            req_privileges = request.args['privileges'].split(",")
            grantable = []
            for priv in req_privileges:
                # check if requested privilege exists
                item = session.query(Privilege).filter_by(identifier=priv)
                if item.scalar() is not None:
                    # check if privilege can be granted to users
                    privilege = item.one()
                    if privilege.assignable is not False:
                        grantable.append(privilege)

            # assign grantable privileges to flag
            flag.privileges = grantable

            for privilege in grantable:
                granted.append(privilege.identifier)

        session.add(flag)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": {
                "flag": dictionarize(flag),
                "privileges": granted
            }
        }
        return jsonify(response)
Beispiel #3
0
    def put(self, id):
        '''Atualiza os dados de uma notificação'''
        session = Session()

        # look up given flag
        given_notification = session.query(Notification).filter_by(id=id)

        # check if given notification exists
        if given_notification.scalar() is None:
            namespace.abort(404)

        # update notification entry with given values
        notification = given_notification.one()

        for datafield in request.args:
            if datafield != 'user_id':
                setattr(notification, datafield, request.args[datafield])

        if 'user_id' in request.args:
            # check if given user exists
            user = session.query(User).filter_by(id=request.args['user_id'])
            if user.scalar() is None:
                response = {
                    "status": 404,
                    "message": "Not Found",
                    "error": True,
                    "response": "User has not been found"
                }
                return jsonify(response)

            notification.user = user.one()

        session.add(notification)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(notification)
        }
        return jsonify(response)
Beispiel #4
0
    def post(self):
        '''Cria uma nova resposta'''
        session = Session()

        # get reply data provided in the request
        replydata = request.get_json(force=True)

        # create database model
        new_reply = Reply(content=replydata['content'])

        # check if given user and report exists
        user_id = replydata['user']
        given_user = session.query(User).filter_by(id=user_id)

        report_id = replydata['report']
        given_report = session.query(Report).filter_by(id=report_id)

        if (given_user.scalar() is None) or (given_report.scalar() is None):
            response = {
                "status": 404,
                "message": "Not Found",
                "error": True,
                "response": "User or Report not found"
            }
            return jsonify(response)

        # attach user and report to reply
        new_reply.user = given_user.one()
        new_reply.report = given_report.one()

        # add new reply to database
        session.add(new_reply)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(new_reply)
        }
        return jsonify(response)
Beispiel #5
0
    def delete(self, id):
        '''Deleta uma resposta específica'''
        session = Session()

        given_reply = session.query(Reply).filter_by(id=id)

        if given_reply.scalar() is None:
            namespace.abort(404)

        # delete reply from database
        report = given_reply.one()
        records = dictionarize(report)
        session.delete(report)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": records
        }
        return jsonify(response)
Beispiel #6
0
    def post(self):
        '''Cria um novo privilégio'''
        session = Session()

        # get privilege information provided in the request
        privilege_data = request.get_json(force=True)

        # create database model
        new_privilege = Privilege(identifier=privilege_data['identifier'],
                                  assignable=privilege_data['assignable'])

        # add new privilege entity to database
        session.add(new_privilege)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(new_privilege)
        }
        return jsonify(response)
Beispiel #7
0
    def post(self):
        '''Cria uma nova notificação'''
        session = Session()

        # get notification information provided in the request
        notif_data = request.get_json(force=True)

        # check if given user exists
        user_id = notif_data['user_id']
        user = session.query(User).filter_by(id=user_id)
        if user.scalar() is None:
            response = {
                "status": 404,
                "message": "Not Found",
                "error": True,
                "response": "User has not been found"
            }
            return jsonify(response)

        # create database model
        new_notification = Notification(content=notif_data['content'],
                                        notiftype=notif_data['notiftype'])

        new_notification.user = user.one()

        # add new notification entity to database
        session.add(new_notification)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(new_notification)
        }
        return jsonify(response)
Beispiel #8
0
    def put(self, id):
        '''Atualiza os dados de uma denúncia'''
        session = Session()

        # look up given report
        given_report = session.query(Report).filter_by(id=id)

        # check if given report exists
        if given_report.scalar() is None:
            namespace.abort(404)

        # update report data with given values
        report = given_report.one()

        for datafield in request.args:
            setattr(report, datafield, request.args[datafield])

        session.add(report)
        session.commit()

        attachs = []

        for item in report.attachments:
            attachs.append(dictionarize(item))

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": {
                "report": dictionarize(report),
                "attachments": attachs
            }
        }

        return jsonify(response)
Beispiel #9
0
    def post(self):
        '''Cria uma nova flag'''
        session = Session()

        # get flag information provided in the request
        flag_data = request.get_json(force=True)

        # check if flag already exists
        flag_id = flag_data['identifier']
        flag_scalar = session.query(Flag).filter_by(
            identifier=flag_id).scalar()
        if flag_scalar is not None:
            response = {
                "status": 409,
                "message": "Conflict",
                "error": True,
                "response": "Flag already exists"
            }
            return jsonify(response)

        # create database model
        new_flag = Flag(
            identifier=flag_id,
            description=flag_data['description'],
            title=flag_data['title'],
        )

        if 'privileges' in flag_data:
            # get list of privileges
            req_privileges = flag_data['privileges']
            grantable = []
            for priv in req_privileges:
                # check if requested privilege exists
                item = session.query(Privilege).filter_by(identifier=priv)
                if item.scalar() is not None:
                    # check if privilege can be granted to users
                    privilege = item.one()
                    if privilege.assignable is not False:
                        grantable.append(privilege)

            # assign grantable privileges to flag
            new_flag.privileges = grantable

        # add new flag entity to database
        session.add(new_flag)
        session.commit()

        # generate a list of granted privilege identifiers
        granted = []
        for privilege in grantable:
            granted.append(privilege.identifier)

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": {
                "flag": dictionarize(new_flag),
                "privileges": granted
            }
        }
        return jsonify(response)