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})
|
||||
|
||||
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})
|
||||
@@ -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"<br>%(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"<br>%(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"<br>%(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)
|
||||
|
@@ -5,7 +5,7 @@
|
||||
{% block email_con %}
|
||||
{% 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;">
|
||||
{% blocktrans count num=notice_count %}
|
||||
|
@@ -18,15 +18,15 @@
|
||||
|
||||
{% if notice.is_group_msg %}
|
||||
<p class="brief">{{ notice.format_group_message_title|safe }}
|
||||
{% if notice.format_group_message_detail %}
|
||||
{{ notice.format_group_message_detail|safe }}
|
||||
{% if notice.format_group_message_detail %}<br>
|
||||
{{ notice.format_group_message_detail }}
|
||||
{% endif %}
|
||||
</p>
|
||||
|
||||
{% elif notice.is_grpmsg_reply %}
|
||||
<p class="brief">{{ notice.format_grpmsg_reply_title|safe }}
|
||||
{% if notice.format_grpmsg_reply_detail %}
|
||||
{{ notice.format_grpmsg_reply_detail|safe }}
|
||||
{% if notice.format_grpmsg_reply_detail %}<br>
|
||||
{{ notice.format_grpmsg_reply_detail}}
|
||||
{% endif %}
|
||||
</p>
|
||||
|
||||
@@ -41,8 +41,8 @@
|
||||
|
||||
{% elif notice.is_user_message %}
|
||||
<p class="brief">{{ notice.format_user_message_title|safe }}
|
||||
{% if notice.format_user_message_detail %}
|
||||
{{ notice.format_user_message_detail|safe }}
|
||||
{% if notice.format_user_message_detail %}<br>
|
||||
{{ notice.format_user_message_detail }}
|
||||
{% endif %}
|
||||
</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_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()
|
||||
|
Reference in New Issue
Block a user