def seed_db(): """Seeds the database with 2 images.""" db.session.add( Image(path=os.path.join('project', 'examples', 'connie.jpg'))) db.session.add( Image(path=os.path.join('project', 'examples', 'NicksParty-50.jpg'))) db.session.commit()
def add_album(title, description, user_email, created_at=datetime.datetime.now()): album = Album(title=title, description=description, user_email=user_email, created_at=created_at) img1 = Image(name='img1.jpg', url='http://1') img2 = Image(name='img2.jpg', url='http://2') album.images = [img1, img2] db.session.add(album) db.session.commit() return album
def seed_db(): """Seeds the database.""" album1 = Album(title='polina', description="Polina's album with many awesome pictures.") img1 = Image(name='pigs3.jpg', url='https://raw.githubusercontent.com/jastr945/xperiments/880e033191ca56e1612049fcf76565b7a8377911/subscriber/coffee5.jpg') img2 = Image(name='pigs4.jpg', url='https://raw.githubusercontent.com/jastr945/xperiments/880e033191ca56e1612049fcf76565b7a8377911/subscriber/coffee2.jpg') album2 = Album(title='pofi', description="Pofi's cute pictures.") img3 = Image(name='pigs7.jpg', url='https://raw.githubusercontent.com/jastr945/xperiments/880e033191ca56e1612049fcf76565b7a8377911/subscriber/coffee.jpg') img4 = Image(name='pigs8.jpg', url='https://raw.githubusercontent.com/jastr945/xperiments/880e033191ca56e1612049fcf76565b7a8377911/subscriber/coffee4.jpg') album1.images = [img1, img2] album2.images = [img3, img4] db.session.add(album1) db.session.add(album2) db.session.commit()
def add_image(): ''' POST: add new image to the database. If the path of the image already exists in the database, only overwrite if a name is added to the record. ''' post_data = request.get_json() response_object = {'status': 'fail', 'message': 'Invalid payload'} if not post_data: return jsonify(response_object), 400 path = post_data.get('path') names = post_data.get('names') try: image = Image.query.filter_by(path=path).first() if not image: db.session.add(Image(path=path, names=names)) db.session.commit() response_object = { 'status': 'success', 'message': f'{path} was added!' } return jsonify(response_object), 201 else: if image.names is not None: response_object[ 'message'] = 'Sorry. That image already exists.' return jsonify(response_object), 400 elif names: image.names = names db.session.commit() response_object = { 'status': 'success', 'message': f'{path} was updated!' } return jsonify(response_object), 201 else: response_object[ 'message'] = 'Name was not contained in payload.' return jsonify(response_object), 400 except exc.IntegrityError as e: db.session.rollback() return jsonify(response_object), 400
def add_album(): credentials = storage.get() if credentials and len(credentials.id_token['email']) > 0: post_data = request.form if not post_data: response_object = {'status': 'fail', 'message': 'Invalid payload.'} return jsonify(response_object), 400 title = request.form['title'] try: album = Album.query.filter_by(title=title).first() if not album: description = request.form['description'] new_album = Album(title=title, description=description, user_email=credentials.id_token['email']) new_album.images = [] for i in request.files.getlist('photos'): filename = photos.save( i) # saving images via Flask-Uploads img_url = photos.url( filename) # extracting image url with Flask-Uploads new_image = Image(name=filename, url=img_url) new_album.images.append( new_image) # One-to-many relationship instantiation db.session.add(new_album) db.session.commit() response_object = { 'status': 'success', 'message': f'{title} was added!' } return jsonify(response_object), 200 else: response_object = { 'status': 'fail', 'message': 'Sorry. That title already exists.' } return jsonify(response_object), 400 except (exc.IntegrityError, ValueError) as e: db.session().rollback() response_object = {'status': 'fail', 'message': 'Invalid payload.'} return jsonify(response_object), 400 else: response_object = { 'status': 'fail', 'message': 'Unauthorized access. Please log in.' } return jsonify(response_object), 403
def load_examples(): '''Seeds the database with all the images found in the project/examples/* folder.''' imgs = {} walk_dir = os.path.join('project', 'examples') assert os.path.isdir(walk_dir) imgid = 0 for root, subdirs, files in os.walk(walk_dir): for filename in files: file_path = os.path.join(root, filename) _, ext = os.path.splitext(file_path) if ext.lower() in ['.jpg', '.png']: imgs[imgid] = file_path imgid += 1 for k, fpath in imgs.items(): db.session.add(Image(path=fpath)) db.session.commit()
def seed_db(): """Seeds the database.""" album1 = Album( title='Example Gallery: Columbia River Gorge', description= 'The Columbia River Gorge is a canyon of the Columbia River in the Pacific Northwest of the United States.', user_email='*****@*****.**') img1 = Image( name='1.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Columbia_River_Gorge/DSC03751.JPG' ) img2 = Image( name='2.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Columbia_River_Gorge/DSC03754.JPG' ) img3 = Image( name='3.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Columbia_River_Gorge/DSC03755.JPG' ) img4 = Image( name='4.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Columbia_River_Gorge/DSC03756.JPG' ) album2 = Album( title='Example Gallery: Crystal Springs Rhododendron Garden', description= 'Beautiful rhododendron garden in gorgeous Southeast Portland!', user_email="*****@*****.**") img5 = Image( name='5.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03714.JPG' ) img6 = Image( name='6.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03715.JPG' ) img7 = Image( name='7.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03719.JPG' ) img8 = Image( name='8.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03723.JPG' ) img9 = Image( name='9.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03726.JPG' ) img10 = Image( name='10.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03727.JPG' ) img11 = Image( name='11.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03735.JPG' ) img12 = Image( name='12.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03741.JPG' ) img13 = Image( name='13.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03744.JPG' ) img14 = Image( name='14.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03745.JPG' ) img15 = Image( name='15.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Crystal_Springs_Rhododendron_Garden/DSC03746.JPG' ) album3 = Album( title='Example Gallery: Magnolia Trail in the Washington Park', description='Hoyt Arboretum, Portland, Oregon. Spring 2018.', user_email='*****@*****.**') img16 = Image( name='16.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Magnolia_Trail_in_the_Washington_Park/DSC03639.JPG' ) img17 = Image( name='17.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Magnolia_Trail_in_the_Washington_Park/DSC03642.JPG' ) img18 = Image( name='18.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Magnolia_Trail_in_the_Washington_Park/DSC03643.JPG' ) img19 = Image( name='19.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Magnolia_Trail_in_the_Washington_Park/DSC03644.JPG' ) img20 = Image( name='20.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Magnolia_Trail_in_the_Washington_Park/DSC03655.JPG' ) img21 = Image( name='21.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Magnolia_Trail_in_the_Washington_Park/DSC03654.JPG' ) img22 = Image( name='22.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Magnolia_Trail_in_the_Washington_Park/DSC03660.JPG' ) album4 = Album( title='Example Gallery: Portland Japanese Garden', description= 'This place is considered to be the most authentic Japanese Garden outside of Japan.', user_email='*****@*****.**') img23 = Image( name='23.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Japanese_Garden/DSC03668.JPG' ) img24 = Image( name='24.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Japanese_Garden/DSC03678.JPG' ) img25 = Image( name='25.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Japanese_Garden/DSC03688.JPG' ) album5 = Album( title='Example Gallery: The Mt. Hood National Forest', description= 'The Mt. Hood National Forest is 20 miles away from Portland.', user_email='*****@*****.**') img26 = Image( name='26.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Mount_Hood/DSC03233.JPG' ) img27 = Image( name='27.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Mount_Hood/DSC03234.JPG' ) img28 = Image( name='28.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Mount_Hood/DSC03236.JPG' ) img29 = Image( name='29.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Mount_Hood/DSC03238.JPG' ) img30 = Image( name='30.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Mount_Hood/DSC03239.JPG' ) img31 = Image( name='31.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Mount_Hood/bird.jpg' ) img32 = Image( name='32.jpg', url= 'https://raw.githubusercontent.com/jastr945/xperiments/master/flask-photoalbum/sample_albums/Mount_Hood/mt_hood.jpg' ) album1.images = [img1, img2, img3, img4] album2.images = [ img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15 ] album3.images = [img16, img17, img18, img19, img20, img21, img22] album4.images = [img23, img24, img25] album5.images = [img26, img27, img28, img29, img30, img31, img32] db.session.add(album1) db.session.add(album2) db.session.add(album3) db.session.add(album4) db.session.add(album5) db.session.commit()
def add_image(fpath, names=None): image = Image(path=fpath, names=names) db.session.add(image) db.session.commit() return image