Permalink
Browse files

Add permanent dismissals

  • Loading branch information...
1 parent 1b2ca39 commit 8079135f8f8123ad7166add3d8eb207ef95c7fff @paltman paltman committed Nov 10, 2011
Showing with 77 additions and 13 deletions.
  1. +20 −0 docs/changelog.rst
  2. +8 −2 docs/settings.rst
  3. +12 −0 reminders/models.py
  4. +20 −5 reminders/templatetags/reminders_tags.py
  5. +17 −6 reminders/views.py
View
20 docs/changelog.rst
@@ -3,6 +3,26 @@
ChangeLog
=========
+0.2
+---
+
+- added permanent dismissals
+
+Migrations
+^^^^^^^^^^
+
+Here is a sample migration that should work with Postgresql/nashvegas::
+
+ ### New Model: reminders.Dismissal
+ CREATE TABLE "reminders_dismissal" (
+ "id" serial NOT NULL PRIMARY KEY,
+ "user_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED,
+ "label" varchar(200) NOT NULL,
+ "dismissed_at" timestamp with time zone NOT NULL
+ )
+ ;
+ CREATE INDEX "reminders_dismissal_user_id" ON "reminders_dismissal" ("user_id");
+
0.1
---
View
10 docs/settings.rst
@@ -16,14 +16,20 @@ configured by this setting. Here is an example::
"profile_completed": {
"test": "profiles.reminders.completed",
"message": "You have only completed %(percentage)s%% of your <a href="%(url)s">profile</a>.",
- "dismissable": False
+ "dismissable": "permanent"
},
"email_confirmed": {
"test": lambda user: confirmed(user),
- "message": "Please <a href="%(url)">confirm</a> your email address."
+ "message": "Please <a href="%(url)">confirm</a> your email address.",
+ "dismissable": "no"
}
}
+Valid values for the `dismissable` key are `permanent`, `session`, and `no`. If
+left out of the settings it will default to `session`. As you might have guessed,
+this controls whether or not a user can dismiss a reminder and if they do, whether
+it is dismissed for the duration of their session or for good.
+
Callable API
^^^^^^^^^^^^
View
12 reminders/models.py
@@ -0,0 +1,12 @@
+import datetime
+
+from django.db import models
+
+from django.contrib.auth.models import User
+
+
+class Dismissal(models.Model):
+
+ user = models.ForeignKey(User, related_name="reminder_dismissals")
+ label = models.CharField(max_length=200)
+ dismissed_at = models.DateTimeField(default=datetime.datetime.now)
View
25 reminders/templatetags/reminders_tags.py
@@ -4,6 +4,8 @@
from django.utils.importlib import import_module
from django.utils.safestring import mark_safe
+from reminders.models import Dismissal
+
register = template.Library()
@@ -24,6 +26,16 @@ def load_callable(path_to_callable):
return func
+def is_dismissed(label, dismissal_type, request):
+ if dismissal_type == "session":
+ dismissed = label in request.session
+ elif dismissal_type == "permanent":
+ dismissed = Dismissal.objects.filter(user=request.user, label=label).exists()
+ else:
+ dismissed = False
+ return dismissed
+
+
class RemindersNode(template.Node):
@classmethod
@@ -40,18 +52,21 @@ def render(self, context):
request = context["request"]
reminders = []
for label in settings.REMINDERS:
- if label not in request.session:
- test = settings.REMINDERS[label]["test"]
- message = settings.REMINDERS[label]["message"]
+ reminder = settings.REMINDERS[label]
+ if not is_dismissed(label, reminder.get("dismissable"), request):
+ test = reminder["test"]
+ message = reminder["message"]
if not callable(test):
test = load_callable(test)
url = None
- if settings.REMINDERS[label].get("dismissable", True):
+ if reminder.get("dismissable") != "no":
url = reverse("reminders_dismiss", kwargs={"label": label})
result = test(request.user)
if result:
+ if isinstance(result, dict):
+ message = message % result
reminders.append({
- "message": mark_safe(message % test(request.user)),
+ "message": mark_safe(message),
"dismiss_url": url
})
context[self.as_var] = reminders
View
23 reminders/views.py
@@ -1,15 +1,26 @@
from django.conf import settings
from django.http import HttpResponse, Http404, HttpResponseNotAllowed
+from reminders.models import Dismissal
+
def dismiss(request, label):
+
if request.method != "POST":
return HttpResponseNotAllowed(["POST"])
+
if label not in settings.REMINDERS:
return Http404()
- if not settings.REMINDERS.get(label).get("dismissable", True):
- return HttpResponse(status=409)
- for reminder in settings.REMINDERS:
- if reminder == label and settings.REMINDERS[reminder].get("dismissable", True):
- request.session[label] = "dismissed"
- return HttpResponse()
+
+ dismiss_type = settings.REMINDERS[label].get("dismissable", "session")
+
+ if dismiss_type == "session":
+ request.session[label] = "dismissed"
+ status = 200
+ elif dismiss_type == "permanent":
+ Dismissal.objects.create(user=request.user, label=label)
+ status = 200
+ else:
+ status = 409
+
+ return HttpResponse(status=status)

0 comments on commit 8079135

Please sign in to comment.