sonnetteweb/sonnette.py

48 lines
1.1 KiB
Python

""" Sonnetteweb — Enfin une sonnette à l'Arcoloc
Sonnez via le réseau local. C'est cool, hein ?
"""
from datetime import datetime, timedelta
import subprocess
from flask import Flask, render_template, request
app = Flask('sonnetteweb')
class Ringer:
_instance = None
def __init__(self):
self.last_rung = datetime.fromtimestamp(0)
@staticmethod
def get():
if Ringer._instance is None:
Ringer._instance = Ringer()
return Ringer._instance
def ring(self):
if datetime.now() - self.last_rung < timedelta(seconds=5):
return False
self.last_rung = datetime.now()
print('Dring!')
subprocess.run(['bash',
'-c',
'aplay ringtone.wav &'])
return True
@app.route('/', methods=['GET', 'POST'])
def view_home():
context = {}
if request.method == 'POST':
if request.form['ding'] == 'dong':
if Ringer.get().ring():
context['has_rung'] = True
else:
context['too_much_dring'] = True
return render_template('homepage.html', **context)