def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ if os.environ.get('OPENSHIFT_APP_NAME', None): url = u"postgresql+psycopg2://%(username)s:%(password)s@%(host)s:%(port)s/%(database)s" % { 'username': os.environ['OPENSHIFT_DB_USERNAME'], 'password': os.environ['OPENSHIFT_DB_PASSWORD'], 'host': os.environ['OPENSHIFT_DB_HOST'], 'port': os.environ['OPENSHIFT_DB_PORT'], 'database': os.environ['OPENSHIFT_APP_NAME'] } engine = create_engine(url) else: engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) Base.metadata.bind = engine Base.metadata.create_all(engine) authn_policy = AuthTktAuthenticationPolicy('pc0lalugs0secret', callback=groupfinder) authz_policy = ACLAuthorizationPolicy() session_factory = session_factory_from_settings(settings) data_dir = get_data_dir() settings['upload_dir'] = os.path.join(data_dir, 'uploads') config = Configurator(settings=settings) config = Configurator( settings=settings, authentication_policy=authn_policy, authorization_policy=authz_policy, session_factory=session_factory ) renderer = jinja2_renderer_factory(default_templates='deform_jinja2:uni_templates', translator=PyramidTranslator() ) deform.Form.set_default_renderer(renderer) config.registry.registerUtility(DBSession, IDBSession) config.include('pyramid_mailer') config.include('horus') config.add_view('horus.views.AuthController', attr='login', route_name='horus_login', renderer='pcolalug:templates/login.jinja2') config.add_view('horus.views.ForgotPasswordController', attr='forgot_password', route_name='horus_forgot_password', renderer='pcolalug:templates/forgot_password.jinja2') config.add_view('horus.views.ForgotPasswordController', attr='reset_password', route_name='horus_reset_password', renderer='pcolalug:templates/reset_password.jinja2') config.add_view('horus.views.RegisterController', attr='register', route_name='horus_register', renderer='pcolalug:templates/register.jinja2') config.add_view('horus.views.ProfileController', attr='profile', route_name='horus_profile', renderer='pcolalug:templates/profile.jinja2', permission='access_user') override_forms = [ IHorusLoginForm, IHorusRegisterForm, IHorusForgotPasswordForm, IHorusResetPasswordForm, IHorusProfileForm ] for form in override_forms: config.registry.registerUtility(UNIForm, form) config.registry.registerUtility(LUGProfileSchema, IHorusProfileSchema) config.add_static_view('static', 'pcolalug:static', cache_max_age=3600) config.add_static_view('data', get_data_dir(), cache_max_age=3600) config.add_route('index', '/') config.add_route('contact', '/contact') config.add_route('calendar', '/calendar') config.add_route('admin', '/admin') config.add_route('presentations', '/presentations') config.add_route('view_presentation', '/presentations/{pk}') config.add_route('add_presentation', '/add/presentation') # config.add_route('admin_profile', '/profile/{id}') config.add_subscriber(handle_profile_group, ProfileUpdatedEvent) config.scan() return config.make_wsgi_app()
def presentation(request): pk = request.matchdict.get('pk') presentation = DBSession.query(Presentation).get(pk) return {'presentation': presentation}
def add_presentation(request): schema = PresentationSchema() schema = schema.bind(request=request) form = UNIForm(schema=schema) if request.method == 'GET': return {'form': form.render()} elif request.method == 'POST': try: controls = request.POST.items() data = form.validate(controls) except deform.ValidationFailure, e: return {'form': e.render(), 'errors': e.error.children} presentation = Presentation() presentation.name = data['Name'] presentation.description = data['Description'] presentation.date = data['Date'] presentation.presenter_pk = int(data['Presenter']) DBSession.add(presentation) # data is the posted values from the profile form, was a Photo # selected? if data['File']: # we are going to store this photo as <id>-profile in the upload_dir filename = data['File']['filename'] name_without_ext, ext = os.path.splitext(filename) mimetype = data['File']['mimetype'] uid = str(uuid.uuid4()) uid = "%s%s" % (uid, ext) size = data['File']['size'] input_file = data['File']['fp'] upload_dir = request.registry.settings['upload_dir'] if not os.path.exists(upload_dir): os.makedirs(upload_dir) file_path = os.path.join(upload_dir, uid) output_file = open(file_path, 'wb') input_file.seek(0) # read in all the bytes from the posted file and write them to disk while 1: file_data = input_file.read(2<<16) if not file_data: break output_file.write(file_data) output_file.close() # if the user already has a profile photo model defined, just re-use it # and delete the current file if presentation.file: f = presentation.file try: os.remove(os.path.join(upload_dir, f.uid)) except OSError: pass DBSession.delete(f) new_file = File(user_pk=request.user.pk) new_file.mimetype = mimetype new_file.uid = uid new_file.size = size new_file.filename = filename presentation.file = new_file DBSession.add(new_file) request.session.flash('Presentation successfully created.', 'success') return HTTPFound(location=request.route_url('presentations'))
def presentations(request): presentations = DBSession.query(Presentation).all() return {'presentations': presentations}