def upload():
    # upload one or more pictures to the filesystem and register it/them in the database
    if not 'MyWebsite_user_id' in session.keys():
        return redirect('/')
    if not User.is_logged_in(session['MyWebsite_user_id'],session['login_session']):
        return redirect('/danger')    
    user=User.get_one(session['MyWebsite_user_id'])
    album_id=request.form['active_album']
    # f=request.files['new_pic']
    files=request.files.getlist('new_pic')
    for eachfile in files:
        filename=secure_filename(eachfile.filename)
        ALLOWED_EXTENSIONS = ('bmp','png', 'jpg', 'jpeg', 'gif')
        if '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS:
            print(filename)
            if path.exists('UserFiles/'+user.email+'/'+filename):
                # Album.add_pic(user,pic,album_id)
                filename=filename.rsplit('.',1)
                filename[0]=filename[0]+str(round(datetime.timestamp(datetime.now())))
                filename='.'.join(filename)

            # Save pic to the filesystem
            eachfile.save('UserFiles/'+user.email+'/'+filename)
            # Add pic to the pictures db
            pic=Picture.new(user.id,user.email+'/'+filename,filename)
            # Add pic to the active album
            Album.add_pic(user,pic,album_id)
            # Create thumbnail image using PIL
            im=Image.open('UserFiles/'+user.email+'/'+filename)
            im.thumbnail((100,100),Image.ANTIALIAS)
            im.save('UserFiles/thumbnails/'+user.email+'/'+filename)
            user.set_active_album(album_id)
        else:
            print('invalid file extension.')
    return redirect('/dashboard')
def reorder_album():
    if not 'MyWebsite_user_id' in session.keys():
        return redirect('/')
    if not User.is_logged_in(session['MyWebsite_user_id'],session['login_session']):
        return redirect('/danger')    
    python_obj = json.loads(request.form['json'])
    album_order=Album_to_Pic()
    # This section to be replaced by album_order.set_order(python_obj)
    old_order=album_order.get_order(python_obj["album_id"])
    album=Album.query.get(python_obj["album_id"])
    # Remove pictures from album if they are no longer in the list
    for picture_id in old_order:
        if picture_id not in python_obj["ordering"]:
            picture=Picture.query.get(picture_id)
            album.pictures.remove(picture)
            album_order.commit()
    # Rewrite all the rank values in the db table using the index value of the list
    for rank,picture_id in enumerate(python_obj["ordering"],1):
        # print(rank, picture_id)
        if picture_id.isnumeric():
            # if the list contains a picture that is not in the album, then add it
            picture=Picture.query.get(picture_id)
            if picture not in album.pictures:
                Album.add_pic(user=None,picture=picture,album_id=album.id)
            record=album_order.get_one(python_obj["album_id"],picture_id)
            record.rank=rank
    album_order.commit()
    # print(album_order.get_order(python_obj["album_id"]))
    return redirect('/dashboard')
def reorder_album():
    if not 'MyWebsite_user_id' in session.keys():
        return redirect('/')
    if not User.is_logged_in(session['MyWebsite_user_id'],
                             session['login_session']):
        return redirect('/danger')
    # print("form:", request.form['json'])
    # print(request.form['album_id'])
    python_obj = json.loads(request.form['json'])
    # print("jSON:", json.loads(request.form['json']))
    # print(python_obj["album_id"])
    # print(python_obj["ordering"])
    # print("jSON:", request.get_json(force=True))
    album_order = Album_to_Pic()
    # This section to be replaced by album_order.set_order(python_obj)
    old_order = album_order.get_order(python_obj["album_id"])
    album = Album.query.get(python_obj["album_id"])
    for picture_id in old_order:
        if picture_id not in python_obj["ordering"]:
            picture = Picture.query.get(picture_id)
            album.pictures.remove(picture)
            album_order.commit()
    for rank, picture_id in enumerate(python_obj["ordering"], 1):
        # print(rank, picture_id)

        picture = Picture.query.get(picture_id)
        if picture not in album.pictures:
            Album.add_pic(user=None, picture=picture, album_id=album.id)

        record = album_order.get_one(python_obj["album_id"], picture_id)
        record.rank = rank
    album_order.commit()
    # print(album_order.get_order(python_obj["album_id"]))
    return redirect('/dashboard')