Add author column to photos

This commit is contained in:
voussoir 2016-12-20 14:54:23 -08:00
parent f53b089b79
commit 232f8231e0
4 changed files with 26 additions and 2 deletions

View file

@ -48,6 +48,7 @@ SQL_PHOTO_COLUMNS = [
'created',
'thumbnail',
'tagged_at',
'author_id',
]
SQL_TAG_COLUMNS = [
'id',

View file

@ -31,6 +31,13 @@ def upgrade_2_to_3(sql):
cur.execute('CREATE INDEX IF NOT EXISTS index_user_id ON users(id)')
cur.execute('CREATE INDEX IF NOT EXISTS index_user_username ON users(username COLLATE NOCASE)')
def upgrade_3_to_4(sql):
'''
Add an `author_id` column to Photos.
'''
cur = sql.cursor()
cur.execute('ALTER TABLE photos ADD COLUMN author_id TEXT')
cur.execute('CREATE INDEX IF NOT EXISTS index_photo_author on photos(author_id)')
def upgrade_all(database_filename):
'''

View file

@ -287,6 +287,7 @@ class Photo(ObjectBase):
self.created = row_tuple['created']
self.thumbnail = row_tuple['thumbnail']
self.tagged_at = row_tuple['tagged_at']
self.author_id = row_tuple['author_id']
def __reinit__(self):
'''
@ -336,6 +337,9 @@ class Photo(ObjectBase):
albums = [self.photodb.get_album(f[0]) for f in fetch]
return albums
def author(self):
return self.photodb.get_user(id=self.author_id)
def bytestring(self):
return bytestring.bytestring(self.bytes)

View file

@ -28,7 +28,7 @@ logging.getLogger('PIL.PngImagePlugin').setLevel(logging.WARNING)
# Note: Setting user_version pragma in init sequence is safe because it only
# happens after the out-of-date check occurs, so no chance of accidentally
# overwriting it.
DATABASE_VERSION = 3
DATABASE_VERSION = 4
DB_INIT = '''
PRAGMA count_changes = OFF;
PRAGMA cache_size = 10000;
@ -52,7 +52,8 @@ CREATE TABLE IF NOT EXISTS photos(
bytes INT,
created INT,
thumbnail TEXT,
tagged_at INT
tagged_at INT,
author_id TEXT
);
CREATE TABLE IF NOT EXISTS tags(
id TEXT,
@ -96,6 +97,7 @@ CREATE INDEX IF NOT EXISTS index_photo_path on photos(filepath COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS index_photo_fakepath on photos(override_filename COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS index_photo_created on photos(created);
CREATE INDEX IF NOT EXISTS index_photo_extension on photos(extension);
CREATE INDEX IF NOT EXISTS index_photo_author on photos(author_id);
-- Tag
CREATE INDEX IF NOT EXISTS index_tag_id on tags(id);
@ -435,6 +437,7 @@ class PDBPhotoMixin:
filename,
*,
allow_duplicates=False,
author=None,
commit=True,
do_metadata=True,
do_thumbnail=True,
@ -461,6 +464,14 @@ class PDBPhotoMixin:
exc.photo = existing
raise exc
if isinstance(author, objects.User):
if author.photodb != self:
raise ValueError('That user does not belong to this photodb')
author_id = author.id
elif author is not None:
# Just to confirm
author_id = self.get_user(id=author).id
extension = os.path.splitext(filename)[1]
extension = extension.replace('.', '')
extension = self.normalize_tagname(extension)
@ -474,6 +485,7 @@ class PDBPhotoMixin:
'extension': extension,
'created': created,
'tagged_at': None,
'author_id': author_id,
# These will be filled in during the metadata stage.
'bytes': None,
'width': None,