diff --git a/pricemon/forms.py b/pricemon/forms.py index 8809dc8..39df460 100644 --- a/pricemon/forms.py +++ b/pricemon/forms.py @@ -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).', } diff --git a/pricemon/migrations/0004_alert_price_threshold_recurring.py b/pricemon/migrations/0004_alert_price_threshold_recurring.py new file mode 100644 index 0000000..3ba900d --- /dev/null +++ b/pricemon/migrations/0004_alert_price_threshold_recurring.py @@ -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), + ), + ] diff --git a/pricemon/models.py b/pricemon/models.py index 2f9d4b2..fa73257 100644 --- a/pricemon/models.py +++ b/pricemon/models.py @@ -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) diff --git a/pricemon/tasks.py b/pricemon/tasks.py index a73d0fa..138a0b0 100644 --- a/pricemon/tasks.py +++ b/pricemon/tasks.py @@ -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: diff --git a/templates/pricemon/dashboard.html b/templates/pricemon/dashboard.html index 0e34066..86d9ddc 100644 --- a/templates/pricemon/dashboard.html +++ b/templates/pricemon/dashboard.html @@ -19,14 +19,6 @@ {% trans "Add Watcher" %} - - - - - - diff --git a/templates/pricemon/watcher_form.html b/templates/pricemon/watcher_form.html index 2bc130c..a22c4bb 100644 --- a/templates/pricemon/watcher_form.html +++ b/templates/pricemon/watcher_form.html @@ -4,7 +4,7 @@ {% block title %}{{ title }}{% endblock %} {% block content %} -
+
-
+
+ {% csrf_token %} - {% for field in form %} + {% if form.non_field_errors %} +
+ {{ form.non_field_errors }} +
+ {% endif %} + +
+ {{ form.name }} + {% for e in form.name.errors %}

{{ e }}

{% endfor %} +
- {% if field.html_name == 'css_selector' %} - {# CSS selector field gets an inline Test button #} + +
+ + {{ form.url }} + {% for e in form.url.errors %}

{{ e }}

{% endfor %} +
+ + +
+
-
{{ field }}
+
{{ form.css_selector }}
- {% else %} - {{ field }} - {% endif %} - - {% if field.help_text %} -

{{ field.help_text }}

- {% endif %} - {% for error in field.errors %} -

{{ error }}

- {% endfor %} +

{{ form.css_selector.help_text }}

+ {% for e in form.css_selector.errors %}

{{ e }}

{% endfor %}
- {% endfor %} -
+ +
+
+ + {{ form.check_interval_hours }} + {% for e in form.check_interval_hours.errors %}

{{ e }}

{% endfor %} +
+
+ + {{ form.alert_price_threshold }} +

{{ form.alert_price_threshold.help_text }}

+ {% for e in form.alert_price_threshold.errors %}

{{ e }}

{% endfor %} +
+
+ + +
+
+ {{ form.recurring_notification }} +
+ +

{{ form.recurring_notification.help_text }}

+
+ {% for e in form.recurring_notification.errors %}

{{ e }}

{% endfor %} +
+
+ {{ form.enabled }} +
+ +

{% trans "Uncheck to pause price checks for this watcher." %}

+
+ {% for e in form.enabled.errors %}

{{ e }}

{% endfor %} +
+
+ +
@@ -91,18 +140,15 @@ {% block extra_js %}