def _create_photo_dict_for(username): photo_holder.photo_storage[username] = {} photo_holder.IDs[username] = ID() if photo_holder.db: query( sql.SQL("""CREATE TABLE IF NOT EXISTS {}.{} ( id serial primary key, filename text not null, url text not null, content_type text not null, image bytea not null );""").format(sql.Identifier(app.config['PHOTO_SCHEMA']), sql.Identifier(username)))
def get_photos_for(username): result = {} cached = photo_holder.photo_storage.get(username) if cached is not None and len(cached) != 0 and all( os.path.exists(cached[id].get_full_path()) for id in cached): result = dict(cached.items()) elif photo_holder.db: photo_holder.create_photo_dict_for(username) dbRows = query( sql.SQL("""SELECT * FROM {}.{};""").format( sql.Identifier(app.config['PHOTO_SCHEMA']), sql.Identifier(username))) if dbRows: for row in dbRows: photo = Photo( row[0], row[1], row[2], os.path.join(app.config['UPLOADED_PHOTOS_DEST'], username), row[3]) if not os.path.exists(photo.filepath): os.makedirs(photo.filepath) with open(photo.get_full_path(), 'wb') as f: f.write(bytes(row[4])) print("Loaded photo", photo.getName(), "id", photo.id, "for", username, "photo:", photo) #debug print result[photo.id] = photo photo_holder._add_photos_for(username, list(result.values())) return result
def load(): rs = {} if User.db: rs = query( sql.SQL("""SELECT * FROM {}.{};""").format( sql.Identifier(app.config['PHOTO_SCHEMA']), sql.Identifier(app.config['USERS_TABLE']))) if User.db and rs and len(rs) != 0: print('Loading', len(rs), 'users from db') #debug print User.users = {} for rowUser in rs: user = User(id=rowUser[0], name=rowUser[1], password=rowUser[2]) User.users[user.name] = user for username in User.users: User.save(User.users[username], False) else: print('loading users from local') #debug print local_users_dump = join(app.config['USERS_DEST'], app.config['USERS_FILE']) if (exists(local_users_dump)): with open(local_users_dump, 'rb') as f: User.users = pickle.load(f) for username in User.users: user = User.users[username] ID.map[user.id] = username ID.value = max(ID.value, user.id) return User.users
def remove(username, id): photos = photo_holder.photo_storage.get(username) if photos is not None: photo = photos.get(id) if photo is not None: os.remove(photo.get_full_path()) print('File removed for', username, ':', photo.id, ':', photo.getName()) #debug print photos.pop(id) if photo_holder.db: result = query( sql.SQL("""DELETE FROM {}.{} WHERE id = %s RETURNING id;"""). format(sql.Identifier(app.config['PHOTO_SCHEMA']), sql.Identifier(username)), [id]) print("Deleted", result[0][0], "for", username) #debug print
def get_photos_ids_for(username): result = [] cached = photo_holder.photo_storage.get(username) if photo_holder.db: dbRows = query( sql.SQL("""SELECT id FROM {}.{};""").format( sql.Identifier(app.config['PHOTO_SCHEMA']), sql.Identifier(username))) if dbRows: for row in dbRows: result.append(row[0]) elif cached is not None and len(cached) != 0 and all( os.path.exists(cached[id].get_full_path()) for id in cached): result.append(list(cached.keys())) return result
def gen_next_id(username): id = photo_holder.get_next_photo_id(username) try: id = int(file.filename.split('.', 2)[0]) except: rs = None if photo_holder.db: rs = query( sql.SQL( """SELECT nextval(pg_get_serial_sequence({},'id'));""" ).format( sql.Literal(app.config['PHOTO_SCHEMA'] + '.' + username))) if rs: id = photo_holder.override_photo_id(username, int(rs[0][0])) return id
def rename(username, id, name): photos = photo_holder.photo_storage.get(username) if photos is not None: photo = photos.get(id) if photo is not None: oldPath = photo.get_full_path() photo.setName(name) os.rename(oldPath, photo.get_full_path()) print('File renamed for', username, ':', photo.id, ':', photo.getName()) #debug print if photo_holder.db: result = query( sql.SQL("""UPDATE {}.{} SET filename = {} WHERE id = %s RETURNING id;""").format( sql.Identifier(app.config['PHOTO_SCHEMA']), sql.Identifier(username), sql.Literal(name)), [id]) print("Updated", result[0][0], "for", username) #debug print
def save(user, to_db=True): if User.db and to_db: id = query( sql.SQL("""INSERT INTO {}.{} (id, username, password) VALUES (DEFAULT,%s,%s) ON CONFLICT DO NOTHING RETURNING id;""").format( sql.Identifier(app.config['PHOTO_SCHEMA']), sql.Identifier(app.config['USERS_TABLE'])), (user.name, user.password)) user.id = id[0][0] ID.map[user.id] = user.name print('Saved user', user.name, 'with id:', user.id) #debug print User.users[user.name] = user if not exists(app.config['USERS_DEST']): makedirs(app.config['USERS_DEST']) with open(join(app.config['USERS_DEST'], app.config['USERS_FILE']), 'wb') as f: pickle.dump(User.users, f, pickle.HIGHEST_PROTOCOL)
def add_photos_for(username, photos): for photo in photos: with open(photo.get_full_path(), 'rb') as file: if photo_holder.db: id = query( sql.SQL( """INSERT INTO {}.{} (id, filename, url, content_type, image) VALUES (""" + (str(photo.id) if photo.id else 'DEFAULT') + """,%s,%s,%s,%s) RETURNING id; """).format(sql.Identifier(app.config['PHOTO_SCHEMA']), sql.Identifier(username)), [ photo.getName(), photo.url, photo.content_type, Binary(file.read()) ]) photo.id = id[0][0] else: photo.id = photo_holder.get_next_photo_id(username) print("Saved photo with id:", id, "for", username) #debug print photo_holder._add_photos_for(username, photos) return photos