]>
git.datanom.net - pwp.git/blob - app/tools.py
1 from .models
import User
, Portfolio
, Album
, Photo
, AccessRight
, Role
, MyAnonymous
2 #from flask_login import mixins
4 class DBQueryException(Exception):
9 def get_albums(self
, object):
11 if hasattr(object, '__tablename__'):
12 if isinstance(object, User
):
13 portfolios
= Portfolio
.query
.filter_by(owner
=object)
15 albums
.extend(Album
.query
.filter_by(portfolio
=p
))
16 elif isinstance(object, Portfolio
):
17 albums
= Album
.query
.filter_by(portfolio
=object)
18 elif isinstance(object, Photo
):
19 albums
.append(object.album
)
21 raise DBQueryException("%s: not supported" % object.__tablename
__)
27 raise DBQueryException("{}: not supported".format(object))
29 def get_portfolios(self
, object):
31 if hasattr(object, '__tablename__'):
32 if isinstance(object, User
):
33 portfolios
= Portfolio
.query
.filter_by(owner
=object)
34 elif isinstance(object, Album
):
35 portfolios
.append(object.portfolio
)
36 elif isinstance(object, Photo
):
37 album
= self
.get_albums(object)
38 portfolios
.append(album
[0].portfolio
)
40 raise DBQueryException("%s: not supported" % object.__tablename
__)
42 for portfolio
in portfolios
:
43 result
.append(portfolio
)
46 raise DBQueryException("{}: not supported".format(object))
48 def get_owner(self
, object):
49 if hasattr(object, '__tablename__'):
50 if isinstance(object, Portfolio
):
52 elif isinstance(object, Album
):
55 elif isinstance(object, Photo
):
60 raise DBQueryException("%s: not supported" % object.__tablename
__)
62 raise DBQueryException("{}: not supported".format(object))
64 def get_users(self
, object):
65 if hasattr(object, '__tablename__'):
67 if isinstance(object, Album
):
68 objects
= AccessRight
.query
.join(AccessRight
.album
).filter(Album
.id == object.id)
71 elif isinstance(object, Photo
):
72 objects
= AccessRight
.query
.join(AccessRight
.photo
).filter(Photo
.id == object.id)
75 elif isinstance(object, Portfolio
):
76 objects
= AccessRight
.query
.join(AccessRight
.portfolio
).filter(Portfolio
.id == object.id)
80 raise DBQueryException("%s: not supported" % object.__tablename
__)
83 raise DBQueryException("{}: not supported".format(object))
85 def has_access(self
, user
, object):
86 #if hasattr(user, '__tablename__') and hasattr(object, '__tablename__') and isinstance(user, User):
87 if hasattr(object, '__tablename__'):
89 if isinstance(object, Portfolio
):
90 acl
= self
.get_acl(user
, object)
93 elif isinstance(object, Album
):
94 acl
= self
.get_acl(user
, object)
98 portfolio
= self
.get_portfolios(object)
99 acl
= self
.get_acl(user
, portfolio
[0])
102 elif isinstance(object, Photo
):
103 acl
= self
.get_acl(user
, object)
107 albums
= self
.get_albums(object)
108 acl
= self
.get_acl(user
, albums
[0])
112 portfolio
= self
.get_portfolios(object)
113 acl
= self
.get_acl(user
, portfolio
[0])
117 raise DBQueryException("%s: not supported" % object.__tablename
__)
120 raise DBQueryException("{}: not supported".format(object))
122 def can_read(self
, user
, object):
123 return self
.has_access(user
, object)
125 def can_write(self
, user
, object):
126 #if hasattr(user, '__tablename__') and hasattr(object, '__tablename__') and isinstance(user, User):
127 if hasattr(object, '__tablename__'):
129 if isinstance(object, Portfolio
):
130 acl
= self
.get_acl(user
, object)
131 if acl
is not None and acl
> Role
.read
:
133 elif isinstance(object, Album
):
134 acl
= self
.get_acl(user
, object)
135 if acl
is not None and acl
> Role
.read
:
138 portfolio
= self
.get_portfolios(object)
139 acl
= self
.get_acl(user
, portfolio
[0])
140 if acl
is not None and acl
> Role
.read
:
142 elif isinstance(object, Photo
):
143 acl
= self
.get_acl(user
, object)
144 if acl
is not None and acl
> Role
.read
:
147 albums
= self
.get_albums(object)
148 acl
= self
.get_acl(user
, albums
[0])
149 if acl
is not None and acl
> Role
.read
:
152 portfolio
= self
.get_portfolios(object)
153 acl
= self
.get_acl(user
, portfolio
[0])
154 if acl
is not None and acl
> Role
.read
:
157 raise DBQueryException("%s: not supported" % object.__tablename
__)
160 raise DBQueryException("{}: not supported".format(object))
162 def get_acl(self
, user
, object):
163 #if hasattr(user, '__tablename__') and hasattr(object, '__tablename__') and isinstance(user, User):
164 if hasattr(object, '__tablename__'):
165 if isinstance(object, Album
):
166 if user
.is_admin
or user
== self
.get_owner(object):
168 acl
= AccessRight
.query
.join(AccessRight
.album
).filter(Album
.id == object.id)
170 if AccessRight
.query
.get(a
.id).user_id
== user
.id:
172 if object.visible
and object.public
:
174 # Check if access given by parent (Portfolio)
175 return self
.get_acl(user
, object.portfolio
)
176 elif isinstance(object, Photo
):
177 if user
.is_admin
or user
== self
.get_owner(object):
179 acl
= AccessRight
.query
.join(AccessRight
.photo
).filter(Photo
.id == object.id)
181 if AccessRight
.query
.get(a
.id).user_id
== user
.id:
183 if user
.is_anonymous
and object.visible
and object.public
:
185 # Check if access given by parent (Album)
186 return self
.get_acl(user
, object.album
)
187 elif isinstance(object, Portfolio
):
188 if user
.is_admin
or user
== self
.get_owner(object):
190 acl
= AccessRight
.query
.join(AccessRight
.portfolio
).filter(Portfolio
.id == object.id)
192 if AccessRight
.query
.get(a
.id).user_id
== user
.id:
194 if user
.is_anonymous
and object.visible
and object.public
:
197 raise DBQueryException("%s: not supported" % object.__tablename
__)
200 raise DBQueryException("{}: not supported".format(object))
202 def get_albums_for_user(self
, user
, owner
= False, hidden
= False):
204 if isinstance(user
, User
):
205 # Find all albums with direct access
206 #objects = db.session.query(AlbumAcl, Album, AccessRight, Portfolio).join(
207 # Album, AlbumAcl.c.album == Album.id).join(
208 # AccessRight, AccessRight.id == AlbumAcl.c.acl).join(
209 # Portfolio, Portfolio.id == Album.portfolio_id).filter(
210 # AccessRight.user == user).all()
211 #for object in objects:
212 #if not owner and object.Portfolio.owner == user:
214 #if not hidden and not object.Album.visible:
216 # albums.append(object.Album)
217 # Find all albums with access through inherited access rights from portfolio
219 #objects = db.session.query(PortfolioAcl, Album, AccessRight, Portfolio).join(
220 # Portfolio, PortfolioAcl.c.portfolio == Portfolio.id).join(
221 # AccessRight, AccessRight.id == PortfolioAcl.c.acl).join(
222 # Album, Album.portfolio_id == Portfolio.id).all()
223 #for object in objects:
224 # if not owner and object.Portfolio.owner == user:
226 # if not hidden and not object.Album.visible:
228 # temp.append(object.Album)
229 # Last get all visible and public albums
231 temp
= self
.get_albums_for_user(u
)
232 print("1) Other albums: %s" % temp
)
234 portfolios
= self
.get_portfolios(user
)
236 albs
= albs
+ self
.get_albums(p
)
238 if not owner
and self
.get_owner(a
) == user
:
240 if not hidden
and not a
.visible
:
243 print("2) Own albums: %s" % albums
)
245 temp
= list(set(temp
) - set(albs
))
246 print("3) Other albums: %s" % temp
)
247 # Merge albums with temp removing duplicates
248 albums
= albums
+ temp
252 if user
.is_anonymous
:
253 # Find all albums which is public and visible
254 albums
= Album
.query
.filter_by(public
=True,visible
=True).all()
255 except AttributeError:
256 raise DBQueryException("%s: Not a User object" % user
)
257 print("Anonymous albums: %s" % albums
)
260 def is_safe_url(target
):
261 # TODO: Implement test to deside whether url is safe or not
266 if isinstance(obj
, list):
272 s
+= '{0}'.format(item
) + "\n"
This page took 0.227563 seconds and 6 git commands to generate.