Esempio n. 1
0
    def delete(self, id):
        '''Deleta uma denúncia existente'''
        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)

        # delete report from database
        report = given_report.one()
        # delete the attachments of the report
        attachs = []
        for attach in report.attachments:
            attachs.append(dictionarize(attach))
            session.delete(attach)
        records = dictionarize(report)
        session.delete(report)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": {
                "report": records,
                "attachments": attachs
            }
        }
        return jsonify(response)
Esempio n. 2
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)
Esempio n. 3
0
    def delete(self, id):
        '''Deleta um usuário existente'''
        session = Session()

        # look up given user
        given_user = session.query(User).filter_by(id=id)

        # check if given user exists
        if given_user.scalar() is None:
            namespace.abort(404)

        # delete user from database
        user = given_user.one()
        records = dictionarize(user)
        session.delete(user)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": records
        }
        return jsonify(response)
Esempio n. 4
0
    def delete(self, id):
        '''Deleta um privilégio'''
        session = Session()

        # look up given privilege
        given_privilege = session.query(Privilege).filter_by(identifier=id)

        # check if given privilege exists
        if given_privilege.scalar() is None:
            namespace.abort(404)

        # delete privilege entry from database
        privilege = given_privilege.one()
        records = dictionarize(privilege)
        session.delete(privilege)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": records
        }
        return jsonify(response)
Esempio n. 5
0
    def put(self, id):
        '''Atualiza os dados de um usuário'''
        session = Session()

        # look up given user
        given_user = session.query(User).filter_by(id=id)

        # check if given user exists
        if given_user.scalar() is None:
            namespace.abort(404)

        # update user data with given values
        user = given_user.one()

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

        session.add(user)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(user)
        }
        return jsonify(response)
Esempio n. 6
0
    def get(self):
        '''Lista todas as flags'''
        session = Session()

        # get list of flags
        flags = []
        for flag in session.query(Flag).all():
            # generate list of flag privileges
            privileges = []
            for privilege in flag.privileges:
                privileges.append(privilege.identifier)

            flags.append({
                "flag": dictionarize(flag),
                "privileges": privileges
            })

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": flags
        }
        return jsonify(response)
Esempio n. 7
0
    def put(self, id):
        '''Atualiza os dados de um privilégio'''
        session = Session()

        # look up given privilege
        given_privilege = session.query(Privilege).filter_by(identifier=id)

        # check if given privilege exists
        if given_privilege.scalar() is None:
            namespace.abort(404)

        # update privilege entry with given values
        privilege = given_privilege.one()

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

        session.add(privilege)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(privilege)
        }
        return jsonify(response)
Esempio n. 8
0
    def post(self):
        '''Cria um novo usuário'''
        session = Session()

        # get user data provided in the request
        userdata = request.get_json(force=True)

        # create database model
        new_user = User(name=userdata['name'],
                        email=userdata['email'],
                        phone=userdata['phone'],
                        passhash=userdata['passhash'],
                        salt=userdata['salt'],
                        birthday=userdata['birthday'],
                        zip_code=userdata['zip_code'],
                        state_abbr=userdata['state_abbr'],
                        city_name=userdata['city_name'],
                        city_number=userdata['city_number'],
                        area=userdata['area'])

        # add new user to database
        session.add(new_user)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(new_user)
        }
        return jsonify(response)
Esempio n. 9
0
    def put(self, id):
        '''Atualiza os dados de uma noticia'''
        session = Session()

        # get news
        determined_news = session.query(News).filter_by(id=id)

        #check if news exists
        if determined_news.scalar() is None:
            namespace.abort(404)

        # update news data with given values
        news = determined_news.one()
        for datafield in request.args:
            setattr(news, datafield, request.args[datafield])
        session.add(news)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(news)
        }
        return jsonify(response)
Esempio n. 10
0
    def delete(self, id):
        '''Deletar uma notícia'''
        session = Session()

        # look up given determined news
        determined_news = session.query(News).filter_by(id=id)

        # check if determined news exists
        if determined_news.scalar() is None:
            namespace.abort(404)

        # delete news from database
        news = determined_news.one()
        records = dictionarize(news)
        session.delete(news)
        session.commit()

        #respond request
        response = {
            "status": 200,
            "message": "Sucess",
            "error": False,
            "response": records
        }
        return jsonify(response)
Esempio n. 11
0
    def delete(self, id):
        '''Deleta 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)

        # delete flag entry from database
        flag = given_flag.one()
        records = dictionarize(flag)

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

        session.delete(flag)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": {
                "flag": records,
                "privileges": privileges
            }
        }
        return jsonify(response)
Esempio n. 12
0
    def post(self):
        session = Session()

        # get news information provided in the request
        news_data = request.get_json(force=True)

        #create database model
        new_news = News(title=news_data['title'],
                        content=news_data['content'],
                        source=news_data['source'],
                        published_at=news_data['published_at'],
                        external_link=news_data['external_link'],
                        state_abbr=news_data['state_abbr'],
                        city_name=news_data['city_name'])

        # add new news entity to database
        session.add(new_news)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(new_news)
        }
        return jsonify(response)
Esempio n. 13
0
    def delete(self, id):
        '''Deleta 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)

        # delete notification entry from database
        notification = given_notification.one()
        records = dictionarize(notification)

        session.delete(notification)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": records
        }
        return jsonify(response)
Esempio n. 14
0
    def put(self, id):
        '''Altera uma resposta específica'''
        session = Session()

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

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

        reply = given_reply.one()

        # user and report cant be changed
        for datafield in request.args:
            if (datafield.lower() != 'user') and (datafield.lower() !=
                                                  'report'):
                setattr(reply, datafield, request.args[datafield])

        session.add(reply)
        session.commit()

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(reply)
        }
        return jsonify(response)
Esempio n. 15
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)
Esempio n. 16
0
    def get(self):
        '''Listagem de todas as denúncias'''
        session = Session()

        # get list of reports
        res = []
        for report in session.query(Report).all():
            attachs = []
            for attach in report.attachments:
                attachs.append(dictionarize(attach))

            resp = {"report": dictionarize(report), "attachments": attachs}
            res.append(resp)

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": res
        }
        return jsonify(response)
Esempio n. 17
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)
Esempio n. 18
0
    def get(self, id):
        '''Mostrar uma denúncia específica'''
        session = Session()

        report = session.query(Report).filter_by(id=id).first()
        if report is None:
            namespace.abort(404)

        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)
Esempio n. 19
0
    def get(self, id):
        '''Mostrar um usuário específico'''
        session = Session()

        user = session.query(User).filter_by(id=id).first()
        if user is None:
            namespace.abort(404)

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(user)
        }
        return jsonify(response)
Esempio n. 20
0
    def get(self, id):
        '''Mostra um privilégio específico'''
        session = Session()

        privilege = session.query(Privilege).filter_by(identifier=id).first()
        if privilege is None:
            namespace.abort(404)

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(privilege)
        }
        return jsonify(response)
Esempio n. 21
0
    def get(self, id):
        '''Mostra uma notificação específica'''
        session = Session()

        notification = session.query(Notification).filter_by(id=id).first()
        if notification is None:
            namespace.abort(404)

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(notification)
        }
        return jsonify(response)
Esempio n. 22
0
    def get(self):
        '''Lista todas as notificações'''
        session = Session()

        # get list of notifications
        notifications = []
        for notification in session.query(Notification).all():
            notifications.append(dictionarize(notification))

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": notifications
        }
        return jsonify(response)
Esempio n. 23
0
    def get(self):
        '''Listagem de todos os usuários'''
        session = Session()

        # get list of users
        users = []
        for user in session.query(User).all():
            users.append(dictionarize(user))

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": users
        }
        return jsonify(response)
Esempio n. 24
0
    def get(self):
        '''Listagem de todas as respostas'''
        session = Session()

        res = []
        for reply in session.query(Reply).all():
            res.append(dictionarize(reply))

        #respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": res
        }

        return jsonify(response)
Esempio n. 25
0
    def get(self, id):
        '''Mostra uma resposta específica'''
        session = Session()

        reply = session.query(Reply).filter_by(id=id).first()
        if reply is None:
            namespace.abort(404)

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

        return jsonify(response)
Esempio n. 26
0
    def get(self):
        '''Lista todos os privilégios'''
        session = Session()

        # get list of privileges
        privileges = []
        for privilege in session.query(Privilege).all():
            privileges.append(dictionarize(privilege))

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": privileges
        }
        return jsonify(response)
Esempio n. 27
0
    def get(self):
        '''Listagem de todas as notícias'''
        session = Session()

        # get list of all news
        news_list = []
        for news in session.query(News).all():
            news_list.append(dictionarize(news))

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": news_list
        }

        return jsonify(response)
Esempio n. 28
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)
Esempio n. 29
0
    def get(self, id):
        '''Mostrar uma notícia especifica'''
        session = Session()

        # get news by id
        news = session.query(News).filter_by(id=id).first()

        # check if the news exists
        if news is None:
            namespace.abort(404)

        # respond request
        response = {
            "status": 200,
            "message": "Success",
            "error": False,
            "response": dictionarize(news)
        }
        return jsonify(response)
Esempio n. 30
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)