def delete_association(): form = AssociationDeleteForm(request.form) if form.validate(): acn_id = form.acn_id.data acn_name = form.acn_name.data try: association = Associations.query.get(acn_id) new_history = History('del_association',current_user.id, tgt_acn_id= acn_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', association.acn_name) db.session.add(new_content) db.session.delete(association) db.session.commit() except Exception as e: print(e) db.session.rollback() flash('Something went wrong. Refresh the page and try again.','danger') return redirect(url_for('administration_associations.association_settings')) flash('{} was successfully removed'.format(acn_name),'success') return redirect(url_for('administration_associations.association_settings')) flash('Something went wrong. Refresh the page and try again.','danger') return redirect(url_for('administration_associations.association_settings'))
def delete_user(): form = DeleteForm(request.form) if form.validate(): user_id = form.id.data try: user = Users.query.get(int(user_id)) if user.is_deleted: abort(404) except Exception as e: print(e) flash( 'Something went wrong. Please try again after refreshing the page.', 'danger') return redirect(url_for('administration_users.user_settings_get')) if user.is_admin and not current_user.is_master: flash('Only the master admin can delete admin accounts', 'danger') return redirect(url_for('administration_users.user_settings_get')) if user.is_master: flash("You cannot the master admin's account", 'danger') return redirect(url_for('administration_users.user_settings_get')) if user.id == current_user.id: flash('You cannot delete your own account', 'danger') return redirect(url_for('administration_users.user_settings_get')) email = user.email try: new_history = History('delete_user', current_user.id, tgt_user_id=user.id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', email) db.session.add(new_content) db.session.flush() user.is_master = False user.is_admin = False user.is_deleted = True user.delete_date = datetime.utcnow() db.session.commit() except Exception as e: print(e) db.session.rollback() flash( 'User was not successfully removed. Please try again after refreshing the page.', 'danger') return redirect(url_for('administration_users.user_settings_get')) flash('User "{}" was successfully removed.'.format(email), 'success') return redirect(url_for('administration_users.user_settings_get')) flash('Something went wrong. Please refresh the page and try again.', 'danger') return redirect(url_for('administration_users.user_settings_get'))
def add_association(): form = AssociationForm(request.form) if request.method =='POST' and form.validate(): association = Associations(acn_name = form.acn_name.data, acn_loc = form.acn_loc.data, acn_url = form.acn_url.data) try: db.session.add(association) db.session.flush() new_history = History('add_association', current_user.id, tgt_acn_id = association.acn_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', association.acn_name) db.session.add(new_content) db.session.commit() flash('Association was successfully added.','success') return redirect(url_for('administration_associations.add_association')) except Exception as e: print(e) db.session.rollback() flash('Something went wrong. Refresh the page and try again.','danger') return render_template('administration/associations/add_association.html',form=form) return render_template('administration/associations/add_association.html',form=form)
def delete_about(): form = DeleteForm(request.form) if request.method == 'POST' and form.validate(): try: about = AboutInfo.query.get(int(form.id.data)) print(about) if not about: abort(404) except Exception as e: print(e) abort(404) # new_history = History('del_about', 1, tgt_about_id=about.aboutinfo_id) new_history = History('del_about', current_user.id, tgt_about_id=about.aboutinfo_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', "{} {}".format(about.first, about.last)) db.session.add(new_content) s = delete_file_from_s3('{}_{}'.format('about', about.aboutinfo_id), app.config['ABOUT_S3_BUCKET'], app.config["S3_BUCKET"]) if not s: return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' }) try: db.session.delete(about) db.session.commit() flash( 'The employee "{} {}" was successfully deleted'.format( about.first, about.last), 'success') return jsonify({ 'status': 'success', 'msg': 'The employee was successfully deleted', 'reload': True }) except Exception as e: print(e) db.session.rollback() return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' })
def delete_property(): form = DeleteForm(request.form) if not form.validate(): flash('Something went wrong. Please refresh the page and try again.', 'danger') return redirect(url_for('administration_property.property_settings')) try: property_id = int(request.form['id']) except: abort(404) try: property = Properties.query.get(property_id) new_history = History('del_property', current_user.id, tgt_prop_id=property.property_id) db.session.add(new_history) db.session.flush() address = "{}; {}; {}, {} {}".format(property.address_l1, property.address_l2, property.city, property.state, property.zipcode) new_content = HistoryContent(new_history.history_id, 'Identifier', address) db.session.add(new_content) images = property.images if images: for i in images: s = delete_file_from_s3( 'property_{}_{}'.format(property.property_id, i.img_id), app.config['PROPERTY_S3_BUCKET'], app.config["S3_BUCKET"]) if not s: raise db.session.delete(property) db.session.commit() # return jsonify({'status':'success', 'msg':'Property successfully deleted!', 'reload':'true'}) flash('Property was successfully deleted', 'success') return redirect(url_for('administration_property.property_settings')) except Exception as e: print(e) db.session.rollback() flash('Something went wrong. Please refresh the page and try again.', 'danger') return redirect(url_for('administration_property.property_settings'))
def register(token): form = RegisterForm(request.form) try: email = s.loads(token, salt='email_confirm', max_age=60 * 60) except SignatureExpired: alert = { 'status': 'danger', 'msg': 'The token has expired. Ask an administrator to resend the invitation.' } return render_template('administration/general/registration.html', alert=alert, form=form) except: abort(403) if request.method == 'POST' and form.validate(): try: user = Users.query.filter_by(email=email, is_deleted=False).one() user.password = sha256_crypt.encrypt(form.password.data) user.first = form.first_name.data user.last = form.last_name.data user.is_verified = True new_history = History('user_join', user.id, tgt_user_id=user.id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', user.email) db.session.add(new_content) db.session.commit() except Exception as e: print(e) db.session.rollback() flash('Something went wrong. Refresh the page and try again.', 'danger') return render_template('administration/general/registration.html', form=form) flash('Successfully registered!', 'success') return redirect(url_for('administration_general.login')) return render_template('administration/general/registration.html', form=form)
def delete_file(): form = DeleteForm(request.form) if request.method == 'POST' and form.validate(): file = Files.query.get(int(form.id.data)) if not file: abort(404) if file.file_url != "": s = delete_file_from_s3('{}_{}'.format('file', file.file_id), app.config['FILES_S3_BUCKET'], app.config["S3_BUCKET"]) if not s: flash( 'Something went wrong deleting the file. Refresh the page and try again', 'danger') return redirect(url_for('administration_files.file_settings')) try: new_history = History('del_file', current_user.id, tgt_file_id=file.file_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', file.file_name) db.session.add(new_content) db.session.delete(file) db.session.commit() flash('File was successfully deleted.', 'success') return redirect(url_for('administration_files.file_settings')) except Exception as e: print(e) db.session.rollback() flash( 'Something went wrong deleting the file. Refresh the page and try again', 'danger') return redirect(url_for('administration_files.file_settings')) flash( 'Something went wrong deleting the file. Refresh the page and try again', 'danger') return redirect(url_for('administration_files.file_settings'))
def add_email(): form = EmailForm(request.form) role_name = form.role.data email = form.email.data if form.validate(): new_email=None try: check = Emails.query.filter_by(email=email).first() if check: new_email=check else: new_email = Emails(email) db.session.add(new_email) db.session.flush() role = Roles.query.filter_by(role_name = role_name).one() role.add_email(new_email) new_history = History('add_email', current_user.id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', email) db.session.add(new_content) db.session.commit() except Exception as e: print(e) db.session.rollback() flash('Something went wrong. Refresh the page and try again.', 'danger') return redirect(url_for('administration_emails.email_settings')) flash('Email was successfully added!','success') return redirect(url_for('administration_emails.email_settings')) msg="" if form.email.errors: msg = form.email.errors[0] else: msg = 'Something went wrong. Refresh the page and try again.' flash(msg,'danger') return redirect(url_for('administration_emails.email_settings'))
def delete_email(): form = EmailForm(request.form) role_name = form.role.data form_email = form.email.data if form.validate(): try: email = Emails.query.filter_by(email=form_email).one() role = Roles.query.filter_by(role_name = role_name).one() role_emails = [r.email for r in role.emails] if len(role_emails) <= 1: flash('You must have at least one recipient for each form.','danger') return redirect(url_for('administration_emails.email_settings')) else: role.remove_email(email) email_roles = [e.role_name for e in email.roles] if len(email_roles) <= 0: db.session.delete(email) new_history = History('del_email', current_user.id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', form_email) db.session.add(new_content) db.session.commit() except Exception as e: print(e) db.session.rollback() flash('Something went wrong. Refresh the page and try again.','danger') return redirect(url_for('administration_emails.email_settings')) flash('Email was removed successfully.','success') return redirect(url_for('administration_emails.email_settings')) flash('Something went wrong. Refresh the page and try again.','danger') return redirect(url_for('administration_emails.email_settings'))
def add_file(): form = FileForm(request.form) if request.method == 'POST' and form.validate(): if "user_file" not in request.files: flash('Please select a file.', 'danger') return render_template('administration/files/add_file.html', form=form) upload = request.files["user_file"] if upload.filename == "": flash('Please select a file.', 'danger') return render_template('administration/files/add_file.html', form=form) if upload and allowed_file(upload.filename, ALLOWED_EXTENSIONS): file = Files.query.filter_by(file_name=form.file_name.data).first() if file: flash('A file by this name already exists!', 'danger') return render_template('administration/files/add_file.html', form=form) try: file = Files(form.file_name.data, "") db.session.add(file) db.session.flush() upload.filename = 'file_{}'.format(file.file_id) #if filename is the same, the file is overwritten output = upload_file_to_s3(upload, app.config['FILES_S3_BUCKET'], app.config["S3_BUCKET"]) # print(output) if not output: flash( 'Something went wrong uploading the file. Please refresh the page and try again.', 'danger') return render_template( 'administration/files/add_file.html', form=form, file=file) new_history = History('add_file', current_user.id, tgt_file_id=file.file_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', form.file_name.data) db.session.add(new_content) new_content = HistoryContent(new_history.history_id, 'File', output) db.session.add(new_content) file.file_url = output db.session.commit() flash('File was successfully added!', 'success') return redirect(url_for('administration_files.add_file')) except Exception as e: # print(e) db.session.rollback() flash( 'Something went wrong uploading the file. Please refresh the page and try again.', 'danger') return render_template('administration/files/add_file.html', form=form) else: flash('Only pdf files are accepted.', 'danger') return render_template('administration/files/add_file.html', form=form) return render_template('administration/files/add_file.html', form=form)
def edit_file(id): try: file = Files.query.get(int(id)) if not file: abort(404) except: abort(404) form = FileForm(request.form) if request.method == 'POST' and form.validate(): upload = None if "user_file" in request.files: upload = request.files["user_file"] if not upload and file.file_name == form.file_name.data: flash('No changes were made!', 'danger') return render_template('administration/files/edit_file.html', form=form, file=file) if file.file_name != form.file_name.data and Files.query.filter_by( file_name=form.file_name.data).first(): flash('A file by this name already exists!', 'danger') return render_template('administration/files/edit_file.html', form=form, file=file) if upload and (upload.filename == "" or not allowed_file(upload.filename, ALLOWED_EXTENSIONS)): flash( 'Only {} files are accepted.'.format( ', '.join(ALLOWED_EXTENSIONS)), 'danger') return render_template('administration/files/edit_file.html', form=form, file=file) print(upload) output = None if upload: upload.filename = '{}_{}'.format('file', file.file_id) output = upload_file_to_s3(upload, app.config['FILES_S3_BUCKET'], app.config["S3_BUCKET"]) if not output: flash( 'Something went wrong uploading the file. Please refresh the page and try again.', 'danger') return render_template('administration/files/edit_file.html', form=form, file=file) try: new_history = History('edit_file', current_user.id, tgt_file_id=file.file_id) db.session.add(new_history) db.session.flush() if file.file_name != form.file_name.data: print('file name not equal') new_content = HistoryContent(new_history.history_id, 'Identifier', form.file_name.data) db.session.add(new_content) new_content = HistoryContent( new_history.history_id, 'File Name', '"{}" to "{}"'.format(file.file_name, form.file_name.data)) db.session.add(new_content) file.file_name = form.file_name.data else: print('file name equal') new_content = HistoryContent(new_history.history_id, 'Identifier', file.file_name) db.session.add(new_content) if upload: file.file_url = output new_content = HistoryContent(new_history.history_id, 'File', output) db.session.add(new_content) print('upload file') db.session.commit() flash('File was successfully edited!', 'success') return redirect(url_for('administration_files.edit_file', id=id)) except Exception as e: print(e) db.session.rollback() flash( 'Something went wrong uploading the file. Please refresh the page and try again.', 'danger') return render_template('administration/files/edit_file.html', form=form, file=file) return render_template('administration/files/edit_file.html', form=form, file=file)
def edit_association(acn_id): form = AssociationEditForm(request.form) acn = Associations.query.get(acn_id) if not acn: abort(404) if request.method == 'POST' and form.validate(): new_name = form.acn_name.data new_loc = form.acn_loc.data new_url = form.acn_url.data if acn.acn_name.lower() == new_name.lower(): pass else: associations = Associations.query.all() for a in associations: if a.acn_name.lower() == new_name.lower(): flash('This association name already exists!','danger') return render_template('administration/associations/edit_association.html',form=form, acn=acn) try: content = [] for p in acn.__dict__: for f in form.__dict__.keys(): if p == f: if acn.__dict__[p] != form.__dict__[p].data: content.append({ form.__dict__[p].label.text: "\"{}\" to \"{}\"".format(acn.__dict__[p], form.__dict__[p].data) }) setattr(acn, p, form.__dict__[p].data) new_history = History('edit_association', current_user.id, tgt_acn_id=acn.acn_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', acn.acn_name) db.session.add(new_content) if len(content) >= 1: for c in content: for k in c: new_content = HistoryContent(new_history.history_id, k, c[k]) db.session.add(new_content) # acn.acn_name = new_name # acn.acn_loc = new_loc # acn.acn_url = new_url db.session.commit() flash('Association changes were saved!','success') return redirect(url_for('administration_associations.edit_association', acn_id=acn_id)) except Exception as e: print(e) db.session.rollback() flash('Something went wrong. Please refresh the page and try again.','danger') return render_template('administration/associations/edit_association.html',form=form, acn=acn) return render_template('administration/associations/edit_association.html',form=form, acn=acn)
def add_property(): msg = 'Something went wrong uploading the file. Please refresh the page and try again.' form = PropertyForm(request.form) if request.method == 'POST' and form.validate(): # make sure file is a valid file if 'user_file' not in request.files: return jsonify({ 'status': 'danger', 'msg': 'You must upload an image.' }) uploads = request.files.getlist('user_file') if not uploads: return jsonify({ 'status': 'danger', 'msg': 'You must upload an image.' }) for file in uploads: if file.filename == '': del uploads[uploads.index(file)] # return jsonify({'status':'danger','msg':'Please upload an image.'}) if not allowed_file(file.filename, ALLOWED_EXTENSIONS): return jsonify({ 'msg': 'Only {} files are accepted.'.format( ', '.join(ALLOWED_EXTENSIONS)), 'status': 'danger' }) try: #initiate property data property = Properties(form.data) db.session.add(property) db.session.flush() #initiatiate history data address = "{}; {}; {}, {} {}".format(form.address_l1.data, form.address_l2.data, form.city.data, form.state.data, form.zipcode.data) new_history = History('add_property', current_user.id, tgt_prop_id=property.property_id) db.session.add(new_history) db.session.flush() #record history of new property content = [] for f in form.__dict__['_fields'].keys(): content.append( {form.__dict__[f].label.text: form.__dict__[f].data}) print(content) new_content = HistoryContent(new_history.history_id, 'Identifier', address) db.session.add(new_content) for c in content: for k in c: new_content = HistoryContent(new_history.history_id, k, c[k]) db.session.add(new_content) print('here') #upload pictures. if successful, record history for file in uploads: image = PropertyImgs(property.property_id, "") db.session.add(image) db.session.flush() property.add_image(image) file.filename = "property_{}_{}".format( property.property_id, image.img_id) output = upload_file_to_s3(file, app.config['PROPERTY_S3_BUCKET'], app.config["S3_BUCKET"]) if not output: raise else: image.img_url = output new_content = HistoryContent(new_history.history_id, 'Image', output) db.session.add(new_content) db.session.commit() flash('Property was successfully added!', 'success') return jsonify({ 'status': 'success', 'msg': 'Property was successfully added!', 'reload': True }) except Exception as e: print(e) db.session.rollback() return jsonify({'msg': msg, 'status': 'danger'}) if form.errors: return jsonify({ 'status': 'danger', 'msg': 'There were errors in the form.', 'form_errors': form.errors }) else: return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' })
def add_about_post(): form = AboutInfoForm(request.form) if request.method == 'POST' and form.validate(): if not "user_file" in request.files: return jsonify({ 'status': 'danger', 'msg': 'You must upload an image.' }) upload = request.files['user_file'] if upload.filename == "": return jsonify({ 'status': 'danger', 'msg': 'Please upload an image.' }) if upload and allowed_file(upload.filename, ALLOWED_EXTENSIONS): try: new = AboutInfo(form.first.data, form.last.data, form.position.data, form.email.data, form.phone.data, "") db.session.add(new) db.session.flush() upload.filename = "about_{}".format(new.aboutinfo_id) output = upload_file_to_s3(upload, app.config['ABOUT_S3_BUCKET'], app.config["S3_BUCKET"]) if not output: raise new.img_url = output # new_history = History('add_about', 1, tgt_about_id=new.aboutinfo_id) new_history = History('add_about', current_user.id, tgt_about_id=new.aboutinfo_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent( new_history.history_id, 'Identifier', "{} {}".format(form.first.data, form.last.data)) db.session.add(new_content) for f in form.__dict__['_fields'].keys(): col_name = form.__dict__[f].label.text col_content = form.__dict__[f].data new_content = HistoryContent(new_history.history_id, col_name, col_content) db.session.add(new_content) new_content = HistoryContent(new_history.history_id, 'Image', output) db.session.add(new_content) db.session.commit() flash('The employee was successfully added', 'success') return jsonify({ 'status': 'success', 'msg': 'The employee was successfully added', 'reload': True }) except Exception as e: print(e) db.session.rollback() return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' }) else: return jsonify({ 'msg': 'Only {} files are accepted.'.format( ', '.join(ALLOWED_EXTENSIONS)), 'status': 'danger' }) if form.errors: return jsonify({ 'status': 'danger', 'msg': 'There were errors in the form.', 'form_errors': form.errors }) else: return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' })
def edit_about_post(id): try: about = AboutInfo.query.get(int(id)) if not about: abort(404) except: abort(404) form = AboutInfoForm(request.form) if request.method == 'POST' and form.validate(): upload = None if "user_file" in request.files: upload = request.files['user_file'] if upload.filename == "": return jsonify({ 'status': 'danger', 'msg': 'Please upload an image.' }) if not upload or not allowed_file(upload.filename, ALLOWED_EXTENSIONS): return jsonify({ 'msg': 'Only {} files are accepted.'.format( ', '.join(ALLOWED_EXTENSIONS)), 'status': 'danger' }) content = [] # print(dir(about)) for p in about.__dict__: for f in form.__dict__['_fields'].keys(): if p == f: if about.__dict__[p] != form.__dict__[p].data: content.append({ form.__dict__[p].label.text: "\"{}\" to \"{}\"".format(about.__dict__[p], form.__dict__[p].data) }) setattr(about, p, form.__dict__[p].data) if len(content) <= 0 and not upload: return jsonify({ 'status': 'danger', 'msg': 'No changes were made.' }) # new_history = History('edit_about', 1, tgt_about_id=about.aboutinfo_id) new_history = History('edit_about', current_user.id, tgt_about_id=about.aboutinfo_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent( new_history.history_id, 'Identifier', "{} {}".format(form.first.data, form.last.data)) db.session.add(new_content) for c in content: for k in c: new_content = HistoryContent(new_history.history_id, k, c[k]) db.session.add(new_content) if upload: print('here') upload.filename = "about_{}".format(about.aboutinfo_id) # s = delete_file_from_s3('{}_{}'.format('about', about.aboutinfo_id), app.config['ABOUT_S3_BUCKET'], app.config["S3_BUCKET"]) # if not s: # raise output = upload_file_to_s3(upload, app.config['ABOUT_S3_BUCKET'], app.config["S3_BUCKET"]) if output: print(output) new_content = HistoryContent(new_history.history_id, 'Image', output) about.img_url = output db.session.add(new_content) else: return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' }) try: db.session.commit() flash('The employee was successfully edited', 'success') return jsonify({ 'status': 'success', 'msg': 'The employee was successfully edited', 'reload': True }) except Exception as e: print(e) db.session.rollback() return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' }) if form.errors: return jsonify({ 'status': 'danger', 'msg': 'There were errors in the form.', 'form_errors': form.errors }) else: return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' })
def edit_property(property_id): form = PropertyForm(request.form) property = Properties.query.get(property_id) if not property: abort(404) if request.method == 'POST' and form.validate(): try: filelist = request.form.getlist('file-order') newlist = request.files.getlist('user_file') if not filelist: return jsonify({ 'status': 'error', 'msg': 'Something went wrong. Please refresh the page and try again.' }) currentimgs = [] imglist = property.images if imglist: for i in imglist: currentimgs.append(i.img_id) # check for changes in images change = False if len(filelist) != len(currentimgs): change = True else: for i, j in zip(currentimgs, filelist): if j == 'new': change = True break elif i != int(j): change = True break #figure out which columns have changed and set values and record history content = [] for p in property.__dict__: for f in form.__dict__.keys(): if p == f: if property.__dict__[p] != form.__dict__[p].data: content.append({ form.__dict__[p].label.text: "\"{}\" to \"{}\"".format( property.__dict__[p], form.__dict__[p].data) }) setattr(property, p, form.__dict__[p].data) address = "{}; {}; {}, {} {}".format(property.address_l1, property.address_l2, property.city, property.state, property.zipcode) new_history = None if len(content) >= 1 or change: #record history if there are changes new_history = History('edit_property', current_user.id, tgt_prop_id=property.property_id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', address) db.session.add(new_content) for c in content: for k in c: new_content = HistoryContent(new_history.history_id, k, c[k]) db.session.add(new_content) else: return jsonify({ 'status': 'danger', 'msg': 'No changes were specified.' }) #add new pictures and redetermine order. if change: new_content = HistoryContent(new_history.history_id, 'Images', 'Images edited') db.session.add(new_content) newcount = 0 for filenum in filelist: if filenum == 'new': newfile = newlist[newcount] if newfile.filename == '' or not allowed_file( newfile.filename, ALLOWED_EXTENSIONS): return jsonify({ 'msg': 'Only {} files are accepted.'.format( ', '.join(ALLOWED_EXTENSIONS)), 'status': 'danger' }) image = PropertyImgs(property.property_id, "") db.session.add(image) db.session.flush() property.add_image(image) newfile.filename = "property_{}_{}".format( property.property_id, image.img_id) output = upload_file_to_s3( newfile, app.config['PROPERTY_S3_BUCKET'], app.config["S3_BUCKET"]) if not output: raise else: image.img_url = output new_content = HistoryContent( new_history.history_id, 'Image', output) db.session.add(new_content) newcount += 1 else: img_id = int(filenum) image = PropertyImgs.query.get(img_id) if not image or image.property_id != property.property_id: raise image.date_added = datetime.utcnow() del currentimgs[currentimgs.index(image.img_id)] # delete deleted images for i in currentimgs: del_img = PropertyImgs.query.get(int(i)) if not del_img or del_img.property_id != property.property_id: raise s = delete_file_from_s3( 'property_{}_{}'.format(property.property_id, del_img.img_id), app.config['PROPERTY_S3_BUCKET'], app.config["S3_BUCKET"]) if not s: raise else: new_content = HistoryContent(new_history.history_id, 'Image', 'deleted') db.session.add(new_content) db.session.delete(del_img) db.session.commit() flash('Property was successfully edited', 'success') return jsonify({ 'status': 'success', 'msg': 'Property was successfully edited', 'reload': True }) except Exception as e: print(e) db.session.rollback() return jsonify({ 'status': 'error', 'msg': 'Something went wrong. Please refresh the page and try again.' }) if form.errors: return jsonify({ 'status': 'danger', 'msg': 'There were errors in the form.', 'form_errors': form.errors }) else: return jsonify({ 'status': 'danger', 'msg': 'Something went wrong. Please refresh the page and try again.' })
def create_user(): form = CreateUser(request.form) if request.method == 'POST' and form.validate(): email = (form.email.data).strip() check = Users.query.filter_by(email=email, is_deleted=False).first() if check: flash( 'This email is already used by a user or is waiting validation.', 'danger') return render_template('administration/users/create_user.html', form=form) user = Users(email=email) token = s.dumps(email, salt='email_confirm') msg = Message('Confirm Email', sender=MAIL_USERNAME, recipients=[email]) link = url_for('administration_general.register', token=token, external=True) msg.html = 'Go to <a href="{}{}">this</a> link to register as a moderator for Huntington West Properties website.'.format( request.url_root, link) # msg.body = 'Go to this link to register as a moderator for Huntington West Properties website: {}'.format(link) try: db.session.add(user) db.session.flush() new_history = History('add_user', current_user.id, tgt_user_id=user.id) db.session.add(new_history) db.session.flush() new_content = HistoryContent(new_history.history_id, 'Identifier', email) db.session.add(new_content) db.session.commit() except Exception as e: db.session.rollback() flash('Something went wrong. Refresh the page and try again.', 'danger') return render_template('administration/users/create_user.html', form=form) try: mail.send(msg) except: flash('Invitation failed to send. Refresh and try again.', 'danger') return render_template('administration/users/create_user.html', form=form) flash('Invitation successfully sent!', 'success') return redirect(url_for('administration_users.create_user')) return render_template('administration/users/create_user.html', form=form)
def consultation_form_post(): form = ConsultationForm(request.form) if not form.validate(): return jsonify({'status': 'error', 'errors': form.errors}) name = form.first_name.data + " " + form.last_name.data email_content = html_consultation_form(name=name, email=form.email.data, phone_num=form.phone_num.data, regarding=form.regarding.data, msg=form.msg.data) try: receivers = Roles.query.filter_by(role_name='Consultation Form').one() except: return jsonify({ 'status': 'error', 'msg': 'Something went wrong. Please refresh the page and try again.' }) recipients = [r.email for r in receivers.emails] msg = Message('"{}" Consultation Form Submission'.format(name), sender=app.config['MAIL_USERNAME'], recipients=recipients) msg2 = Message('Consultation Form Submission Receipt', sender=app.config['MAIL_USERNAME'], recipients=[form.email.data]) msg.html = email_content msg2.html = "Thank you for contacting Huntington West Properties. Someone will be in contact with you shortly.<br><br>{}".format( email_content) try: new_history = History('consultation_form', None) db.session.add(new_history) db.session.flush() id_content = HistoryContent(new_history.history_id, 'Identifier', 'Consultation Form Submission') name_content = HistoryContent(new_history.history_id, 'Name', name) email_content = HistoryContent(new_history.history_id, 'Email', form.email.data) phone_content = HistoryContent(new_history.history_id, 'Phone Number', form.phone_num.data) regarding_content = HistoryContent(new_history.history_id, 'Subject', form.regarding.data) msg_content = HistoryContent(new_history.history_id, 'Message', form.msg.data) db.session.add(id_content) db.session.add(name_content) db.session.add(email_content) db.session.add(phone_content) db.session.add(regarding_content) db.session.add(msg_content) db.session.commit() mail.send(msg) mail.send(msg2) except Exception as e: print(e) db.session.rollback() return jsonify({ 'status': 'error', 'msg': 'Form failed to send. Please try again.' }) return jsonify({ 'status': 'success', 'msg': 'Consultation form was successfully sent! Someone will contact you soon.' })