Skip to content

Instantly share code, notes, and snippets.

@JoeyCodinja
Created March 22, 2016 23:53
Show Gist options
  • Save JoeyCodinja/e00c16ce400da7627668 to your computer and use it in GitHub Desktop.
Save JoeyCodinja/e00c16ce400da7627668 to your computer and use it in GitHub Desktop.
Not able to view Tables from SQLAlchemy Mapping with Pyramid_SACRUD
from pyramid.config import Configurator
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid_redis_sessions import session_factory_from_settings
from pyramid.security import (
Allow,
Authenticated,
ALL_PERMISSIONS,
)
from pyramid_sacrud import PYRAMID_SACRUD_HOME
from sqlalchemy import engine_from_config, MetaData
from sqlalchemy.ext.automap import automap_base
import transaction
from .models import *
from .urls import setup_paths
class Root(object):
__acl__ = [
(Allow, Authenticated, 'book'),
(Allow, Authenticated, 'reschedule'),
(Allow, Authenticated, 'cancel'),
(Allow, 'timingq:customer', 'search_company'),
(Allow, 'timingq:company','search_customer'),
(Allow, 'timingq:customer','get_available_times'),
(Allow, 'timingq:admin', 'administrate'),
(Allow, 'timingq:admin', 'manage'),
(Allow, 'timingq:admin', ALL_PERMISSIONS)
# (Allow, 'company', ''),
# (Allow, 'company', '')
]
def __init__(self, request):
self.request = request
def groupfinder(user_id, request):
company_id_search = [entities for entities in
DBSession.query(Company.CompanyID).filter(Company.CompanyID == user_id)]
customer_id_search = [entities for entities in
DBSession.query(Customer.CustomerID).filter(Customer.CustomerID == user_id)]
transaction.commit()
if len(company_id_search) > 0:
return ['timingq:company', 'timingq:admin']
elif len(customer_id_search) > 0:
return ['timingq:customer']
else:
return []
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
initialize_sql(engine)
config = Configurator(settings=settings)
setup_paths(config)
#Pyramid SystemAdminstration Dashboard
pyramid_sacrud_settings = config.registry.settings
pyramid_sacrud_settings['pyramid_sacrud.models'] = (('Customer', [Customer]),
('Company', [Company]),
)
config.include('ps_alchemy')
config.include('pyramid_sacrud')
# Session Factory
session_factory = session_factory_from_settings(settings)
config.set_session_factory(session_factory)
# Authentication & Authorization Policies
authenti_policy = AuthTktAuthenticationPolicy('secret',
hashalg='sha512',
callback=groupfinder)
authori_policy = ACLAuthorizationPolicy()
config.set_authentication_policy(authenti_policy)
config.set_authorization_policy(authori_policy)
# Root factory
config.set_root_factory(Root)
return config.make_wsgi_app()
import logging
import transaction
from sqlalchemy import (
Table,
MetaData,
)
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
mapper,
)
from zope.sqlalchemy import ZopeTransactionExtension
log = logging.getLogger(__name__)
DBSession = scoped_session(
sessionmaker(extension=ZopeTransactionExtension()))
metadata = MetaData()
class Company(Table):
__tablename__ = 'company'
class Customer(Table):
__tablename__ = 'customer'
class Appointments(Table):
__tablename__ = 'queuemaster'
class CustomerSMSDetails(Table):
__tablename__ = 'custphone'
class CompanySMSDetails(Table):
__tablename__ = 'compphone'
class Personnel(Table):
__tablename__ = 'personnel'
class OperatingHours(Table):
__tablename__ = 'operhours'
class Services(Table):
__tablename__ = 'servicetype'
class Stations(Table):
__tablename__ = 'stationmast'
class Authentication(Table):
__tablename__ = 'useraccess'
def map_tables(to_reflect):
for _class in to_reflect:
# log.info('Reflecting {0} from table {1}'
# .format(_class, _class.__tablename__))
table = Table(_class.__tablename__, metadata, autoload=True)
mapper(_class, table)
def initialize_sql(engine):
DBSession.configure(bind=engine)
metadata.bind = engine
to_reflect = (
Company,
Customer,
Appointments,
Authentication,
Personnel,
OperatingHours,
Services,
Stations,
CustomerSMSDetails,
CompanySMSDetails
)
map_tables(to_reflect)
@uralbash
Copy link

Please try to replace it

pyramid_sacrud_settings['pyramid_sacrud.models'] = (('Customer', [Customer]),
                                                    ('Company', [Company]),
                                                    )

to

pyramid_sacrud_settings['pyramid_sacrud.models'] = (('Customer', [Customer, ]),
                                                    ('Company', [Company, ]),
                                                    )

and I have a similar example, I think it will help https://gist.github.com/uralbash/019c0629e1448c9d4e71

If still does not work, please lay out a complete code example that I could run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment