def newItem(collection_id): #if 'username' not in login_session: # return redirect('/login') if request.method == 'POST': now = datetime.datetime.now() file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) newPath = os.path.join(app.config['UPLOAD_FOLDER'], "%s.%s" % (file.filename.rsplit('.',1)[0]+now.strftime("%Y-%m-%d-%H-%M-%S-%f"), file.filename.rsplit('.', 1)[1])) file.save(newPath) file_create, orientation = getExif(newPath) item_url = newPath flash("File Uploaded!") create_datetime = request.form['create_date'] if create_datetime == '': if file_create: create_datetime = file_create else: create_datetime = now newItem = Items(title = request.form['name'], description = request.form['description'], type = request.form['type'], note = request.form['notes'], create_date = create_datetime, shelf_location = request.form['shelf'], date_added = datetime.datetime.now()) #user_id=login_session['id']) newItem.archive_url = item_url subject_ids = request.form.getlist('subject') for subj_id in subject_ids: subj = session.query(Subject).filter_by(id = subj_id).one() newItem.subject.append(subj) coll = session.query(Collections).filter_by(id = collection_id).one() newItem.collections.append(coll) session.add(newItem) #author_name = request.form['author'] #isAuthor = session.query(Authors).filter_by(name = author_name).one() #if !isAuthor: # addNewAuthor(author_name) #people_name = request.form['depicted_name'] session.commit() flash("New Item Created!") return redirect(url_for('showCollection', collection_id=collection_id)) else: collectionList = session.query(Collections).all() subjectList = session.query(Subject).all() authorList = session.query(Authors).all() return render_template('additem.html', collection_id=collection_id, collectionList=collectionList, authorList=authorList, subjectList=subjectList)
def newItem(collection_id): """Allows a logged-in user to add a new item to the database, associating an image file, if desired, as well as a number of metadata categories Args: collection_id""" if 'username' not in login_session: return redirect('/login') if request.method == 'POST': # Get the list of files to work with, multiple files supported upload_files = request.files.getlist('up_files') print "upload_files length is: " print len(upload_files) print upload_files[0] # Get the title information title = request.form['name'] if not title: title = 'untitled' # Get the subject(s) associated with the item subject_ids = request.form.getlist('subject') # Get the author(s) associated with the item, # then add the author's name to the database if it isn't present author_names = request.form.getlist('author') handleAuthors(author_names) # Get the person/people associated with the item, # then add the name(s) to the database if not present people_names = request.form.getlist('people') handlePeople(people_names) if len(upload_files[0].filename) > 0: for file in upload_files: # Save the creation date of the record now = datetime.datetime.now() create_datetime = now # Copy the file to the correct location, and return its path item_url, newPath = uploadFile(file, now) # Collect relevant EXIF information from the file file_create, orientation = getExif(newPath) # If there was EXIF information about the date the image was created, # save this as the create_date, otherwise continue to use 'now' if file_create: file_string = ''.join(file_create) dt_obj = datetime.datetime.strptime(file_string, "%Y:%m:%d %H:%M:%S") create_datetime = dt_obj # Create the new item newItem = Items(title = title, description = request.form['description'], item_type = 'image', note = request.form['notes'], ## the only difference ## create_date = create_datetime, date_added = datetime.datetime.now(), user_id=login_session['user_id']) newItem.archive_url = item_url # Create many-to-many associations for subj_id in subject_ids: subj = session.query(Subject).filter_by(id = subj_id).one() newItem.subject.append(subj) for auth_name in author_names: thisAuth = session.query(Authors).filter_by(name = auth_name).one() newItem.authors.append(thisAuth) for person_name in people_names: thisPerson = session.query(People).filter_by(name = person_name).one() newItem.people.append(thisPerson) # Eventually, we will add the ability to assign an image to multiple # collections, but for now, we just add to the current collection. coll = session.query(Collections).filter_by(id = collection_id).one() newItem.collections.append(coll) # Whew! Let's add this item and commit it. session.add(newItem) session.commit() else: # Create the new item newItem = Items(title = title, description = request.form['description'], ## change item type to unknown ## item_type = 'unknown', note = request.form['notes'], date_added = datetime.datetime.now(), user_id=login_session['user_id']) ## NO item url # Create many-to-many associations for subj_id in subject_ids: subj = session.query(Subject).filter_by(id = subj_id).one() newItem.subject.append(subj) for auth_name in author_names: thisAuth = session.query(Authors).filter_by(name = auth_name).one() newItem.authors.append(thisAuth) for person_name in people_names: thisPerson = session.query(People).filter_by(name = person_name).one() newItem.people.append(thisPerson) # Eventually, we will add the ability to assign an image to multiple # collections, but for now, we just add to the current collection. coll = session.query(Collections).filter_by(id = collection_id).one() newItem.collections.append(coll) # Whew! Let's add this item and commit it. session.add(newItem) session.commit() flash("New Item(s) Created!") return redirect(url_for('showCollection', collection_id=collection_id)) else: # Pass the list of all subjects to populate the dropdown menu subjectList = session.query(Subject).all() # So, eventually, there will be some sort of autofill using these lists, # so we're passing them even though it's not in place yet. collectionList = session.query(Collections).all() authorList = session.query(Authors).all() personList = session.query(People).all() return render_template('additem.html', collection_id=collection_id, collectionList=collectionList, authorList=authorList, subjectList=subjectList, personList=personList)
def newItem(collection_id): if 'username' not in login_session: return redirect('/login') if request.method == 'POST': #We will use now to save the creation date of the record, #and as a default value for the file creation date. now = datetime.datetime.now() #Get the list of files to work with, multiple files supported upload_files = request.files.getlist('up_files') #Get the create date, which will be reassigned if not specified create_datetime = request.form['create_date'] #Get the subject(s) associated with the item subject_ids = request.form.getlist('subject') #Get the author(s) associated with the item, #then add the author's name to the database if it isn't present allAuthors = session.query(Authors.name).all() authorList = [i[0] for i in allAuthors] author_names = request.form.getlist('author') for auth_name in author_names: if auth_name not in authorList: addAuthor(auth_name) #Get the person/people associated with the item, #then add the name(s) to the database if not present allPeople = session.query(People.name).all() peopleList = [i[0] for i in allPeople] people_names = request.form.getlist('people') for person_name in people_names: if person_name not in peopleList: addPerson(person_name) for file in upload_files: filename = secure_filename(file.filename) savename = file.filename.rsplit('.',1)[0]+now.strftime("%Y-%m-%d-%H-%M-%S-%f"), file.filename.rsplit('.', 1)[1] item_url = ''.join(savename) newPath = os.path.join(app.config['UPLOAD_FOLDER'], item_url) photos.save(newPath) file_create, orientation = getExif(newPath) flash("File Uploaded!") if create_datetime == '': if file_create: file_string = ''.join(file_create) dt_obj = datetime.datetime.strptime(file_string, "%Y:%m:%d %H:%M:%S") create_datetime = dt_obj else: create_datetime = now newItem = Items(title = request.form['name'], description = request.form['description'], type = request.form['type'], note = request.form['notes'], create_date = create_datetime, collection_id = collection_id, shelf_location = request.form['shelf'], date_added = datetime.datetime.now(), user_id=login_session['user_id']) newItem.archive_url = item_url for subj_id in subject_ids: subj = session.query(Subject).filter_by(id = subj_id).one() newItem.subject.append(subj) for auth_name in author_names: thisAuth = session.query(Authors).filter_by(name = auth_name).one() newItem.authors.append(thisAuth) for person_name in people_names: thisPerson = session.query(People).filter_by(name = person_name).one() newItem.people.append(thisPerson) coll = session.query(Collections).filter_by(id = collection_id).one() newItem.collections.append(coll) session.add(newItem) session.commit() flash("New Item Created!") return redirect(url_for('showCollection', collection_id=collection_id)) else: collectionList = session.query(Collections).all() subjectList = session.query(Subject).all() authorList = session.query(Authors).all() personList = session.query(People).all() return render_template('additem.html', collection_id=collection_id, collectionList=collectionList, authorList=authorList, subjectList=subjectList, personList=personList)