diff --git a/app/__init__.py b/app/__init__.py index e1d0db5..e6c0d68 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -162,6 +162,13 @@ def clear_api_limits(): for l in limits: l.delete() +def ensure_default_collection(): + from .db import db + from .models import BookmarkCollections + with db.transaction(): + for missing_user in BookmarkCollections.get_users_without_default(): + BookmarkCollections.create_default(missing_user) + cache = Cache() def create_app(): @@ -243,6 +250,8 @@ def create_app(): reparse_babycode() + ensure_default_collection() + bind_default_badges(app.config['BADGES_PATH']) app.config['SESSION_COOKIE_SECURE'] = True diff --git a/app/models.py b/app/models.py index f476d51..c129291 100644 --- a/app/models.py +++ b/app/models.py @@ -506,10 +506,24 @@ class BookmarkCollections(Model): @classmethod def create_default(cls, user_id): - q = """INSERT INTO bookmark_collections (user_id, name, is_default, sort_order) - VALUES (?, "Bookmarks", 1, 0) RETURNING id - """ - res = db.fetch_one(q, user_id) + return cls.create({ + 'user_id': user_id, + 'name': 'Bookmarks', + 'is_default': True, + 'sort_order': 0, + }) + + @staticmethod + def get_users_without_default(): + q = """ + SELECT users.id FROM users + WHERE NOT EXISTS ( + SELECT 1 FROM bookmark_collections bc + WHERE bc.user_id = users.id + AND bc.is_default = 1 + ) + """ + return [row['id'] for row in db.query(q)] @classmethod def get_for_user(cls, user_id):