mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-23 04:18:21 +00:00
Refactor user notices
This commit is contained in:
@@ -56,7 +56,8 @@ def priv_file_share_msg_to_json(share_from, file_name, priv_share_token):
|
|||||||
'priv_share_token': priv_share_token})
|
'priv_share_token': priv_share_token})
|
||||||
|
|
||||||
def group_msg_to_json(group_id, msg_from, message):
|
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):
|
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})
|
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()
|
notice.save()
|
||||||
except UserNotification.InvalidDetailError:
|
except UserNotification.InvalidDetailError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def seen_group_msg_reply_notice(self, to_user, msg_id=None):
|
def seen_group_msg_reply_notice(self, to_user, msg_id=None):
|
||||||
"""Mark all group message replies of a user as seen.
|
"""Mark all group message replies of a user as seen.
|
||||||
|
|
||||||
@@ -187,14 +188,11 @@ class UserNotificationManager(models.Manager):
|
|||||||
user_notices = super(UserNotificationManager, self).filter(
|
user_notices = super(UserNotificationManager, self).filter(
|
||||||
to_user=to_user, seen=False, msg_type=MSG_TYPE_USER_MESSAGE)
|
to_user=to_user, seen=False, msg_type=MSG_TYPE_USER_MESSAGE)
|
||||||
for notice in user_notices:
|
for notice in user_notices:
|
||||||
try:
|
notice_from_user = notice.user_message_detail_to_dict().get('msg_from')
|
||||||
notice_from_user = notice.user_message_detail_to_dict().get('msg_from')
|
if from_user == notice_from_user:
|
||||||
if from_user == notice_from_user:
|
if notice.seen is False:
|
||||||
if notice.seen is False:
|
notice.seen = True
|
||||||
notice.seen = True
|
notice.save()
|
||||||
notice.save()
|
|
||||||
except UserNotification.InvalidDetailError:
|
|
||||||
continue
|
|
||||||
|
|
||||||
def remove_group_msg_notices(self, to_user, group_id):
|
def remove_group_msg_notices(self, to_user, group_id):
|
||||||
"""Remove group message notices of a user.
|
"""Remove group message notices of a user.
|
||||||
@@ -456,7 +454,6 @@ class UserNotification(models.Model):
|
|||||||
Arguments:
|
Arguments:
|
||||||
- `self`:
|
- `self`:
|
||||||
|
|
||||||
Raises ``InvalidDetailError`` if detail field can not be parsed.
|
|
||||||
"""
|
"""
|
||||||
assert self.is_user_message()
|
assert self.is_user_message()
|
||||||
|
|
||||||
@@ -571,11 +568,8 @@ class UserNotification(models.Model):
|
|||||||
return _(u"Internal error")
|
return _(u"Internal error")
|
||||||
|
|
||||||
message = d.get('message')
|
message = d.get('message')
|
||||||
if message:
|
if message is not None:
|
||||||
msg = _(u"<br>%(message)s") % {
|
return message
|
||||||
'message': message,
|
|
||||||
}
|
|
||||||
return msg
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -621,11 +615,8 @@ class UserNotification(models.Model):
|
|||||||
return _(u"Internal error")
|
return _(u"Internal error")
|
||||||
|
|
||||||
message = d.get('message')
|
message = d.get('message')
|
||||||
|
if message is not None:
|
||||||
if message:
|
return message
|
||||||
msg = _(u"<br>%(message)s") % {
|
|
||||||
'message': message}
|
|
||||||
return msg
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -665,11 +656,8 @@ class UserNotification(models.Model):
|
|||||||
return _(u"Internal error")
|
return _(u"Internal error")
|
||||||
|
|
||||||
reply_msg = d.get('reply_msg')
|
reply_msg = d.get('reply_msg')
|
||||||
|
if reply_msg is not None:
|
||||||
if reply_msg:
|
return reply_msg
|
||||||
msg = _(u"<br>%(reply_msg)s") % {
|
|
||||||
'reply_msg': reply_msg}
|
|
||||||
return msg
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -772,7 +760,7 @@ def grpmsg_added_cb(sender, **kwargs):
|
|||||||
message = kwargs['message']
|
message = kwargs['message']
|
||||||
group_members = seaserv.get_group_members(int(group_id))
|
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)
|
detail = group_msg_to_json(group_id, from_email, message)
|
||||||
UserNotification.objects.bulk_add_group_msg_notices(notify_members, detail)
|
UserNotification.objects.bulk_add_group_msg_notices(notify_members, detail)
|
||||||
|
@@ -5,7 +5,7 @@
|
|||||||
{% block email_con %}
|
{% block email_con %}
|
||||||
{% autoescape off %}
|
{% autoescape off %}
|
||||||
|
|
||||||
<p style="color:#121214;font-size:14px;">Hi, {% blocktrans with name=to_user|email2nickname %}{{ name }}{% endblocktrans %}</p>
|
<p style="color:#121214;font-size:14px;">Hi, {{ name|email2nickname }}</p>
|
||||||
|
|
||||||
<p style="font-size:14px;color:#434144;">
|
<p style="font-size:14px;color:#434144;">
|
||||||
{% blocktrans count num=notice_count %}
|
{% blocktrans count num=notice_count %}
|
||||||
|
@@ -18,15 +18,15 @@
|
|||||||
|
|
||||||
{% if notice.is_group_msg %}
|
{% if notice.is_group_msg %}
|
||||||
<p class="brief">{{ notice.format_group_message_title|safe }}
|
<p class="brief">{{ notice.format_group_message_title|safe }}
|
||||||
{% if notice.format_group_message_detail %}
|
{% if notice.format_group_message_detail %}<br>
|
||||||
{{ notice.format_group_message_detail|safe }}
|
{{ notice.format_group_message_detail }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{% elif notice.is_grpmsg_reply %}
|
{% elif notice.is_grpmsg_reply %}
|
||||||
<p class="brief">{{ notice.format_grpmsg_reply_title|safe }}
|
<p class="brief">{{ notice.format_grpmsg_reply_title|safe }}
|
||||||
{% if notice.format_grpmsg_reply_detail %}
|
{% if notice.format_grpmsg_reply_detail %}<br>
|
||||||
{{ notice.format_grpmsg_reply_detail|safe }}
|
{{ notice.format_grpmsg_reply_detail}}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
@@ -41,8 +41,8 @@
|
|||||||
|
|
||||||
{% elif notice.is_user_message %}
|
{% elif notice.is_user_message %}
|
||||||
<p class="brief">{{ notice.format_user_message_title|safe }}
|
<p class="brief">{{ notice.format_user_message_title|safe }}
|
||||||
{% if notice.format_user_message_detail %}
|
{% if notice.format_user_message_detail %}<br>
|
||||||
{{ notice.format_user_message_detail|safe }}
|
{{ notice.format_user_message_detail }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@@ -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_repo_last_modify, gen_file_upload_url, is_org_context, \
|
||||||
get_org_user_events, get_user_events
|
get_org_user_events, get_user_events
|
||||||
from seahub.utils.star import star_file, unstar_file
|
from seahub.utils.star import star_file, unstar_file
|
||||||
from seahub.avatar.util import get_default_avatar_url
|
|
||||||
|
|
||||||
# Get an instance of a logger
|
# Get an instance of a logger
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -1276,7 +1275,12 @@ def unseen_notices_count(request):
|
|||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def get_popup_notices(request):
|
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:
|
Arguments:
|
||||||
- `request`:
|
- `request`:
|
||||||
@@ -1287,21 +1291,23 @@ def get_popup_notices(request):
|
|||||||
content_type = 'application/json; charset=utf-8'
|
content_type = 'application/json; charset=utf-8'
|
||||||
username = request.user.username
|
username = request.user.username
|
||||||
|
|
||||||
count = UserNotification.objects.count_unseen_user_notifications(username)
|
|
||||||
result_notices = []
|
result_notices = []
|
||||||
unseen_notices = []
|
unseen_notices = []
|
||||||
seen_notices = []
|
seen_notices = []
|
||||||
list_num = 5
|
|
||||||
|
|
||||||
|
list_num = 5
|
||||||
|
count = UserNotification.objects.count_unseen_user_notifications(username)
|
||||||
if count == 0:
|
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:
|
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:
|
else:
|
||||||
unseen_notices = UserNotification.objects.filter(to_user = username, seen = False)
|
unseen_notices = UserNotification.objects.get_user_notifications(
|
||||||
seen_notices = UserNotification.objects.filter(
|
username, seen=False)
|
||||||
to_user = username,
|
seen_notices = UserNotification.objects.get_user_notifications(
|
||||||
seen = True)[:list_num-count]
|
username, seen=True)[:list_num - count]
|
||||||
|
|
||||||
result_notices += unseen_notices
|
result_notices += unseen_notices
|
||||||
result_notices += seen_notices
|
result_notices += seen_notices
|
||||||
@@ -1310,49 +1316,42 @@ def get_popup_notices(request):
|
|||||||
if notice.is_user_message():
|
if notice.is_user_message():
|
||||||
d = notice.user_message_detail_to_dict()
|
d = notice.user_message_detail_to_dict()
|
||||||
notice.msg_from = d.get('msg_from')
|
notice.msg_from = d.get('msg_from')
|
||||||
continue
|
|
||||||
|
|
||||||
elif notice.is_group_msg():
|
elif notice.is_group_msg():
|
||||||
d = json.loads(notice.detail)
|
d = notice.group_message_detail_to_dict()
|
||||||
notice.msg_from = d['msg_from']
|
notice.msg_from = d.get('msg_from')
|
||||||
continue
|
|
||||||
|
|
||||||
elif notice.is_grpmsg_reply():
|
elif notice.is_grpmsg_reply():
|
||||||
d = json.loads(notice.detail)
|
d = notice.grpmsg_reply_detail_to_dict()
|
||||||
notice.msg_from = d['reply_from']
|
notice.msg_from = d.get('reply_from')
|
||||||
continue
|
|
||||||
|
|
||||||
elif notice.is_file_uploaded_msg():
|
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()
|
notice.default_avatar_url = get_default_avatar_url()
|
||||||
continue
|
|
||||||
|
|
||||||
elif notice.is_repo_share_msg():
|
elif notice.is_repo_share_msg():
|
||||||
d = json.loads(notice.detail)
|
d = json.loads(notice.detail)
|
||||||
notice.msg_from = d['share_from']
|
notice.msg_from = d['share_from']
|
||||||
continue
|
|
||||||
|
|
||||||
elif notice.is_priv_file_share_msg():
|
elif notice.is_priv_file_share_msg():
|
||||||
d = json.loads(notice.detail)
|
d = json.loads(notice.detail)
|
||||||
notice.msg_from = d['share_from']
|
notice.msg_from = d['share_from']
|
||||||
continue
|
|
||||||
|
|
||||||
elif notice.is_group_join_request():
|
elif notice.is_group_join_request():
|
||||||
d = json.loads(notice.detail)
|
d = json.loads(notice.detail)
|
||||||
notice.msg_from = d['username']
|
notice.msg_from = d['username']
|
||||||
continue
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
ctx_notices = {"notices": result_notices}
|
ctx_notices = {"notices": result_notices}
|
||||||
|
|
||||||
notifications_popup_html = render_to_string(
|
notifications_popup_html = render_to_string(
|
||||||
'snippets/notifications_popup.html', ctx_notices,
|
'snippets/notifications_popup.html', ctx_notices,
|
||||||
context_instance=RequestContext(request))
|
context_instance=RequestContext(request))
|
||||||
|
|
||||||
return HttpResponse(json.dumps({"notifications_popup_html": notifications_popup_html}),
|
return HttpResponse(json.dumps({
|
||||||
content_type=content_type)
|
"notifications_popup_html": notifications_popup_html,
|
||||||
|
}), content_type=content_type)
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def set_notices_seen(request):
|
def set_notices_seen(request):
|
||||||
@@ -1367,7 +1366,8 @@ def set_notices_seen(request):
|
|||||||
content_type = 'application/json; charset=utf-8'
|
content_type = 'application/json; charset=utf-8'
|
||||||
username = request.user.username
|
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:
|
for notice in unseen_notices:
|
||||||
notice.seen = True
|
notice.seen = True
|
||||||
notice.save()
|
notice.save()
|
||||||
|
Reference in New Issue
Block a user