def register(): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) form = RegistrationForm() # We will set a temp password for the new user defaultPass = "******" if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(defaultPass).decode( 'utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password, fName=form.fName.data, lName=form.lName.data) db.session.add(user) db.session.commit() flash('Account has been created! They are now able to log in', 'success') # Here we want to send an email letting them know their account has been created # And ask them to please reset their password on login send_welcome_email(user) return redirect(url_for('main_bp.home')) return render_template('register.html', title='Register a new User', form=form)
def new_project_simple(): # Must be Manager or greater if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) form = NewProjectSimpleForm() if form.validate_on_submit(): project = Project( name=form.name.data, client_id=form.client_id.data.id, site_id=form.site_id.data.id, # default status to inprogress status_id=3, typeOfWork_id=form.typeOfWork_id.data.id, description=form.description.data, date_start=form.date_start.data, damage_comment=form.damage_comment.data, extenuating_circumstances=form.extenuating_circumstances.data) db.session.add(project) db.session.commit() flash('New Project Added Successfully!', 'success') return redirect(url_for('projects_bp.projects')) return render_template('project_new_simple.html', title='Add New Project', form=form, legend='Add a New Project')
def area_filter(cur_proj_id): # Must be Admin if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) proj = Project.query.get_or_404(cur_proj_id) # Get Project from url site = Site.query.get(proj.site_id) # get site of project # This is a drop down filter showing all areas in given site. filterForm = AreaFilterForm() filterForm.areas.query = Area.query.filter_by(site_id=proj.site_id) if filterForm.validate_on_submit(): selected_area_id = filterForm.areas.data.id flash('Area Filter Applied', 'success') return redirect( url_for( 'project_bp.add_room_to_project', title='Add Rooms to a Projects', legend='Add Rooms to a Project', # Drop filterForm? selected_area_id=selected_area_id, proj=proj, filterForm=filterForm, site=site, cur_proj_id=cur_proj_id)) return render_template('project_area_filter.html', title='Add Rooms to a Project', legend='Select Area to filter by', cur_proj_id=cur_proj_id, site=site, filterForm=filterForm, proj=proj)
def admin_create_ts_entry(): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) form=TimesheetForm() if form.validate_on_submit(): employee = User.query.get(form.user_id.data.id) proj = Project.query.get(form.project_id.data.id) ts = Timesheet(date_submit=datetime.utcnow(), user_id= employee.id, date_of_work = form.date_of_work.data, project_id = form.project_id.data.id, hours = form.hours.data, comment = form.comment.data) db.session.add(ts) # Add to project_timesheet table proj.timesheets.append(ts) # Add to UserTimesheet table ts.user.append(employee) db.session.commit() msg = f'Timesheet entry created successfully' flash(msg, 'success') return redirect(url_for('admin_bp.admin_review')) return render_template("admin_create_TS.html", form=form)
def site_area_list(cur_site_id): if not roleAuth('Client'): return redirect(url_for('main_bp.home')) site = Site.query.get_or_404( cur_site_id) # get current site from the site_id in url areas = Area.query.filter_by(site_id=site.id).order_by(Area.name).all() # Save the starting directory startDir = os.getcwd() # change to static/area_color_sheets cs_dir = os.path.join(current_app.root_path, 'static/area_color_sheets') os.chdir(cs_dir) for area in areas: cs_list = area.color_sheets for cs in cs_list: cs_fn = cs.name cs_path = os.path.join(current_app.root_path, 'static/area_color_sheets', cs_fn) # check if it is on local file system if not os.path.isfile(cs_path): # If the color sheet isn't stored locally, # go and download it from amazon S3 bucket download_file(cs_fn, colorSheetBucket, cs_fn) print('downloaded ' + str(cs_fn) + ' from AWS S3 Bucket') # return to the starting dir os.chdir(startDir) return render_template('site_area_list.html', title='Area List', areas=areas, site=site)
def admin_timesheet_update(ts_id): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) ts = Timesheet.query.get(ts_id) form = TimesheetForm() user = ts.user.first() if form.validate_on_submit(): ts.date_of_work = form.date_of_work.data ts.project_id = form.project_id.data.id ts.hours = form.hours.data ts.comment = form.comment.data db.session.commit() msg = f'Timesheet for {ts.user.first().username} updated successfully' flash(msg, 'success') return redirect(url_for('admin_bp.admin_review')) elif request.method== 'GET': form.date_of_work.data = ts.date_of_work form.project_id.data = Project.query.get(ts.project_id) form.hours.data = ts.hours form.comment.data = ts.comment return render_template("timesheet_update.html", ts_id=ts.id, form=form, user=user)
def new_project(): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) form = NewProjectForm() if form.validate_on_submit(): project = Project( name=form.name.data, client_id=form.client_id.data.id, site_id=form.site_id.data.id, status_id=form.status_id.data.id, typeOfWork_id=form.typeOfWork_id.data.id, description=form.description.data, date_start=form.date_start.data, target_end_date=form.target_end_date.data, hours_estimate=form.hours_estimate.data, # area_list= form.area_list.data, quote_amt=form.quote_amt.data, damage_comment=form.damage_comment.data, extenuating_circumstances=form.extenuating_circumstances.data) db.session.add(project) db.session.commit() flash('New Project Added Successfully!', 'success') return redirect(url_for('main_bp.home')) return render_template('project_new.html', title='Add New Project', form=form, legend='Add a New Project')
def client_list(): if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) clients = Client.query.all() return render_template('client_list.html', title="Client List", clients=clients)
def admin_unapprove(ts_id): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) ts = Timesheet.query.get(ts_id) ts.approved = False db.session.commit() return redirect(url_for('admin_bp.admin_review'))
def update_project(cur_proj_id): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) proj = Project.query.get_or_404(cur_proj_id) proj_site_id = proj.site_id site = Site.query.get(proj_site_id) orig_status = proj.status_id form = UpdateProjectForm() # This allows me to change the query and apply a filter to it for the page! # form.area_list.query = Area.query.filter_by(site_id=proj.site_id) if form.validate_on_submit(): proj.name = form.name.data proj.client_id = form.client_id.data.id proj.site_id = form.site_id.data.id proj.status_id = form.status_id.data.id proj.typeOfWork_id = form.typeOfWork_id.data.id proj.description = form.description.data proj.date_start = form.date_start.data proj.target_end_date = form.target_end_date.data proj.hours_estimate = form.hours_estimate.data proj.quote_amt = form.quote_amt.data proj.damage_comment = form.damage_comment.data proj.extenuating_circumstances = form.extenuating_circumstances.data # proj.area_list = form.area_list.data # Check if the project status has been changed and update accordingly check_status_change(proj, orig_status) # commit changes db.session.commit() flash('Project Updated Successfully!', 'success') return redirect(url_for('project_bp.project_list')) elif request.method == 'GET': form.name.data = proj.name form.client_id.data = Client.query.get(proj.client_id) form.site_id.data = Site.query.get(proj.site_id) form.status_id.data = Status.query.get(proj.status.id) form.typeOfWork_id.data = Worktype.query.get(proj.typeOfWork_id) form.description.data = proj.description form.date_start.data = proj.date_start form.target_end_date.data = proj.target_end_date form.hours_estimate.data = proj.hours_estimate form.quote_amt.data = proj.quote_amt form.damage_comment.data = proj.damage_comment form.extenuating_circumstances.data = proj.extenuating_circumstances # form.area_list.data = proj.area_list.filter_by(site_id=proj_site_id) return render_template('project_update.html', title='Update Project', form=form, legend='Update Project', site=site, proj=proj)
def delete_ts_entry(ts_id): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) ts = Timesheet.query.get_or_404(ts_id) db.session.delete(ts) db.session.commit() flash('The timesheet entry has been deleted!', 'success') return redirect(url_for('admin_bp.admin_review'))
def admin_payroll_review(): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) # Build the Most recent weeks Payroll Emails tsdf = get_timesheets() sub1, msg1, m1 = timesheet_to_email_summarized(tsdf) sub2, msg2, m2 = timesheet_to_email(tsdf) return render_template("admin_payroll_review.html", title="Admin", sub1=sub1, msg1=msg1, sub2=sub2, msg2=msg2)
def area_room_list(cur_site_id, cur_area_id): if not roleAuth('Client'): return redirect(url_for('main_bp.home')) site = Site.query.get_or_404( cur_site_id) # get current site from the site_id in url area = Area.query.get_or_404( cur_area_id) # get current area from the area_id in url rooms = Room.query.filter_by(area_id=area.id).all() return render_template('rooms_by_area.html', title='Room List', rooms=rooms, area=area, site=site)
def area_update(cur_site_id, cur_area_id): if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) site = Site.query.get_or_404(cur_site_id) # Get current site_id from URL area = Area.query.get_or_404(cur_area_id) # Get current area_id form = UpdateAreaForm() if form.validate_on_submit(): if form.color_sheet.data: print("color_sheet has data") picture_file, picture_path = save_area_picture( form.color_sheet.data, area) print("filename:" + picture_file) # Define the S3 Bucket upload_file(picture_path, colorSheetBucket, picture_file) # Create the entry into the ColorSheet Table cs = ColorSheet(area_id=area.id, name=picture_file) db.session.add(cs) area.color_sheets.append(cs) area.color_sheet = picture_file print("Picture saved") # Check for form data AND that it is different than the current area.site_id if (form.site_id.data and form.site_id.data != area.site_id): updated_site = Site.query.filter_by( id=form.site_id.data.id).first() area.site_id = updated_site.id area.name = form.name.data area.code = form.code.data area.building = form.building.data area.level = form.level.data area.descriptor = form.descriptor.data db.session.commit() flash('The Area has been updated!', 'success') return redirect(url_for('site_bp.site_list')) elif request.method == 'GET': # site_id was blank when loading page, despite being set below... form.site_id.data = Site.query.get(area.site_id) form.name.data = area.name form.code.data = area.code form.building.data = area.building form.level.data = area.level form.descriptor.data = area.descriptor # add color_sheet so it displays? # color_sheet = url_for('static', filename='area_color_sheets/' + area.color_sheet) return render_template('area_update.html', title='Update Area', form=form, legend='Update Area', site=site, area=area)
def areasJson(site_id): if not roleAuth('Client'): return redirect(url_for('main_bp.home')) # Get all brands for the given supplier areas = Area.query.filter_by(site_id=site_id).all() # Create an empty list areaArray = [] # go through returned brands and append as obj to list for area in areas: areaObj = {} areaObj['id'] = area.id areaObj['name'] = area.name areaArray.append(areaObj) # Return 'brands' of given supplier as json data return jsonify({'areas': areaArray})
def admin_review(): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) # Review / Approve / Edit Employee Time Entries # We want to get those timesheets within the last month / 30 days. numDays = 30 # using 90 because we don't have anything more recent. startDate = datetime.now().date() endDate = startDate-timedelta(days=numDays) # Get the last 30 days worth of Timesheets tss = Timesheet.query.filter(and_(Timesheet.date_of_work <= startDate, Timesheet.date_of_work >= endDate)).order_by(Timesheet.approved.asc()).order_by(Timesheet.date_of_work.desc()).order_by(Timesheet.user_id) return render_template("admin_review.html", title="Timesheet Review", tss=tss)
def new_client(): if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) form = NewClientForm() if form.validate_on_submit(): client = Client(name=form.name.data, contactName=form.contactName.data, contactEmail=form.contactEmail.data, contactPhone=form.contactPhone.data) db.session.add(client) db.session.commit() flash('A new Client has been created!', 'success') return redirect(url_for('site_bp.client_list')) return render_template('client_new.html', title='Add New Client', form=form, legend='Add New Client')
def new_site(): if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) form = NewSiteForm() if form.validate_on_submit(): site = Site(name=form.name.data, code=form.code.data, addr_str=form.addr_str.data, city=form.city.data) db.session.add(site) db.session.commit() flash('A new Site has been created!', 'success') return redirect(url_for('site_bp.site_list')) return render_template('site_new.html', title='Add New Site', form=form, legend='Add New Site')
def as_needed(cur_site_id): if not roleAuth('Client'): return redirect(url_for('main_bp.home')) site = Site.query.get_or_404(cur_site_id) site_id = site.id name = site.name if site_id not in [1, 2, 5]: flash('We do not have a schedule for ' + name, 'warning') return redirect(url_for('site_bp.site_area_list', cur_site_id=site_id)) as_needed_rooms = build_asneededDF(site_id) return render_template('site_as_needed.html', tables=[as_needed_rooms.to_html(classes='data')], titles=as_needed_rooms.columns.values, title='As Needed Rooms', legend='As Needed Rooms', site=site)
def clientProjects(): """ This route returns a JSON obj of the specific project data for projects that fit the filter criteria. """ # This needs to be set the same as the project_list if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) # Grab the ?params from the URL, default None if missing cl_id = request.args.get('cl_id', None) si_id = request.args.get('si_id', None) st_id = request.args.get('st_id', None) tw_id = request.args.get('tw_id', None) sda = request.args.get('sda', None) sdb = request.args.get('sdb', None) fda = request.args.get('fda', None) fdb = request.args.get('fdb', None) # Get all projects for the given form data project_id_list = build_filtered_project_list(cl_id, si_id, st_id, tw_id, sda, sdb, fda, fdb) projects = Project.query.filter(Project.id.in_(project_id_list)).order_by( desc(Project.date_start)).all() # Create an empty list projectArray = [] # go through returned brands and append as obj to list for project in projects: projectObj = {} projectObj['id'] = project.id projectObj['name'] = project.name projectObj['view_url'] = (url_for('project_bp.view_project', cur_proj_id=project.id)) if current_user.access_level == 7: projectObj['update_url'] = (url_for('project_bp.update_project', cur_proj_id=project.id)) elif current_user.access_level == 5: projectObj['update_url'] = (url_for( 'project_bp.update_project_simple', cur_proj_id=project.id)) projectObj['description'] = project.description projectArray.append(projectObj) # Return 'brands' of given supplier as json data return jsonify({'projects': projectArray})
def admincp(): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) # get list of Employees, show they're average daily, weekly, monthly hours (some stats) users = User.query.all() # Add new Employee # We need to be able to change Users' attributes from the control panel. # Update Access Level and isEmployed # Review / Approve / Edit Employee Time Entries # done on a different Route return render_template("admin_cp.html", title="Admin", users=users)
def new_area(): if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) form = NewAreaForm() if form.validate_on_submit(): area = Area(name=form.name.data, code=form.code.data, site_id=form.site_id.data.id, building=form.building.data, level=form.level.data, descriptor=form.descriptor.data) db.session.add(area) db.session.commit() flash('New Area Added Successfully!', 'success') return redirect(url_for('main_bp.home')) return render_template('area_new.html', title='Add New Area', form=form, legend='Add a New Area')
def room_update(cur_room_id): if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) # We don't really need the site or area for this... room = Room.query.get_or_404(cur_room_id) # get the room for editting # The room has the site_id and area_id values in it # We'll grab these for the redirect site_id = room.site_id area_id = room.area_id form = RoomForm() if form.validate_on_submit(): room.bm_id = form.bm_id.data room.name = form.name.data room.location = form.location.data room.description = form.description.data room.date_last_paint = form.date_last_paint.data room.freq = form.freq.data room.site_id = form.site_id.data.id room.area_id = form.area_id.data.id db.session.commit() flash('The Room has been updated!', 'success') return redirect( url_for('site_bp.area_room_list', cur_site_id=site_id, cur_area_id=area_id)) elif request.method == 'GET': form.bm_id.data = room.bm_id form.name.data = room.name form.location.data = room.location form.description.data = room.description form.site_id.data = Site.query.get(room.site_id) form.area_id.data = Area.query.get(room.area_id) form.freq.data = room.freq form.date_last_paint.data = room.date_last_paint return render_template('room_update.html', title='Update Room', legend='Update Room Info', room=room, form=form)
def add_area(cur_site_id): if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) site = Site.query.get_or_404(cur_site_id) form = NewAreaForm() if form.validate_on_submit(): area = Area(name=form.name.data, code=form.code.data, site_id=site.id, building=form.building.data, level=form.level.data, descriptor=form.descriptor.data) db.session.add(area) db.session.commit() flash('New Area Added Successfully!', 'success') return redirect(url_for('site_bp.site_area_list', cur_site_id=site.id)) return render_template('add_area_to_site.html', title='Add New Area', site=site, form=form)
def new_room(): if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) form = RoomForm() if form.validate_on_submit(): room = Room(bm_id=form.bm_id.data, name=form.name.data, location=form.location.data, description=form.description.data, date_last_paint=form.date_last_paint.data, freq=form.freq.data, site_id=form.site_id.data.id, area_id=form.area_id.data.id) db.session.add(room) db.session.commit() flash('New Room Added Successfully!', 'success') return redirect(url_for('main_bp.home')) return render_template('room_new.html', title='Add New Room', form=form, legend='Add a New Room')
def view_project(cur_proj_id): # Must be employee or higher if not roleAuth('Employee'): return redirect(url_for('main_bp.home')) # get basic project info to display proj = Project.query.get_or_404(cur_proj_id) if proj.client_id != None: client = Client.query.get(proj.client_id) else: client = None site = Site.query.get(proj.site_id) status = Status.query.get(proj.status_id) typeOfWork = Worktype.query.get(proj.typeOfWork_id) # Get list of areas in Project # areas = proj.area_list # Get list of rooms in Project rooms = proj.room_list # get list of Timesheets for Project tss = proj.timesheets.all() # Sum up the total hours for display total_hours = 0 for ts in tss: total_hours += ts.hours return render_template( 'project_details.html', title='Project Info', legend='Project Info', proj=proj, site=site, status=status, typeOfWork=typeOfWork, # areas=areas, client=client, rooms=rooms, tss=tss, total_hours=total_hours)
def excel_project(cur_proj_id): # Must be Admin if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) wb = build_project_report(cur_proj_id) # get name of project and strip any special characters name = Project.query.get_or_404(cur_proj_id).name name = re.sub(r'\W+', ' ', name) name = name.strip() file_name = name + '.xlsx' #output as bytes output = io.BytesIO() wb.save(output) output.seek(0) return send_file( output, as_attachment=True, attachment_filename=file_name, mimetype= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
def update_client(cur_client_id): if not roleAuth('Admin'): return redirect(url_for('main_bp.home')) form = UpdateClientForm() client = Client.query.get_or_404(cur_client_id) if form.validate_on_submit(): client.name = form.name.data client.contactName = form.contactName.data client.contactEmail = form.contactEmail.data client.contactPhone = form.contactPhone.data db.session.commit() flash('A new Client has been created!', 'success') return redirect(url_for('site_bp.client_list')) elif request.method == 'GET': form.name.data = client.name form.contactName.data = client.contactName form.contactEmail.data = client.contactEmail form.contactPhone.data = client.contactPhone return render_template('client_update.html', title='Update Client', form=form, legend='Update Client')
def project_list(): if not roleAuth('Manager'): return redirect(url_for('main_bp.home')) # Create the filter form object. filt = FilterProjectsForm() # We do not "submit" this form. # The Form object provides validation and controls the users input # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * Below is the old, no JS way of loading the data. * # # * Instead, window.onload() queries the /getprojects route * # # * and fills <div class="project_list"> with project articles * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # # Get the default list of projects # def get_default_project_list(): # prjs = [] # # Should apply a couple of sorts here, we'll build the list in our custom order # # First should be active projects, IE status_id=3 # active = Project.query.filter_by(status_id=3).order_by(desc(Project.date_start)).all() # for item in active: # prjs.append(item) # # then Painting Complete, IE status_id=4 # paintComplete = Project.query.filter_by(status_id=4).all() # for item in paintComplete: # prjs.append(item) # # then we want to get the upcoming and quoted IE status_id = 1||2 # upcoming = Project.query.filter_by(status_id=1).all() # for item in upcoming: # prjs.append(item) # quoted = Project.query.filter_by(status_id=2).all() # for item in quoted: # prjs.append(item) # # then paused, IE status_id=6 # paused = Project.query.filter_by(status_id=6).all() # for item in paused: # prjs.append(item) # # then invoiced, IE status_id=5, needs to be sorted by date completed # invoiced = Project.query.filter_by(status_id=5).order_by(Project.date_finished.desc()).all() # for item in invoiced: # prjs.append(item) # # then finally cancelled, IE status_id=7 # cancelled = Project.query.filter_by(status_id=7).all() # for item in cancelled: # prjs.append(item) # # prjs = Project.query.order_by(Project.status_id.desc()).all() # return prjs # projects = get_default_project_list() # for project in projects: # project.view_url = (url_for('project_bp.view_project', cur_proj_id=project.id)) # project.update_url = (url_for('project_bp.update_project', cur_proj_id=project.id)) # project.update_simple_url = (url_for('project_bp.update_project_simple', cur_proj_id=project.id)) return render_template('project_list.html', title='Project List', filt=filt, legend="Filter Project List")
def site_list(): if not roleAuth('Client'): return redirect(url_for('main_bp.home')) sites = Site.query.all() return render_template('site_list.html', title='Site List', sites=sites)