From 6a851c384b9f71c4b010856c6330e8e67a353568 Mon Sep 17 00:00:00 2001 From: josch Date: Sun, 17 Aug 2008 20:44:42 +0000 Subject: [PATCH] new file upload controller, new upload template, new utils lib, some elixir basics git-svn-id: http://yolanda.mister-muffin.de/svn@373 7eef14d0-6ed0-489d-bf55-20463b2d70db --- trunk/yolanda/config/environment.py | 6 ++++++ trunk/yolanda/controllers/upload.py | 23 +++++++++++++++++++++++ trunk/yolanda/lib/base.py | 7 ++++++- trunk/yolanda/lib/utils.py | 8 ++++++++ trunk/yolanda/model/__init__.py | 21 +++++++++++++++++++++ trunk/yolanda/model/entities.py | 8 ++++++++ trunk/yolanda/templates/xhtml/upload.mako | 17 +++++++++++++++++ trunk/yolanda/websetup.py | 7 +++++++ 8 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 trunk/yolanda/controllers/upload.py create mode 100644 trunk/yolanda/lib/utils.py create mode 100644 trunk/yolanda/model/entities.py create mode 100644 trunk/yolanda/templates/xhtml/upload.mako diff --git a/trunk/yolanda/config/environment.py b/trunk/yolanda/config/environment.py index efd35df..2c606da 100644 --- a/trunk/yolanda/config/environment.py +++ b/trunk/yolanda/config/environment.py @@ -7,6 +7,9 @@ import yolanda.lib.app_globals as app_globals import yolanda.lib.helpers from yolanda.config.routing import make_map +from sqlalchemy import engine_from_config +from yolanda.model import init_model + def load_environment(global_conf, app_conf): """Configure the Pylons environment via the ``pylons.config`` object @@ -31,3 +34,6 @@ def load_environment(global_conf, app_conf): # CONFIGURATION OPTIONS HERE (note: all config options will override # any Pylons config options) + + engine = engine_from_config(config, 'sqlalchemy.') + init_model(engine) diff --git a/trunk/yolanda/controllers/upload.py b/trunk/yolanda/controllers/upload.py new file mode 100644 index 0000000..4f96adc --- /dev/null +++ b/trunk/yolanda/controllers/upload.py @@ -0,0 +1,23 @@ +import logging + +from yolanda.lib.base import * + +log = logging.getLogger(__name__) + +class UploadController(BaseController): + + def index(self): + return render('/xhtml/upload.mako') + + def upload(self): + myfile = request.params['file'] + permanent_file = open(os.path.join( + myfile.filename.lstrip(os.sep)), + 'w') + + u.copyfileobj(myfile.file, permanent_file) + myfile.file.close() + permanent_file.close() + + return 'Successfully uploaded: %s'%myfile.filename + diff --git a/trunk/yolanda/lib/base.py b/trunk/yolanda/lib/base.py index 9b12a78..e4a034a 100644 --- a/trunk/yolanda/lib/base.py +++ b/trunk/yolanda/lib/base.py @@ -11,7 +11,9 @@ from pylons.i18n import _, ungettext, N_ from pylons.templating import render import yolanda.lib.helpers as h +import yolanda.lib.utils as u import yolanda.model as model +import os class BaseController(WSGIController): @@ -21,7 +23,10 @@ class BaseController(WSGIController): # the request is routed to. This routing information is # available in environ['pylons.routes_dict'] response.headers['Content-type'] = "application/xml" - return WSGIController.__call__(self, environ, start_response) + try: + return WSGIController.__call__(self, environ, start_response) + finally: + Session.remove() # Include the '_' function in the public names __all__ = [__name for __name in locals().keys() if not __name.startswith('_') \ diff --git a/trunk/yolanda/lib/utils.py b/trunk/yolanda/lib/utils.py new file mode 100644 index 0000000..18b1d27 --- /dev/null +++ b/trunk/yolanda/lib/utils.py @@ -0,0 +1,8 @@ +#borrowed from shutil to minimize dependencies +def copyfileobj(fsrc, fdst, length=16*1024): + """copy data from file-like object fsrc to file-like object fdst""" + while 1: + buf = fsrc.read(length) + if not buf: + break + fdst.write(buf) diff --git a/trunk/yolanda/model/__init__.py b/trunk/yolanda/model/__init__.py index e69de29..2c524b7 100644 --- a/trunk/yolanda/model/__init__.py +++ b/trunk/yolanda/model/__init__.py @@ -0,0 +1,21 @@ +from sqlalchemy.orm import scoped_session, sessionmaker +import elixir + +# replace the elixir session with our own +Session = scoped_session(sessionmaker(autoflush=True, transactional=True)) +elixir.session = Session +elixir.options_defaults.update({ + 'shortnames': True +}) + +# use the elixir metadata +metadata = elixir.metadata + +# this will be called in config/environment.py +def init_model(engine): + metadata.bind = engine + +# import your entities, and set them up +from entities import * +elixir.setup_all() + diff --git a/trunk/yolanda/model/entities.py b/trunk/yolanda/model/entities.py new file mode 100644 index 0000000..117d1db --- /dev/null +++ b/trunk/yolanda/model/entities.py @@ -0,0 +1,8 @@ +from elixir.entity import * +from elixir.fields import * +from sqlalchemy.types import * +from datetime import datetime + +class Person(Entity): + name = Field(Unicode(128)) + birthdate = Field(DateTime, default=datetime.now) diff --git a/trunk/yolanda/templates/xhtml/upload.mako b/trunk/yolanda/templates/xhtml/upload.mako new file mode 100644 index 0000000..59f177d --- /dev/null +++ b/trunk/yolanda/templates/xhtml/upload.mako @@ -0,0 +1,17 @@ +<%inherit file="base.mako"/> + +<%def name="title()"> + front page + + +<%def name="heading()"> + Upload Video + + + + ${h.form(h.url_for(action='upload'), multipart=True)} + ${h.file_field('file')}
+ ${h.text_field('name')}
+ ${h.submit('Upload')} + + ${h.end_form()} diff --git a/trunk/yolanda/websetup.py b/trunk/yolanda/websetup.py index 012683e..a60da37 100644 --- a/trunk/yolanda/websetup.py +++ b/trunk/yolanda/websetup.py @@ -6,9 +6,16 @@ from pylons import config from yolanda.config.environment import load_environment +from yolanda.model import metadata + log = logging.getLogger(__name__) def setup_config(command, filename, section, vars): """Place any commands to setup yolanda here""" conf = appconfig('config:' + filename) load_environment(conf.global_conf, conf.local_conf) + + log.info("Creating tables") + metadata.create_all() + log.info("Successfully setup") +