]>
git.datanom.net - pwp.git/blob - app/tools.py
1 from .models
import User
, Portfolio
, Album
, Photo
, AccessRight
, Role
, MyAnonymous
3 #from flask_login import mixins
5 class DBQueryException(Exception):
10 def get_albums(self
, object):
12 if hasattr(object, '__tablename__'):
13 if isinstance(object, User
):
14 portfolios
= Portfolio
.query(user
=object.id)
16 albums
.extend(Album
.query
.filter_by(portfolio
=p
))
17 elif isinstance(object, Portfolio
):
18 albums
= Album
.query(portfolio
=object.id)
19 elif isinstance(object, Photo
):
20 albums
.append(object.album
)
22 raise DBQueryException("%s: not supported" % object.__tablename
__)
28 raise DBQueryException("{}: not supported".format(object))
30 def get_portfolios(self
, object):
32 if hasattr(object, '__tablename__'):
33 if isinstance(object, User
):
34 portfolios
= Portfolio
.query(user
=object.id)
35 elif isinstance(object, Album
):
36 portfolios
.append(object.portfolio
)
37 elif isinstance(object, Photo
):
38 album
= self
.get_albums(object)
39 portfolios
.append(album
[0].portfolio
)
41 raise DBQueryException("%s: not supported" % object.__tablename
__)
43 for portfolio
in portfolios
:
44 result
.append(portfolio
)
47 raise DBQueryException("{}: not supported".format(object))
49 def get_owner(self
, object):
50 if hasattr(object, '__tablename__'):
51 if isinstance(object, Portfolio
):
53 elif isinstance(object, Album
):
56 elif isinstance(object, Photo
):
61 raise DBQueryException("%s: not supported" % object.__tablename
__)
63 raise DBQueryException("{}: not supported".format(object))
65 def get_users(self
, object):
66 if hasattr(object, '__tablename__'):
68 if isinstance(object, Album
):
69 # TODO: get rit of query.join
70 objects
= AccessRight
.query
.join(AccessRight
.album
).filter(Album
.id == object.id)
73 elif isinstance(object, Photo
):
74 objects
= AccessRight
.query
.join(AccessRight
.photo
).filter(Photo
.id == object.id)
77 elif isinstance(object, Portfolio
):
78 objects
= AccessRight
.query
.join(AccessRight
.portfolio
).filter(Portfolio
.id == object.id)
82 raise DBQueryException("%s: not supported" % object.__tablename
__)
85 raise DBQueryException("{}: not supported".format(object))
87 def has_access(self
, user
, object):
88 #if hasattr(user, '__tablename__') and hasattr(object, '__tablename__') and isinstance(user, User):
89 if hasattr(object, '__tablename__'):
91 if isinstance(object, Portfolio
):
92 acl
= self
.get_acl(user
, object)
95 elif isinstance(object, Album
):
96 acl
= self
.get_acl(user
, object)
100 portfolio
= self
.get_portfolios(object)
101 acl
= self
.get_acl(user
, portfolio
[0])
104 elif isinstance(object, Photo
):
105 acl
= self
.get_acl(user
, object)
109 albums
= self
.get_albums(object)
110 acl
= self
.get_acl(user
, albums
[0])
114 portfolio
= self
.get_portfolios(object)
115 acl
= self
.get_acl(user
, portfolio
[0])
119 raise DBQueryException("%s: not supported" % object.__tablename
__)
122 raise DBQueryException("{}: not supported".format(object))
124 def can_read(self
, user
, object):
125 return self
.has_access(user
, object)
127 def can_write(self
, user
, object):
128 #if hasattr(user, '__tablename__') and hasattr(object, '__tablename__') and isinstance(user, User):
129 if hasattr(object, '__tablename__'):
131 if isinstance(object, Portfolio
):
132 acl
= self
.get_acl(user
, object)
133 if acl
is not None and acl
> Role
.read
:
135 elif isinstance(object, Album
):
136 acl
= self
.get_acl(user
, object)
137 if acl
is not None and acl
> Role
.read
:
140 portfolio
= self
.get_portfolios(object)
141 acl
= self
.get_acl(user
, portfolio
[0])
142 if acl
is not None and acl
> Role
.read
:
144 elif isinstance(object, Photo
):
145 acl
= self
.get_acl(user
, object)
146 if acl
is not None and acl
> Role
.read
:
149 albums
= self
.get_albums(object)
150 acl
= self
.get_acl(user
, albums
[0])
151 if acl
is not None and acl
> Role
.read
:
154 portfolio
= self
.get_portfolios(object)
155 acl
= self
.get_acl(user
, portfolio
[0])
156 if acl
is not None and acl
> Role
.read
:
159 raise DBQueryException("%s: not supported" % object.__tablename
__)
162 raise DBQueryException("{}: not supported".format(object))
164 def get_acl(self
, user
, object):
165 #if hasattr(user, '__tablename__') and hasattr(object, '__tablename__') and isinstance(user, User):
166 if hasattr(object, '__tablename__'):
167 if isinstance(object, Album
):
168 if user
.is_admin
or user
== self
.get_owner(object):
170 acl
= AccessRight
.query
.join(AccessRight
.album
).filter(Album
.id == object.id)
172 if AccessRight
.query
.get(a
.id).user_id
== user
.id:
174 if object.visible
and object.public
:
176 # Check if access given by parent (Portfolio)
177 return self
.get_acl(user
, object.portfolio
)
178 elif isinstance(object, Photo
):
179 if user
.is_admin
or user
== self
.get_owner(object):
181 acl
= AccessRight
.query
.join(AccessRight
.photo
).filter(Photo
.id == object.id)
183 if AccessRight
.query
.get(a
.id).user_id
== user
.id:
185 if user
.is_anonymous
and object.visible
and object.public
:
187 # Check if access given by parent (Album)
188 return self
.get_acl(user
, object.album
)
189 elif isinstance(object, Portfolio
):
190 if user
.is_admin
or user
== self
.get_owner(object):
192 acl
= AccessRight
.query
.join(AccessRight
.portfolio
).filter(Portfolio
.id == object.id)
194 if AccessRight
.query
.get(a
.id).user_id
== user
.id:
196 if user
.is_anonymous
and object.visible
and object.public
:
199 raise DBQueryException("%s: not supported" % object.__tablename
__)
202 raise DBQueryException("{}: not supported".format(object))
204 def get_albums_for_user(self
, user
, owner
= False, hidden
= False):
206 if isinstance(user
, User
):
207 # Find all albums with direct access
208 #objects = db.session.query(AlbumAcl, Album, AccessRight, Portfolio).join(
209 # Album, AlbumAcl.c.album == Album.id).join(
210 # AccessRight, AccessRight.id == AlbumAcl.c.acl).join(
211 # Portfolio, Portfolio.id == Album.portfolio_id).filter(
212 # AccessRight.user == user).all()
213 #for object in objects:
214 #if not owner and object.Portfolio.owner == user:
216 #if not hidden and not object.Album.visible:
218 # albums.append(object.Album)
219 # Find all albums with access through inherited access rights from portfolio
221 #objects = db.session.query(PortfolioAcl, Album, AccessRight, Portfolio).join(
222 # Portfolio, PortfolioAcl.c.portfolio == Portfolio.id).join(
223 # AccessRight, AccessRight.id == PortfolioAcl.c.acl).join(
224 # Album, Album.portfolio_id == Portfolio.id).all()
225 #for object in objects:
226 # if not owner and object.Portfolio.owner == user:
228 # if not hidden and not object.Album.visible:
230 # temp.append(object.Album)
231 # Last get all visible and public albums
233 temp
= self
.get_albums_for_user(u
)
234 app
.logger
.info("1) Other albums: %s" % temp
)
236 portfolios
= self
.get_portfolios(user
)
238 albs
= albs
+ self
.get_albums(p
)
240 if not owner
and self
.get_owner(a
) == user
:
242 if not hidden
and not a
.visible
:
245 app
.logger
.info("2) Own albums: %s" % albums
)
247 temp
= list(set(temp
) - set(albs
))
248 app
.logger
.info("3) Other albums: %s" % temp
)
249 # Merge albums with temp removing duplicates
250 albums
= albums
+ temp
254 if user
.is_anonymous
:
255 # Find all albums which is public and visible
256 albums
= Album
.query(public
=True, visible
=True)
257 except AttributeError:
258 raise DBQueryException("%s: Not a User object" % user
)
259 app
.logger
.info("Anonymous albums: %s" % albums
)
262 def is_safe_url(target
):
263 # TODO: Implement test to deside whether url is safe or not
268 if isinstance(obj
, list):
274 s
+= '{0}'.format(item
) + "\n"
This page took 0.142882 seconds and 6 git commands to generate.