Skip to content

Commit

Permalink
#14 create collection if user logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
Maziyar Boustani committed Oct 9, 2015
1 parent 6865de5 commit ae991c5
Showing 1 changed file with 17 additions and 96 deletions.
113 changes: 17 additions & 96 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import os
import json
#from werkzeug import secure_filename
#from geo_tika import file_to_text, create_json, extract_loc_name, loc_name_lat_lon

import cherrypy
import cgi
import tempfile

from girder.models.user import User
from girder.models.collection import Collection

from mako.template import Template
from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=['templates'])
Expand All @@ -15,103 +13,36 @@
UPLOAD_FOLDER = "./static/uploaded_files/"
CURRENT_DIR = os.getcwd()

GIRDER_USERNAME = "girder"
GIRDER_PASSWORD = "girder"

class myFieldStorage(cgi.FieldStorage):
"""Our version uses a named temporary file instead of the default
non-named file; keeping it visibile (named), allows us to create a
2nd link after the upload is done, thus avoiding the overhead of
making a copy to the destination filename."""

def make_file(self, binary=None):
return tempfile.NamedTemporaryFile()


def noBodyProcess():
"""Sets cherrypy.request.process_request_body = False, giving
us direct control of the file upload destination. By default
cherrypy loads it to memory, we are directing it to disk."""
cherrypy.request.process_request_body = False

cherrypy.tools.noBodyProcess = cherrypy.Tool('before_request_body', noBodyProcess)
GIRDER_COLLECTION = "GeoParser_Upload_Files"


def get_uploaded_files():
"""
This function returns list of uploaded files.
"""
list_of_uploaded_files = []
for item in os.listdir(UPLOAD_FOLDER):
if not item.startswith('.'):
list_of_uploaded_files.append(item)
print list_of_uploaded_files
return list_of_uploaded_files


class GeoParser(object):

@cherrypy.expose
def index(self):
"""Simplest possible HTML file upload form. Note that the encoding
type must be multipart/form-data."""

tmpl = lookup.get_template("index.html")
return tmpl.render(salutation="Hello", target="World")


@cherrypy.expose
@cherrypy.tools.noBodyProcess()
def upload(self, theFile=None):
"""upload action
We use our variation of cgi.FieldStorage to parse the MIME
encoded HTML form data containing the file."""

# the file transfer can take a long time; by default cherrypy
# limits responses to 300s; we increase it to 1h
cherrypy.response.timeout = 3600

# convert the header keys to lower case
lcHDRS = {}
for key, val in cherrypy.request.headers.iteritems():
lcHDRS[key.lower()] = val

# at this point we could limit the upload on content-length...
# incomingBytes = int(lcHDRS['content-length'])

# create our version of cgi.FieldStorage to parse the MIME encoded
# form data where the file is contained
formFields = myFieldStorage(fp=cherrypy.request.rfile,
headers=lcHDRS,
environ={'REQUEST_METHOD':'POST'},
keep_blank_values=True)

# we now create a 2nd link to the file, using the submitted
# filename; if we renamed, there would be a failure because
# the NamedTemporaryFile, used by our version of cgi.FieldStorage,
# explicitly deletes the original filename
theFile = formFields.list[0] # TODO: This is to be reviewed
os.link(theFile.file.name, UPLOAD_FOLDER + theFile.filename)

return "ok, got it filename='%s'" % theFile.filename


class Status:

exposed = True

def GET(self, status=None):
print status


class Search:
if __name__ == '__main__':

exposed = True
girderUser = User()
girderCollection = Collection()

def GET(self, search=None):
print search
currentUser = girderUser.getAdmins()
existinig_collections = [each['name'] for each in girderCollection.list()]

if not existinig_collections:
if not GIRDER_COLLECTION in existinig_collections: ##TODO: Check if any user already registered
girderCollection.createCollection(GIRDER_COLLECTION, currentUser)
else:
print "User not registered or logged in."

if __name__ == '__main__':

main_conf = {
'/': {
Expand All @@ -124,21 +55,11 @@ def GET(self, search=None):
}
}

status_conf = {'/':
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}

search_conf = {'/':
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}


cherrypy.server.max_request_body_size = 0
cherrypy.server.socket_timeout = 60

cherrypy.tree.mount(GeoParser(), '/', main_conf)
cherrypy.tree.mount(Status(), '/status', status_conf)
cherrypy.quickstart(Search(), '/search', search_conf)

cherrypy.config.update({'server.socket_host': '127.0.0.1',
'server.socket_port': 8080,
Expand Down

0 comments on commit ae991c5

Please sign in to comment.