diff --git a/src/model.py b/src/model.py index edf7d4f..eadbdfb 100755 --- a/src/model.py +++ b/src/model.py @@ -186,6 +186,20 @@ class NotificationGroups(db.Model): def __repr__(self): return '' % (self.notification_id, self.group_name) +class UserNotificationPair(db.Model): + id = db.Column(db.Integer, primary_key=True) + userName = db.Column(db.String(10)) + notifyId = db.Column(db.Integer) + isRead = db.Column(db.Integer) + + def __init__(self, username, notifyid): + self.userName = username + self.notifyId = notifyid + self.isRead = 0 + + def __repr__(self): + return '' % (self.userName, self.notifyId) + class VNode(db.Model): __bind_key__ = 'history' name = db.Column(db.String(100), primary_key=True) diff --git a/src/notificationmgr.py b/src/notificationmgr.py index 6713dea..c94826f 100644 --- a/src/notificationmgr.py +++ b/src/notificationmgr.py @@ -1,7 +1,7 @@ import json from log import logger -from model import db, Notification, NotificationGroups, User +from model import db, Notification, NotificationGroups, User, UserNotificationPair from userManager import administration_required, token_required import smtplib from email.mime.text import MIMEText @@ -21,6 +21,10 @@ class NotificationMgr: NotificationGroups.query.all() except: db.create_all() + try: + UserNotificationPair.query.all() + except: + db.create_all() logger.info("Notification Manager init done!") def query_user_notifications(self, user): @@ -102,6 +106,16 @@ class NotificationMgr: db.session.commit() if 'sendMail' in form: self.mail_notification(notify.id) + users = User.query.all() + for user in users: + user_group = user.user_group + for group_name in group_names: + if user_group == group_name: + tempPair = UserNotificationPair(user.username, notify.id) + db.session.add(tempPair) + break; + db.session.commit() + return {"success": 'true'} @administration_required @@ -158,36 +172,62 @@ class NotificationMgr: db.session.delete(notify_groups) db.session.delete(notify) db.session.commit() + temppairs = UserNotificationPair.query.filter_by(notifyId=notify_id).all() + for temppair in temppairs: + db.session.delete(temppair) + db.session.commit() return {"success": 'true'} @token_required def query_self_notification_simple_infos(self, *args, **kwargs): user = kwargs['cur_user'] + username = user.username notifies = self.query_user_notifications(user) notify_simple_infos = [] for notify in notifies: if notify is None or notify.status != 'open': continue + notifyid = notify.id + temppair = UserNotificationPair.query.filter_by(userName=username, notifyId=notifyid).first() + if temppair == None: + isRead = 0 + temppair = UserNotificationPair(username, notifyid) + db.session.add(temppair) + db.session.commit() + else: + isRead = temppair.isRead notify_simple_infos.append({ 'id': notify.id, 'title': notify.title, - 'create_date': notify.create_date + 'create_date': notify.create_date, + 'isRead': isRead }) return {'success': 'true', 'data': notify_simple_infos} @token_required def query_self_notifications_infos(self, *args, **kwargs): user = kwargs['cur_user'] + username = user.username notifies = self.query_user_notifications(user) notify_infos = [] for notify in notifies: if notify is None or notify.status != 'open': continue + notifyid = notify.id + temppair = UserNotificationPair.query.filter_by(userName=username, notifyId=notifyid).first() + if temppair == None: + temppair = UserNotificationPair(username, notifyid) + db.session.add(temppair) + isRead = 1 + temppair.isRead = 1 + db.session.add(temppair) + db.session.commit() notify_infos.append({ 'id': notify.id, 'title': notify.title, 'content': notify.content, - 'create_date': notify.create_date + 'create_date': notify.create_date, + 'isRead': isRead }) return {'success': 'true', 'data': notify_infos} @@ -208,6 +248,10 @@ class NotificationMgr: 'content': notify.content, 'create_date': notify.create_date } + usernotifypair = UserNotificationPair.query.filter_by(userName=user.username, notifyId=notify.id).first() + usernotifypair.isRead = 1 + db.session.add(usernotifypair) + db.session.commit() if notify.status != 'open': notify_info['title'] = 'This notification is not available' notify_info['content'] = 'Sorry, it seems that the administrator has closed this notification.' diff --git a/web/templates/base_AdminLTE.html b/web/templates/base_AdminLTE.html index d1666be..c9db229 100644 --- a/web/templates/base_AdminLTE.html +++ b/web/templates/base_AdminLTE.html @@ -265,29 +265,35 @@ var notifies = data.data; var len = notifies.length; var cnt = 0; + var unread = 0; var now = new Date().getTime(); for(var t = 0; t < len; t++) { var notify = notifies[t]; var createDate = notify.create_date.substring(0, 19); + var isRead = notify.isRead; createDate = createDate.replace(/-/g,'/'); var from = new Date(createDate).getTime(); var delta = now - from; if(delta <= 604800000) { cnt ++; } + if(isRead == 0){ + unread ++; + } } $('#notificationIcon').find('span').remove(); $('#notificationList').empty(); - if(cnt != 0) { - $(""+cnt+"").appendTo($('#notificationIcon')); - } + if(unread != 0) + $(""+unread+"").appendTo($('#notificationIcon')); + $("#notificationHeader").html("You have " + len + " notifications, "+cnt+" in 1 week"); for(var i = 0; i < len; i++) { notify = notifies[i]; console.log(notify); + var isRead = notify.isRead; var a = $(" "+ notify.title +"") var item = $("
  • "); item.append(a);