From 22e32b9e518987541e7c5d75cd76916566812d74 Mon Sep 17 00:00:00 2001 From: katkamrachana Date: Wed, 5 Jul 2017 13:30:29 +0530 Subject: [PATCH 1/6] commenting code block --- .../gnowsys_ndf/ndf/views/discussion.py | 104 +++++++++--------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/discussion.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/discussion.py index b24ad98054..dfc4a4fc3a 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/discussion.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/discussion.py @@ -265,58 +265,58 @@ def discussion_reply(request, group_id, node_id): current_group_id=group_id, active_user_id_or_list=active_user_ids_list) - - # if gs_type_node.if_file.mime_type : - # file_creator_id = gs_type_node.created_by - # if file_creator_id != request.user.id : - # counter_obj = Counter.get_counter_obj(request.user.id, ObjectId(group_id)) - # # counter_obj.no_comments_on_others_files += 1 - # counter_obj['file']['commented_on_others_res'] += 1 - # # counter_obj.no_comments_by_user += 1 - # counter_obj['total_comments_by_user'] += 1 - # # counter_obj.course_score += GSTUDIO_COMMENT_POINTS - # counter_obj['group_points'] += GSTUDIO_COMMENT_POINTS - - # counter_obj_creator = Counter.get_counter_obj(file_creator_id, ObjectId(group_id)) - # # counter_obj_creator.no_comments_received_on_files += 1 - # counter_obj_creator['file']['comments_gained'] += 1 - # # counter_obj_creator.no_comments_for_user += 1 - # if str(counter_obj.user_id) in counter_obj_creator.file['comments_by_others_on_res'].keys(): - # # counter_obj_creator.comments_by_others_on_files[str(counter_obj.user_id)] += 1 - # counter_obj_creator['file']['comments_by_others_on_res'][str(counter_obj.user_id)] += 1 - # else: - # # counter_obj_creator.comments_by_others_on_files.update({str(counter_obj.user_id):1}) - # counter_obj_creator.file['comments_by_others_on_res'].update({str(counter_obj.user_id):1}) - # counter_obj.last_update = datetime.now() - # counter_obj_creator.last_update = datetime.now() - # counter_obj.save() - # counter_obj_creator.save() - # else : - # note_creator_id = gs_type_node.created_by - # if note_creator_id != request.user.id : - # counter_obj = Counter.get_counter_obj(request.user.id, ObjectId(group_id)) - # # counter_obj.no_comments_by_user += 1 - # counter_obj['total_comments_by_user'] += 1 - # # counter_obj.no_comments_on_others_notes += 1 - # counter_obj['page']['blog']['commented_on_others_res'] += 1 - # counter_obj['group_points'] += GSTUDIO_COMMENT_POINTS - - # counter_obj_creator = Counter.get_counter_obj(note_creator_id, ObjectId(group_id)) - # # counter_obj_creator.no_comments_for_user += 1 - # # counter_obj_creator.no_comments_received_on_notes += 1 - # counter_obj_creator['page']['blog']['comments_gained'] += 1 - - # # if str(counter_obj.user_id) in counter_obj_creator.comments_by_others_on_notes.keys(): - # if str(counter_obj.user_id) in counter_obj_creator.page.blog['comments_by_others_on_res'].keys(): - # # counter_obj_creator.comments_by_others_on_notes[str(counter_obj.user_id)] += 1 - # counter_obj_creator['page']['blog']['comments_by_others_on_res'][str(counter_obj.user_id)] += 1 - # else: - # counter_obj_creator.page.blog['comments_by_others_on_res'].update({str(counter_obj.user_id):1}) - # counter_obj.last_update = datetime.now() - # counter_obj_creator.last_update = datetime.now() - # counter_obj.save() - # counter_obj_creator.save() - + ''' + if gs_type_node.if_file.mime_type : + file_creator_id = gs_type_node.created_by + if file_creator_id != request.user.id : + counter_obj = Counter.get_counter_obj(request.user.id, ObjectId(group_id)) + # counter_obj.no_comments_on_others_files += 1 + counter_obj['file']['commented_on_others_res'] += 1 + # counter_obj.no_comments_by_user += 1 + counter_obj['total_comments_by_user'] += 1 + # counter_obj.course_score += GSTUDIO_COMMENT_POINTS + counter_obj['group_points'] += GSTUDIO_COMMENT_POINTS + + counter_obj_creator = Counter.get_counter_obj(file_creator_id, ObjectId(group_id)) + # counter_obj_creator.no_comments_received_on_files += 1 + counter_obj_creator['file']['comments_gained'] += 1 + # counter_obj_creator.no_comments_for_user += 1 + if str(counter_obj.user_id) in counter_obj_creator.file['comments_by_others_on_res'].keys(): + # counter_obj_creator.comments_by_others_on_files[str(counter_obj.user_id)] += 1 + counter_obj_creator['file']['comments_by_others_on_res'][str(counter_obj.user_id)] += 1 + else: + # counter_obj_creator.comments_by_others_on_files.update({str(counter_obj.user_id):1}) + counter_obj_creator.file['comments_by_others_on_res'].update({str(counter_obj.user_id):1}) + counter_obj.last_update = datetime.now() + counter_obj_creator.last_update = datetime.now() + counter_obj.save() + counter_obj_creator.save() + else : + note_creator_id = gs_type_node.created_by + if note_creator_id != request.user.id : + counter_obj = Counter.get_counter_obj(request.user.id, ObjectId(group_id)) + # counter_obj.no_comments_by_user += 1 + counter_obj['total_comments_by_user'] += 1 + # counter_obj.no_comments_on_others_notes += 1 + counter_obj['page']['blog']['commented_on_others_res'] += 1 + counter_obj['group_points'] += GSTUDIO_COMMENT_POINTS + + counter_obj_creator = Counter.get_counter_obj(note_creator_id, ObjectId(group_id)) + # counter_obj_creator.no_comments_for_user += 1 + # counter_obj_creator.no_comments_received_on_notes += 1 + counter_obj_creator['page']['blog']['comments_gained'] += 1 + + # if str(counter_obj.user_id) in counter_obj_creator.comments_by_others_on_notes.keys(): + if str(counter_obj.user_id) in counter_obj_creator.page.blog['comments_by_others_on_res'].keys(): + # counter_obj_creator.comments_by_others_on_notes[str(counter_obj.user_id)] += 1 + counter_obj_creator['page']['blog']['comments_by_others_on_res'][str(counter_obj.user_id)] += 1 + else: + counter_obj_creator.page.blog['comments_by_others_on_res'].update({str(counter_obj.user_id):1}) + counter_obj.last_update = datetime.now() + counter_obj_creator.last_update = datetime.now() + counter_obj.save() + counter_obj_creator.save() + ''' formated_time = reply_obj.created_at.strftime("%B %d, %Y, %I:%M %p") files = [] From 75329f938823f2fa810f29e14d83c2e166691c9a Mon Sep 17 00:00:00 2001 From: katkamrachana Date: Wed, 5 Jul 2017 13:35:53 +0530 Subject: [PATCH 2/6] on create, call email notification --- gnowsys-ndf/gnowsys_ndf/ndf/views/node.py | 30 ++++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/node.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/node.py index 27e6fed233..9e16e04162 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/node.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/node.py @@ -19,8 +19,10 @@ from gnowsys_ndf.ndf.models import GSystemType, Group, Node, GSystem, Buddy, Counter #, Triple from gnowsys_ndf.ndf.models import node_collection -from gnowsys_ndf.ndf.views.methods import get_execution_time, staff_required +from gnowsys_ndf.ndf.views.methods import get_execution_time, staff_required, get_group_name_id from gnowsys_ndf.ndf.views.methods import get_language_tuple, create_gattribute, create_thread_for_node +from gnowsys_ndf.ndf.views.notify import activity_notification + ''' -- common db queries -- ''' @@ -38,18 +40,26 @@ def node_create_edit(request, # check for POST method to node update operation if request.method == "POST": + object_type = "Object" + creation_flag = False # put validations if node_type not in node_collection.db.connection._registered_documents.keys(): raise ValueError('Improper node_type passed') - kwargs={} - group_name, group_id = Group.get_group_name_id(group_id) + kwargs = {} + group_obj = get_group_name_id(group_id, get_obj=True) + group_name = group_altname = group_obj.name + if group_obj.altnames: + group_altname = group_obj.altnames + group_id = group_obj._id + member_of_name, member_of_id = GSystemType.get_gst_name_id(member_of) if node_id: # existing node object node_obj = Node.get_node_by_id(node_id) else: # create new + creation_flag = True kwargs={ 'group_set': group_id, 'member_of': member_of_id @@ -63,14 +73,16 @@ def node_create_edit(request, node_obj.save(group_id=group_id) node_id = node_obj['_id'] - # Consider for Blog page creation if member_of_name == "Page": blog_page_gst_name, blog_page_gst_id = GSystemType.get_gst_name_id("Blog page") + # Consider for Blog page creation if blog_page_gst_id in node_obj.type_of: + object_type = "Note" discussion_enable_at = node_collection.one({"_type": "AttributeType", "name": "discussion_enable"}) create_gattribute(node_obj._id, discussion_enable_at, True) return_status = create_thread_for_node(request,group_id, node_obj) + # Update Counter objects active_user_ids_list = [request.user.id] if GSTUDIO_BUDDY_LOGIN: active_user_ids_list += Buddy.get_buddy_userids_list_within_datetime(request.user.id, datetime.datetime.now()) @@ -83,6 +95,16 @@ def node_create_edit(request, each_counter_obj.last_update = datetime.datetime.now() each_counter_obj.save() + if creation_flag: + object_type = member_of_name + # Send email notifications + try: + activity_notification(from_user_id=request.user.id, + group_name=group_altname, instance_type=object_type, to_user_id=group_obj.author_set) + except Exception as notification_err: + print "\n!!!Error while sending notification. ", notification_err + pass + post_req = request.POST attrs_to_create_update = [f for f in post_req.keys() if ('attribute' in f)] attrs_to_create_update = [a.split('_')[1] for a in attrs_to_create_update] From 3fb349c113a1bc45f4a4cf65faf600378c30eb0b Mon Sep 17 00:00:00 2001 From: katkamrachana Date: Wed, 5 Jul 2017 13:38:04 +0530 Subject: [PATCH 3/6] New noticeType 'activity_notification' added. --- gnowsys-ndf/gnowsys_ndf/ndf/views/notify.py | 6 +++- gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py | 31 +++++++++++++++++-- .../gnowsys_ndf/notification/models.py | 3 +- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/notify.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/notify.py index a3afd601e6..1d5ef3fd9b 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/notify.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/notify.py @@ -10,7 +10,7 @@ from gnowsys_ndf.ndf.models import node_collection, triple_collection from gnowsys_ndf.ndf.templatetags.ndf_tags import get_all_user_groups from gnowsys_ndf.ndf.views.methods import get_execution_time, get_all_subscribed_users, get_group_name_id -from gnowsys_ndf.ndf.views.tasks import task_set_notify_val +from gnowsys_ndf.ndf.views.tasks import task_set_notify_val, activity_notify_queue import json try: @@ -275,3 +275,7 @@ def invite_admins(request,group_id): except Exception as e: print "Exception in invite_admins in notify view "+str(e) return HttpResponse("Failure") + +@get_execution_time +def activity_notification(from_user_id, group_name, instance_type, to_user_id): + return activity_notify_queue(from_user_id, group_name, instance_type, to_user_id) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py index fab5edf8fe..1108b2966f 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py @@ -13,11 +13,11 @@ from django.contrib.auth.models import User from django.template.loader import render_to_string from django.contrib.sites.models import Site - +from django.utils.safestring import SafeText from gnowsys_ndf.notification import models as notification from gnowsys_ndf.ndf.models import Node from gnowsys_ndf.ndf.models import node_collection, triple_collection, filehive_collection, benchmark_collection -from gnowsys_ndf.settings import MEDIA_ROOT +from gnowsys_ndf.settings import MEDIA_ROOT, GSTUDIO_SITE_NAME, GSTUDIO_SITE_LOGO try: from bson import ObjectId @@ -61,6 +61,33 @@ def task_set_notify_val(request_user_id, group_id, msg, activ, to_user): return False +@task +def activity_notify_queue(from_user_id, group_name, instance_type, to_user): + ''' + Attach notification mail to celery task + ''' + from_user = User.objects.get(id=from_user_id) + to_user = list(User.objects.filter(pk__in=to_user)) + try: + rendered_msg = render_to_string( + "notification/activity_notification.html", + { + 'contributor': from_user.username, + 'site_logo': GSTUDIO_SITE_LOGO, + 'site_name': GSTUDIO_SITE_NAME, + 'group_name': group_name, + 'instance_type': instance_type, + # 'instance_url': instance_url, + } + ) + notification.create_notice_type("activity_notification", "Activity Notification" , "notification") + notification.send(users=to_user, extra_context=rendered_msg, label= "activity_notification") + return True + except Exception as e: + print "Error in sending notification- "+str(e) + return False + + @task def convertVideo(userid, file_id, filename): """ diff --git a/gnowsys-ndf/gnowsys_ndf/notification/models.py b/gnowsys-ndf/gnowsys_ndf/notification/models.py index 64fd5284ea..b9b00350b3 100644 --- a/gnowsys-ndf/gnowsys_ndf/notification/models.py +++ b/gnowsys-ndf/gnowsys_ndf/notification/models.py @@ -27,7 +27,6 @@ class LanguageStoreNotAvailable(Exception): def create_notice_type(label, display, description, **kwargs): NoticeType.create(label, display, description, **kwargs) - class NoticeType(models.Model): label = models.CharField(_("label"), max_length=40) @@ -55,7 +54,7 @@ def create(cls, label, display, description, default=2, verbosity=1): This is intended to be used by other apps as a post_syncdb manangement step. """ try: - notice_type = cls._default_manager.get(label=label) + notice_type = cls.objects.get(label=label) updated = False if display != notice_type.display: notice_type.display = display From c9818105ff748db0cb55994ddbccf6f0a503c49d Mon Sep 17 00:00:00 2001 From: katkamrachana Date: Wed, 5 Jul 2017 13:38:42 +0530 Subject: [PATCH 4/6] new template added for noticeType 'activity_notification'. --- .../templates/notification/activity_notification.html | 9 +++++++++ .../templates/notification/activity_notification.html | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 gnowsys-ndf/gnowsys_ndf/ndf/templates/notification/activity_notification.html create mode 100644 gnowsys-ndf/gnowsys_ndf/notification/templates/notification/activity_notification.html diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/templates/notification/activity_notification.html b/gnowsys-ndf/gnowsys_ndf/ndf/templates/notification/activity_notification.html new file mode 100644 index 0000000000..86f8246a1d --- /dev/null +++ b/gnowsys-ndf/gnowsys_ndf/ndf/templates/notification/activity_notification.html @@ -0,0 +1,9 @@ + +You have one new notification! + +Update in '{{group_name}}'. + +{{contributor}} has added a {{instance_type}}. + +Best, +{{site_name}} Team. diff --git a/gnowsys-ndf/gnowsys_ndf/notification/templates/notification/activity_notification.html b/gnowsys-ndf/gnowsys_ndf/notification/templates/notification/activity_notification.html new file mode 100644 index 0000000000..86f8246a1d --- /dev/null +++ b/gnowsys-ndf/gnowsys_ndf/notification/templates/notification/activity_notification.html @@ -0,0 +1,9 @@ + +You have one new notification! + +Update in '{{group_name}}'. + +{{contributor}} has added a {{instance_type}}. + +Best, +{{site_name}} Team. From d9d73c99cd4324205097dd4fb7c2c28f88327e87 Mon Sep 17 00:00:00 2001 From: katkamrachana Date: Wed, 5 Jul 2017 13:41:34 +0530 Subject: [PATCH 5/6] calling classmethod. --- gnowsys-ndf/gnowsys_ndf/ndf/views/node.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/node.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/node.py index 9e16e04162..9f12b6e7ed 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/node.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/node.py @@ -19,7 +19,7 @@ from gnowsys_ndf.ndf.models import GSystemType, Group, Node, GSystem, Buddy, Counter #, Triple from gnowsys_ndf.ndf.models import node_collection -from gnowsys_ndf.ndf.views.methods import get_execution_time, staff_required, get_group_name_id +from gnowsys_ndf.ndf.views.methods import get_execution_time, staff_required from gnowsys_ndf.ndf.views.methods import get_language_tuple, create_gattribute, create_thread_for_node from gnowsys_ndf.ndf.views.notify import activity_notification @@ -47,7 +47,7 @@ def node_create_edit(request, raise ValueError('Improper node_type passed') kwargs = {} - group_obj = get_group_name_id(group_id, get_obj=True) + group_obj = Group.get_group_name_id(group_id, get_obj=True) group_name = group_altname = group_obj.name if group_obj.altnames: group_altname = group_obj.altnames From d2dcd2a4495f39a23b539cb9b5f2d121b9446594 Mon Sep 17 00:00:00 2001 From: katkamrachana Date: Wed, 5 Jul 2017 13:46:13 +0530 Subject: [PATCH 6/6] removing import stmt for django safestr --- gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py index 1108b2966f..56d802258a 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/tasks.py @@ -13,7 +13,7 @@ from django.contrib.auth.models import User from django.template.loader import render_to_string from django.contrib.sites.models import Site -from django.utils.safestring import SafeText + from gnowsys_ndf.notification import models as notification from gnowsys_ndf.ndf.models import Node from gnowsys_ndf.ndf.models import node_collection, triple_collection, filehive_collection, benchmark_collection