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
This commit is contained in:
parent
124c37f6f8
commit
6a851c384b
8 changed files with 96 additions and 1 deletions
|
@ -7,6 +7,9 @@ import yolanda.lib.app_globals as app_globals
|
||||||
import yolanda.lib.helpers
|
import yolanda.lib.helpers
|
||||||
from yolanda.config.routing import make_map
|
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):
|
def load_environment(global_conf, app_conf):
|
||||||
"""Configure the Pylons environment via the ``pylons.config``
|
"""Configure the Pylons environment via the ``pylons.config``
|
||||||
object
|
object
|
||||||
|
@ -31,3 +34,6 @@ def load_environment(global_conf, app_conf):
|
||||||
|
|
||||||
# CONFIGURATION OPTIONS HERE (note: all config options will override
|
# CONFIGURATION OPTIONS HERE (note: all config options will override
|
||||||
# any Pylons config options)
|
# any Pylons config options)
|
||||||
|
|
||||||
|
engine = engine_from_config(config, 'sqlalchemy.')
|
||||||
|
init_model(engine)
|
||||||
|
|
23
trunk/yolanda/controllers/upload.py
Normal file
23
trunk/yolanda/controllers/upload.py
Normal file
|
@ -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
|
||||||
|
|
|
@ -11,7 +11,9 @@ from pylons.i18n import _, ungettext, N_
|
||||||
from pylons.templating import render
|
from pylons.templating import render
|
||||||
|
|
||||||
import yolanda.lib.helpers as h
|
import yolanda.lib.helpers as h
|
||||||
|
import yolanda.lib.utils as u
|
||||||
import yolanda.model as model
|
import yolanda.model as model
|
||||||
|
import os
|
||||||
|
|
||||||
class BaseController(WSGIController):
|
class BaseController(WSGIController):
|
||||||
|
|
||||||
|
@ -21,7 +23,10 @@ class BaseController(WSGIController):
|
||||||
# the request is routed to. This routing information is
|
# the request is routed to. This routing information is
|
||||||
# available in environ['pylons.routes_dict']
|
# available in environ['pylons.routes_dict']
|
||||||
response.headers['Content-type'] = "application/xml"
|
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
|
# Include the '_' function in the public names
|
||||||
__all__ = [__name for __name in locals().keys() if not __name.startswith('_') \
|
__all__ = [__name for __name in locals().keys() if not __name.startswith('_') \
|
||||||
|
|
8
trunk/yolanda/lib/utils.py
Normal file
8
trunk/yolanda/lib/utils.py
Normal file
|
@ -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)
|
|
@ -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()
|
||||||
|
|
8
trunk/yolanda/model/entities.py
Normal file
8
trunk/yolanda/model/entities.py
Normal file
|
@ -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)
|
17
trunk/yolanda/templates/xhtml/upload.mako
Normal file
17
trunk/yolanda/templates/xhtml/upload.mako
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<%inherit file="base.mako"/>
|
||||||
|
|
||||||
|
<%def name="title()">
|
||||||
|
front page
|
||||||
|
</%def>
|
||||||
|
|
||||||
|
<%def name="heading()">
|
||||||
|
Upload Video
|
||||||
|
</%def>
|
||||||
|
|
||||||
|
|
||||||
|
${h.form(h.url_for(action='upload'), multipart=True)}
|
||||||
|
${h.file_field('file')}<br />
|
||||||
|
${h.text_field('name')}<br />
|
||||||
|
${h.submit('Upload')}
|
||||||
|
|
||||||
|
${h.end_form()}
|
|
@ -6,9 +6,16 @@ from pylons import config
|
||||||
|
|
||||||
from yolanda.config.environment import load_environment
|
from yolanda.config.environment import load_environment
|
||||||
|
|
||||||
|
from yolanda.model import metadata
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
def setup_config(command, filename, section, vars):
|
def setup_config(command, filename, section, vars):
|
||||||
"""Place any commands to setup yolanda here"""
|
"""Place any commands to setup yolanda here"""
|
||||||
conf = appconfig('config:' + filename)
|
conf = appconfig('config:' + filename)
|
||||||
load_environment(conf.global_conf, conf.local_conf)
|
load_environment(conf.global_conf, conf.local_conf)
|
||||||
|
|
||||||
|
log.info("Creating tables")
|
||||||
|
metadata.create_all()
|
||||||
|
log.info("Successfully setup")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue