mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-22 20:08:19 +00:00
Refactor user notices
This commit is contained in:
@@ -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