Beispiel #1
0
    def post(self):
        context = self.get_argument('context', None)
        word = self.get_argument('word', None)
        newWord = self.get_argument('newWord', None)
        langpair = self.get_argument('langpair', None)
        recap = self.get_argument('g-recaptcha-response', None)

        if not newWord:
            self.send_error(400, explanation='A suggestion is required')
            return

        if not recap:
            self.send_error(400, explanation='The ReCAPTCHA is required')
            return

        if not all([context, word, langpair, newWord, recap]):
            self.send_error(400, explanation='All arguments were not provided')
            return

        logging.info("Suggestion (%s): Context is %s \n Word: %s ; New Word: %s " % (langpair, context, word, newWord))
        logging.info('Now verifying ReCAPTCHA.')

        if not self.recaptcha_secret:
            logging.error('No ReCAPTCHA secret provided!')
            self.send_error(400, explanation='Server not configured correctly for suggestions')
            return

        # for nginx or when behind a proxy
        x_real_ip = self.request.headers.get("X-Real-IP")
        user_ip = x_real_ip or self.request.remote_ip
        payload = {
            'secret': self.recaptcha_secret,
            'response': recap,
            'remoteip': user_ip
        }
        recapRequest = self.wiki_session.post(RECAPTCHA_VERIFICATION_URL,
                                              data=payload)
        if recapRequest.json()['success']:
            logging.info('ReCAPTCHA verified, adding data to wiki')
        else:
            logging.info('ReCAPTCHA verification failed, stopping')
            self.send_error(400, explanation='ReCAPTCHA verification failed')
            return

        from util import addSuggestion
        data = {
            'context': context, 'langpair': langpair,
            'word': word, 'newWord': newWord
        }
        result = addSuggestion(self.wiki_session,
                               self.SUGGEST_URL, self.wiki_edit_token,
                               data)

        if result:
            self.sendResponse({
                'responseData': {
                    'status': 'Success'
                },
                'responseDetails': None,
                'responseStatus': 200
            })
        else:
            logging.info('Page update failed, trying to get new edit token')
            self.wiki_edit_token = wikiGetToken(
                SuggestionHandler.wiki_session, 'edit', 'info|revisions')
            logging.info('Obtained new edit token. Trying page update again.')
            result = addSuggestion(self.wiki_session,
                                   self.SUGGEST_URL, self.wiki_edit_token,
                                   data)
            if result:
                self.sendResponse({
                    'responseData': {
                        'status': 'Success'
                    },
                    'responseDetails': None,
                    'responseStatus': 200
                })
            else:
                self.send_error(400, explanation='Page update failed')
Beispiel #2
0
    def post(self):
        context = self.get_argument('context', None)
        word = self.get_argument('word', None)
        newWord = self.get_argument('newWord', None)
        langpair = self.get_argument('langpair', None)
        recap = self.get_argument('g-recaptcha-response', None)

        if not newWord:
            self.send_error(400, explanation='A suggestion is required')
            return

        if not recap:
            self.send_error(400, explanation='The ReCAPTCHA is required')
            return

        if not all([context, word, langpair, newWord, recap]):
            self.send_error(400, explanation='All arguments were not provided')
            return

        logging.info(
            "Suggestion (%s): Context is %s \n Word: %s ; New Word: %s " %
            (langpair, context, word, newWord))
        logging.info('Now verifying ReCAPTCHA.')

        if not self.recaptcha_secret:
            logging.error('No ReCAPTCHA secret provided!')
            self.send_error(
                400,
                explanation='Server not configured correctly for suggestions')
            return

        # for nginx or when behind a proxy
        x_real_ip = self.request.headers.get("X-Real-IP")
        user_ip = x_real_ip or self.request.remote_ip
        payload = {
            'secret': self.recaptcha_secret,
            'response': recap,
            'remoteip': user_ip
        }
        recapRequest = self.wiki_session.post(RECAPTCHA_VERIFICATION_URL,
                                              data=payload)
        if recapRequest.json()['success']:
            logging.info('ReCAPTCHA verified, adding data to wiki')
        else:
            logging.info('ReCAPTCHA verification failed, stopping')
            self.send_error(400, explanation='ReCAPTCHA verification failed')
            return

        from util import addSuggestion
        data = {
            'context': context,
            'langpair': langpair,
            'word': word,
            'newWord': newWord
        }
        result = addSuggestion(self.wiki_session, self.SUGGEST_URL,
                               self.wiki_edit_token, data)

        if result:
            self.sendResponse({
                'responseData': {
                    'status': 'Success'
                },
                'responseDetails': None,
                'responseStatus': 200
            })
        else:
            logging.info('Page update failed, trying to get new edit token')
            self.wiki_edit_token = wikiGetToken(SuggestionHandler.wiki_session,
                                                'edit', 'info|revisions')
            logging.info('Obtained new edit token. Trying page update again.')
            result = addSuggestion(self.wiki_session, self.SUGGEST_URL,
                                   self.wiki_edit_token, data)
            if result:
                self.sendResponse({
                    'responseData': {
                        'status': 'Success'
                    },
                    'responseDetails': None,
                    'responseStatus': 200
                })
            else:
                self.send_error(400, explanation='Page update failed')
Beispiel #3
0
        try:
            import requests
            requestsImported = True
        except ImportError:
            logging.error('requests module is required for SuggestionHandler')

        if requestsImported:
            from wiki_util import wikiLogin, wikiGetToken
            SuggestionHandler.SUGGEST_URL = args.wiki_url
            SuggestionHandler.recaptcha_secret = args.recaptcha_secret
            SuggestionHandler.wiki_session = requests.Session()
            SuggestionHandler.auth_token = wikiLogin(
                SuggestionHandler.wiki_session,
                args.wiki_username,
                args.wiki_password)
            SuggestionHandler.wiki_edit_token = wikiGetToken(
                SuggestionHandler.wiki_session, 'edit', 'info|revisions')

    global http_server
    if args.ssl_cert and args.ssl_key:
        http_server = tornado.httpserver.HTTPServer(application, ssl_options={
            'certfile': args.ssl_cert,
            'keyfile': args.ssl_key,
        })
        logging.info('Serving at https://localhost:%s' % args.port)
    else:
        http_server = tornado.httpserver.HTTPServer(application)
        logging.info('Serving at http://localhost:%s' % args.port)

    signal.signal(signal.SIGTERM, sig_handler)
    signal.signal(signal.SIGINT, sig_handler)
Beispiel #4
0
        requestsImported = False
        try:
            import requests
            requestsImported = True
        except ImportError:
            logging.error('requests module is required for SuggestionHandler')

        if requestsImported:
            from wiki_util import wikiLogin, wikiGetToken
            SuggestionHandler.SUGGEST_URL = args.wiki_url
            SuggestionHandler.recaptcha_secret = args.recaptcha_secret
            SuggestionHandler.wiki_session = requests.Session()
            SuggestionHandler.auth_token = wikiLogin(
                SuggestionHandler.wiki_session, args.wiki_username,
                args.wiki_password)
            SuggestionHandler.wiki_edit_token = wikiGetToken(
                SuggestionHandler.wiki_session, 'edit', 'info|revisions')

    global http_server
    if args.ssl_cert and args.ssl_key:
        http_server = tornado.httpserver.HTTPServer(application,
                                                    ssl_options={
                                                        'certfile':
                                                        args.ssl_cert,
                                                        'keyfile':
                                                        args.ssl_key,
                                                    })
        logging.info('Serving at https://localhost:%s' % args.port)
    else:
        http_server = tornado.httpserver.HTTPServer(application)
        logging.info('Serving at http://localhost:%s' % args.port)