Pyramid

From RHS Wiki
Revision as of 19:30, 13 February 2016 by Rafahsolis (talk | contribs)
Jump to navigation Jump to search

Pyramid with postgresql and SQLAlchemy
Requirements

  • Pip
  • PostgreSQL
  • Virtualenv (optional VirtualenvWrapper)
  • SQLAlchemy (sudo apt-get install python-dev && pip install SQLAlchemy)

Install

easy_install "pyramid==1.6.1" 

or

sudo apt-get install python-pyramid

Create proyect

pcreate --list # shows options
pcreate -s alchemy indeed

run development server

pserve development.ini

Postgresql database config

in production.ini and development.in set:

sqlalchemy.url = postgresql://user:password@host/indeed

Migrations

from project folder:

initialize_proyectname_db

Insert into db script

<script lang="python"> import sys from models import Rooms, DBSession from sqlalchemy import engine_from_config from pyramid.paster import get_appsettings, setup_logging

from sqlalchemy.orm import (

   scoped_session,
   sessionmaker,
   )

from zope.sqlalchemy import ZopeTransactionExtension

config_uri = '../development.ini' setup_logging(config_uri) engine = engine_from_config(get_appsettings(config_uri), 'sqlalchemy.')

Session = sessionmaker(bind=engine) session = Session()

try:

   a = int(sys.argv[1])

except:

   print "Parametro erroneo, debe especificar id numerico", sys.argv
   exit()

new = Rooms(id=a)

try:

   session.add(new)
   session.commit()
   print "Insertada Room", new.id

except Exception as e:

   print "Error insertando Room {romid}: {error}".format(romid=new.id, error=e)

finally:

   session.close()
</script>