Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pymongo settings upgrade #19

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,105 @@
settings_local.py
*.pyc
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msm
*.msp
*.lnk
*~
.fuse_hidden*
.directory
.Trash-*
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
*.manifest
*.spec
pip-log.txt
pip-delete-this-directory.txt
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
*.mo
*.pot
*.log
local_settings.py
instance/
.webassets-cache
.scrapy
docs/_build/
target/
.ipynb_checkpoints
.python-version
celerybeat-schedule
.env
venv/
ENV/
.spyderproject
.ropeproject
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
.idea/gradle.xml
.idea/libraries
.idea/mongoSettings.xml
*.iws
/out/
.idea_modules/
atlassian-ide-plugin.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
*.DS_Store
.AppleDouble
.LSOverride
Icon
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
.idea/
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:2.7

WORKDIR app
ADD requirements.txt requirements.txt
RUN pip install uwsgi
RUN pip install -r requirements.txt
COPY . .
CMD uwsgi --socket=:8001 --module=wsgi:app
17 changes: 13 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ def serve_partial_file_request(request, headers, file, start, end, callbacks=[])
headers=headers, status=206)


def serve_request(request, connection, _id=None):
def serve_request(request, connection, _id=None, filename=None):
fs = GridFS(connection[settings.MONGO_DB_NAME])
try:
file = fs.get(_id)
except:
if _id:
try:
file = fs.get(_id)
except:
abort(404)
elif filename:
try:
file = fs.get_last_version(filename)
except:
abort(404)
else:
abort(404)

if getattr(file, 'pending', False):
Expand Down Expand Up @@ -91,6 +99,7 @@ def serve_request(request, connection, _id=None):

url_map = Map([
Rule('/<ObjectId:_id>'),
Rule('/<path:filename>'),
], converters={
'ObjectId': ObjectIdConverter,
})
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Werkzeug==0.8.2
pymongo==2.4.2
Werkzeug==0.11.11
pymongo==3.4.0
WebTest==2.0.9
Unidecode==0.04.9
Unidecode==0.04.19
26 changes: 16 additions & 10 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@


# Mongo section
MONGO_HOST = 'localhost'
MONGO_PORT = 27017
MONGO_DB_NAME = 'grid_fs'
MONGO_REPLICATION_ON = True
MONGO_REPLICA_SET_URI = 'localhost:27017,localhost:27018'
MONGO_REPLICA_SET_NAME = 'test_set'
MONGO_READ_PREFERENCE = ReadPreference.PRIMARY_PREFERRED
# MONGO_HOST = 'localhost'
# MONGO_PORT = 27017
# MONGO_DB_NAME = 'grid_fs'

# MONGO_REPLICATION_ON = True
# MONGO_REPLICA_SET_URI = 'localhost:27017,localhost:27018'
# MONGO_REPLICA_SET_NAME = 'test_set'
# MONGO_READ_PREFERENCE = ReadPreference.PRIMARY_PREFERRED

# MONGO_PASSWORD = None
# MONGO_USER = None


MONGO_DB_URL = 'mongodb://localhost:27017,localhost:27018/grid_fs?' \
'replicaSet=test_set&readPreference=primaryPreferred'

# App section for builtin server
APP_HOST = '0.0.0.0'
APP_PORT = 5000
DEBUG = True


try:
from settings_local import *
except ImportError:
pass


if 'test' in sys.argv[0]: # Is there another way?
if 'test' in sys.argv[0]: # Is there another way?
try:
from settings_test import *
except ImportError:
Expand Down
3 changes: 1 addition & 2 deletions settings_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DEBUG = True

MONGO_DB_NAME = 'grid_fs_test'
MONGO_REPLICATION_ON = False
MONGO_DB_URL = 'mongodb://localhost:27017/grid_fs_test'
7 changes: 6 additions & 1 deletion tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def setUpClass(cls):

def put_file(self, path):
f = open(path, 'rb')
filename = os.path.basename(path)

filename = os.path.relpath(path)
return self.fs.put(f.read(), filename=filename)

def get_file(self, _id, headers={}):
Expand All @@ -38,6 +39,10 @@ def test(self):
content = StringIO(r._app_iter[0])
self.assertEquals(open(file_path).read(), content.read())

r = self.app.get('/%s' % os.path.relpath(file_path))
content = StringIO(r._app_iter[0])
self.assertEquals(open(file_path).read(), content.read())

self.app.get('/%s/' % file_id, status=404)

def test_range(self):
Expand Down
9 changes: 1 addition & 8 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ def next(self):


def get_mongodb_connection():
if settings.MONGO_REPLICATION_ON:
return MongoReplicaSetClient(
settings.MONGO_REPLICA_SET_URI,
read_preference=settings.MONGO_READ_PREFERENCE,
replicaset=settings.MONGO_REPLICA_SET_NAME, w=1)
else:
return MongoClient(settings.MONGO_HOST, settings.MONGO_PORT)

return MongoClient(settings.MONGO_DB_URL)


class MongoDBConnection(object):
Expand Down