def map(): if request.method == "POST": if request.form.get('send-map') == 'yes': map_id = request.form['map_id'] pos_x = request.form['pos_x'] pos_y = request.form['pos_y'] conn = sqlite3.connect(app.db) c = conn.cursor() c.execute("UPDATE maps SET pos_x=?, pos_y=? WHERE map_id=?", ( pos_x, pos_y, map_id, )) conn.commit() conn.close() conn = sqlite3.connect(app.db) conn.row_factory = sqlite3.Row c = conn.cursor() c.execute( "select sensors.name, sensors.tmp, types.unit, types.unit2, types.ico, types.title, \ sensors.type, sensors.alarm, sensors.time, maps.map_id, maps.background_color, \ maps.display_name, maps.font_color, maps.transparent_bkg, sensors.tmp_min, sensors.tmp_max, \ maps.background_low, maps.background_high, maps.font_size \ FROM sensors INNER JOIN types ON sensors.type = types.type INNER JOIN maps ON sensors.map_id = maps.map_id WHERE maps.map_on='on' " ) sensors = c.fetchall() c.execute( "select maps.map_id, maps.pos_x, maps.pos_y FROM sensors INNER JOIN maps ON sensors.map_id = maps.map_id WHERE maps.map_on='on'" ) group = c.fetchall() conn.close() if os.path.exists('data/upload/map.jpg'): mapimage = 'new' else: mapimage = None return render_template('map.html', sensors=sensors, group=json.dumps([dict(ix) for ix in group]), nt_settings=dict(nt_settings()), mapimage=mapimage)
def nettemp_settings(): if request.method == "POST": if request.form.get('send-theme') == 'yes': theme = request.form['theme'] conn = sqlite3.connect(app.db) c = conn.cursor() c.execute("UPDATE nt_settings SET value=? WHERE option='nt_theme'", (theme, )) conn.commit() conn.close() if request.form.get('send-reboot') == 'yes': import os os.system('sudo reboot') if request.form.get('send-alarms-clear') == 'yes': conn = sqlite3.connect(app.dba) c = conn.cursor() c.execute("DELETE FROM def") conn.commit() conn.close() return render_template('nettemp_settings.html', nt_settings=dict(nt_settings()))
def charts_settings(): if request.method == "POST": if request.form.get('send-quick-charts') == 'yes': quickcharts = request.form['quick-charts'] conn = sqlite3.connect(app.db) c = conn.cursor() c.execute( "UPDATE nt_settings SET value=? WHERE option='quick_charts'", (quickcharts, )) conn.commit() conn.close() if request.form.get('send-theme') == 'yes': theme = request.form['theme'] conn = sqlite3.connect(app.db) c = conn.cursor() c.execute( "UPDATE nt_settings SET value=? WHERE option='charts_theme'", (theme, )) conn.commit() conn.close() return render_template('charts_settings.html', nt_settings=dict(nt_settings()))
def charts(): conn = sqlite3.connect(app.db) max = request.args.get("max") single = request.args.get("single") group = request.args.get("group") type = request.args.get("type") datetime = request.args.get("datetime") mode = request.args.get("mode") if group: c = conn.cursor() c.execute("select name FROM sensors WHERE ch_group=? AND charts='on'", (group, )) names = c.fetchall() elif single: c = conn.cursor() c.execute("select name FROM sensors WHERE name=? AND charts='on'", (single, )) names = c.fetchall() else: c = conn.cursor() c.execute("select name FROM sensors WHERE type=? AND charts='on'", (type, )) names = c.fetchall() c.execute(''' select DISTINCT type FROM sensors WHERE charts='on' ''') types = c.fetchall() c.execute( ''' select DISTINCT ch_group FROM sensors WHERE charts='on' AND ch_group!='none' ''' ) groups = c.fetchall() c.execute(''' select DISTINCT type, unit FROM types ''') units = c.fetchall() conn.close() names = [i[0] for i in names] names = list(set(names)) groups = [i[0] for i in groups] groups = list(set(groups)) groupmax = [ '15min', 'hour', 'day', 'week', 'month', '6month', 'year', 'all' ] return render_template('highcharts.html', groups=groups, types=types, max=max, single=single, type=type, names=names, groupmax=groupmax, group=group, units=units, mode=mode, datetime=datetime, nt_settings=dict(nt_settings()))
def mail_settings(): if request.method == "POST": if request.form.get('send-mail') == 'yes': smtp_user = request.form['smtp_user'] smtp_p = request.form['smtp_p'] smtp_server = request.form['smtp_server'] smtp_port = request.form['smtp_port'] mail_subject = request.form['mail_subject'] conn = sqlite3.connect(app.db) c = conn.cursor() c.execute( "UPDATE nt_settings SET value=? WHERE option='smtp_user'", (smtp_user, )) c.execute("UPDATE nt_settings SET value=? WHERE option='smtp_p'", (smtp_p, )) c.execute( "UPDATE nt_settings SET value=? WHERE option='smtp_server'", (smtp_server, )) c.execute( "UPDATE nt_settings SET value=? WHERE option='smtp_port'", (smtp_port, )) c.execute( "UPDATE nt_settings SET value=? WHERE option='mail_subject'", (mail_subject, )) conn.commit() conn.close() if request.form.get('send-test-mail') == 'yes': import smtplib, ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart recipient = request.form['recipient'] try: if recipient: conn = sqlite3.connect(app.db) conn.row_factory = sqlite3.Row c = conn.cursor() c.execute(''' SELECT option, value FROM nt_settings ''') s = c.fetchall() conn.close() for k, v in s: if k == 'smtp_user': smtp_user = v if k == 'smtp_p': smtp_p = v if k == 'smtp_server': smtp_server = v if k == 'smtp_port': smtp_port = v if k == 'mail_subject': mail_subject = v print(mail_subject) data = "Test mail fom nettemp!" message = MIMEMultipart("alternative") message["Subject"] = mail_subject message["From"] = smtp_user message["To"] = recipient text = """\ Hi, {data} """.format(data=data) html = """\ <html> <body> <p> {data}<br> <br > <a href="http://techfreak.pl/tag/nettemp"> <img src="http://techfreak.pl/wp-content/uploads/2012/12/nettemp.pl_.png" style="width:120px;height:40px;"></a><br> </p> </body> </html> """.format(data=data) part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") message.attach(part1) message.attach(part2) context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server: server.login(smtp_user, smtp_p) server.sendmail( smtp_user, recipient, message.as_string(), ) info = ("Mail was send to %s" % (recipient)) return render_template('mail_settings.html', nt_settings=dict(nt_settings()), info=info) except SMTPException as e: info = 'Error: %s' % e return render_template('mail_settings.html', nt_settings=dict(nt_settings()), info=info) except smtplib.socket.gaierror as e: info = 'Error: %s:' % e return render_template('mail_settings.html', nt_settings=dict(nt_settings()), info=info) except SMTPAuthenticationError: info = 'Error: %s:' % e return render_template('mail_settings.html', nt_settings=dict(nt_settings()), info=info) except SomeSendMailError: info = 'Error: %s:' % e return render_template('mail_settings.html', nt_settings=dict(nt_settings()), info=info) return render_template('mail_settings.html', nt_settings=dict(nt_settings()))