Exemple #1
0
def edit_drawer_widget(field, group_id, node, checked=None):

	drawers = None
	drawer1 = None
	drawer2 = None

	if node :
		if field == "collection":
			if checked == "Quiz":
				checked = "QuizItem"
			elif checked == "Theme":
				checked = "Theme"
			else:
				checked = None
			drawers = get_drawers(group_id, node._id, node.collection_set, checked)
		elif field == "prior_node":
			checked = None
			drawers = get_drawers(group_id, node._id, node.prior_node, checked)
		elif field == "module":
			checked = "Module"
			drawers = get_drawers(group_id, node._id, node.collection_set, checked)
		
		drawer1 = drawers['1']
		drawer2 = drawers['2']

	else:
		if field == "collection" and checked == "Quiz":
			checked = "QuizItem"

		elif field == "collection" and checked == "Theme":
			checked = "Theme"
			
		elif field == "module":
			checked = "Module"
			
		else:
			# To make the collection work as Heterogenous one, by default
			checked = None

		drawer1 = get_drawers(group_id, None, [], checked)

	return {'template': 'ndf/drawer_widget.html', 'widget_for': field, 'drawer1': drawer1, 'drawer2': drawer2, 'group_id': group_id,'groupid': group_id}
def select_drawer(request, group_id):
    
    if request.is_ajax() and request.method == "POST":
        
        checked = request.POST.get("homo_collection", '')
        selected_collection_list = request.POST.get("collection_list", '')
        node_id = request.POST.get("node_id", '')

        gcollection = db[Node.collection_name]

        if node_id:
            node_id = ObjectId(node_id)
        else:
            node_id = None

        if selected_collection_list:
            selected_collection_list = selected_collection_list.split(",")
            collection_list_ids = []
        
            i = 0
            while (i < len(selected_collection_list)):
                cn_node_id = ObjectId(selected_collection_list[i])
                
                if gcollection.Node.one({"_id": cn_node_id}):
                    collection_list_ids.append(cn_node_id)

                i = i+1

            drawer = get_drawers(group_id, node_id, collection_list_ids, checked)
        
            drawer1 = drawer['1']
            drawer2 = drawer['2']
                                      
            return render_to_response("ndf/drawer_widget.html", 
                                      {"widget_for": "collection",
                                       "drawer1": drawer1, 
                                       "drawer2": drawer2,
                                       "groupid": group_id
                                      },
                                      context_instance=RequestContext(request)
            )
          
        else:          
            
          # For creating a resource collection   
          if node_id is None:                             
            drawer = get_drawers(group_id, node_id, [], checked)  

            return render_to_response("ndf/drawer_widget.html", 
                                       {"widget_for": "collection", 
                                        "drawer1": drawer, 
                                        "groupid": group_id
                                       }, 
                                       context_instance=RequestContext(request)
            )

          # For editing a resource collection   
          else:

            drawer = get_drawers(group_id, node_id, [], checked)  
       
            return render_to_response("ndf/drawer_widget.html", 
                                       {"widget_for": "collection", 
                                        "drawer1": drawer['1'], 
                                        "groupid": group_id
                                       }, 
                                       context_instance=RequestContext(request)
            )
def theme_topic_create_edit(request, group_id, app_id=None, app_set_id=None):

	app_collection_set = [] 
	nodes_dict = []
 	create_edit = True
 	themes_hierarchy = False
 	themes_list_items = ""
 	title = ""
 	node = ""
 	theme_topic_node = ""
 	drawers = None
	drawer = None
	nodes_list = []
	parent_nodes_collection = ""

 	
 	app = collection.Node.find_one({"_id":ObjectId(app_id)})
	if app: 
		for each in app.collection_set:
			app_set = collection.Node.find_one({"_id":each})
			app_collection_set.append({"id":str(app_set._id),"name":app_set.name}) 	


	if request.method == "POST":

 		app_GST = collection.Node.find_one({"_id":ObjectId(app_set_id)})
		if app_GST:

			create_edit = True
			themes_list_items = ""
			root_themes = []
			name = request.POST.get('name')
			collection_list = request.POST.get('collection_list','')

			# To find the root nodes to maintain the uniquness while creating and editing themes
			nodes = collection.Node.find({'member_of': {'$all': [theme_GST._id]},'group_set':{'$all': [ObjectId(group_id)]}})
			for each in nodes:
				if each.collection_set:
					for k in each.collection_set:
						nodes_list.append(k)

			nodes.rewind()
			for each in nodes:
				if each._id not in nodes_list:
					root_themes.append(each.name)


			if app_GST.name == "Theme" or app_GST.name == "Topic":
				# For creating new themes & Topics
				themes_list_items = False				
				create_edit = False
				themes_hierarchy = True
				
				if name:
					if not name.upper() in (theme_name.upper() for theme_name in root_themes):

						theme_topic_node = collection.GSystem()
						get_node_common_fields(request, theme_topic_node, group_id, app_GST)
						theme_topic_node.save()

				# This will return to Themes Hierarchy  
				if theme_GST:
					node = theme_GST
				
			else:
				# For edititng themes 
				themes_list_items = False				
				create_edit = False
				themes_hierarchy = True

				theme_topic_node = collection.Node.one({'_id': ObjectId(app_GST._id)})
				if theme_GST._id in app_GST.member_of:
					if name:
						if not name.upper() in (theme_name.upper() for theme_name in root_themes):
							get_node_common_fields(request, theme_topic_node, group_id, theme_GST)
							theme_topic_node.save() 

					# For storing and maintaning collection order
					theme_topic_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 collection.Node.one({"_id": node_id}):
							theme_topic_node.collection_set.append(node_id)

						i = i+1
					theme_topic_node.save() 
					# End of storing collection

					title = theme_GST.name
					# This will return to Themes Hierarchy  
					if theme_GST:
						node = theme_GST


				elif topic_GST._id in app_GST.member_of:
					get_node_common_fields(request, theme_topic_node, group_id, topic_GST)
					theme_topic_node.save()
					title = topic_GST.name 
					node = theme_topic_node


	else:
		app_node = None
		nodes_list = []

		app_GST = collection.Node.find_one({"_id":ObjectId(app_set_id)})
		if app_GST:
			# For adding new Theme & Topic
			if app_GST.name == "Theme" or app_GST.name == "Topic":
				title = app_GST.name
				node = ""
				root_themes = []

				# To find the root nodes to maintain the uniquness while creating new themes
				nodes = collection.Node.find({'member_of': {'$all': [app_GST._id]},'group_set':{'$all': [ObjectId(group_id)]}})
				for each in nodes:
					if each.collection_set:
						for k in each.collection_set:
							nodes_list.append(k)

				nodes.rewind()
				for each in nodes:
					if each._id not in nodes_list:
						root_themes.append(each.name)


				root_themes = json.dumps(root_themes)
				nodes_list = root_themes
				# End of finding unique root level Themes

			else:
				# For editing theme & topic
				if theme_GST._id in app_GST.member_of:
					title = theme_GST.name
					node = app_GST
					prior_theme_collection = [] 
					parent_nodes_collection = ""
					# To display the theme-topic drawer while create or edit theme
					checked = "Theme"
					drawers = get_drawers(group_id, node._id, node.collection_set, checked)
					drawer = drawers['2']

					# To find themes uniqueness within the context of its parent Theme collection, while editing theme name
					nodes = collection.Node.find({'member_of': {'$all': [theme_GST._id]},'group_set':{'$all': [ObjectId(group_id)]}})
					for each in nodes:
						if app_GST._id in each.collection_set:
							for k in each.collection_set:
								prior_theme = collection.Node.one({'_id': ObjectId(k) })
								prior_theme_collection.append(prior_theme.name)

					parent_nodes_collection = json.dumps(prior_theme_collection)	
 					# End of finding unique theme names for editing name

 					# For adding a sub-themes and maintianing their uniqueness within their context
 					for each in app_GST.collection_set:
			 			sub_theme = collection.Node.one({'_id': ObjectId(each) })
 						nodes_list.append(sub_theme.name)

 					nodes_list = json.dumps(nodes_list)
 					# End of finding unique sub themes

				elif topic_GST._id in app_GST.member_of:
					title = topic_GST.name
					node = app_GST


	return render_to_response("ndf/theme.html",
	                           {'app_collection_set':app_collection_set,
	                           	'group_id': group_id,'groupid': group_id, 'drawer': drawer,
	                           	'create_edit': create_edit, 'themes_hierarchy': themes_hierarchy,'app_id': app_id,
	                           	'nodes_list': nodes_list,'title': title,'node': node, 'parent_nodes_collection': parent_nodes_collection,
	                           	'theme_GST_id': theme_GST._id, 'topic_GST_id': topic_GST._id,
	                           	'themes_list_items': themes_list_items,'nodes':nodes_dict
	                           },context_instance = RequestContext(request)

	)
def dashboard(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
    auth = collection.Node.one({'_type': 'Author', 'name': unicode(request.user.username) })
    prof_pic = collection.Node.one({'_type': u'RelationType', 'name': u'has_profile_pic'})
    uploaded = "None"

    if request.method == "POST" :
      """
      This will take the image uploaded by user and it searches if its already available in gridfs 
      using its md5 
      """ 	
      for index, each in enumerate(request.FILES.getlist("doc[]", "")):
      	fcol = db[File.collection_name]
    	fileobj = fcol.File()
    	filemd5 = hashlib.md5(each.read()).hexdigest()
    	if fileobj.fs.files.exists({"md5":filemd5}):
    	  coll = get_database()['fs.files']
    	  a = coll.find_one({"md5":filemd5})
    	  # prof_image takes the already available document of uploaded image from its md5 
    	  prof_image = collection.Node.one({'_type': 'File', '_id': ObjectId(a['docid']) })

    	else:
    	  # If uploaded image is not found in gridfs stores this new image 
      	  submitDoc(request, group_id)
      	  # prof_image takes the already available document of uploaded image from its name
      	  prof_image = collection.Node.one({'_type': 'File', 'name': unicode(each) })

      # prof_img takes already available relation of user with its profile image
      prof_img = collection.GRelation.one({'subject': ObjectId(auth._id), 'right_subject': ObjectId(prof_image._id) })
      # If prof_img not found then it creates the relation of new uploaded image with its user
      if not prof_img:
        prof_img = collection.GRelation()
        prof_img.subject = ObjectId(auth._id) 
        prof_img.relation_type = prof_pic
        prof_img.right_subject = ObjectId(prof_image._id)
        prof_img.save()
      else:
        obj_img = collection.Node.one({'_id': ObjectId(prof_img.right_subject) })
        uploaded = obj_img.name


    ID = request.user.pk
    date_of_join = request.user.date_joined
    
    page_drawer = get_drawers(group_id,None,None,"Page")
    image_drawer = get_drawers(group_id,None,None,"Image")
    video_drawer = get_drawers(group_id,None,None,"Video")
    file_drawer = get_drawers(group_id,None,None,"File")
    quiz_drawer = get_drawers(group_id,None,None,"OnlyQuiz")
    group_drawer = get_drawers(None,None,None,"Group")
    forum_drawer = get_drawers(group_id,None,None,"Forum")
    
    obj = collection.Node.find({'_type': {'$in' : [u"GSystem", u"File"]}, 'created_by': int(ID) ,'group_set': {'$all': [ObjectId(group_id)]}})
    collab_drawer = []	
    
    for each in obj.sort('last_update', -1):  	# To populate collaborators according to their latest modification of particular resource
        for val in each.contributors:
            name = User.objects.get(pk=val).username 		
            collab_drawer.append(name)			
            

    shelves = []
    shelf_list = {}
    if auth:
      dbref_profile_pic = prof_pic.get_dbref()
      prof_pic_rel = collection_tr.Triple.find({'_type': 'GRelation', 'subject': ObjectId(auth._id), 'relation_type': dbref_profile_pic })        

      # prof_pic_rel will get the cursor object of relation of user with its profile picture 
      if prof_pic_rel.count() :
        index = prof_pic_rel.count() - 1
        Index = prof_pic_rel[index].right_subject
        # img_obj = collection.Node.one({'_type': 'File', '_id': ObjectId(prof_pic_rel['right_subject']) })      
        img_obj = collection.Node.one({'_type': 'File', '_id': ObjectId(Index) })      
      else:
        img_obj = "" 


      has_shelf_RT = collection.Node.one({'_type': 'RelationType', 'name': u'has_shelf' })
      dbref_has_shelf = has_shelf_RT.get_dbref()

      shelf = collection_tr.Triple.find({'_type': 'GRelation', 'subject': ObjectId(auth._id), 'relation_type': dbref_has_shelf })        

      if shelf:
        for each in shelf:
          shelf_name = collection.Node.one({'_id': ObjectId(each.right_subject)})           
          shelves.append(shelf_name)

          shelf_list[shelf_name.name] = []         
          for ID in shelf_name.collection_set:
            shelf_item = collection.Node.one({'_id': ObjectId(ID) })
            shelf_list[shelf_name.name].append(shelf_item.name)

      else:
        shelves = []

    return render_to_response("ndf/userDashboard.html",
                              {'username': request.user.username, 'user_id': ID, 'DOJ': date_of_join, 
                               'prof_pic_obj': img_obj,
                               'group_id':group_id,              
                               'author':auth,
                               'already_uploaded': uploaded,
                               'shelf_list': shelf_list,'shelves': shelves,
                               'page_drawer':page_drawer,'image_drawer': image_drawer,
                               'video_drawer':video_drawer,'file_drawer': file_drawer,
                               'quiz_drawer':quiz_drawer,'group_drawer': group_drawer,
                               'forum_drawer':forum_drawer,'collab_drawer': collab_drawer,
                               'groupid':group_id
                              },
                              context_instance=RequestContext(request)
    )