mirror of
https://github.com/wahyd4/links.git
synced 2026-08-08 21:04:53 +10:00
UI tweaks
This commit is contained in:
+5
-3
@@ -11,17 +11,19 @@ class PriceWatcherForm(forms.ModelForm):
|
||||
model = PriceWatcher
|
||||
fields = [
|
||||
'name', 'url', 'css_selector', 'check_interval_hours',
|
||||
'alert_threshold_pct', 'enabled',
|
||||
'alert_price_threshold', 'recurring_notification', 'enabled',
|
||||
]
|
||||
widgets = {
|
||||
'name': forms.TextInput(attrs={'class': _input}),
|
||||
'url': forms.URLInput(attrs={'class': _input}),
|
||||
'css_selector': forms.TextInput(attrs={'class': _mono, 'placeholder': 'e.g. span.price or .product-price'}),
|
||||
'check_interval_hours': forms.Select(attrs={'class': _select}),
|
||||
'alert_threshold_pct': forms.NumberInput(attrs={'class': _input, 'step': '0.01', 'min': '0'}),
|
||||
'alert_price_threshold': forms.NumberInput(attrs={'class': _input, 'step': '0.01', 'min': '0', 'placeholder': 'e.g. 999.00'}),
|
||||
'recurring_notification': forms.CheckboxInput(attrs={'class': 'h-4 w-4 text-indigo-600 border-gray-300 rounded'}),
|
||||
'enabled': forms.CheckboxInput(attrs={'class': 'h-4 w-4 text-indigo-600 border-gray-300 rounded'}),
|
||||
}
|
||||
help_texts = {
|
||||
'css_selector': 'CSS selector pointing to the element containing the price text.',
|
||||
'alert_threshold_pct': 'Minimum percentage drop to send an alert. Use 0 to alert on any drop.',
|
||||
'alert_price_threshold': 'Notify when detected price is at or below this amount. Leave blank to disable alerts.',
|
||||
'recurring_notification': 'Keep notifying on every check while the price meets the threshold. Uncheck to notify only once (until the alert is dismissed).',
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('pricemon', '0003_delete_pricemonsettings'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='pricewatcher',
|
||||
old_name='alert_threshold_pct',
|
||||
new_name='alert_price_threshold',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='pricewatcher',
|
||||
name='alert_price_threshold',
|
||||
field=models.DecimalField(
|
||||
blank=True, decimal_places=2, max_digits=12, null=True,
|
||||
help_text='Send an alert when the detected price is at or below this value. Leave blank to disable alerts.',
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='pricewatcher',
|
||||
name='recurring_notification',
|
||||
field=models.BooleanField(
|
||||
default=True,
|
||||
help_text='Keep notifying on every check while the price meets the threshold. Disable to notify only once (until you dismiss the alert).',
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='pricealert',
|
||||
name='old_price',
|
||||
field=models.DecimalField(blank=True, decimal_places=2, max_digits=12, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='pricealert',
|
||||
name='drop_pct',
|
||||
field=models.DecimalField(decimal_places=2, default=0, max_digits=5),
|
||||
),
|
||||
]
|
||||
+9
-5
@@ -18,9 +18,13 @@ class PriceWatcher(models.Model):
|
||||
help_text='CSS selector for the price element, e.g. span.price or .product-price',
|
||||
)
|
||||
check_interval_hours = models.IntegerField(choices=INTERVAL_CHOICES, default=24)
|
||||
alert_threshold_pct = models.DecimalField(
|
||||
max_digits=5, decimal_places=2, default=0,
|
||||
help_text='Minimum % price drop to trigger an alert (0 = any drop)',
|
||||
alert_price_threshold = models.DecimalField(
|
||||
max_digits=12, decimal_places=2, null=True, blank=True,
|
||||
help_text='Send an alert when the detected price is at or below this value. Leave blank to disable alerts.',
|
||||
)
|
||||
recurring_notification = models.BooleanField(
|
||||
default=True,
|
||||
help_text='Keep notifying on every check while the price meets the threshold. Disable to notify only once (until you dismiss the alert).',
|
||||
)
|
||||
enabled = models.BooleanField(default=True)
|
||||
last_checked_at = models.DateTimeField(null=True, blank=True)
|
||||
@@ -50,9 +54,9 @@ class PriceSnapshot(models.Model):
|
||||
|
||||
class PriceAlert(models.Model):
|
||||
watcher = models.ForeignKey(PriceWatcher, on_delete=models.CASCADE, related_name='alerts')
|
||||
old_price = models.DecimalField(max_digits=12, decimal_places=2)
|
||||
old_price = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True)
|
||||
new_price = models.DecimalField(max_digits=12, decimal_places=2)
|
||||
drop_pct = models.DecimalField(max_digits=5, decimal_places=2)
|
||||
drop_pct = models.DecimalField(max_digits=5, decimal_places=2, default=0)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
dismissed = models.BooleanField(default=False)
|
||||
|
||||
|
||||
+22
-10
@@ -28,16 +28,28 @@ def check_watcher(watcher_pk: int) -> None:
|
||||
error=error or '',
|
||||
)
|
||||
|
||||
if price is not None and watcher.last_price is not None and price < watcher.last_price:
|
||||
drop_pct = (watcher.last_price - price) / watcher.last_price * 100
|
||||
if drop_pct >= watcher.alert_threshold_pct:
|
||||
alert = PriceAlert.objects.create(
|
||||
watcher=watcher,
|
||||
old_price=watcher.last_price,
|
||||
new_price=price,
|
||||
drop_pct=drop_pct,
|
||||
)
|
||||
notify_telegram(watcher, alert)
|
||||
if price is not None and watcher.alert_price_threshold:
|
||||
if price <= watcher.alert_price_threshold:
|
||||
should_alert = True
|
||||
|
||||
if not watcher.recurring_notification:
|
||||
# Only alert if there is no undismissed alert already
|
||||
if PriceAlert.objects.filter(watcher=watcher, dismissed=False).exists():
|
||||
should_alert = False
|
||||
|
||||
if should_alert:
|
||||
old_price = watcher.last_price
|
||||
drop_pct = 0
|
||||
if old_price and old_price > price:
|
||||
drop_pct = (old_price - price) / old_price * 100
|
||||
|
||||
alert = PriceAlert.objects.create(
|
||||
watcher=watcher,
|
||||
old_price=old_price,
|
||||
new_price=price,
|
||||
drop_pct=drop_pct,
|
||||
)
|
||||
notify_telegram(watcher, alert)
|
||||
|
||||
update_fields = ['last_checked_at']
|
||||
if price is not None:
|
||||
|
||||
@@ -19,14 +19,6 @@
|
||||
</svg>
|
||||
{% trans "Add Watcher" %}
|
||||
</a>
|
||||
<a href="{% url 'site-settings' %}"
|
||||
class="inline-flex items-center px-3 py-2 bg-white border border-gray-200 text-gray-600 text-sm font-medium rounded-lg hover:bg-gray-50 transition" title="Settings (Telegram, etc.)">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/>
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
|
||||
</svg>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<!-- Stat cards -->
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{% block title %}{{ title }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="max-w-2xl mx-auto px-4 py-6">
|
||||
<div class="max-w-5xl mx-auto px-4 py-6">
|
||||
|
||||
<div class="flex items-center gap-3 mb-6">
|
||||
<a href="{% url 'pricemon-dashboard' %}"
|
||||
@@ -26,57 +26,106 @@
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
<div class="bg-white rounded-lg shadow-sm border border-gray-100 p-6">
|
||||
<form method="post" id="watcher-form" class="space-y-5">
|
||||
<div class="bg-white rounded-lg shadow-sm border border-gray-100 p-8">
|
||||
<form method="post" id="watcher-form" class="space-y-6">
|
||||
{% csrf_token %}
|
||||
|
||||
{% for field in form %}
|
||||
{% if form.non_field_errors %}
|
||||
<div class="px-4 py-3 rounded-lg bg-red-50 border border-red-100 text-sm text-red-700">
|
||||
{{ form.non_field_errors }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<!-- Name (full width) -->
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">
|
||||
{{ field.label }}{% if field.field.required %} <span class="text-red-500">*</span>{% endif %}
|
||||
{{ form.name.label }} <span class="text-red-500">*</span>
|
||||
</label>
|
||||
{{ form.name }}
|
||||
{% for e in form.name.errors %}<p class="text-xs text-red-600 mt-1">{{ e }}</p>{% endfor %}
|
||||
</div>
|
||||
|
||||
{% if field.html_name == 'css_selector' %}
|
||||
{# CSS selector field gets an inline Test button #}
|
||||
<!-- URL (full width) -->
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">
|
||||
{{ form.url.label }} <span class="text-red-500">*</span>
|
||||
</label>
|
||||
{{ form.url }}
|
||||
{% for e in form.url.errors %}<p class="text-xs text-red-600 mt-1">{{ e }}</p>{% endfor %}
|
||||
</div>
|
||||
|
||||
<!-- CSS Selector + Test button (full width) -->
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">
|
||||
{{ form.css_selector.label }} <span class="text-red-500">*</span>
|
||||
</label>
|
||||
<div class="flex gap-2 items-start">
|
||||
<div class="flex-1">{{ field }}</div>
|
||||
<div class="flex-1">{{ form.css_selector }}</div>
|
||||
<button type="button"
|
||||
id="test-selector-btn"
|
||||
class="flex-shrink-0 mt-px inline-flex items-center gap-1.5 px-3 py-2 text-sm font-medium text-indigo-700 bg-indigo-50 border border-indigo-200 rounded-md hover:bg-indigo-100 transition"
|
||||
hx-post="{% url 'pricemon-test-selector' %}"
|
||||
hx-include="#watcher-form [name='url'], #watcher-form [name='css_selector']"
|
||||
hx-target="#selector-test-result"
|
||||
hx-swap="innerHTML"
|
||||
hx-indicator="#test-selector-btn">
|
||||
hx-swap="innerHTML">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
|
||||
</svg>
|
||||
<span class="htmx-indicator-hide">{% trans "Test" %}</span>
|
||||
<span class="htmx-indicator-show hidden">{% trans "Testing…" %}</span>
|
||||
<span id="test-btn-label">{% trans "Test" %}</span>
|
||||
</button>
|
||||
</div>
|
||||
<div id="selector-test-result" class="mt-2"></div>
|
||||
{% else %}
|
||||
{{ field }}
|
||||
{% endif %}
|
||||
|
||||
{% if field.help_text %}
|
||||
<p class="text-xs text-gray-400 mt-1">{{ field.help_text }}</p>
|
||||
{% endif %}
|
||||
{% for error in field.errors %}
|
||||
<p class="text-xs text-red-600 mt-1">{{ error }}</p>
|
||||
{% endfor %}
|
||||
<p class="text-xs text-gray-400 mt-1">{{ form.css_selector.help_text }}</p>
|
||||
{% for e in form.css_selector.errors %}<p class="text-xs text-red-600 mt-1">{{ e }}</p>{% endfor %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
<div class="flex items-center gap-3 pt-2">
|
||||
<!-- Row: Check interval + Alert price threshold -->
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">
|
||||
{{ form.check_interval_hours.label }} <span class="text-red-500">*</span>
|
||||
</label>
|
||||
{{ form.check_interval_hours }}
|
||||
{% for e in form.check_interval_hours.errors %}<p class="text-xs text-red-600 mt-1">{{ e }}</p>{% endfor %}
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">
|
||||
{{ form.alert_price_threshold.label }}
|
||||
</label>
|
||||
{{ form.alert_price_threshold }}
|
||||
<p class="text-xs text-gray-400 mt-1">{{ form.alert_price_threshold.help_text }}</p>
|
||||
{% for e in form.alert_price_threshold.errors %}<p class="text-xs text-red-600 mt-1">{{ e }}</p>{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Row: Recurring notification + Enabled -->
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
|
||||
<div class="flex items-start gap-3">
|
||||
{{ form.recurring_notification }}
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700">{{ form.recurring_notification.label }}</label>
|
||||
<p class="text-xs text-gray-400 mt-0.5">{{ form.recurring_notification.help_text }}</p>
|
||||
</div>
|
||||
{% for e in form.recurring_notification.errors %}<p class="text-xs text-red-600 mt-1">{{ e }}</p>{% endfor %}
|
||||
</div>
|
||||
<div class="flex items-start gap-3">
|
||||
{{ form.enabled }}
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700">{{ form.enabled.label }}</label>
|
||||
<p class="text-xs text-gray-400 mt-0.5">{% trans "Uncheck to pause price checks for this watcher." %}</p>
|
||||
</div>
|
||||
{% for e in form.enabled.errors %}<p class="text-xs text-red-600 mt-1">{{ e }}</p>{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-3 pt-2 border-t border-gray-100">
|
||||
<button type="submit"
|
||||
class="px-5 py-2 bg-indigo-600 text-white text-sm font-medium rounded-lg hover:bg-indigo-700 transition">
|
||||
class="px-6 py-2 bg-indigo-600 text-white text-sm font-medium rounded-lg hover:bg-indigo-700 transition">
|
||||
{% trans "Save" %}
|
||||
</button>
|
||||
<a href="{% url 'pricemon-dashboard' %}"
|
||||
class="px-5 py-2 bg-gray-100 text-gray-700 text-sm font-medium rounded-lg hover:bg-gray-200 transition">
|
||||
class="px-6 py-2 bg-gray-100 text-gray-700 text-sm font-medium rounded-lg hover:bg-gray-200 transition">
|
||||
{% trans "Cancel" %}
|
||||
</a>
|
||||
</div>
|
||||
@@ -91,18 +140,15 @@
|
||||
|
||||
{% block extra_js %}
|
||||
<script>
|
||||
/* Show/hide the loading text on the Test button while HTMX is in-flight */
|
||||
document.addEventListener('htmx:beforeRequest', function(e) {
|
||||
if (e.target.id === 'test-selector-btn') {
|
||||
e.target.querySelector('.htmx-indicator-hide').classList.add('hidden');
|
||||
e.target.querySelector('.htmx-indicator-show').classList.remove('hidden');
|
||||
document.getElementById('test-btn-label').textContent = '{% trans "Testing…" %}';
|
||||
e.target.disabled = true;
|
||||
}
|
||||
});
|
||||
document.addEventListener('htmx:afterRequest', function(e) {
|
||||
if (e.target.id === 'test-selector-btn') {
|
||||
e.target.querySelector('.htmx-indicator-hide').classList.remove('hidden');
|
||||
e.target.querySelector('.htmx-indicator-show').classList.add('hidden');
|
||||
document.getElementById('test-btn-label').textContent = '{% trans "Test" %}';
|
||||
e.target.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user