From f3f9409aab0ecdac771611aaad69e5e415d1204d Mon Sep 17 00:00:00 2001 From: zhengxie Date: Thu, 19 Jun 2014 12:31:24 +0800 Subject: [PATCH] Refactor user notices --- seahub/notifications/models.py | 42 ++++++--------- .../templates/notifications/notice_email.html | 2 +- .../snippets/notifications_popup.html | 12 ++--- seahub/views/ajax.py | 52 +++++++++---------- 4 files changed, 48 insertions(+), 60 deletions(-) diff --git a/seahub/notifications/models.py b/seahub/notifications/models.py index 692dadd6ba..a1e67688f9 100644 --- a/seahub/notifications/models.py +++ b/seahub/notifications/models.py @@ -56,7 +56,8 @@ def priv_file_share_msg_to_json(share_from, file_name, priv_share_token): 'priv_share_token': priv_share_token}) def group_msg_to_json(group_id, msg_from, message): - return json.dumps({'group_id': group_id, 'msg_from': msg_from, 'message': message}) + return json.dumps({'group_id': group_id, 'msg_from': msg_from, + 'message': message}) def grpmsg_reply_to_json(msg_id, reply_from, reply_msg): return json.dumps({'msg_id': msg_id, 'reply_from': reply_from, 'reply_msg': reply_msg}) @@ -158,7 +159,7 @@ class UserNotificationManager(models.Manager): notice.save() except UserNotification.InvalidDetailError: continue - + def seen_group_msg_reply_notice(self, to_user, msg_id=None): """Mark all group message replies of a user as seen. @@ -187,14 +188,11 @@ class UserNotificationManager(models.Manager): user_notices = super(UserNotificationManager, self).filter( to_user=to_user, seen=False, msg_type=MSG_TYPE_USER_MESSAGE) for notice in user_notices: - try: - notice_from_user = notice.user_message_detail_to_dict().get('msg_from') - if from_user == notice_from_user: - if notice.seen is False: - notice.seen = True - notice.save() - except UserNotification.InvalidDetailError: - continue + notice_from_user = notice.user_message_detail_to_dict().get('msg_from') + if from_user == notice_from_user: + if notice.seen is False: + notice.seen = True + notice.save() def remove_group_msg_notices(self, to_user, group_id): """Remove group message notices of a user. @@ -456,7 +454,6 @@ class UserNotification(models.Model): Arguments: - `self`: - Raises ``InvalidDetailError`` if detail field can not be parsed. """ assert self.is_user_message() @@ -571,11 +568,8 @@ class UserNotification(models.Model): return _(u"Internal error") message = d.get('message') - if message: - msg = _(u"
%(message)s") % { - 'message': message, - } - return msg + if message is not None: + return message else: return None @@ -621,11 +615,8 @@ class UserNotification(models.Model): return _(u"Internal error") message = d.get('message') - - if message: - msg = _(u"
%(message)s") % { - 'message': message} - return msg + if message is not None: + return message else: return None @@ -665,11 +656,8 @@ class UserNotification(models.Model): return _(u"Internal error") reply_msg = d.get('reply_msg') - - if reply_msg: - msg = _(u"
%(reply_msg)s") % { - 'reply_msg': reply_msg} - return msg + if reply_msg is not None: + return reply_msg else: return None @@ -772,7 +760,7 @@ def grpmsg_added_cb(sender, **kwargs): message = kwargs['message'] group_members = seaserv.get_group_members(int(group_id)) - notify_members = [ x.user_name for x in group_members if x.user_name != from_email ] + notify_members = [x.user_name for x in group_members if x.user_name != from_email] detail = group_msg_to_json(group_id, from_email, message) UserNotification.objects.bulk_add_group_msg_notices(notify_members, detail) diff --git a/seahub/notifications/templates/notifications/notice_email.html b/seahub/notifications/templates/notifications/notice_email.html index 1ec3c7b684..b9052ed022 100644 --- a/seahub/notifications/templates/notifications/notice_email.html +++ b/seahub/notifications/templates/notifications/notice_email.html @@ -5,7 +5,7 @@ {% block email_con %} {% autoescape off %} -

Hi, {% blocktrans with name=to_user|email2nickname %}{{ name }}{% endblocktrans %}

+

Hi, {{ name|email2nickname }}

{% blocktrans count num=notice_count %} diff --git a/seahub/templates/snippets/notifications_popup.html b/seahub/templates/snippets/notifications_popup.html index 828ff66b42..6c1635b3a4 100644 --- a/seahub/templates/snippets/notifications_popup.html +++ b/seahub/templates/snippets/notifications_popup.html @@ -18,15 +18,15 @@ {% if notice.is_group_msg %}

{{ notice.format_group_message_title|safe }} - {% if notice.format_group_message_detail %} - {{ notice.format_group_message_detail|safe }} + {% if notice.format_group_message_detail %}
+ {{ notice.format_group_message_detail }} {% endif %}

{% elif notice.is_grpmsg_reply %}

{{ notice.format_grpmsg_reply_title|safe }} - {% if notice.format_grpmsg_reply_detail %} - {{ notice.format_grpmsg_reply_detail|safe }} + {% if notice.format_grpmsg_reply_detail %}
+ {{ notice.format_grpmsg_reply_detail}} {% endif %}

@@ -41,8 +41,8 @@ {% elif notice.is_user_message %}

{{ notice.format_user_message_title|safe }} - {% if notice.format_user_message_detail %} - {{ notice.format_user_message_detail|safe }} + {% if notice.format_user_message_detail %}
+ {{ notice.format_user_message_detail }} {% endif %}

diff --git a/seahub/views/ajax.py b/seahub/views/ajax.py index 266e907c93..04ab604b65 100644 --- a/seahub/views/ajax.py +++ b/seahub/views/ajax.py @@ -39,7 +39,6 @@ from seahub.utils import check_filename_with_rename, EMPTY_SHA1, \ get_repo_last_modify, gen_file_upload_url, is_org_context, \ get_org_user_events, get_user_events from seahub.utils.star import star_file, unstar_file -from seahub.avatar.util import get_default_avatar_url # Get an instance of a logger logger = logging.getLogger(__name__) @@ -1276,7 +1275,12 @@ def unseen_notices_count(request): @login_required def get_popup_notices(request): - """Get user's unseen notices: + """Get user's notifications. + + If unseen notices > 5, return all unseen notices. + If unseen notices = 0, return last 5 notices. + Otherwise return all unseen notices, pluse some seen notices to make the + sum equal to 5. Arguments: - `request`: @@ -1287,21 +1291,23 @@ def get_popup_notices(request): content_type = 'application/json; charset=utf-8' username = request.user.username - count = UserNotification.objects.count_unseen_user_notifications(username) result_notices = [] unseen_notices = [] seen_notices = [] - list_num = 5 + list_num = 5 + count = UserNotification.objects.count_unseen_user_notifications(username) if count == 0: - seen_notices = UserNotification.objects.filter(to_user = username)[:list_num] + seen_notices = UserNotification.objects.get_user_notifications( + username)[:list_num] elif count > list_num: - unseen_notices = UserNotification.objects.filter(to_user = username, seen = False) + unseen_notices = UserNotification.objects.get_user_notifications( + username, seen=False) else: - unseen_notices = UserNotification.objects.filter(to_user = username, seen = False) - seen_notices = UserNotification.objects.filter( - to_user = username, - seen = True)[:list_num-count] + unseen_notices = UserNotification.objects.get_user_notifications( + username, seen=False) + seen_notices = UserNotification.objects.get_user_notifications( + username, seen=True)[:list_num - count] result_notices += unseen_notices result_notices += seen_notices @@ -1310,49 +1316,42 @@ def get_popup_notices(request): if notice.is_user_message(): d = notice.user_message_detail_to_dict() notice.msg_from = d.get('msg_from') - continue elif notice.is_group_msg(): - d = json.loads(notice.detail) - notice.msg_from = d['msg_from'] - continue + d = notice.group_message_detail_to_dict() + notice.msg_from = d.get('msg_from') elif notice.is_grpmsg_reply(): - d = json.loads(notice.detail) - notice.msg_from = d['reply_from'] - continue + d = notice.grpmsg_reply_detail_to_dict() + notice.msg_from = d.get('reply_from') elif notice.is_file_uploaded_msg(): - d = json.loads(notice.detail) + from seahub.avatar.util import get_default_avatar_url notice.default_avatar_url = get_default_avatar_url() - continue elif notice.is_repo_share_msg(): d = json.loads(notice.detail) notice.msg_from = d['share_from'] - continue elif notice.is_priv_file_share_msg(): d = json.loads(notice.detail) notice.msg_from = d['share_from'] - continue elif notice.is_group_join_request(): d = json.loads(notice.detail) notice.msg_from = d['username'] - continue else: pass ctx_notices = {"notices": result_notices} - notifications_popup_html = render_to_string( 'snippets/notifications_popup.html', ctx_notices, context_instance=RequestContext(request)) - return HttpResponse(json.dumps({"notifications_popup_html": notifications_popup_html}), - content_type=content_type) + return HttpResponse(json.dumps({ + "notifications_popup_html": notifications_popup_html, + }), content_type=content_type) @login_required def set_notices_seen(request): @@ -1367,7 +1366,8 @@ def set_notices_seen(request): content_type = 'application/json; charset=utf-8' username = request.user.username - unseen_notices = UserNotification.objects.filter(to_user = username, seen = False) + unseen_notices = UserNotification.objects.get_user_notifications(username, + seen=False) for notice in unseen_notices: notice.seen = True notice.save()