Esempio n. 1
0
    def post(self):
        '''Adds a new pedido to be submited to eSIC.'''
        args = api.general_parse()
        decoded = decode_token(args['token'], sv, api)
        author_name = decoded['username']

        text = bleach.clean(args['text'], strip=True)

        # Size limit enforced by eSIC
        if len(text) > 6000:
            api.abort_with_msg(400, 'Text size limit exceeded.', ['text'])

        # Validate 'orgao'
        if args['orgao']:
            orgao_exists = db.session.query(Orgao).filter_by(
                name=args['orgao']).count() == 1
            if not orgao_exists:
                api.abort_with_msg(400, 'Orgao not found.', ['orgao'])
        else:
            api.abort_with_msg(400, 'No Orgao specified.', ['orgao'])

        # Get author (add if needed)
        try:
            author_id = db.session.query(
                Author.id).filter_by(name=author_name).one()
        except NoResultFound:
            author = Author(name=author_name)
            db.session.add(author)
            db.session.commit()
            author_id = author.id

        pre_pedido = PrePedido(author_id=author_id, orgao_name=args['orgao'])

        # Set keywords
        for keyword_name in args['keywords']:
            try:
                keyword = (db.session.query(Keyword)
                           .filter_by(name=keyword_name).one())
            except NoResultFound:
                keyword = Keyword(name=keyword_name)
                db.session.add(keyword)
                db.session.commit()
        pre_pedido.keywords = ','.join(k for k in args['keywords'])
        pre_pedido.text = text
        pre_pedido.state = 'WAITING'
        pre_pedido.created_at = arrow.now()

        db.session.add(pre_pedido)
        db.session.commit()
        return {'status': 'ok'}
Esempio n. 2
0
def parse_and_decode():
    '''Return args and username'''
    args = api.general_parse()
    return args, decode_token(args['token'], sv, api)['username']