def add_event_db(): try: event_type = request.form['event_type'] title = request.form['title'] thumbnail = 'default.jpg' is_empty_thumbnail = request.form['is_empty_thumbnail'] thumbnail_detail = 'default.jpg' is_empty_thumbnail_detail = request.form['is_empty_thumbnail_detail'] description = request.form['description'] short_description = request.form['short_description'] created_date = datetime.now() created_date = '{0}/{1}/{2}'.format(created_date.year, created_date.month, created_date.day) created_by = 'admin' is_important = request.form['is_important'] is_approve = 'true' if is_empty_thumbnail == 'false': #save thumbnail to server files = request.files['thumbnail_file'] file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER']) file_path = os.path.join( 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER'], file_name) # save file to disk files.save(file_path) thumbnail = file_name if is_empty_thumbnail_detail == 'false': #save thumbnail detail to server files = request.files['thumbnail_file_detail'] file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER']) file_path = os.path.join( 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER'], file_name) # save file to disk files.save(file_path) thumbnail_detail = file_name new_event = { "event_type": event_type, "title": title, "thumbnail": thumbnail, "thumbnail_detail": thumbnail_detail, "short_description": short_description, "description": description, "is_important": is_important, "is_approve": is_approve, "created_date": created_date, "created_by": created_by } common.current_db.Event.insert(new_event) return simplejson.dumps({"result": 'success'}) except Exception, e: print 'error' + str(e) return simplejson.dumps({"result": 'error'})
def upload_banner(): files = request.files['file'] if files: try: file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['BANNER_IMAGE_FOLDER']) banner_number = request.form['banner_number'] old_file_name = request.form['old_file_name'] if banner_number > 0: #Delete old banner image, database if old_file_name != '': old_file_path = os.path.join( 'TheMarch/' + app.config['BANNER_IMAGE_FOLDER'], old_file_name) if os.path.exists(old_file_path): os.remove(old_file_path) #delete database common.current_db.Banner.remove({ "file_name": old_file_name, "index": banner_number }) #Inset new image if float(banner_number) == 0: #Get max index max_banner = common.current_db.Banner.find().sort( "index", DESCENDING).limit(1) if max_banner.count() == 0: banner_number = 1 else: banner_number = int(max_banner[0]['index']) + 1 banner_number = str(banner_number) #file_name = file_name.replace(os.path.splitext(file_name)[0], #banner_number + '_banner') file_name = banner_number + '_' + file_name file_path = os.path.join( 'TheMarch/' + app.config['BANNER_IMAGE_FOLDER'], file_name) # save file to disk files.save(file_path) #Image.open(files).save(file_path) # Save database common.current_db.Banner.insert({ "file_name": file_name, "index": banner_number }) return simplejson.dumps({ 'result': 'success', 'file_name': file_name }) else: return simplejson.dumps({ 'result': 'success', 'file_name': 'No file' }) except Exception, e: return simplejson.dumps({ 'result': 'error', 'error_message': str(e), 'file_name': 'No file' })
def upload_band_thumbnail(): files = request.files['file'] if files: try: file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['BAND_IMAGE_FOLDER']) band_id = request.form['band_id'] band_index = request.form['band_index'] old_file_name = request.form['old_file_name'] #Delete old band image, database if old_file_name != '' and old_file_name != 'default.jpg': old_file_path = os.path.join( 'TheMarch/' + app.config['BAND_IMAGE_FOLDER'], old_file_name) if os.path.exists(old_file_path): os.remove(old_file_path) file_name = band_index + '_' + file_name file_path = os.path.join( 'TheMarch/' + app.config['BAND_IMAGE_FOLDER'], file_name) # save file to disk files.save(file_path) #Image.open(files).save(file_path) # Save database common.current_db.Band_thumbnail.update( {"_id": ObjectId(band_id)}, {"$set": { "thumbnail": file_name }}) return simplejson.dumps({ 'result': 'success', 'file_name': file_name }) except Exception, e: return simplejson.dumps({ 'result': 'error', 'error_message': str(e), 'file_name': 'No file' })
def update_band_detail_db(): try: band_id = request.form['band_id'] band_name = request.form['band_name'] band_type = request.form['band_type'] title = request.form['title'] #thumbnail image old_thumbnail = request.form['old_thumbnail'] thumbnail = old_thumbnail is_empty_thumbnail = request.form['is_empty_thumbnail'] #thumbnail detail image old_thumbnail_detail = request.form['old_thumbnail_detail'] thumbnail_detail = old_thumbnail_detail is_empty_thumbnail_detail = request.form['is_empty_thumbnail_detail'] description = request.form['description'] short_description = request.form['short_description'] is_important = request.form['is_important'] score = request.form['score'] is_approve = request.form['is_approve'] if score == 'undefined' or score.strip() == "": score = 0 else: score = int(score) if is_empty_thumbnail == 'false': #delete old thumnail #Delete old thumnail image, database if old_thumbnail != '' and old_thumbnail != 'default.jpg': old_file_path = os.path.join( 'TheMarch/' + app.config['BAND_IMAGE_FOLDER'], old_thumbnail) if os.path.exists(old_file_path): os.remove(old_file_path) #save thumbnail to server files = request.files['thumbnail_file'] file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['BAND_IMAGE_FOLDER']) file_path = os.path.join( 'TheMarch/' + app.config['BAND_IMAGE_FOLDER'], file_name) # save file to disk files.save(file_path) thumbnail = file_name if is_empty_thumbnail_detail == 'false': #delete old thumnail detail #Delete old thumnail detail image, database if old_thumbnail_detail != '' and old_thumbnail_detail != 'default.jpg': old_file_path = os.path.join( 'TheMarch/' + app.config['BAND_IMAGE_FOLDER'], old_thumbnail_detail) if os.path.exists(old_file_path): os.remove(old_file_path) #save thumbnail to server files = request.files['thumbnail_file_detail'] file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['BAND_IMAGE_FOLDER']) file_path = os.path.join( 'TheMarch/' + app.config['BAND_IMAGE_FOLDER'], file_name) # save file to disk files.save(file_path) thumbnail_detail = file_name # Check current user if current_user.role == 'admin': update_event = { "band_name": band_name, "band_type": band_type, "title": title, "thumbnail": thumbnail, "thumbnail_detail": thumbnail_detail, "short_description": short_description, "description": description, "is_important": is_important, "is_approve": is_approve, "score": score } else: update_event = { "band_name": band_name, "band_type": band_type, "title": title, "thumbnail": thumbnail, "thumbnail_detail": thumbnail_detail, "short_description": short_description, "description": description, "is_important": is_important, "is_approve": is_approve } common.current_db.Band_detail.update({"_id": ObjectId(band_id)}, {"$set": update_event}) #Get userid of band band = common.current_db.Band_detail.find_one( {"_id": ObjectId(band_id)}) if band != None: band_id = str(band["userId"]) #update band name in user table common.current_db.User.update({"_id": ObjectId(band_id)}, {"$set": { "name": band_name }}) return simplejson.dumps({"result": 'success'}) except Exception, e: print 'error' + str(e) return simplejson.dumps({"result": 'error'})
def add_band_detail_db(): try: band_name = request.form['band_name'] band_type = request.form['band_type'] title = request.form['title'] thumbnail = 'default.jpg' is_empty_thumbnail = request.form['is_empty_thumbnail'] thumbnail_detail = 'default.jpg' is_empty_thumbnail_detail = request.form['is_empty_thumbnail_detail'] description = request.form['description'] short_description = request.form['short_description'] created_date = datetime.now() created_date = '{0}/{1}/{2}'.format(created_date.year, created_date.month, created_date.day) created_by = current_user.user_name is_important = request.form['is_important'] score = request.form['score'] if score == 'undefined' or score.strip() == "": score = 0 else: score = int(score) is_approve = 'false' if is_empty_thumbnail == 'false': #save thumbnail to server files = request.files['thumbnail_file'] file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['BAND_IMAGE_FOLDER']) file_path = os.path.join( 'TheMarch/' + app.config['BAND_IMAGE_FOLDER'], file_name) # save file to disk files.save(file_path) thumbnail = file_name if is_empty_thumbnail_detail == 'false': #save thumbnail detail to server files = request.files['thumbnail_file_detail'] file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['BAND_IMAGE_FOLDER']) file_path = os.path.join( 'TheMarch/' + app.config['BAND_IMAGE_FOLDER'], file_name) # save file to disk files.save(file_path) thumbnail_detail = file_name new_event = { "userId": ObjectId(current_user.id), "band_name": band_name, "band_type": band_type, "title": title, "thumbnail": thumbnail, "thumbnail_detail": thumbnail_detail, "short_description": short_description, "description": description, "is_important": is_important, "is_approve": is_approve, "created_date": created_date, "created_by": created_by, "score": score } common.current_db.Band_detail.insert(new_event) #update band name in user table common.current_db.User.update({"_id": ObjectId(current_user.id)}, {"$set": { "name": band_name }}) return simplejson.dumps({"result": 'success'}) except Exception, e: print 'error' + str(e) return simplejson.dumps({"result": 'error'})
def update_event_db(): try: event_id = request.form['event_id'] event_type = request.form['event_type'] title = request.form['title'] #thumbnail image old_thumbnail = request.form['old_thumbnail'] thumbnail = old_thumbnail is_empty_thumbnail = request.form['is_empty_thumbnail'] #thumbnail detail image old_thumbnail_detail = request.form['old_thumbnail_detail'] thumbnail_detail = old_thumbnail_detail is_empty_thumbnail_detail = request.form['is_empty_thumbnail_detail'] description = request.form['description'] short_description = request.form['short_description'] is_important = request.form['is_important'] if is_empty_thumbnail == 'false': #delete old thumnail #Delete old thumnail image, database if old_thumbnail != '' and old_thumbnail != 'default.jpg': old_file_path = os.path.join( 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER'], old_thumbnail) if os.path.exists(old_file_path): os.remove(old_file_path) #save thumbnail to server files = request.files['thumbnail_file'] file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER']) file_path = os.path.join( 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER'], file_name) # save file to disk files.save(file_path) thumbnail = file_name if is_empty_thumbnail_detail == 'false': #delete old thumnail detail #Delete old thumnail detail image, database if old_thumbnail_detail != '' and old_thumbnail_detail != 'default.jpg': old_file_path = os.path.join( 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER'], old_thumbnail_detail) if os.path.exists(old_file_path): os.remove(old_file_path) #save thumbnail to server files = request.files['thumbnail_file_detail'] file_name = secure_filename(files.filename) file_name = common.gen_file_name( file_name, 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER']) file_path = os.path.join( 'TheMarch/' + app.config['EVENT_THUMBNAIL_FOLDER'], file_name) # save file to disk files.save(file_path) thumbnail_detail = file_name update_event = { "event_type": event_type, "title": title, "thumbnail": thumbnail, "thumbnail_detail": thumbnail_detail, "short_description": short_description, "description": description, "is_important": is_important, } common.current_db.Event.update({"_id": ObjectId(event_id)}, {"$set": update_event}) return simplejson.dumps({"result": 'success'}) except Exception, e: print 'error' + str(e) return simplejson.dumps({"result": 'error'})