def upload_file(): if 'user' in session: if 'profile_pic' in request.files: #make sure that the filename is unique profile_pic = request.files['profile_pic'] files = mongo.db.fs.files.find({'filename':profile_pic.filename}).count() #print(files) if files ==0: # if files is not empty #upload the pic mongo.save_file(profile_pic.filename, profile_pic) #get previous filename user = User.get_by_username(session['user'].name) previousFilename = user.profile_pic #update the profile query = { 'uname':session['user'].name} updates = { "$set": { "profile_pic": profile_pic.filename } } mongo.db.users.update_one(query, updates) # delete the old file if it isn't default if previousFilename != 'default': # get fs.files _id for previousfile #find_one doesn't work. fileobject = mongo.db.fs.files.find({'filename':previousFilename}) #print(fileobject[0]['_id']) #delete the old file chunks from fs.chunks mongo.db.fs.chunks.remove({ 'files_id' : fileobject[0]['_id'] }) #delete the old file record from fs.files mongo.db.fs.files.remove({ '_id' : fileobject[0]['_id'] }) else: flash('The filename has been used. Please choice a different file name.') return redirect(url_for('web.myprofile')) else: return redirect(url_for('web.index'))
def insert_resume(): data = request.get_json() e = request.form['email'] resume = request.files['resume'] mongo.save_file(resume.filename, resume) mongo.db.candidates.update_one({'email': e}, { "$set": { "resume": resume.filename, "date": datetime.datetime.utcnow(), "status": "scheduled" } }) return jsonify({ 'success': 'success', 'message': 'Inserted successfully!' }), 200
def uploaded(): f = request.files['userFile'] fileSize = request.form['fileSize'] account = mongo.db.Accounts.find_one({'Email': session['account'][1]}) newSize = account['SpaceUsed'] + int(fileSize) if newSize >= session['account'][4]: return redirect(url_for('homepage', nospaceleft=1)) mongo.save_file(f.filename, f) file_length = size(int(fileSize)) today = datetime.today().strftime('%Y-%m-%d %H:%M:%S') mongo.db.Accounts.update_one({'Email': session['account'][1]}, {'$push': {'Files': "{}>{}>{}".format(f.filename, file_length, today)} }) mongo.db.Accounts.update_one({'Email': session['account'][1]}, {'$set': {'SpaceUsed': newSize} }) session['account'][2].append("{}>{}>{}".format(f.filename, file_length, today)) print(session) return redirect(url_for("homepage"))