Skip to content

Commit 7995bd8

Browse files
committed
Migrated to Vagrant 2.0.2
1 parent e14fc48 commit 7995bd8

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

Vagrantfile

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# -*- mode: ruby -*-
22
# vi: set ft=ruby :
33

4-
# All Vagrant configuration is done below. The "2" in Vagrant.configure
5-
# configures the configuration version (we support older styles for
6-
# backwards compatibility). Please don't change it unless you know what
7-
# you're doing.
4+
# This file requires Vagrant 2.0.2 or later
85
Vagrant.configure(2) do |config|
96
# The most common configuration options are documented and commented below.
107
# For a complete reference, please see the online documentation at
@@ -15,7 +12,7 @@ Vagrant.configure(2) do |config|
1512

1613
# Windows users need to change the permissions explicitly so that Windows doesn't
1714
# set the execute bit on all of your files which messes with GitHub users on Mac and Linux
18-
config.vm.synced_folder "./", "/vagrant", owner: "ubuntu", mount_options: ["dmode=755,fmode=644"]
15+
config.vm.synced_folder "./", "/vagrant", owner: "vagrant", mount_options: ["dmode=755,fmode=644"]
1916

2017
# Example for VirtualBox:
2118
config.vm.provider "virtualbox" do |vb|
@@ -43,7 +40,7 @@ Vagrant.configure(2) do |config|
4340
apt-get -y autoremove
4441
pip install --upgrade pip
4542
# Make vi look nice
46-
sudo -H -u ubuntu echo "colorscheme desert" > ~/.vimrc
43+
sudo -H -u vagrant echo "colorscheme desert" > ~/.vimrc
4744
# Install app dependencies
4845
echo "\n******************************"
4946
echo " Installing App Dependencies"
@@ -58,7 +55,7 @@ Vagrant.configure(2) do |config|
5855
config.vm.provision "shell", inline: <<-SHELL
5956
# Prepare Redis data share
6057
sudo mkdir -p /var/lib/redis/data
61-
sudo chown ubuntu:ubuntu /var/lib/redis/data
58+
sudo chown vagrant:vagrant /var/lib/redis/data
6259
SHELL
6360

6461
# Add Redis docker container

server.py

+28-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import os
3131
import sys
3232
import logging
33+
from functools import wraps
3334
from flask import Flask, jsonify, request, url_for, make_response, abort
3435
from models import Pet, DataValidationError
3536

@@ -93,6 +94,25 @@ def internal_server_error(error):
9394
app.logger.info(message)
9495
return jsonify(status=500, error='Internal Server Error', message=message), 500
9596

97+
######################################################################
98+
# DECORATORS
99+
######################################################################
100+
def requires_content_type(*content_types):
101+
""" Use this decorator to check content type """
102+
def decorator(func):
103+
""" Inner decorator """
104+
@wraps(func)
105+
def wrapper(*args, **kwargs):
106+
""" Checks that the content type is correct """
107+
for content_type in content_types:
108+
if request.headers['Content-Type'] == content_type:
109+
return func(*args, **kwargs)
110+
111+
app.logger.error('Invalid Content-Type: %s', request.headers['Content-Type'])
112+
abort(HTTP_415_UNSUPPORTED_MEDIA_TYPE,
113+
'Content-Type must be {}'.format(content_types))
114+
return wrapper
115+
return decorator
96116

97117
######################################################################
98118
# GET INDEX
@@ -143,14 +163,14 @@ def get_pets(pet_id):
143163
# ADD A NEW PET
144164
######################################################################
145165
@app.route('/pets', methods=['POST'])
166+
@requires_content_type('application/json', 'application/x-www-form-urlencoded')
146167
def create_pets():
147168
"""
148169
Creates a Pet
149170
150171
This endpoint will create a Pet based the data in the body that is posted
151172
or data that is sent via an html form post.
152173
"""
153-
check_content_type('application/json')
154174
data = {}
155175
# Check for form submission data
156176
if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded':
@@ -174,13 +194,13 @@ def create_pets():
174194
# UPDATE AN EXISTING PET
175195
######################################################################
176196
@app.route('/pets/<int:pet_id>', methods=['PUT'])
197+
@requires_content_type('application/json')
177198
def update_pets(pet_id):
178199
"""
179200
Update a Pet
180201
181202
This endpoint will update a Pet based the body that is posted
182203
"""
183-
check_content_type('application/json')
184204
pet = Pet.find(pet_id)
185205
if not pet:
186206
abort(HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id))
@@ -240,12 +260,12 @@ def data_reset():
240260
""" Removes all Pets from the database """
241261
Pet.remove_all()
242262

243-
def check_content_type(content_type):
244-
""" Checks that the media type is correct """
245-
if request.headers['Content-Type'] == content_type:
246-
return
247-
app.logger.error('Invalid Content-Type: %s', request.headers['Content-Type'])
248-
abort(HTTP_415_UNSUPPORTED_MEDIA_TYPE, 'Content-Type must be {}'.format(content_type))
263+
# def check_content_type(content_type):
264+
# """ Checks that the media type is correct """
265+
# if request.headers['Content-Type'] == content_type:
266+
# return
267+
# app.logger.error('Invalid Content-Type: %s', request.headers['Content-Type'])
268+
# abort(HTTP_415_UNSUPPORTED_MEDIA_TYPE, 'Content-Type must be {}'.format(content_type))
249269

250270
# @app.before_first_request
251271
def initialize_logging(log_level=logging.INFO):

0 commit comments

Comments
 (0)