Exemplo n.º 1
0
def discussion_reply(request, group_id, node_id):
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

    try:
        group_object = node_collection.one({'_id': ObjectId(group_id)})
        prior_node = request.POST.get("prior_node_id", "")
        content_org = request.POST.get("reply_text_content", "") # reply content
        node = node_collection.one({"_id": ObjectId(node_id)})
        gs_type_node_id = None

        if u'Twist' not in node.member_of_names_list:
            grel_dict = get_relation_value(node_id,'thread_of', True)
            node = grel_dict['grel_node']
        if node.prior_node:
            gs_type_node_id = node.prior_node[0]

        # if node and node.relation_set:
        #     for each_rel in node.relation_set:
        #         if each_rel and "thread_of" in each_rel:
        #             gs_type_node_id = each_rel['thread_of'][0]
        #             break

        # grel_dict = get_relation_value(node_id,'thread_of')
        # is_cursor = grel_dict.get("cursor",False)
        # if not is_cursor:
        #     gs_type_node_id = grel_dict.get("grel_node")
        #     # grel_id = grel_dict.get("grel_id")

        # print "\n\n node.name === ", node.member_of_names_list, node._id, node.name

        # process and save node if it reply has content
        if content_org:

            user_id = int(request.user.id)
            user_name = unicode(request.user.username)

            # auth = node_collection.one({'_type': 'Author', 'name': user_name })

            # creating empty GST and saving it
            reply_obj = node_collection.collection.GSystem()

            reply_obj.name = unicode("Reply of:" + str(prior_node))
            reply_obj.status = u"PUBLISHED"

            reply_obj.created_by = user_id
            reply_obj.modified_by = user_id
            reply_obj.contributors.append(user_id)

            reply_obj.member_of.append(ObjectId(reply_st._id))
            reply_obj.prior_node.append(ObjectId(prior_node))
            reply_obj.group_set.append(ObjectId(group_id))

            reply_obj.content_org = unicode(content_org)
            filename = slugify(unicode("Reply of:" + str(prior_node))) + "-" + user_name + "-"
            # reply_obj.content = org2html(content_org, file_prefix=filename)
            reply_obj.content = content_org
            if gs_type_node_id:
                reply_obj.origin.append({'prior_node_id_of_thread': ObjectId(gs_type_node_id)})
            if node_id:
                reply_obj.origin.append({'thread_id': ObjectId(node_id)})

            # ==============================
            # try:

            upload_files_count=int(request.POST.get("upload_cnt",0))
            # print "upfiles=",upload_files_count
            lst=[]
            lstobj_collection=[]
            usrid = int(request.user.id)
            if upload_files_count > 0:
                # print "uploaded items",request.FILES.items()
                try:
                    thread_obj = node_collection.one({'_id': ObjectId(prior_node)})
                    # print "thread_obj : ", thread_obj
                    if thread_obj.access_policy:
                        access_policy = thread_obj.access_policy
                    else:
                        access_policy = u'PUBLIC'

                except:
                    access_policy = u'PUBLIC'

                for key,value in request.FILES.items():
                    fname=unicode(value.__dict__['_name'])
                    # print "key=",key,"value=",value,"fname=",fname

                    fileobj,fs=save_file(value,fname,usrid,group_id, "", "", username=unicode(request.user.username), access_policy=access_policy, count=0, first_object="", oid=True)


                    if type(fileobj) == list:
                        obid = str(list(fileobj)[1])
                    else:
                        obid=str(fileobj)

                    try:
                        file_obj=node_collection.find_one({'_id': ObjectId(obid)})
                        lstobj_collection.append(file_obj._id)
                    except:
                        pass
                    if "CourseEventGroup" not in group_object.member_of_names_list:
                        if group_object.edit_policy == 'EDITABLE_MODERATED':
                            t = create_moderator_task(request, file_obj.group_set[0], file_obj._id,on_upload=True)
                # print "::: lstobj_collection: ", lstobj_collection
            # except:
                # lstobj_collection = []
            # ==============================
            reply_obj.collection_set = lstobj_collection
            # print "=== lstobj_collection: ", lstobj_collection

            # saving the reply obj
            reply_obj.save()

            #Update Counter Collection
            # thread_obj = node_collection.one({'_id':ObjectId(node_id)})
            if gs_type_node_id:
                gs_type_node = node_collection.one({'_id':gs_type_node_id})
                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.now())
                    # removing redundancy of user ids:
                    active_user_ids_list = dict.fromkeys(active_user_ids_list).keys()
                Counter.add_comment_pt(resource_obj_or_id=gs_type_node,
                                       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()

            formated_time = reply_obj.created_at.strftime("%B %d, %Y, %I:%M %p")

            files = []
            for each_coll_item in reply_obj.collection_set:
                temp_list = []
                temp = node_collection.one({'_id': ObjectId(each_coll_item)}, {'mime_type': 1, 'name': 1})
                temp_list.append(str(temp['_id']))
                temp_list.append(str(temp['mime_type']))
                temp_list.append(str(temp['name']))

                files.append(temp_list)

            # print files
            user_names = reply_obj.user_details_dict["contributors"]
            is_grp_admin = False
            if request.user.id in group_object.group_admin:
                is_grp_admin = True
            # ["status_info", "reply_id", "prior_node", "html_content", "org_content", "user_id", "user_name", "created_at" ]
            reply = json.dumps( [ "reply_saved", str(reply_obj._id), str(reply_obj.prior_node[0]), reply_obj.content, reply_obj.content_org, user_id, user_names, formated_time, files,is_grp_admin], cls=DjangoJSONEncoder )

            # print "===========", reply

            # ---------- mail/notification sending -------
            try:
                node_creator_user_obj = User.objects.get(id=node.created_by)
                node_creator_user_name = node_creator_user_obj.username
                if int(request.user.id) not in node.author_set:
                    node.author_set.append(int(request.user.id))
                    node.save()
                site = Site.objects.get(pk=1)
                site = site.name.__str__()

                from_user = user_name

                to_user_list = [node_creator_user_obj]

                msg = "\n\nDear " + node_creator_user_name + ",\n\n" + \
                      "A reply has been added in discussion under the " + \
                      node.member_of_names_list[0] + " named: '" + \
                      node.name + "' by '" + user_name + "'."

                activity = "Discussion Reply"
                render_label = render_to_string(
                    "notification/label.html",
                    {
                        # "sender": from_user,
                        "activity": activity,
                        "conjunction": "-",
                        "link": "url_link"
                    }
                )
                notification.create_notice_type(render_label, msg, "notification")
                notification.send(to_user_list, render_label, {"from_user": from_user})
            except Exception as notification_err:
                print "\n Unable to send notification", notification_err
            # ---------- END of mail/notification sending ---------
            return HttpResponse( reply )

        else: # no reply content

            return HttpResponse(json.dumps(["no_content"]))

    except Exception as e:

        error_message = "\n DiscussionReplyCreateError: " + str(e) + "\n"
        raise Exception(error_message)

        return HttpResponse(json.dumps(["Server Error"]))
Exemplo n.º 2
0
def discussion_reply(request, group_id, node_id):

    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

    try:
        group_object = node_collection.one({'_id': ObjectId(group_id)})
        prior_node = request.POST.get("prior_node_id", "")
        content_org = request.POST.get("reply_text_content",
                                       "")  # reply content
        node = node_collection.one({"_id": ObjectId(node_id)})
        # gs_type_node_id = get_relation_value(node_id,'thread_of')
        gs_type_node_id = None
        if node and node.relation_set:
            for each_rel in node.relation_set:
                if each_rel and "thread_of" in each_rel:
                    gs_type_node_id = each_rel['thread_of'][0]
                    break
        # grel_dict = get_relation_value(node_id,'thread_of')
        # is_cursor = grel_dict.get("cursor",False)
        # if not is_cursor:
        #     gs_type_node_id = grel_dict.get("grel_node")
        #     # grel_id = grel_dict.get("grel_id")

        # print "\n\n node.name === ", node.member_of_names_list, node._id, node.name

        # process and save node if it reply has content
        if content_org:

            user_id = int(request.user.id)
            user_name = unicode(request.user.username)

            # auth = node_collection.one({'_type': 'Author', 'name': user_name })

            # creating empty GST and saving it
            reply_obj = node_collection.collection.GSystem()

            reply_obj.name = unicode("Reply of:" + str(prior_node))
            reply_obj.status = u"PUBLISHED"

            reply_obj.created_by = user_id
            reply_obj.modified_by = user_id
            reply_obj.contributors.append(user_id)

            reply_obj.member_of.append(ObjectId(reply_st._id))
            reply_obj.prior_node.append(ObjectId(prior_node))
            reply_obj.group_set.append(ObjectId(group_id))

            reply_obj.content_org = unicode(content_org)
            filename = slugify(
                unicode("Reply of:" + str(prior_node))) + "-" + user_name + "-"
            # reply_obj.content = org2html(content_org, file_prefix=filename)
            reply_obj.content = content_org
            if gs_type_node_id:
                reply_obj.origin.append(
                    {'prior_node_id_of_thread': ObjectId(gs_type_node_id)})
            if node_id:
                reply_obj.origin.append({'thread_id': ObjectId(node_id)})

            # ==============================
            # try:

            upload_files_count = int(request.POST.get("upload_cnt", 0))
            # print "upfiles=",upload_files_count
            lst = []
            lstobj_collection = []
            usrid = int(request.user.id)
            if upload_files_count > 0:
                # print "uploaded items",request.FILES.items()
                try:
                    thread_obj = node_collection.one(
                        {'_id': ObjectId(prior_node)})
                    # print "thread_obj : ", thread_obj
                    if thread_obj.access_policy:
                        access_policy = thread_obj.access_policy
                    else:
                        access_policy = u'PUBLIC'

                except:
                    access_policy = u'PUBLIC'

                for key, value in request.FILES.items():
                    fname = unicode(value.__dict__['_name'])
                    # print "key=",key,"value=",value,"fname=",fname

                    fileobj, fs = save_file(value,
                                            fname,
                                            usrid,
                                            group_id,
                                            "",
                                            "",
                                            username=unicode(
                                                request.user.username),
                                            access_policy=access_policy,
                                            count=0,
                                            first_object="",
                                            oid=True)

                    if type(fileobj) == list:
                        obid = str(list(fileobj)[1])
                    else:
                        obid = str(fileobj)

                    try:
                        file_obj = node_collection.find_one(
                            {'_id': ObjectId(obid)})
                        lstobj_collection.append(file_obj._id)
                    except:
                        pass
                    if "CourseEventGroup" not in group_object.member_of_names_list:
                        if group_object.edit_policy == 'EDITABLE_MODERATED':
                            t = create_moderator_task(request,
                                                      file_obj.group_set[0],
                                                      file_obj._id,
                                                      on_upload=True)
                # print "::: lstobj_collection: ", lstobj_collection
            # except:
            # lstobj_collection = []
            # ==============================
            reply_obj.collection_set = lstobj_collection
            # print "=== lstobj_collection: ", lstobj_collection

            # saving the reply obj
            reply_obj.save()
            formated_time = reply_obj.created_at.strftime(
                "%B %d, %Y, %I:%M %p")

            files = []
            for each_coll_item in reply_obj.collection_set:
                temp_list = []
                temp = node_collection.one({'_id': ObjectId(each_coll_item)}, {
                    'mime_type': 1,
                    'name': 1
                })
                temp_list.append(str(temp['_id']))
                temp_list.append(str(temp['mime_type']))
                temp_list.append(str(temp['name']))

                files.append(temp_list)

            # print files

            # ["status_info", "reply_id", "prior_node", "html_content", "org_content", "user_id", "user_name", "created_at" ]
            reply = json.dumps([
                "reply_saved",
                str(reply_obj._id),
                str(reply_obj.prior_node[0]), reply_obj.content,
                reply_obj.content_org, user_id, user_name, formated_time, files
            ],
                               cls=DjangoJSONEncoder)

            # print "===========", reply

            # ---------- mail/notification sending -------
            try:
                node_creator_user_obj = User.objects.get(id=node.created_by)
                node_creator_user_name = node_creator_user_obj.username
                if int(request.user.id) not in node.author_set:
                    node.author_set.append(int(request.user.id))
                    node.save()
                site = Site.objects.get(pk=1)
                site = site.name.__str__()

                from_user = user_name

                to_user_list = [node_creator_user_obj]

                msg = "\n\nDear " + node_creator_user_name + ",\n\n" + \
                      "A reply has been added in discussion under the " + \
                      node.member_of_names_list[0] + " named: '" + \
                      node.name + "' by '" + user_name + "'."

                activity = "Discussion Reply"
                render_label = render_to_string(
                    "notification/label.html",
                    {
                        # "sender": from_user,
                        "activity": activity,
                        "conjunction": "-",
                        "link": "url_link"
                    })
                notification.create_notice_type(render_label, msg,
                                                "notification")
                notification.send(to_user_list, render_label,
                                  {"from_user": from_user})
            except Exception as notification_err:
                print "\n Unable to send notification", notification_err
            # ---------- END of mail/notification sending ---------
            return HttpResponse(reply)

        else:  # no reply content

            return HttpResponse(json.dumps(["no_content"]))

    except Exception as e:

        error_message = "\n DiscussionReplyCreateError: " + str(e) + "\n"
        raise Exception(error_message)

        return HttpResponse(json.dumps(["Server Error"]))
Exemplo n.º 3
0
def discussion_reply(request, group_id, node_id):
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

    try:
        group_object = node_collection.one({'_id': ObjectId(group_id)})
        prior_node = request.POST.get("prior_node_id", "")
        content_org = request.POST.get("reply_text_content", "") # reply content
        node = node_collection.one({"_id": ObjectId(node_id)})
        gs_type_node_id = None

        if u'Twist' not in node.member_of_names_list:
            # grel_dict = get_relation_value(node_id,'thread_of', True)
            grel_dict = get_relation_value(node._id,'has_thread', True)
            node = grel_dict['grel_node']

        if node.prior_node:
            gs_type_node_id = node.prior_node[0]
        else:
            try:
                has_thread_at = node_collection.one({'_type': 'RelationType', 'name': 'has_thread'})
                has_thread_grel = triple_collection.find_one({'_type': 'GRelation',
                    'relation_type': has_thread_at._id, 'right_subject': node._id,
                    'status': 'PUBLISHED'})
                gs_type_node_id = has_thread_grel.subject
            except Exception as e:
                print "\n Node asssociated with Thread node NOT found.", e
                pass

        # process and save node if it reply has content
        if content_org:

            user_id = int(request.user.id)
            user_name = unicode(request.user.username)
            # auth = node_collection.one({'_type': 'Author', 'name': user_name })

            # creating empty GST and saving it
            reply_obj = node_collection.collection.GSystem()

            reply_obj.name = unicode("Reply of:" + str(prior_node))
            reply_obj.status = u"PUBLISHED"

            reply_obj.created_by = user_id
            reply_obj.modified_by = user_id
            reply_obj.contributors.append(user_id)

            reply_obj.member_of.append(ObjectId(reply_st._id))
            reply_obj.prior_node.append(ObjectId(prior_node))
            reply_obj.group_set.append(ObjectId(group_id))

            reply_obj.content_org = unicode(content_org)
            filename = slugify(unicode("Reply of:" + str(prior_node))) + "-" + user_name + "-"
            # reply_obj.content = org2html(content_org, file_prefix=filename)
            reply_obj.content = content_org
            if gs_type_node_id:
                reply_obj.origin.append({'prior_node_id_of_thread': ObjectId(gs_type_node_id)})
            if node_id:
                reply_obj.origin.append({'thread_id': ObjectId(node_id)})

            # ==============================
            # try:

            upload_files_count=int(request.POST.get("upload_cnt",0))
            # print "upfiles=",upload_files_count
            lst=[]
            lstobj_collection=[]
            usrid = int(request.user.id)
            if upload_files_count > 0:
                # print "uploaded items",request.FILES.items()
                try:
                    thread_obj = node_collection.one({'_id': ObjectId(prior_node)})
                    # print "thread_obj : ", thread_obj
                    if thread_obj.access_policy:
                        access_policy = thread_obj.access_policy
                    else:
                        access_policy = u'PUBLIC'

                except:
                    access_policy = u'PUBLIC'

                for key,value in request.FILES.items():
                    fname=unicode(value.__dict__['_name'])
                    # print "key=",key,"value=",value,"fname=",fname

                    fileobj,fs=save_file(value,fname,usrid,group_id, "", "", username=unicode(request.user.username), access_policy=access_policy, count=0, first_object="", oid=True)


                    if type(fileobj) == list:
                        obid = str(list(fileobj)[1])
                    else:
                        obid=str(fileobj)

                    try:
                        file_obj=node_collection.find_one({'_id': ObjectId(obid)})
                        lstobj_collection.append(file_obj._id)
                    except:
                        pass
                    if "CourseEventGroup" not in group_object.member_of_names_list:
                        if group_object.edit_policy == 'EDITABLE_MODERATED':
                            t = create_moderator_task(request, file_obj.group_set[0], file_obj._id,on_upload=True)
                # print "::: lstobj_collection: ", lstobj_collection
            # except:
                # lstobj_collection = []
            # ==============================
            reply_obj.collection_set = lstobj_collection
            # print "=== lstobj_collection: ", lstobj_collection

            # saving the reply obj
            reply_obj.save()

            #Update Counter Collection
            # thread_obj = node_collection.one({'_id':ObjectId(node_id)})
            if gs_type_node_id:
                gs_type_node = node_collection.one({'_id':gs_type_node_id})
                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.now())
                    # removing redundancy of user ids:
                    active_user_ids_list = dict.fromkeys(active_user_ids_list).keys()
                Counter.add_comment_pt(resource_obj_or_id=gs_type_node,
                                       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()

            formated_time = reply_obj.created_at.strftime("%B %d, %Y, %I:%M %p")

            files = []
            for each_coll_item in reply_obj.collection_set:
                temp_list = []
                temp = node_collection.one({'_id': ObjectId(each_coll_item)}, {'mime_type': 1, 'name': 1})
                temp_list.append(str(temp['_id']))
                temp_list.append(str(temp['mime_type']))
                temp_list.append(str(temp['name']))

                files.append(temp_list)

            # print files
            user_names = reply_obj.user_details_dict["contributors"]
            is_grp_admin = False
            if request.user.id in group_object.group_admin:
                is_grp_admin = True
            # ["status_info", "reply_id", "prior_node", "html_content", "org_content", "user_id", "user_name", "created_at" ]
            reply = json.dumps( [ "reply_saved", str(reply_obj._id), str(reply_obj.prior_node[0]), reply_obj.content, reply_obj.content_org, user_id, user_names, formated_time, files,is_grp_admin], cls=DjangoJSONEncoder )

            # print "===========", reply

            # ---------- mail/notification sending -------
            try:
                node_creator_user_obj = User.objects.get(id=node.created_by)
                node_creator_user_name = node_creator_user_obj.username
                if int(request.user.id) not in node.author_set:
                    node.author_set.append(int(request.user.id))
                    node.save()
                site = Site.objects.get(pk=1)
                site = site.name.__str__()

                from_user = user_name

                to_user_list = [node_creator_user_obj]

                msg = "\n\nDear " + node_creator_user_name + ",\n\n" + \
                      "A reply has been added in discussion under the " + \
                      node.member_of_names_list[0] + " named: '" + \
                      node.name + "' by '" + user_name + "'."

                activity = "Discussion Reply"
                render_label = render_to_string(
                    "notification/label.html",
                    {
                        # "sender": from_user,
                        "activity": activity,
                        "conjunction": "-",
                        "link": "url_link"
                    }
                )
                notification.create_notice_type(render_label, msg, "notification")
                notification.send(to_user_list, render_label, {"from_user": from_user})
            except Exception as notification_err:
                print "\n Unable to send notification", notification_err
            # ---------- END of mail/notification sending ---------
            return HttpResponse( reply )

        else: # no reply content

            return HttpResponse(json.dumps(["no_content"]))

    except Exception as e:

        error_message = "\n DiscussionReplyCreateError: " + str(e) + "\n"
        raise Exception(error_message)

        return HttpResponse(json.dumps(["Server Error"]))
Exemplo n.º 4
0
def discussion_reply(request, group_id, node_id):

    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

    try:
        group_object = node_collection.one({'_id': ObjectId(group_id)})
        prior_node = request.POST.get("prior_node_id", "")
        content_org = request.POST.get("reply_text_content", "") # reply content
        node = node_collection.one({"_id": ObjectId(node_id)})
        # gs_type_node_id = get_relation_value(node_id,'thread_of')
        gs_type_node_id = None
        if node and node.relation_set:
            for each_rel in node.relation_set:
                if each_rel and "thread_of" in each_rel:
                    gs_type_node_id = each_rel['thread_of'][0]
                    break
        # grel_dict = get_relation_value(node_id,'thread_of')
        # is_cursor = grel_dict.get("cursor",False)
        # if not is_cursor:
        #     gs_type_node_id = grel_dict.get("grel_node")
        #     # grel_id = grel_dict.get("grel_id")

        # print "\n\n node.name === ", node.member_of_names_list, node._id, node.name

        # process and save node if it reply has content  
        if content_org:
      
            user_id = int(request.user.id)
            user_name = unicode(request.user.username)

            # auth = node_collection.one({'_type': 'Author', 'name': user_name })
            
            # creating empty GST and saving it
            reply_obj = node_collection.collection.GSystem()

            reply_obj.name = unicode("Reply of:" + str(prior_node))
            reply_obj.status = u"PUBLISHED"

            reply_obj.created_by = user_id
            reply_obj.modified_by = user_id
            reply_obj.contributors.append(user_id)

            reply_obj.member_of.append(ObjectId(reply_st._id))
            reply_obj.prior_node.append(ObjectId(prior_node))
            reply_obj.group_set.append(ObjectId(group_id))
        
            reply_obj.content_org = unicode(content_org)
            filename = slugify(unicode("Reply of:" + str(prior_node))) + "-" + user_name + "-"
            # reply_obj.content = org2html(content_org, file_prefix=filename)
            reply_obj.content = content_org
            if gs_type_node_id:
                reply_obj.origin.append({'prior_node_id_of_thread': ObjectId(gs_type_node_id)})
            if node_id:
                reply_obj.origin.append({'thread_id': ObjectId(node_id)})

            # ==============================
            # try:

            upload_files_count=int(request.POST.get("upload_cnt",0))
            # print "upfiles=",upload_files_count
            lst=[]
            lstobj_collection=[]
            usrid = int(request.user.id)
            if upload_files_count > 0:
                # print "uploaded items",request.FILES.items()
                try:
                    thread_obj = node_collection.one({'_id': ObjectId(prior_node)})
                    # print "thread_obj : ", thread_obj
                    if thread_obj.access_policy:
                        access_policy = thread_obj.access_policy
                    else:
                        access_policy = u'PUBLIC'
                        
                except:
                    access_policy = u'PUBLIC'

                for key,value in request.FILES.items():
                    fname=unicode(value.__dict__['_name'])
                    # print "key=",key,"value=",value,"fname=",fname
                    
                    fileobj,fs=save_file(value,fname,usrid,group_id, "", "", username=unicode(request.user.username), access_policy=access_policy, count=0, first_object="", oid=True)


                    if type(fileobj) == list:
                        obid = str(list(fileobj)[1])
                    else:
                        obid=str(fileobj)

                    try:
                        file_obj=node_collection.find_one({'_id': ObjectId(obid)})
                        lstobj_collection.append(file_obj._id) 
                    except:
                        pass
                    if "CourseEventGroup" not in group_object.member_of_names_list:
                        if group_object.edit_policy == 'EDITABLE_MODERATED':
                            t = create_moderator_task(request, file_obj.group_set[0], file_obj._id,on_upload=True)
                # print "::: lstobj_collection: ", lstobj_collection
            # except:
                # lstobj_collection = []
            # ==============================
            reply_obj.collection_set = lstobj_collection
            # print "=== lstobj_collection: ", lstobj_collection
        
            # saving the reply obj
            reply_obj.save()
            formated_time = reply_obj.created_at.strftime("%B %d, %Y, %I:%M %p")

            files = []
            for each_coll_item in reply_obj.collection_set:
                temp_list = []
                temp = node_collection.one({'_id': ObjectId(each_coll_item)}, {'mime_type': 1, 'name': 1})
                temp_list.append(str(temp['_id']))
                temp_list.append(str(temp['mime_type']))
                temp_list.append(str(temp['name']))
                
                files.append(temp_list)

            # print files

            # ["status_info", "reply_id", "prior_node", "html_content", "org_content", "user_id", "user_name", "created_at" ]
            reply = json.dumps( [ "reply_saved", str(reply_obj._id), str(reply_obj.prior_node[0]), reply_obj.content, reply_obj.content_org, user_id, user_name, formated_time, files], cls=DjangoJSONEncoder )

            # print "===========", reply

            # ---------- mail/notification sending -------
            try:
                node_creator_user_obj = User.objects.get(id=node.created_by)
                node_creator_user_name = node_creator_user_obj.username
                if int(request.user.id) not in node.author_set:
                    node.author_set.append(int(request.user.id))
                    node.save()
                site = Site.objects.get(pk=1)
                site = site.name.__str__()
                
                from_user = user_name

                to_user_list = [node_creator_user_obj]

                msg = "\n\nDear " + node_creator_user_name + ",\n\n" + \
                      "A reply has been added in discussion under the " + \
                      node.member_of_names_list[0] + " named: '" + \
                      node.name + "' by '" + user_name + "'."

                activity = "Discussion Reply"
                render_label = render_to_string(
                    "notification/label.html",
                    {
                        # "sender": from_user,
                        "activity": activity,
                        "conjunction": "-",
                        "link": "url_link"
                    }
                )
                notification.create_notice_type(render_label, msg, "notification")
                notification.send(to_user_list, render_label, {"from_user": from_user})
            except Exception as notification_err:
                print "\n Unable to send notification", notification_err
            # ---------- END of mail/notification sending ---------
            return HttpResponse( reply )

        else: # no reply content

            return HttpResponse(json.dumps(["no_content"]))      

    except Exception as e:
      
        error_message = "\n DiscussionReplyCreateError: " + str(e) + "\n"
        raise Exception(error_message)

        return HttpResponse(json.dumps(["Server Error"]))