trash.py 4.91 KB
Newer Older
Rachana's avatar
Rachana committed
1
from django.http import HttpResponse, HttpResponseRedirect
makfire's avatar
makfire committed
2 3 4
from django.shortcuts import render_to_response  # , render
from django.template import RequestContext
from gnowsys_ndf.ndf.models import *
Rachana's avatar
Rachana committed
5 6
# from gnowsys_ndf.ndf.views.page import page
# from gnowsys_ndf.ndf.views.file import file
makfire's avatar
makfire committed
7
from gnowsys_ndf.ndf.views.group import group_dashboard
makfire's avatar
makfire committed
8
from gnowsys_ndf.ndf.views.methods import *
Rachana's avatar
Rachana committed
9 10
from django.core.urlresolvers import reverse
from gnowsys_ndf.settings import GSTUDIO_SITE_NAME
makfire's avatar
makfire committed
11

12
@get_execution_time
makfire's avatar
makfire committed
13
def trash_resource(request,group_id,node_id):
Rachana's avatar
Rachana committed
14 15 16 17 18 19
	'''
	Delete Action.
	This method removes the group_id from the node's group_set.
	Iff node's group_set is empty, send to Trash group. 
	'''

saurabh's avatar
saurabh committed
20 21

	auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
Rachana's avatar
Rachana committed
22
	node_obj = node_collection.find_one({"_id":ObjectId(node_id)})
23
	group_obj = node_collection.find_one({"_id":ObjectId(group_id)})
Rachana's avatar
Rachana committed
24
	trash_group = node_collection.find_one({"name":"Trash"});
saurabh's avatar
saurabh committed
25 26 27 28 29 30 31 32 33 34
	
	if trash_group._id in node_obj.group_set:
		try:
			if node_obj._id:
				delete_node(ObjectId(node_obj._id),deletion_type=1)
				response_dict['success'] = True
		except Exception as e:
			pass
		return HttpResponse(json.dumps(group_id))

Rachana's avatar
Rachana committed
35 36
	if ObjectId(group_id) in node_obj.group_set: 		 
		node_obj.group_set.remove(ObjectId(group_id))
saurabh's avatar
saurabh committed
37 38 39 40
		if ObjectId(auth._id) in node_obj.group_set:
			node_obj.group_set.remove(ObjectId(auth._id))
		node_obj.save()
	if not node_obj.group_set:
Rachana's avatar
Rachana committed
41 42 43 44
		# Add Trash group _id to node_obj's group_set
		if trash_group._id not in node_obj.group_set:	
			node_obj.group_set.append(trash_group._id)
		node_obj.status = u"DELETED"
45 46 47 48
	if node_obj.collection_set:
		if trash_group._id not in node_obj.group_set:	
			node_obj.group_set.append(trash_group._id)
		node_obj.status = u"DELETED"
Rachana's avatar
Rachana committed
49
	node_obj.save()
50
	# print "\n\n\nnode_obj.status",node_obj.status
Rachana's avatar
Rachana committed
51 52 53

	# get_member_of = node_collection.find_one({"_id":{'$in':node_obj.member_of}})
	# if get_member_of.name == 'Page':
54
	if "Page" in node_obj.member_of_names_list and not "CourseEventGroup" in group_obj.member_of_names_list:
Rachana's avatar
Rachana committed
55 56
		return HttpResponseRedirect(reverse('page', kwargs={'group_id': group_id}))
		# return (eval('page')(request, group_id))
57
	elif "File" in node_obj.member_of_names_list and not "CourseEventGroup" in group_obj.member_of_names_list :
Rachana's avatar
Rachana committed
58
		return HttpResponse(json.dumps(group_id))
Rachana's avatar
Rachana committed
59 60
		# elif get_member_of.name == 'File':
		# return(eval('file')(request, group_id))
61 62 63 64
	elif "CourseEventGroup" in group_obj.member_of_names_list:
		response_dict = {'success': True }
		return HttpResponse(json.dumps(response_dict))
	else:
Rachana's avatar
Rachana committed
65 66
		return HttpResponseRedirect(reverse('group_dashboard', kwargs={'group_id': group_id}))
		# return(eval('group_dashboard')(request, group_id))   
makfire's avatar
makfire committed
67

68
@get_execution_time
makfire's avatar
makfire committed
69
def delete_resource(request,group_id):
70 71 72
	# NOTE: purge of themes need to be handled differently.
	# all the collection hierarchy needs to be purged in this case.
	response_dict = {'success': False}
makfire's avatar
makfire committed
73
	node_id = request.GET.getlist('node_id','')[0]
74 75 76 77 78 79 80
	try:
		if node_id:
			delete_node(ObjectId(node_id),deletion_type=1)
			response_dict['success'] = True
	except Exception as e:
		pass
	return HttpResponse(json.dumps(response_dict))
81

82
@get_execution_time
83
def restore_resource(request, group_id):
84
	response_dict = {'success': False}
85
	node_id = request.GET.getlist('node_id', '')[0]
86 87
	try:
		if node_id:
88

89 90 91 92 93 94
			node_to_be_restore = node_collection.one({'_id': ObjectId(node_id)})
			# print "==========", node_to_be_restore.snapshot.keys()
			# print "\n\n node_to_be_restore === ", node_to_be_restore._id
			if node_to_be_restore.snapshot.keys():
			
				node_to_be_restore.group_set = [ObjectId(i) for i in node_to_be_restore.snapshot.keys()]
saurabh's avatar
saurabh committed
95
				node_to_be_restore.save(group_id)
96 97 98 99 100 101
				# print "--- ", node_to_be_restore.group_set
				response_dict['success'] = True
	except Exception as e:
		# print "\n Resore Exception ===== ", str(e)
		pass
	return HttpResponse(json.dumps(response_dict))
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

@get_execution_time
def delete_multiple_resources(request,group_id):
	files_list = request.POST.getlist("collection[]", '')
	auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
	group_obj = node_collection.find_one({"_id":ObjectId(group_id)})
	trash_group = node_collection.find_one({"name":"Trash"})
	files_list_obj = []
	for each in files_list:
		node_obj = node_collection.find_one({"_id":ObjectId(each)})
		if ObjectId(group_id) in node_obj.group_set: 		 
			node_obj.group_set.remove(ObjectId(group_id))
			if ObjectId(auth._id) in node_obj.group_set:
				node_obj.group_set.remove(ObjectId(auth._id))
			node_obj.save()
		if not node_obj.group_set:
			# Add Trash group _id to node_obj's group_set
			if trash_group._id not in node_obj.group_set:	
				node_obj.group_set.append(trash_group._id)
			node_obj.status = u"DELETED"
		if node_obj.collection_set:
			if trash_group._id not in node_obj.group_set:	
				node_obj.group_set.append(trash_group._id)
			node_obj.status = u"DELETED"
		node_obj.save()

	return HttpResponse(json.dumps(files_list))