def create_object(name, member_of_id, prior_node_id=None, content_org=None, group_set_id=home_group_id, featured=None, language=('en', 'English')): node = node_collection.collection.GSystem() node.name = unicode(name) node.featured = featured node.language = eval(language) node.access_policy = u"PUBLIC" node.status = u"PUBLISHED" node.modified_by = nroer_team_id node.created_by = nroer_team_id node.contributors.append(nroer_team_id) node.group_set.append(group_set_id) node.member_of.append(member_of_id) if prior_node_id: node.prior_node.append(ObjectId(prior_node_id)) if content_org: node.content_org = unicode(content_org) node.content = org2html(content_org, file_prefix=ObjectId().__str__()) node.save() return node
def get_translate_common_fields(request,get_type,node, group_id, node_type, node_id): """ retrive & update the common fields required for translation of the node """ gcollection = db[Node.collection_name] usrid = int(request.user.id) content_org = request.POST.get('content_org') tags = request.POST.get('tags') name = request.POST.get('name') tags = request.POST.get('tags') usrid = int(request.user.id) language= request.POST.get('lan') if get_type == "File": get_parent_node=collection.Node.one({'_id':ObjectId(node_id)}) get_mime_type=get_parent_node.mime_type get_fs_file_ids=get_parent_node.fs_file_ids node.mime_type=get_mime_type node.fs_file_ids=get_fs_file_ids if not node.has_key('_id'): node.created_by = usrid if get_type == "File": get_node_type = collection.Node.one({'name':get_type}) node.member_of.append(get_node_type._id) if 'image' in get_mime_type: get_image_type = collection.Node.one({'name':'Image'}) node.member_of.append(get_image_type._id) if 'video' in get_mime_type: get_video_type = collection.Node.one({'name':'Video'}) node.member_of.append(get_video_type._id) else: node.member_of.append(node_type._id) node.name = unicode(name) node.language=unicode(language) node.modified_by = usrid if usrid not in node.contributors: node.contributors.append(usrid) group_obj=gcollection.Node.one({'_id':ObjectId(group_id)}) if group_obj._id not in node.group_set: node.group_set.append(group_obj._id) if tags: node.tags = [unicode(t.strip()) for t in tags.split(",") if t != ""] if tags: node.tags = [unicode(t.strip()) for t in tags.split(",") if t != ""] if content_org: node.content_org = unicode(content_org) node.name=unicode(name) # Required to link temporary files with the current user who is modifying this document usrname = request.user.username filename = slugify(name) + "-" + usrname + "-" node.content = org2html(content_org, file_prefix=filename)
def create_theme(row, descrp): # print "\n Its a theme -- ", obj # print "\n row: ", row,"\n" # To find the root themes to maintain the uniquness while creating new themes theme = row[0] theme_node = node_collection.one({'name': unicode(theme), 'group_set': group_id, 'member_of': theme_GST._id }) if not theme_node: theme_node = node_collection.collection.GSystem() theme_node.name = unicode(theme) theme_node.access_policy = u"PUBLIC" theme_node.contributors.append(nroer_team.id) theme_node.created_by = nroer_team.id theme_node.group_set.append(group_id) theme_node.language = u"en" theme_node.member_of.append(theme_GST._id) theme_node.modified_by = nroer_team.id theme_node.status = u"PUBLISHED" theme_node.save() else: print "\nTheme ", theme_node.name," already available" if len(row) > 2: theme_items_list = row[1:-1] prev_node = theme_node for each in theme_items_list: theme_item_node = node_collection.one({'name': unicode(each), 'group_set': group_id, 'member_of': theme_item_GST._id, 'prior_node': prev_node._id }) if not theme_item_node: theme_item_node = node_collection.collection.GSystem() theme_item_node.name = unicode(each) theme_item_node.access_policy = u"PUBLIC" theme_item_node.contributors.append(nroer_team.id) theme_item_node.created_by = nroer_team.id theme_item_node.group_set.append(group_id) theme_item_node.language = u"en" theme_item_node.member_of.append(theme_item_GST._id) theme_item_node.modified_by = nroer_team.id theme_item_node.prior_node.append(prev_node._id) theme_item_node.status = u"PUBLISHED" theme_item_node.save() # after saving successfully add into the collection_set of its prev node prev_node.collection_set.append(theme_item_node._id) prev_node.save() # Add this theme item as prior node for next theme item in for loop prev_node = theme_item_node else: prev_node = theme_item_node print "\n Theme Item ", theme_item_node.name," already available" topic = row[-1] topic_node = node_collection.one({'name': unicode(topic), 'group_set': group_id, 'member_of': topic_GST._id, 'prior_node': prev_node._id }) if not topic_node: topic_node = node_collection.collection.GSystem() topic_node.name = unicode(topic) topic_node.access_policy = u"PUBLIC" topic_node.contributors.append(nroer_team.id) topic_node.created_by = nroer_team.id topic_node.group_set.append(group_id) if descrp: topic_node.content_org = unicode(descrp) # Required to link temporary files with the current user who is # modifying this document usrname = nroer_team.username filename = slugify(topic) + "-" + usrname + "-" + ObjectId().__str__() topic_node.content = org2html(descrp, file_prefix=filename) topic_node.language = u"en" topic_node.member_of.append(topic_GST._id) topic_node.modified_by = nroer_team.id topic_node.prior_node.append(prev_node._id) topic_node.status = u"PUBLISHED" topic_node.save() # after saving successfully add into the collection_set of its prev node prev_node.collection_set.append(topic_node._id) prev_node.save() else: print "\n Topic ", topic_node.name," already available"
def create_edit_quiz_item(request, group_id, node_id=None): """Creates/Modifies details about the given quiz-item. """ ins_objectid = ObjectId() if ins_objectid.is_valid(group_id) is False : group_ins = collection.Node.find_one({'_type': "Group","name": group_id}) auth = collection.Node.one({'_type': 'Author', 'name': unicode(request.user.username) }) if group_ins: group_id = str(group_ins._id) else : auth = collection.Node.one({'_type': 'Author', 'name': unicode(request.user.username) }) if auth : group_id = str(auth._id) else : pass gst_quiz_item = collection.Node.one({'_type': u'GSystemType', 'name': u'QuizItem'}) context_variables = { 'title': gst_quiz_item.name, 'quiz_type_choices': QUIZ_TYPE_CHOICES, 'group_id': group_id, 'groupid': group_id } node = None quiz_node = None quiz_item_node = None if node_id: node = collection.Node.one({'_id': ObjectId(node_id)}) if gst_quiz._id in node.member_of: # Add question from a given Quiz category's context quiz_node = node quiz_item_node = collection.GSystem() else: # Edit a question quiz_item_node = node else: # Add miscellaneous question quiz_item_node = collection.GSystem() if request.method == "POST": usrid = int(request.user.id) usrname = unicode(request.user.username) if not quiz_item_node.has_key('_id'): quiz_item_node.created_by = usrid quiz_item_node.member_of.append(gst_quiz_item._id) quiz_item_node.modified_by = usrid if usrid not in quiz_item_node.contributors: quiz_item_node.contributors.append(usrid) group_object=collection.Group.one({'_id':ObjectId(group_id)}) if group_object._id not in quiz_item_node.group_set: quiz_item_node.group_set.append(group_object._id) user_group_object=collection.Group.one({'$and':[{'_type':u'Group'},{'name':usrname}]}) if user_group_object: if user_group_object._id not in quiz_item_node.group_set: quiz_item_node.group_set.append(user_group_object._id) quiz_type = request.POST.get('quiz_type_val') quiz_item_node['quiz_type'] = unicode(quiz_type) question = request.POST.get('question') quiz_item_node.content_org = unicode(question) name = "quiz-item-" + (question.split()[3] if len(question.split()) > 4 else question.split()[0]) # Extracting the third word of the question if present, otherwise first word quiz_item_node.name = name if type(name) == unicode else unicode(name) # Required to link temporary files with the current user who is modifying this document usrname = request.user.username filename = slugify(name) + "-" + usrname + "-" quiz_item_node.content = org2html(question, file_prefix=filename) # If "quiz_type" is either 'Single-Choice' or 'Multiple-Choice', then only extract options options = [] if quiz_type != QUIZ_TYPE_CHOICES[0]: no_of_options = int(request.POST.get('no_of_options')) quiz_item_node['options'] = [] i = 1 while i <= no_of_options: options.append(request.POST.get("option" if i == 1 else "option_"+str(i))) i = i + 1 quiz_item_node['options'] = options # Extracting correct-answer, depending upon 'Multiple-Choice' / 'Single-Choice' qt_initial = quiz_type[:quiz_type.find("-")].lower() quiz_item_node['correct_answer'] = [] if quiz_type == QUIZ_TYPE_CHOICES[2]: correct_answer = request.POST.getlist('correct_answer_' + qt_initial) quiz_item_node['correct_answer'] = correct_answer else: correct_answer = request.POST.get('correct_answer_' + qt_initial) quiz_item_node['correct_answer'].append(correct_answer) tags = request.POST.get('tags') if tags: quiz_item_node.tags = [unicode(t.strip()) for t in tags.split(",") if t != ""] quiz_item_node.save() if quiz_node: quiz_node.collection_set.append(quiz_item_node._id) quiz_node.save() return HttpResponseRedirect(reverse('quiz', kwargs={'group_id': group_id, 'app_id': quiz_item_node._id})) else: if node_id: context_variables['node'] = quiz_item_node context_variables['groupid'] = group_id context_variables['group_id'] = group_id return render_to_response("ndf/quiz_item_create_edit.html", context_variables, context_instance=RequestContext(request) )
def create_forum(request,group_id): ins_objectid = ObjectId() if ins_objectid.is_valid(group_id) is False : group_ins = collection.Node.find_one({'_type': "Group","name": group_id}) auth = collection.Node.one({'_type': 'Author', 'name': unicode(request.user.username) }) if group_ins: group_id = str(group_ins._id) else : auth = collection.Node.one({'_type': 'Author', 'name': unicode(request.user.username) }) if auth : group_id = str(auth._id) else : pass if request.method == "POST": colg = collection.Group.one({'_id':ObjectId(group_id)}) colf = collection.GSystem() name = unicode(request.POST.get('forum_name',"")) colf.name = name content_org = request.POST.get('content_org',"") if content_org: colf.content_org = unicode(content_org) usrname = request.user.username filename = slugify(name) + "-" + usrname + "-" colf.content = org2html(content_org, file_prefix=filename) usrid=int(request.user.id) usrname = unicode(request.user.username) colf.created_by=usrid colf.modified_by = usrid if usrid not in colf.contributors: colf.contributors.append(usrid) colf.group_set.append(colg._id) user_group_obj = collection.Group.one({'$and':[{'_type':u'Group'},{'name':usrname}]}) if user_group_obj: if user_group_obj._id not in colf.group_set: colf.group_set.append(user_group_obj._id) colf.member_of.append(forum_st._id) sdate=request.POST.get('sdate',"") shrs= request.POST.get('shrs',"") smts= request.POST.get('smts',"") edate= request.POST.get('edate',"") ehrs= request.POST.get('ehrs',"") emts=request.POST.get('emts',"") start_dt={} end_dt={} if not shrs: shrs=0 if not smts: smts=0 if sdate: sdate1=sdate.split("/") st_date = datetime.datetime(int(sdate1[2]),int(sdate1[0]),int(sdate1[1]),int(shrs),int(smts)) start_dt[start_time.name]=st_date if not ehrs: ehrs=0 if not emts: emts=0 if edate: edate1=edate.split("/") en_date= datetime.datetime(int(edate1[2]),int(edate1[0]),int(edate1[1]),int(ehrs),int(emts)) end_dt[end_time.name]=en_date # colf.attribute_set.append(start_dt) # colf.attribute_set.append(end_dt) colf.save() return HttpResponseRedirect(reverse('show', kwargs={'group_id':group_id,'forum_id': colf._id })) # variables=RequestContext(request,{'forum':colf}) # return render_to_response("ndf/forumdetails.html",variables) available_nodes = collection.Node.find({'_type': u'GSystem', 'member_of': ObjectId(forum_st._id) }) nodes_list = [] for each in available_nodes: nodes_list.append(each.name) return render_to_response("ndf/create_forum.html",{'group_id':group_id,'groupid':group_id, 'nodes_list': nodes_list},RequestContext(request))
def add_node(request,group_id): ins_objectid = ObjectId() if ins_objectid.is_valid(group_id) is False : group_ins = collection.Node.find_one({'_type': "Group","name": group_id}) auth = collection.Node.one({'_type': 'Author', 'name': unicode(request.user.username) }) if group_ins: group_id = str(group_ins._id) else : auth = collection.Node.one({'_type': 'Author', 'name': unicode(request.user.username) }) if auth : group_id = str(auth._id) else : pass try: sitename=Site.objects.all()[0].name.__str__() content_org=request.POST.get("reply","") node=request.POST.get("node","") thread=request.POST.get("thread","") forumid=request.POST.get("forumid","") sup_id=request.POST.get("supnode","") tw_name=request.POST.get("twistname","") forumobj="" groupobj="" colg = collection.Group.one({'_id':ObjectId(group_id)}) if forumid: forumobj=collection.GSystem.one({"_id": ObjectId(forumid)}) sup=collection.GSystem.one({"_id": ObjectId(sup_id)}) if not sup : return HttpResponse("failure") colrep=collection.GSystem() if node == "Twist": name=tw_name colrep.member_of.append(twist_st._id) elif node == "Reply": name=unicode("Reply of:"+str(sup._id)) colrep.member_of.append(reply_st._id) colrep.prior_node.append(sup._id) colrep.name=name if content_org: colrep.content_org = unicode(content_org) # Required to link temporary files with the current user who is modifying this document usrname = request.user.username filename = slugify(name) + "-" + usrname + "-" colrep.content = org2html(content_org, file_prefix=filename) usrid=int(request.user.id) colrep.created_by=usrid colrep.modified_by = usrid if usrid not in colrep.contributors: colrep.contributors.append(usrid) colrep.group_set.append(colg._id) colrep.save() groupname=colg.name if node == "Twist" : url="http://"+sitename+"/"+str(group_id)+"/forum/thread/"+str(colrep._id) activity=str(request.user.username)+" -added a thread '" prefix="' on the forum '"+forumobj.name+"'" nodename=name if node == "Reply": threadobj=collection.GSystem.one({"_id": ObjectId(thread)}) url="http://"+sitename+"/"+str(group_id)+"/forum/thread/"+str(threadobj._id) activity=str(request.user.username)+" -added a reply " prefix=" on the thread '"+threadobj.name+"' on the forum '"+forumobj.name+"'" nodename="" link=url for each in colg.author_set: bx=User.objects.get(id=each) msg=activity+"-"+nodename+prefix+" in the group '"+str(groupname)+"'\n"+"Please visit "+link+" to see the updated page" if bx: ret = set_notif_val(request,group_id,msg,activity,bx) bx=User.objects.get(id=colg.created_by) msg=activity+"-"+nodename+prefix+" in the group '"+str(groupname)+"' created by you"+"\n"+"Please visit "+link+" to see the updated page" if bx: ret = set_notif_val(request,group_id,msg,activity,bx) if node == "Reply": # if exstng_reply: # exstng_reply.prior_node =[] # exstng_reply.prior_node.append(colrep._id) # exstng_reply.save() threadobj=collection.GSystem.one({"_id": ObjectId(thread)}) variables=RequestContext(request,{'thread':threadobj,'user':request.user,'forum':forumobj,'groupid':group_id,'group_id':group_id}) return render_to_response("ndf/refreshtwist.html",variables) else: templ=get_template('ndf/refreshthread.html') html = templ.render(Context({'forum':forumobj,'user':request.user,'groupid':group_id,'group_id':group_id})) return HttpResponse(html) except Exception as e: return HttpResponse(""+str(e)) return HttpResponse("success")
def create_thread(request, group_id, forum_id): forum = collection.Node.one({'_id': ObjectId(forum_id)}) forum_data = { 'name':forum.name, 'content':forum.content, 'created_by':User.objects.get(id=forum.created_by).username } # print forum_data forum_threads = [] exstng_reply = collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(forum._id)}]}) exstng_reply.sort('created_at') for each in exstng_reply: forum_threads.append(each.name) if request.method == "POST": colg = collection.Group.one({'_id':ObjectId(group_id)}) name = unicode(request.POST.get('thread_name',"")) content_org = request.POST.get('content_org',"") # ------------------- colrep = collection.GSystem() colrep.member_of.append(twist_st._id) colrep.prior_node.append(forum._id) colrep.name = name if content_org: colrep.content_org = unicode(content_org) # Required to link temporary files with the current user who is modifying this document usrname = request.user.username filename = slugify(name) + "-" + usrname + "-" colrep.content = org2html(content_org, file_prefix=filename) usrid=int(request.user.id) colrep.created_by=usrid colrep.modified_by = usrid if usrid not in colrep.contributors: colrep.contributors.append(usrid) colrep.group_set.append(colg._id) colrep.save() variables = RequestContext(request, { 'forum':forum, 'thread':colrep, 'eachrep':colrep, 'groupid':group_id, 'group_id':group_id, 'user':request.user, 'forum_threads': json.dumps(forum_threads), 'forum_created_by':User.objects.get(id=forum.created_by).username }) return render_to_response("ndf/thread_details.html",variables) else: return render_to_response("ndf/create_thread.html", { 'group_id':group_id, 'groupid':group_id, 'forum': forum, 'forum_threads': json.dumps(forum_threads), 'forum_created_by':User.objects.get(id=forum.created_by).username }, RequestContext(request))
def create_theme(row, descrp): # print "\n Its a theme -- ", obj # print "\n row: ", row,"\n" # To find the root themes to maintain the uniquness while creating new themes theme = row[0] theme_node = node_collection.one({ 'name': unicode(theme), 'group_set': group_id, 'member_of': theme_GST._id }) if not theme_node: theme_node = node_collection.collection.GSystem() theme_node.name = unicode(theme) theme_node.access_policy = u"PUBLIC" theme_node.contributors.append(nroer_team.id) theme_node.created_by = nroer_team.id theme_node.group_set.append(group_id) theme_node.language = u"en" theme_node.member_of.append(theme_GST._id) theme_node.modified_by = nroer_team.id theme_node.status = u"PUBLISHED" theme_node.save() else: print "\nTheme ", theme_node.name, " already available" if len(row) > 2: theme_items_list = row[1:-1] prev_node = theme_node for each in theme_items_list: theme_item_node = node_collection.one({ 'name': unicode(each), 'group_set': group_id, 'member_of': theme_item_GST._id, 'prior_node': prev_node._id }) if not theme_item_node: theme_item_node = node_collection.collection.GSystem() theme_item_node.name = unicode(each) theme_item_node.access_policy = u"PUBLIC" theme_item_node.contributors.append(nroer_team.id) theme_item_node.created_by = nroer_team.id theme_item_node.group_set.append(group_id) theme_item_node.language = u"en" theme_item_node.member_of.append(theme_item_GST._id) theme_item_node.modified_by = nroer_team.id theme_item_node.prior_node.append(prev_node._id) theme_item_node.status = u"PUBLISHED" theme_item_node.save() # after saving successfully add into the collection_set of its prev node prev_node.collection_set.append(theme_item_node._id) prev_node.save() # Add this theme item as prior node for next theme item in for loop prev_node = theme_item_node else: prev_node = theme_item_node print "\n Theme Item ", theme_item_node.name, " already available" topic = row[-1] topic_node = node_collection.one({ 'name': unicode(topic), 'group_set': group_id, 'member_of': topic_GST._id, 'prior_node': prev_node._id }) if not topic_node: topic_node = node_collection.collection.GSystem() topic_node.name = unicode(topic) topic_node.access_policy = u"PUBLIC" topic_node.contributors.append(nroer_team.id) topic_node.created_by = nroer_team.id topic_node.group_set.append(group_id) if descrp: topic_node.content_org = unicode(descrp) # Required to link temporary files with the current user who is # modifying this document usrname = nroer_team.username filename = slugify( topic) + "-" + usrname + "-" + ObjectId().__str__() topic_node.content = org2html(descrp, file_prefix=filename) topic_node.language = u"en" topic_node.member_of.append(topic_GST._id) topic_node.modified_by = nroer_team.id topic_node.prior_node.append(prev_node._id) topic_node.status = u"PUBLISHED" topic_node.save() # after saving successfully add into the collection_set of its prev node prev_node.collection_set.append(topic_node._id) prev_node.save() else: print "\n Topic ", topic_node.name, " already available"
def get_node_common_fields(request, node, group_id, node_type): """Updates the retrieved values of common fields from request into the given node.""" gcollection = db[Node.collection_name] group_obj=gcollection.Node.one({'_id':ObjectId(group_id)}) collection = None name = request.POST.get('name') sub_theme_name = request.POST.get("sub_theme_name", '') usrid = int(request.user.id) usrname = unicode(request.user.username) access_policy = request.POST.get("login-mode", '') language= request.POST.get('lan') tags = request.POST.get('tags') prior_node_list = request.POST.get('prior_node_list','') collection_list = request.POST.get('collection_list','') module_list = request.POST.get('module_list','') content_org = request.POST.get('content_org') map_geojson_data = request.POST.get('map-geojson-data') user_last_visited_location = request.POST.get('last_visited_location') if map_geojson_data: map_geojson_data = map_geojson_data + "," map_geojson_data = list(ast.literal_eval(map_geojson_data)) else: map_geojson_data = [] # --------------------------------------------------------------------------- For create only if not node.has_key('_id'): node.created_by = usrid node.member_of.append(node_type._id) if group_obj._id not in node.group_set: node.group_set.append(group_obj._id) if access_policy == "PUBLIC": node.access_policy = unicode(access_policy) else: node.access_policy = unicode(access_policy) # End of if # --------------------------------------------------------------------------- For create/edit node.name = unicode(name) if sub_theme_name: node.name = unicode(sub_theme_name) node.status = unicode("DRAFT") if language: node.language = unicode(language) else: node.language = u"en" node.location = map_geojson_data # Storing location data if access_policy: # Policy will be changed only by the creator of the resource # via access_policy(public/private) option on the template which is visible only to the creator if access_policy == "PUBLIC": node.access_policy = u"PUBLIC" else: node.access_policy = u"PRIVATE" else: node.access_policy = u"PUBLIC" node.modified_by = usrid if usrid not in node.contributors: node.contributors.append(usrid) # For displaying nodes in home group as well as in creator group. user_group_obj=gcollection.Node.one({'$and':[{'_type':ObjectId(group_id)},{'name':usrname}]}) if group_obj._id not in node.group_set: node.group_set.append(group_obj._id) else: if user_group_obj: if user_group_obj._id not in node.group_set: node.group_set.append(user_group_obj._id) if tags: tags_list = [] for tag in tags.split(","): tag = unicode(tag.strip()) if tag: tags_list.append(tag) node.tags = tags_list # -------------------------------------------------------------------------------- prior_node node.prior_node = [] if prior_node_list != '': prior_node_list = prior_node_list.split(",") i = 0 while (i < len(prior_node_list)): node_id = ObjectId(prior_node_list[i]) if gcollection.Node.one({"_id": node_id}): node.prior_node.append(node_id) i = i+1 # -------------------------------------------------------------------------------- collection node.collection_set = [] if collection_list != '': collection_list = collection_list.split(",") i = 0 while (i < len(collection_list)): node_id = ObjectId(collection_list[i]) if gcollection.Node.one({"_id": node_id}): node.collection_set.append(node_id) i = i+1 # -------------------------------------------------------------------------------- Module node.collection_set = [] if module_list != '': collection_list = module_list.split(",") i = 0 while (i < len(collection_list)): node_id = ObjectId(collection_list[i]) if gcollection.Node.one({"_id": node_id}): node.collection_set.append(node_id) i = i+1 # ------------------------------------------------------------------------------- org-content if content_org: node.content_org = unicode(content_org) # Required to link temporary files with the current user who is modifying this document usrname = request.user.username filename = slugify(name) + "-" + usrname + "-" node.content = org2html(content_org, file_prefix=filename) # ----------------------------------------------------------------------------- visited_location in author class if user_last_visited_location: user_last_visited_location = list(ast.literal_eval(user_last_visited_location)) author = gcollection.Node.one({'_type': "GSystemType", 'name': "Author"}) user_group_location = gcollection.Node.one({'_type': "Author", 'member_of': author._id, 'created_by': usrid, 'name': usrname}) if user_group_location: if node._type == "Author" and user_group_location._id == node._id: node['visited_location'] = user_last_visited_location else: user_group_location['visited_location'] = user_last_visited_location user_group_location.save()
def save_file(files,title, userid, group_id, content_org, tags, img_type = None, language = None, usrname = None, access_policy=None): """ this will create file object and save files in gridfs collection """ global count,first_object fcol = db[File.collection_name] fileobj = fcol.File() filemd5 = hashlib.md5(files.read()).hexdigest() files.seek(0) size, unit = getFileSize(files) size = {'size':round(size, 2), 'unit':unicode(unit)} if fileobj.fs.files.exists({"md5":filemd5}): return files.name #return already exist file else: try: files.seek(0) filetype = magic.from_buffer(files.read(100000), mime = 'true') #Gusing filetype by python-magic filetype1 = mimetypes.guess_type(files.name)[0] if filetype1: filetype1 = filetype1 else : filetype1 = "" filename = files.name fileobj.name = unicode(title) if language: fileobj.language= unicode(language) fileobj.created_by = int(userid) fileobj.modified_by = int(userid) if int(userid) not in fileobj.contributors: fileobj.contributors.append(int(userid)) if access_policy: fileobj.access_policy = unicode(access_policy) # For giving privacy to file objects fileobj.file_size = size group_object=collection.Group.one({'_id':ObjectId(group_id)}) if group_object._id not in fileobj.group_set: fileobj.group_set.append(group_object._id) #group id stored in group_set field if usrname: user_group_object=collection.Node.one({'$and':[{'_type':u'Author'},{'name':usrname}]}) if user_group_object: if user_group_object._id not in fileobj.group_set: # File creator_group_id stored in group_set field fileobj.group_set.append(user_group_object._id) fileobj.member_of.append(GST_FILE._id) fileobj.mime_type = filetype if img_type == None: if content_org: fileobj.content_org = unicode(content_org) # Required to link temporary files with the current user who is modifying this document filename_content = slugify(title) + "-" + usrname + "-" fileobj.content = org2html(content_org, file_prefix = filename_content) fileobj.tags = [unicode(t.strip()) for t in tags.split(",") if t != ""] fileobj.save() files.seek(0) #moving files cursor to start objectid = fileobj.fs.files.put(files.read(), filename=filename, content_type=filetype) #store files into gridfs collection.File.find_and_modify({'_id':fileobj._id},{'$push':{'fs_file_ids':objectid}}) # For making collection if uploaded file more than one if count == 0: first_object = fileobj else: collection.File.find_and_modify({'_id':first_object._id},{'$push':{'collection_set':fileobj._id}}) """ code for converting video into webm and converted video assigning to varible files """ if 'video' in filetype or 'video' in filetype1 or filename.endswith('.webm') == True: collection.File.find_and_modify({'_id':fileobj._id},{'$push':{'member_of':GST_VIDEO._id}}) collection.File.find_and_modify({'_id':fileobj._id},{'$set':{'mime_type':'video'}}) webmfiles, filetype, thumbnailvideo = convertVideo(files, userid, fileobj._id) '''storing thumbnail of video with duration in saved object''' tobjectid = fileobj.fs.files.put(thumbnailvideo.read(), filename=filename+"-thumbnail", content_type="thumbnail-image") collection.File.find_and_modify({'_id':fileobj._id},{'$push':{'fs_file_ids':tobjectid}}) if filename.endswith('.webm') == False: tobjectid = fileobj.fs.files.put(webmfiles.read(), filename=filename+".webm", content_type=filetype) # saving webm video id into file object collection.File.find_and_modify({'_id':fileobj._id},{'$push':{'fs_file_ids':tobjectid}}) '''storing thumbnail of pdf and svg files in saved object''' if 'pdf' in filetype or 'svg' in filetype: thumbnail_pdf = convert_pdf_thumbnail(files,fileobj._id) tobjectid = fileobj.fs.files.put(thumbnail_pdf.read(), filename=filename+"-thumbnail", content_type=filetype) collection.File.find_and_modify({'_id':fileobj._id},{'$push':{'fs_file_ids':tobjectid}}) '''storing thumbnail of image in saved object''' if 'image' in filetype: collection.File.find_and_modify({'_id':fileobj._id},{'$push':{'member_of':GST_IMAGE._id}}) thumbnailimg = convert_image_thumbnail(files) tobjectid = fileobj.fs.files.put(thumbnailimg, filename=filename+"-thumbnail", content_type=filetype) collection.File.find_and_modify({'_id':fileobj._id},{'$push':{'fs_file_ids':tobjectid}}) files.seek(0) mid_size_img = convert_mid_size_image(files) if mid_size_img: mid_img_id = fileobj.fs.files.put(mid_size_img, filename=filename+"-mid_size_img", content_type=filetype) collection.File.find_and_modify({'_id':fileobj._id},{'$push':{'fs_file_ids':mid_img_id}}) count = count + 1 return fileobj._id except Exception as e: print "Some Exception:", files.name, "Execption:", e