Skip to content

Commit

Permalink
Alarm editing and deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Arxari committed Sep 7, 2024
1 parent 2e7fe35 commit 6949c27
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 20 deletions.
47 changes: 33 additions & 14 deletions static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,38 @@ body {
padding: 0;
color: #ffffff;
}

.container {
width: 80%;
margin: auto;
padding: 20px;
background-color: #131516;
margin-top: 20px;
}

h1,
h3 {
color: #ffffff;
}

.alarm-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
}

.alarm {
padding: 15px;
border-radius: 5px;
background-color: #131516;
flex: 1 1 calc(33.33% - 20px);
margin: 10px 0;
}

@media screen and (max-width: 768px) {
.alarm {
flex: 1 1 100%;
}
}

.countdown {
font-weight: bold;
color: #ff5c82;
}

button {
padding: 10px 20px;
background-color: #ff5c82;
Expand All @@ -53,28 +46,23 @@ button {
cursor: pointer;
margin: 5px;
}

button:hover {
background-color: #ff3b62;
}

a {
display: inline-block;
text-decoration: none;
color: #ff5c82;
}

a:hover {
color: #ff3b62;
}

.buttons {
display: flex;
flex-direction: row;
gap: 10px;
justify-content: flex-start;
}

input[type="text"],
input[type="password"],
input[type="number"],
Expand All @@ -88,7 +76,6 @@ select {
border-radius: 5px;
margin: 5px 0;
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
Expand All @@ -97,7 +84,39 @@ select:focus {
outline: none;
background-color: #1f2224;
}

.form-group {
margin-bottom: 15px;
}

.alarm-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}

.alarm-actions {
display: flex;
gap: 10px;
}

.icon-button {
background: none;
border: none;
cursor: pointer;
padding: 5px;
opacity: 0.7;
transition: opacity 0.3s;
}

.icon-button:hover {
opacity: 1;
}

.edit-button svg {
color: #4caf50;
}

.delete-button svg {
color: #f44336;
}
65 changes: 65 additions & 0 deletions templates/edit_alarm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Edit Alarm</title>
<link
rel="stylesheet"
href="{{ url_for('static', filename='styles.css') }}"
/>
</head>
<body>
<div class="container">
<h1>Edit Alarm</h1>
<form method="POST">
<div class="form-group">
<label for="name">Alarm Name:</label>
<input
type="text"
id="name"
name="name"
value="{{ alarm_name }}"
required
/>
</div>
<div class="form-group">
<label for="intensity">Intensity (0-100):</label>
<input
type="number"
id="intensity"
name="intensity"
min="0"
max="100"
value="{{ intensity }}"
required
/>
</div>
<div class="form-group">
<label for="duration">Duration (seconds):</label>
<input
type="number"
id="duration"
name="duration"
min="0.1"
step="0.1"
value="{{ duration }}"
required
/>
</div>
<div class="form-group">
<label for="time">Time (HH:MM):</label>
<input
type="time"
id="time"
name="time"
value="{{ time }}"
required
/>
</div>
<button type="submit">Update Alarm</button>
</form>
<a href="{{ url_for('index') }}">Back to Home</a>
</div>
</body>
</html>
68 changes: 62 additions & 6 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,80 @@
<body>
<div class="container">
<h1>OpenShockClock</h1>

<div class="alarm-list">
{% for name, alarm in alarms.items() %}
<div
class="alarm"
data-alarm-time="{{ alarm[0].strftime('%Y-%m-%dT%H:%M:%S') }}"
>
<h3>{{ name }}</h3>
<div class="alarm-header">
<h3>{{ name }}</h3>
<div class="alarm-actions">
<a
href="{{ url_for('edit_alarm', alarm_name=name) }}"
class="icon-button edit-button"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path
d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"
></path>
<path
d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"
></path>
</svg>
</a>
<a
href="{{ url_for('delete_alarm', alarm_name=name) }}"
class="icon-button delete-button"
onclick="return confirm('Are you sure you want to delete this alarm?');"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<polyline points="3 6 5 6 21 6"></polyline>
<path
d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"
></path>
<line
x1="10"
y1="11"
x2="10"
y2="17"
></line>
<line
x1="14"
y1="11"
x2="14"
y2="17"
></line>
</svg>
</a>
</div>
</div>
<p>Intensity: {{ alarm[1] }}</p>
<p>Duration: {{ alarm[2] / 1000 }} seconds</p>
<p>Time Until Shock: <span class="countdown"></span></p>
</div>
{% endfor %}
</div>

<div class="buttons">
<a href="{{ url_for('add_alarm') }}">
<button>Add Alarm</button>
Expand All @@ -36,7 +95,6 @@ <h3>{{ name }}</h3>
</a>
</div>
</div>

<script>
function updateCountdowns() {
const now = new Date().getTime();
Expand All @@ -45,7 +103,6 @@ <h3>{{ name }}</h3>
alarm.dataset.alarmTime,
).getTime();
const timeDifference = alarmTime - now;

if (timeDifference > 0) {
const hours = Math.floor(
timeDifference / (1000 * 60 * 60),
Expand All @@ -68,7 +125,6 @@ <h3>{{ name }}</h3>
}
});
}

setInterval(updateCountdowns, 1000);
</script>
</body>
Expand Down
81 changes: 81 additions & 0 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def save_alarm_to_user_config(username, alarm_name, alarm_time, intensity, durat
if os.path.exists(user_config_file):
config.read(user_config_file)

if alarm_name in config.sections():
config.remove_section(alarm_name)

config[alarm_name] = {
'time': alarm_time.strftime('%Y-%m-%d %H:%M:%S'),
'intensity': str(intensity),
Expand Down Expand Up @@ -185,6 +188,84 @@ def add_alarm():
return redirect(url_for('index'))
return render_template('add_alarm.html')

@app.route('/edit/<alarm_name>', methods=['GET', 'POST'])
def edit_alarm(alarm_name):
if 'username' not in session:
return redirect(url_for('login'))

username = session['username']
user_config_file = os.path.join(USER_DIR, username, 'config.txt')
config = configparser.ConfigParser()

if not os.path.exists(user_config_file):
flash('User configuration not found.')
return redirect(url_for('index'))

config.read(user_config_file)

if alarm_name not in config.sections():
flash('Alarm not found.')
return redirect(url_for('index'))

if request.method == 'POST':
new_name = request.form['name']
intensity = int(request.form['intensity'])
duration = int(float(request.form['duration']) * 1000)
time_str = request.form['time']
alarm_time = datetime.strptime(time_str, "%H:%M").replace(
year=datetime.now().year,
month=datetime.now().month,
day=datetime.now().day
)
if alarm_time < datetime.now():
alarm_time += timedelta(days=1)

config.remove_section(alarm_name)

config[new_name] = {
'time': alarm_time.strftime('%Y-%m-%d %H:%M:%S'),
'intensity': str(intensity),
'duration': str(duration)
}

with open(user_config_file, 'w') as configfile:
config.write(configfile)

flash('Alarm updated successfully.')
return redirect(url_for('index'))

alarm_time = datetime.strptime(config[alarm_name]['time'], '%Y-%m-%d %H:%M:%S')
intensity = config[alarm_name].getint('intensity')
duration = config[alarm_name].getint('duration')

return render_template('edit_alarm.html', alarm_name=alarm_name,
time=alarm_time.strftime("%H:%M"),
intensity=intensity,
duration=duration/1000)

@app.route('/delete/<alarm_name>')
def delete_alarm(alarm_name):
if 'username' not in session:
return redirect(url_for('login'))

username = session['username']
user_config_file = os.path.join(USER_DIR, username, 'config.txt')
config = configparser.ConfigParser()

if os.path.exists(user_config_file):
config.read(user_config_file)
if alarm_name in config.sections():
config.remove_section(alarm_name)
with open(user_config_file, 'w') as configfile:
config.write(configfile)
flash('Alarm deleted successfully.')
else:
flash('Alarm not found.')
else:
flash('User configuration not found.')

return redirect(url_for('index'))

@app.route('/setup', methods=['GET', 'POST'])
def setup():
if 'username' not in session:
Expand Down

0 comments on commit 6949c27

Please sign in to comment.