]> git.datanom.net - pwp.git/blobdiff - app/tools.py
Half way through migration away from sqlalchemy
[pwp.git] / app / tools.py
index e930a2e6c7f0f98857a435696c441483c5830fd3..f8e05d5080ae7ac5304bfa021048ebe652283657 100644 (file)
@@ -1,4 +1,5 @@
 from .models import User, Portfolio, Album, Photo, AccessRight, Role, MyAnonymous
+from app import app
 #from flask_login import mixins
 
 class DBQueryException(Exception):
@@ -10,11 +11,11 @@ class DBQuery:
         albums = []
         if hasattr(object, '__tablename__'):
             if isinstance(object, User):
-                portfolios = Portfolio.query.filter_by(owner=object)
+                portfolios = Portfolio.query(user=object.id)
                 for p in portfolios:
                     albums.extend(Album.query.filter_by(portfolio=p))
             elif isinstance(object, Portfolio):
-                albums = Album.query.filter_by(portfolio=object)
+                albums = Album.query(portfolio=object.id)
             elif isinstance(object, Photo):
                 albums.append(object.album)
             else:
@@ -30,7 +31,7 @@ class DBQuery:
         portfolios = []
         if hasattr(object, '__tablename__'):
             if isinstance(object, User):
-                portfolios = Portfolio.query.filter_by(owner=object)
+                portfolios = Portfolio.query(user=object.id)
             elif isinstance(object, Album):
                 portfolios.append(object.portfolio)
             elif isinstance(object, Photo):
@@ -48,14 +49,14 @@ class DBQuery:
     def get_owner(self, object):
         if hasattr(object, '__tablename__'):
             if isinstance(object, Portfolio):
-                return object.owner
+                return object.user
             elif isinstance(object, Album):
                 p = object.portfolio
-                return p.owner
+                return p.user
             elif isinstance(object, Photo):
                 a = object.album
                 p = a.portfolio
-                return p.owner
+                return p.user
             else:
                 raise DBQueryException("%s: not supported" % object.__tablename__)
         else:
@@ -65,6 +66,7 @@ class DBQuery:
         if hasattr(object, '__tablename__'):
             users = []
             if isinstance(object, Album):
+                # TODO: get rit of query.join
                 objects = AccessRight.query.join(AccessRight.album).filter(Album.id == object.id)
                 for o in objects:
                     users.append(o.user)
@@ -229,7 +231,7 @@ class DBQuery:
             # Last get all visible and public albums
             u = MyAnonymous()
             temp = self.get_albums_for_user(u)
-            print("1) Other albums: %s" % temp)
+            app.logger.info("1) Other albums: %s" % temp)
             albs = []
             portfolios = self.get_portfolios(user)
             for p in portfolios:
@@ -240,10 +242,10 @@ class DBQuery:
                     if not hidden and not a.visible:
                         continue
                     albums.append(a)
-            print("2) Own albums: %s" % albums)
+            app.logger.info("2) Own albums: %s" % albums)
             # Remove own albums
             temp = list(set(temp) - set(albs))
-            print("3) Other albums: %s" % temp)
+            app.logger.info("3) Other albums: %s" % temp)
             # Merge albums with temp removing duplicates
             albums = albums + temp
         else:
@@ -251,10 +253,10 @@ class DBQuery:
                 # Anonymous user
                 if user.is_anonymous:
                     # Find all albums which is public and visible
-                    albums = Album.query.filter_by(public=True,visible=True).all()
+                    albums = Album.query(public=True, visible=True)
             except AttributeError:
                 raise DBQueryException("%s: Not a User object" % user)
-            print("Anonymous albums: %s" % albums)
+            app.logger.info("Anonymous albums: %s" % albums)
         return albums
 
 def is_safe_url(target):
This page took 0.049953 seconds and 5 git commands to generate.