diff --git a/seahub/api2/endpoints/notifications.py b/seahub/api2/endpoints/notifications.py index 2a389107f2e..25cb01c402e 100644 --- a/seahub/api2/endpoints/notifications.py +++ b/seahub/api2/endpoints/notifications.py @@ -17,7 +17,7 @@ from seahub.notifications.models import get_cache_key_of_unseen_notifications from seahub.notifications.utils import update_notice_detail, update_sdoc_notice_detail from seahub.api2.utils import api_error -from seahub.seadoc.models import SeadocCommentReply, SeadocNotification +from seahub.seadoc.models import SeadocNotification from seahub.utils.timeutils import datetime_to_isoformat_timestr logger = logging.getLogger(__name__) @@ -228,12 +228,15 @@ def get(self, request): return Response(result) def put(self, request): + """mark all sdoc notifications seen""" username = request.user.username - unseen_notices = SeadocNotification.objects.filter(username=username, seen=False) - for notice in unseen_notices: - notice.seen = True - notice.save() - + try: + SeadocNotification.objects.filter(username=username, seen=False).update(seen=True) + except Exception as e: + logger.error(e) + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + cache_key = get_cache_key_of_unseen_sdoc_notifications(username) cache.delete(cache_key) @@ -327,7 +330,7 @@ def get(self, request): per_page = int(request.GET.get('per_page', '')) page = int(request.GET.get('page', '')) except ValueError: - per_page = 25 + per_page = 5 page = 1 if page < 1: diff --git a/seahub/notifications/utils.py b/seahub/notifications/utils.py index b41e6035558..b8f56c1ea0c 100644 --- a/seahub/notifications/utils.py +++ b/seahub/notifications/utils.py @@ -399,30 +399,37 @@ def update_notice_detail(request, notices): def update_sdoc_notice_detail(request, notices): + doc_uuid_set = set() for notice in notices: + doc_uuid_set.add(notice.doc_uuid) + doc_uuid_map = {} + uuids = FileUUIDMap.objects.get_fileuuidmap_in_uuids(doc_uuid_set) + for uuid in uuids: + if uuid not in doc_uuid_map: + origin_file_path = posixpath.join(uuid.parent_path, uuid.filename) + doc_uuid_map[str(uuid.uuid)] = (origin_file_path, uuid.filename, uuid.repo_id) + + for notice in notices: + uuid = doc_uuid_map[notice.doc_uuid] if notice.is_comment(): try: d = json.loads(notice.detail) - uuid = FileUUIDMap.objects.get_fileuuidmap_by_uuid(notice.doc_uuid) - origin_file_path = posixpath.join(uuid.parent_path, uuid.filename) url, _, _ = api_avatar_url(d['author']) d['avatar_url'] = url - d['sdoc_path'] = origin_file_path - d['sdoc_name'] = uuid.filename - d['repo_id'] = uuid.repo_id + d['sdoc_path'] = uuid[0] + d['sdoc_name'] = uuid[1] + d['repo_id'] = uuid[2] notice.detail = d except Exception as e: logger.error(e) elif notice.is_reply(): try: d = json.loads(notice.detail) - uuid = FileUUIDMap.objects.get_fileuuidmap_by_uuid(notice.doc_uuid) - origin_file_path = posixpath.join(uuid.parent_path, uuid.filename) url, _, _ = api_avatar_url(d['author']) d['avatar_url'] = url - d['sdoc_path'] = origin_file_path - d['sdoc_name'] = uuid.filename - d['repo_id'] = uuid.repo_id + d['sdoc_path'] = uuid[0] + d['sdoc_name'] = uuid[1] + d['repo_id'] = uuid[2] notice.detail = d except Exception as e: logger.error(e) diff --git a/seahub/seadoc/apis.py b/seahub/seadoc/apis.py index ac02aa56e03..2f168632034 100644 --- a/seahub/seadoc/apis.py +++ b/seahub/seadoc/apis.py @@ -29,6 +29,7 @@ from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page from django.views.decorators.http import condition +from django.core.cache import cache from seaserv import seafile_api, check_quota, get_org_id_by_repo_id, ccnet_api @@ -53,7 +54,7 @@ from seahub.tags.models import FileUUIDMap from seahub.utils.error_msg import file_type_error_msg from seahub.utils.repo import parse_repo_perm, get_related_users_by_repo -from seahub.seadoc.models import SeadocHistoryName, SeadocRevision, SeadocCommentReply, SeadocNotification +from seahub.seadoc.models import SeadocHistoryName, SeadocRevision, SeadocCommentReply, SeadocNotification, get_cache_key_of_unseen_sdoc_notifications from seahub.avatar.templatetags.avatar_tags import api_avatar_url from seahub.base.templatetags.seahub_tags import email2nickname, \ email2contact_email @@ -1000,11 +1001,6 @@ def post(self, request, file_uuid): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) - try: - avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) - except ValueError: - avatar_size = AVATAR_DEFAULT_SIZE - comment = request.data.get('comment', '') detail = request.data.get('detail', '') author = request.data.get('author', '') @@ -1047,6 +1043,9 @@ def post(self, request, file_uuid): )) try: SeadocNotification.objects.bulk_create(new_notifications) + # delete sdoc notification count cache + sdoc_cache_key = get_cache_key_of_unseen_sdoc_notifications(username) + cache.delete(sdoc_cache_key) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' @@ -1070,11 +1069,6 @@ def get(self, request, file_uuid, comment_id): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) - try: - avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) - except ValueError: - avatar_size = AVATAR_DEFAULT_SIZE - # resource check try: file_comment = FileComment.objects.get(pk=comment_id) @@ -1276,6 +1270,8 @@ def post(self, request, file_uuid, comment_id): )) try: SeadocNotification.objects.bulk_create(new_notifications) + sdoc_cache_key = get_cache_key_of_unseen_sdoc_notifications(username) + cache.delete(sdoc_cache_key) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' diff --git a/seahub/tags/models.py b/seahub/tags/models.py index 74d1d703e54..262100ba725 100644 --- a/seahub/tags/models.py +++ b/seahub/tags/models.py @@ -21,6 +21,12 @@ def get_fileuuidmap_by_uuid(self, uuid): return super(FileUUIDMapManager, self).get(uuid=uuid) except self.model.DoesNotExist: return None + + def get_fileuuidmap_in_uuids(self, uuids): + try: + return super(FileUUIDMapManager, self).filter(uuid__in=uuids) + except self.model.DoesNotExist: + return None def get_or_create_fileuuidmap(self, repo_id, parent_path, filename, is_dir): """ create filemap by repo_id、 parent_path、filename、id_dir