diff options
-rw-r--r-- | taskapp/models.py | 11 | ||||
-rw-r--r-- | taskapp/utilities/notification.py | 25 | ||||
-rw-r--r-- | taskapp/views/user.py | 27 | ||||
-rw-r--r-- | templates/user/browse_notifications.html | 2 |
4 files changed, 47 insertions, 18 deletions
diff --git a/taskapp/models.py b/taskapp/models.py index cd060a8..aa75bc8 100644 --- a/taskapp/models.py +++ b/taskapp/models.py @@ -155,12 +155,15 @@ class Request(models.Model): class Notification(models.Model): - to = models.ManyToManyField(User, related_name = "%(class)s_to", blank = False) - is_read = models.BooleanField(default = False) - sent_date = models.DateTimeField() + sent_to = models.ManyToManyField(User, related_name = "%(class)s_sent_to", blank = False) + sent_from = models.ManyToManyField(User, related_name = "%(class)s_sent_from", blank = True) + sub = models.CharField(max_length = 100) message = models.TextField() - deleted = models.BooleanField(default = False) + + sent_date = models.DateTimeField() + is_read = models.BooleanField(default = False) + is_deleted = models.BooleanField(default = False) tagging.register(Profile) tagging.register(Task) diff --git a/taskapp/utilities/notification.py b/taskapp/utilities/notification.py index 5f72540..5d1d994 100644 --- a/taskapp/utilities/notification.py +++ b/taskapp/utilities/notification.py @@ -1,5 +1,6 @@ -from pytask.taskapp.models import Notification from datetime import datetime +from django.contrib.auth.models import User +from pytask.taskapp.models import Notification def create_notification(to,subject,message): """ @@ -10,7 +11,7 @@ def create_notification(to,subject,message): """ notification = Notification(sent_date = datetime.now()) notification.save() - notification.to = to + notification.sent_to = to notification.sub = subject notification.message = message notification.save() @@ -39,6 +40,24 @@ def delete_notification(notification_id): notification = Notification.objects.get(id = notification_id) except Notification.DoesNotExist: return False - notification.deleted = True + notification.is_deleted = True notification.save() return True + +def get_notification(nid, user): + """ if notification exists, and belongs to the current user, return it. + else return None. + """ + + try: + notify_obj = Notification.objects.get(id=nid) + except Notification.DoesNotExist: + return None + + try: + notify_obj.sent_to.get(id=user.id) + except User.DoesNotExist: + return None + + if not notify_obj.is_deleted: + return notify_obj diff --git a/taskapp/views/user.py b/taskapp/views/user.py index 0af117b..5baf09b 100644 --- a/taskapp/views/user.py +++ b/taskapp/views/user.py @@ -6,10 +6,14 @@ from django.contrib.auth.models import User from django.contrib.auth.decorators import login_required from pytask.taskapp.models import Task, Profile, Request + from pytask.taskapp.events.user import createUser, updateProfile -from pytask.taskapp.forms.user import UserProfileEditForm from pytask.taskapp.events.request import reply_to_request + +from pytask.taskapp.forms.user import UserProfileEditForm + from pytask.taskapp.utilities.request import get_request +from pytask.taskapp.utilities.notification import get_notification def show_msg(user, message, redirect_url=None, url_desc=None): """ simply redirect to homepage """ @@ -40,7 +44,7 @@ def homepage(request): user_profile = user.get_profile() is_mentor = True if user.task_mentors.all() else False can_create_task = False if user_profile.rights == u"CT" else True - notifications = user.notification_to.filter(deleted=False,is_read=False) + notifications = user.notification_sent_to.filter(is_deleted=False,is_read=False) requests = user.request_sent_to.filter(is_replied=False) context = {'user':user, @@ -168,9 +172,7 @@ def browse_notifications(request): user = request.user - active_notifications = user.notification_to.filter(deleted=False).order_by('sent_date').reverse() - for pos, notification in enumerate(reversed(active_notifications)): - notification.pos = pos + active_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date').reverse() context = { 'user':user, @@ -186,8 +188,10 @@ def view_notification(request, nid): """ user = request.user - notifications = user.notification_to.filter(deleted=False).order_by('sent_date') - notification = notifications[int(nid)] + notification = get_notification(nid, user) + if not notification: + raise Http404 + notification.is_read = True notification.save() @@ -206,13 +210,16 @@ def edit_notification(request, nid, action): """ user = request.user - notifications = user.notification_to.filter(deleted=False).order_by('sent_date') - notification = notifications[int(nid)] + notification = get_notification(nid, user) + + if not notification: + raise Http404 + notifications_url = "/user/notifications/" if request.method == "POST": if action == "delete": - notification.deleted = True + notification.is_deleted = True elif action == "unread": notification.is_read = False diff --git a/templates/user/browse_notifications.html b/templates/user/browse_notifications.html index a6a6ff2..21b4ce0 100644 --- a/templates/user/browse_notifications.html +++ b/templates/user/browse_notifications.html @@ -4,7 +4,7 @@ You have no notifications. {% else %} {% for notification in notifications %} - <a href="/user/notifications/nid={{notification.pos}}"> + <a href="/user/notifications/nid={{notification.id}}"> {% if not notification.is_read %} <b> {% endif %} {{notification.sub}} {% if not notification.is_read %} </b> {% endif %}</a><br /> |