Exemplo n.º 1
0
 def questions():
     try:
         if request.method == 'POST':
             app.logger.debug("request:form {}".format(
                 json.dumps(request.get_json())))
             question_text = request.json["question_text"]
             pub_date = datetime.strptime(request.json['pub_date'],
                                          '%Y-%m-%d')
             question = Question(question_text=question_text,
                                 pub_date=pub_date)
             question.save()
             resp = {
                 'question_text': question.question_text,
                 'id': question.id,
                 'pub_date': question.pub_date.strftime('%Y-%m-%d')
             }
             return json.dumps(resp)
         if request.method == 'GET':
             questions = Question.select()
             l = []
             for question in questions:
                 l.append({
                     'question_text': question.question_text,
                     'id': question.id,
                     'pub_date': question.pub_date.strftime('%Y-%m-%d')
                 })
             return json.dumps(l)
     except Exception as ex:
         app.logger.error("questions {}".format(repr(ex)))
         raise ex
Exemplo n.º 2
0
def create_app():
    app = Flask(__name__)

    redis = Redis(host=os.environ["REDIS_HOST"], port=int(os.environ["REDIS_PORT"]), db=0)
    Question.set_db(init_db())

    @app.route('/polls/questions/', methods=['GET', 'POST'])
    def questions():
        redis.incr("hits", amount=1)
        try:
            if request.method == 'POST':
                app.logger.debug("request:form {}".format(json.dumps(request.get_json())))
                question_text = request.json["question_text"]
                pub_date = datetime.strptime(request.json['pub_date'], '%Y-%m-%d')
                question = Question(question_text=question_text, pub_date=pub_date)
                question.save()
                resp = {'question_text': question.question_text, 'id': question.id,
                        'pub_date': question.pub_date.strftime('%Y-%m-%d')}
                return json.dumps(resp)
            if request.method == 'GET':
                questions = Question.select()
                questions_list = []
                for question in questions:
                    questions_list.append({'question_text': question.question_text, 'id': question.id,
                              'pub_date': question.pub_date.strftime('%Y-%m-%d')})
                return json.dumps(questions_list)
        except Exception as ex:
            app.logger.error("questions {}".format(repr(ex)))
            raise ex

    @app.route('/hits/', methods=['GET'])
    def hits():
        return json.dumps({'hits': int(redis.get('hits').decode('utf-8'))})

    return app
Exemplo n.º 3
0
    def test_given_test_data_set_up_when_get_questions_then_empty_json_array_response(
            self):
        test_date = datetime.datetime.now().date()
        question = Question(question_text="test question", pub_date=test_date)
        question.save()

        rv = self.client.get('/polls/questions/')
        self.assertEqual(
            json.dumps([{
                "question_text": "test question",
                "id": 1,
                "pub_date": str(test_date)
            }]), rv.data.decode())
Exemplo n.º 4
0
 def test_given_initial_set_up_when_post_questions_then_database_updated(
         self):
     test_date = datetime.datetime.now().date()
     payload = {
         "question_text": "test question post",
         "pub_date": str(test_date)
     }
     rv = self.client.post('/polls/questions/',
                           data=json.dumps(payload),
                           content_type='application/json')
     self.assertDictEqual(
         {
             "question_text": "test question post",
             "id": 1,
             "pub_date": str(test_date)
         }, json.loads(rv.data.decode()))
     questions = Question.select()
     for question in questions:
         self.assertDictEqual(
             {
                 "question_text": "test question post",
                 "id": 1,
                 "pub_date": str(test_date)
             }, {
                 'question_text': question.question_text,
                 'id': question.id,
                 'pub_date': question.pub_date.strftime('%Y-%m-%d')
             })
Exemplo n.º 5
0
def create_app():
    app = Flask(__name__)

    Question.set_db(init_db())

    @app.route('/polls/questions/', methods=['GET', 'POST'])
    def questions():
        try:
            if request.method == 'POST':
                app.logger.debug("request:form {}".format(
                    json.dumps(request.get_json())))
                question_text = request.json["question_text"]
                pub_date = datetime.strptime(request.json['pub_date'],
                                             '%Y-%m-%d')
                question = Question(question_text=question_text,
                                    pub_date=pub_date)
                question.save()
                resp = {
                    'question_text': question.question_text,
                    'id': question.id,
                    'pub_date': question.pub_date.strftime('%Y-%m-%d')
                }
                return json.dumps(resp)
            if request.method == 'GET':
                questions = Question.select()
                l = []
                for question in questions:
                    l.append({
                        'question_text': question.question_text,
                        'id': question.id,
                        'pub_date': question.pub_date.strftime('%Y-%m-%d')
                    })
                return json.dumps(l)
        except Exception as ex:
            app.logger.error("questions {}".format(repr(ex)))
            raise ex

    return app