def resource_show(resource): """(Controller) Display information about a given resource""" r = Resource.query.filter(Resource.name == resource).one_or_none() tools = Tool.query.filter(Tool.resource_id == r.id).all() if not r: flash("Resource not found") return redirect(url_for('resources.resources')) readonly = True if accesslib.user_privs_on_resource( member=current_user, resource=r) >= AccessByMember.LEVEL_ARM: readonly = False cc = comments.get_comments(resource_id=r.id) maint = MaintSched.query.filter(MaintSched.resource_id == r.id).all() resources = Resource.query.all() return render_template('resource_edit.html', rec=r, resources=resources, readonly=readonly, tools=tools, comments=cc, maint=maint)
def maintenance_post(resource): """(Controller) Display information about a given resource""" r = Resource.query.filter(Resource.name == resource).one_or_none() if not r: flash("Resource not found") return redirect(url_for('resources.resources')) tool = Tool.query.filter(Tool.name == request.form['tool']) tool = tool.filter(Tool.resource_id == r.id).one() if accesslib.user_privs_on_resource(member=current_user, resource=r) < AccessByMember.LEVEL_ARM: flash("No privilages", "danger") return redirect(url_for('resources.maintenance', resource=resource)) if request.form['LogMaint'] == "LogMaint": print request.form dt = datetime.datetime.strptime( request.form['input_maint_log_datetime'], "%Y-%m-%dT%H:%M") print dt # ([('tool', u'laser-rabbit'), ('maint', u'optics-all'), ('input_maint_log_datetime', u'2019-03-26T18:00'), ('LogMaint', u'LogMaint')]) authutil.log(eventtypes.RATTBE_LOGEVENT_TOOL_MAINTENANCE_DONE.id, tool_id=tool.id, message=request.form['maint'], doneby=current_user.id, commit=0, when=dt) flash("Maintenance recorded", "success") db.session.commit() return redirect(url_for('resources.maintenance', resource=resource))
def tools_show(tool): """(Controller) Display information about a given tool""" r = Tool.query.filter(Tool.id == tool).one_or_none() if not r: flash("Tool not found") return redirect(url_for('tools.tools')) privs = accesslib.user_privs_on_resource(member=current_user, resource=r) readonly = False if (not current_user.privs('RATT')): readonly = True if privs < AccessByMember.LEVEL_ARM: flash("You don't have access to this") return redirect(url_for('index')) resources = Resource.query.all() nodes = Node.query.all() nodes.append( Node(id="None", name="UNASSINGED" )) # TODO BUG This "None" match will break a non-sqlite3 database cc = comments.get_comments(tool_id=tool) tool_locked = r.lockout is not None return render_template('tool_edit.html', rec=r, resources=resources, readonly=readonly, nodes=nodes, comments=cc, tool_locked=tool_locked)
def resource_update(resource): """(Controller) Update an existing resource from HTML form POST""" rname = (resource) r = Resource.query.filter(Resource.id == resource).one_or_none() if not r: flash("Error: Resource not found") return redirect(url_for('resources.resources')) if accesslib.user_privs_on_resource(member=current_user, resource=r) < AccessByMember.LEVEL_ARM: flash("Error: Permission denied") return redirect(url_for('resources.resources')) r.name = (request.form['input_name']).strip() r.short = (request.form['input_short']).strip() r.description = (request.form['input_description']).strip() if r.name == "" or r.short == "" or r.description == "": flash("Name, Short and Description are manditory", "warning") return redirect(url_for('resources.resources')) r.owneremail = (request.form['input_owneremail']).strip() r.slack_chan = (request.form['input_slack_chan']).strip() r.slack_admin_chan = (request.form['input_slack_admin_chan']).strip() r.info_url = (request.form['input_info_url']).strip() r.info_text = (request.form['input_info_text']).strip() r.slack_info_text = (request.form['input_slack_info_text']).strip() r.sa_days = int("0" + request.form['input_sa_days']) r.sa_hours = int("0" + request.form['input_sa_hours']) r.sa_url = (request.form['input_sa_url']).strip() r.sa_permit = int("0" + request.form['input_sa_permit']) r.sa_required = int(request.form['input_sa_required']) r.permissions = (request.form['input_permissions']).replace("_", "-").strip() if (r.sa_required == -1): r.sa_required = None if request.form['input_age_restrict']: ar = 0 try: ar = int(request.form['input_age_restrict']) r.age_restrict = ar except: pass if ar <= 0: flash("Age restriction should be empty, or greater than zero", "warning") else: r.age_restrict = None db.session.commit() authutil.kick_backend() flash("Resource updated") return redirect(url_for('resources.resources'))
def tool_lock(tool): r = Tool.query.filter(Tool.id == tool).one_or_none() if not r: flash("Tool not found") return redirect(url_for('tools.tools')) if (not current_user.privs( 'RATT', 'HeadRM')) and (accesslib.user_privs_on_resource( resource=r, member=current_user) < AccessByMember.LEVEL_ARM): flash("You do not have privileges to lock out this tool") return redirect(url_for('tools.tools')) r.lockout = request.form['lockout_reason'] authutil.log(eventtypes.RATTBE_LOGEVENT_TOOL_LOCKOUT_LOCKED.id, tool_id=r.id, message=r.lockout, doneby=current_user.id, commit=0) node = Node.query.filter(Node.id == r.node_id).one() authutil.send_tool_lockout(r.name, node.mac, r.lockout) db.session.commit() flash("Tool is locked", 'info') return redirect(url_for('tools.tools_show', tool=r.id))
sa.append(ar) #print sa return render_template('training.html',training=sa) @blueprint.route('/editquiz', methods=['GET','POST']) @login_required def editquiz(): if request.method == "POST" and request.form: for x in request.form: print x,request.form[x] if 'resource_id' in request.form and request.form['resource_id']: rid = int(request.form['resource_id']) i=1 o=0 if accesslib.user_privs_on_resource(member=current_user,resource_id=rid) < AccessByMember.LEVEL_ARM: flash("You are not authorized to edit this quiz","warning") return redirect(url_for('training.training')) ResourceQuiz.query.filter(ResourceQuiz.resource_id == rid).delete() while True: if 'question_'+str(i) not in request.form or 'answer_'+str(i) not in request.form: break q=request.form['question_'+str(i)].strip() a=request.form['answer_'+str(i)].strip().lower() i+=1 if q != "" or a !="": print "GOT",i,o,q,a o+=1 n = ResourceQuiz(answer=a,question=q,idx=o,resource_id=rid) db.session.add(n) db.session.commit()
def message(resource): """(Controller) Update an existing resource from HTML form POST""" rname = (resource) r = Resource.query.filter(Resource.name == resource).one_or_none() if not r: flash("Error: Resource not found") return redirect(url_for('resources.resources')) if accesslib.user_privs_on_resource(member=current_user, resource=r) < AccessByMember.LEVEL_ARM: flash("Error: Permission denied") return redirect(url_for('resources.resources')) if 'Send' in request.form: emails = [] #print "SENDING",request.form['bodyText'] bodyText = request.form['bodyText'] subject = request.form['subject'] mt_email = True if 'message_type_email' in request.form else False mt_alt_email = True if 'message_type_alt_email' in request.form else False mt_slack = True if 'message_type_slack_individual' in request.form else False mt_slack_group = True if 'message_type_slack_group' in request.form else False mt_slack_admin = True if 'message_type_slack_admingroup' in request.form else False print "RESOURCE", resource print "BODY", bodyText print "SUBJECT", subject print "email", mt_email, "alt_email", mt_alt_email print "slack", mt_slack, "slack_group", mt_slack_group, "slack_admin", mt_slack_admin members = Member.query members = members.join(AccessByMember, (Member.id == AccessByMember.member_id)) members = members.join(Subscription, (Subscription.member_id == Member.id)) members = members.join(Resource, ((Resource.name == resource) & (Resource.id == AccessByMember.resource_id))) members = members.add_column(Member.member) members = members.add_column(Member.email) members = members.add_column(Member.alt_email) members = members.add_column(Member.slack) members = accesslib.addQuickAccessQuery(members) members = members.all() print "SLACK", r.slack_chan print "SLACK_ADMIN", r.slack_admin_chan for x in members: if x.active in ('Active', 'Grace Period'): print x if x.email: emails.append(x.email) if x.alt_email: emails.append(x.alt_email) if mt_slack and x.slack: send_slack_message(x.slack, bodyText) #print emails if mt_slack_group and r.slack_chan: mod = "" if request.form['slack_group_option'] == "here": mod = "<!here|here> " if request.form['slack_group_option'] == "channel": mod = "<!channel> " send_slack_message(r.slack_chan, mod + bodyText) if mt_slack_admin and r.slack_admin_chan: mod = "" if request.form['slack_admingroup_option'] == "here": mod = "<!here|here> " if request.form['slack_admingroup_option'] == "channel": mod = "<!channel> " send_slack_message(r.slack_admin_chan, mod + bodyText) email_errors = 0 email_ok = 0 for e in emails: try: genericEmailSender("*****@*****.**", e, subject, bodyText) except: email_errors += 1 if email_errors: flash("%s email send errors" % email_errors, "warning") else: flash("Sent %s emails" % (email_ok), "success") return render_template('email.html', rec=r)
def maintenance(resource): """(Controller) Display information about a given resource""" r = Resource.query.filter(Resource.name == resource).one_or_none() tools = Tool.query.filter(Tool.resource_id == r.id).all() if not r: flash("Resource not found") return redirect(url_for('resources.resources')) readonly = True if accesslib.user_privs_on_resource( member=current_user, resource=r) >= AccessByMember.LEVEL_ARM: readonly = False tooldata = {} tools = Tool.query.filter(Tool.resource_id == r.id).all() maint = MaintSched.query.filter(MaintSched.resource_id == r.id).all() # Find date of last maintenances for t in tools: tooldata[t.name] = {} tooldata[t.name]['maint'] = {} for m in maint: tooldata[t.name]['maint'][m.name] = {} tooldata[t.name]['maint'][m.name]['desc'] = m.desc log = Logs.query.filter(Logs.tool_id == t.id) log = log.filter(Logs.event_type == eventtypes. RATTBE_LOGEVENT_TOOL_MAINTENANCE_DONE.id) log = log.filter(Logs.message == m.name) log = log.order_by(Logs.time_reported) log = log.limit(1) log = log.one_or_none() last_reported = None if log and log.time_reported: tooldata[t.name]['maint'][ m.name]['lastdone'] = log.time_reported tooldata[t.name]['maint'][ m.name]['clock_time_done'] = datetime.datetime.now( ) - log.time_reported print "CTD IS", tooldata[t.name]['maint'][ m.name]['clock_time_done'] last_reported = log.time_reported # If there is a log entry, find machine time since then # if not, find total machine time usage = UsageLog.query.filter(UsageLog.tool_id == t.id) if (log): usage = usage.filter( UsageLog.time_reported > log.time_reported) usage = usage.add_column( func.sum(UsageLog.activeSecs).label('activeSecs')) usage = usage.one_or_none() machine_units = None if m.machinetime_span: tooldata[t.name]['maint'][m.name]['run_interval'] = "%s %s" % ( m.machinetime_span, m.machinetime_unit) activeSecs = usage.activeSecs if usage.activeSecs else 0 if m.machinetime_unit == "hours": tooldata[t.name]['maint'][ m.name]['activeTime'] = "%s Hrs." % int( activeSecs / 3600) remain_span = int(m.machinetime_span) - int( activeSecs / 3600) machine_units = "Hrs." elif m.machinetime_unit == "minutes": tooldata[t.name]['maint'][ m.name]['activeTime'] = "%s Min." % int( activeSecs / 60) remain_span = int(m.machinetime_span) - int( activeSecs / 60) machine_units = "Min." elif m.machinetime_unit == "days": tooldata[t.name]['maint'][ m.name]['activeTime'] = "%s Days" % int(activeSecs / (3600 * 24)) remain_span = int(m.machinetime_span) - int(activeSecs / (3600 * 24)) machine_units = "Days" elif m.machinetime_unit == "weeks": tooldata[t.name]['maint'][ m.name]['activeTime'] = "%s Weeks" % int( activeSecs / (3600 * 24 * 7)) remain_span = int(m.machinetime_span) - int( activeSecs / (3600 * 24 * 7)) machine_units = "Weeks" elif m.machinetime_unit == "months": tooldata[t.name]['maint'][ m.name]['activeTime'] = "%s Months" % int( activeSecs / (3600 * 24 * 30)) remain_span = int(m.machinetime_span) - int( activeSecs / (3600 * 24 * 30)) machine_units = "Months" else: tooldata[t.name]['maint'][ m.name]['activeTime'] = "%s Sec." % int(activeSecs) remain_span = int(m.machinetime_span) - int(activeSecs) machine_units = "Sec." else: activeSecs = usage.activeSecs if usage else 0 tooldata[t.name]['maint'][ m.name]['activeTime'] = "%s Hrs." % int(activeSecs / 3600) if machine_units: if (remain_span < 0): tooldata[t.name]['maint'][ m.name]['active_remain'] = "%s %s <b>OVERDUE</b>" % ( -remain_span, machine_units) else: tooldata[t.name]['maint'][ m.name]['active_remain'] = "%s %s Remaining" % ( remain_span, machine_units) if m.realtime_span: tooldata[t.name]['maint'][ m.name]['calendar_interval'] = "%s %s" % (m.realtime_span, m.realtime_unit) if 'clock_time_done' in tooldata[t.name]['maint'][m.name]: ctd = tooldata[t.name]['maint'][ m.name]['clock_time_done'].total_seconds() if m.realtime_unit == "hours": ctr = (m.realtime_span - (ctd / 3600)) elif m.realtime_unit == "minutes": ctr = (m.realtime_span - (ctd / 60)) elif m.realtime_unit == "days": ctr = (m.realtime_span - (ctd / (3600 * 24))) elif m.realtime_unit == "weeks": ctr = (m.realtime_span - (ctd / (3600 * 24 * 7))) elif m.realtime_unit == "months": ctr = (m.realtime_span - (ctd / (3600 * 24 * 30))) elif m.realtime_unit == "years": ctr = (m.realtime_span - (ctd / (3600 * 24 * 365))) else: ctr = (m.realtime_span - (ctd)) if (ctr > 0): tooldata[t.name]['maint'][m.name][ 'clock_time_remaining'] = "{0:.1f} {1} Remaining".format( ctr, m.realtime_unit) else: tooldata[t.name]['maint'][m.name][ 'clock_time_remaining'] = "<b>Overdue</b> {0:.1f} {1}".format( -ctr, m.realtime_unit) #print "CTA", tooldata[t.name]['maint'][m.name]['clock_time_ago'] # How long ago when it was done? if 'clock_time_done' in tooldata[t.name]['maint'][m.name]: ctd = tooldata[t.name]['maint'][m.name]['clock_time_done'] tooldata[t.name]['maint'][m.name]['clock_time_ago'] = ago.ago( datetime.datetime.now() - ctd)[1] else: tooldata[t.name]['maint'][m.name]['clock_time_ago'] = "(Never)" current_datetime = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%dT%H:%M") print "RETURNING TOOLDATA", tooldata return render_template('maintenance.html', resource=r, readonly=readonly, tools=tools, maint=maint, tooldata=tooldata, current_datetime=current_datetime)
def resource_usage_reports(resource): """(Controller) Display information about a given resource""" r = Resource.query.filter(Resource.name == resource).one_or_none() tools = Tool.query.filter(Tool.resource_id == r.id).all() if not r: flash("Resource not found") return redirect(url_for('resources.resources')) readonly = True if accesslib.user_privs_on_resource( member=current_user, resource=r) >= AccessByMember.LEVEL_ARM: readonly = False q = UsageLog.query.filter(UsageLog.resource_id == r.id) if 'input_date_start' in request.values and request.values[ 'input_date_start'] != "": dt = datetime.datetime.strptime(request.values['input_date_start'], "%m/%d/%Y") q = q.filter(UsageLog.time_logged >= dt) if 'input_date_end' in request.values and request.values[ 'input_date_end'] != "": dt = datetime.datetime.strptime( request.values['input_date_end'], "%m/%d/%Y") + datetime.timedelta(days=1) q = q.filter(UsageLog.time_logged < dt) q = q.add_column(func.sum(UsageLog.enabledSecs).label('enabled')) q = q.add_column(func.sum(UsageLog.activeSecs).label('active')) q = q.add_column(func.sum(UsageLog.idleSecs).label('idle')) fields = [] if 'by_user' in request.values: q = q.group_by(UsageLog.member_id).add_column( UsageLog.member_id.label("member_id")) fields.append({'name': "member"}) if 'by_tool' in request.values: q = q.group_by(UsageLog.tool_id).add_column( UsageLog.tool_id.label("tool_id")) fields.append({'name': "tool"}) if 'by_day' in request.values: q = q.group_by(func.date(UsageLog.time_logged)).add_column( func.date(UsageLog.time_logged).label("date")) q = q.order_by(func.date(UsageLog.time_logged)) fields.append({'name': "date"}) fields += [{ 'name': 'enabled', 'type': 'num' }, { 'name': 'active', 'type': 'num' }, { 'name': 'idle', 'type': 'num' }] d = q.all() toolcache = {} usercache = {} records = [] for r in d: if 'format' in request.values and request.values['format'] == 'csv': rec = {'enabled': r.enabled, 'active': r.active, 'idle': r.idle} else: rec = { 'enabled': sec_to_hms(r.enabled), 'active': sec_to_hms(r.active), 'idle': sec_to_hms(r.idle), 'enabled_secs': int(r.enabled), 'active_secs': int(r.active), 'idle_secs': int(r.idle) } if 'by_user' in request.values: if r.member_id not in usercache: mm = Member.query.filter( Member.id == r.member_id).one_or_none() if mm: usercache[r.member_id] = mm.member else: usercache[r.member_id] = "Member #" + str(r.member_id) rec['member'] = usercache[r.member_id] if 'by_tool' in request.values: if r.tool_id not in toolcache: mm = Tool.query.filter(Tool.id == r.tool_id).one_or_none() if mm: toolcache[r.tool_id] = mm.name else: toolcache[r.tool_id] = "Tool #" + str(r.tool_id) rec['tool'] = toolcache[r.tool_id] if 'by_day' in request.values: rec['date'] = r.date records.append(rec) meta = {} meta['csvurl'] = request.url + "&format=csv" if 'format' in request.values and request.values['format'] == 'csv': resp = Response(generate_report(fields, records), mimetype='text/csv') resp.headers['Content-Disposition'] = 'attachment' resp.headers['filename'] = 'log.csv' return resp return render_template('resource_usage_reports.html', rec=r, readonly=readonly, tools=tools, records=records, fields=fields, meta=meta)