pyramid

Pyramid authorization for stored items

烂漫一生 提交于 2019-11-28 17:56:52
I'm trying to create an authorization policy that takes "item" ownership into account. For example some user X "owns" items A, B, C. Those are accessed via URLs like /item/{item}/some_options . How can I get the information about {item} to the authorization policy object (permits() call)? Is putting additional information into context a good idea (I'm doing routes-based routing only). How would I do that? You can do this using the ACLAuthorizationPolicy combined with URL Dispatch by using a custom resource tree designed for this purpose. For example, you have permissions for Foo objects, and

Count of related records in many-to-many relationship

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 10:57:29
问题 I am trying to buit a classmethod, which returns number of members associated with a project. I tried: # method of class Project @classmethod def member_count(cls, project_id): return Session.query(ProjectMember).\ filter(ProjectMember.project_id==project_id).count() The many-to-many relationship is defined as: class Member(Base): __tablename__ = 'member' id = Column(Integer, primary_key=True) login = Column(String(50), unique=True, nullable=False) project_list = relationship("ProjectMember",

Using alias() for 'select as' in SQLAlchemy

瘦欲@ 提交于 2019-11-28 10:42:58
Let's say I have a table 'shares' with the following columns: company price quantity Microsoft 100 10 Google 99 5 Google 99 20 Google 101 15 I'd like to run the equivalent of a SQL statement like this: select price, sum(quantity) as num from shares where company='Google' group by price; The closest I've come is: result = dbsession.query(Shares.price, func.sum(Shares.quantity)).filter(Shares.company== 'Google').group_by(Shares.price).all() I'm having trouble with setting up the 'sum(quantity) as num' in sqlalchemy. It appears I need to use alias() but I can't figure out how by looking at the

sqlalchemy existing database query

折月煮酒 提交于 2019-11-28 06:02:40
I am using SQLAlchemy as ORM for a python project. I have created few models/schema and it is working fine. Now I need to query a existing MySQL database, no insert/update just the select statement. How can I create a wrapper around the tables of this existing database? I have briefly gone through the sqlalchemy docs and SO but couldn't find anything relevant. All suggest execute method, where I need to write the raw sql queries, while I want to use the SQLAlchemy query method in same way as I am using with the SA models. For example if the existing db has table name User then I want to query

Check if a user has a permission in pyramid (pylons 2)?

半世苍凉 提交于 2019-11-28 05:49:49
问题 How do i check if a user has a permission in pyramid. For example, I want to show some HTML only if a user has some permission, but have the view available for everybody. 回答1: The usual method is: from pyramid.security import has_permission has_permission('view', someresource, request) See also http://docs.pylonsproject.org/projects/pyramid/1.0/narr/security.html#debugging-imperative-authorization-failures and http://docs.pylonsproject.org/projects/pyramid/1.0/api/security.html#pyramid

Make sure only one worker launches the apscheduler event in a pyramid web app running multiple workers

本秂侑毒 提交于 2019-11-28 05:01:09
We have a web app made with pyramid and served through gunicorn+nginx. It works with 8 worker threads/processes We needed to jobs, we have chosen apscheduler. here is how we launch it from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR from apscheduler.scheduler import Scheduler rerun_monitor = Scheduler() rerun_monitor.start() rerun_monitor.add_interval_job(job_to_be_run,\ seconds=JOB_INTERVAL) The issue is that all the worker processes of gunicorn pick the scheduler up. We tried implementing a file lock but it does not seem like a good enough solution. What would be the best

How to set file name in response

╄→гoц情女王★ 提交于 2019-11-28 00:51:44
问题 I know about content-disposition but I read what it uses for email messages. And I want to know how I can set file name with content-type. ps I use Pyramid framework edit: Web site has button 'download' how to perform Response object for file name too, like return Response(body=f.read(), content_type='application/octet-stream') and what I need to do for showing correct file name in browser. 回答1: You need to set the filename parameter of the Content-Disposition header like so: response.content

Pyramid authorization for stored items

◇◆丶佛笑我妖孽 提交于 2019-11-27 10:57:26
问题 I'm trying to create an authorization policy that takes "item" ownership into account. For example some user X "owns" items A, B, C. Those are accessed via URLs like /item/{item}/some_options . How can I get the information about {item} to the authorization policy object (permits() call)? Is putting additional information into context a good idea (I'm doing routes-based routing only). How would I do that? 回答1: You can do this using the ACLAuthorizationPolicy combined with URL Dispatch by

Using alias() for 'select as' in SQLAlchemy

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 03:44:44
问题 Let's say I have a table 'shares' with the following columns: company price quantity Microsoft 100 10 Google 99 5 Google 99 20 Google 101 15 I'd like to run the equivalent of a SQL statement like this: select price, sum(quantity) as num from shares where company='Google' group by price; The closest I've come is: result = (dbsession.query(Shares.price, func.sum(Shares.quantity)) .filter(Shares.company == 'Google') .group_by(Shares.price) .all()) I'm having trouble with setting up the 'sum

sqlalchemy existing database query

a 夏天 提交于 2019-11-27 01:02:57
问题 I am using SQLAlchemy as ORM for a python project. I have created few models/schema and it is working fine. Now I need to query a existing MySQL database, no insert/update just the select statement. How can I create a wrapper around the tables of this existing database? I have briefly gone through the sqlalchemy docs and SO but couldn't find anything relevant. All suggest execute method, where I need to write the raw sql queries, while I want to use the SQLAlchemy query method in same way as