trash.py 5.5 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
trash_group = node_collection.one({'_type': 'Group', "name": u"Trash"});
13

14
@get_execution_time
makfire's avatar
makfire committed
15
def trash_resource(request,group_id,node_id):
Rachana's avatar
Rachana committed
16 17 18 19 20 21
	'''
	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
22 23

	auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
Rachana's avatar
Rachana committed
24
	node_obj = node_collection.find_one({"_id":ObjectId(node_id)})
25
	group_obj = node_collection.find_one({"_id":ObjectId(group_id)})
Rachana's avatar
Rachana committed
26
	trash_group = node_collection.find_one({"name":"Trash"});
saurabh's avatar
saurabh committed
27 28 29 30 31 32 33 34 35 36
	
	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
37 38
	if ObjectId(group_id) in node_obj.group_set: 		 
		node_obj.group_set.remove(ObjectId(group_id))
saurabh's avatar
saurabh committed
39 40 41 42
		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
43 44 45 46
		# 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"
47 48 49 50
	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
51
	node_obj.save()
52
	# print "\n\n\nnode_obj.status",node_obj.status
Rachana's avatar
Rachana committed
53 54 55

	# get_member_of = node_collection.find_one({"_id":{'$in':node_obj.member_of}})
	# if get_member_of.name == 'Page':
56
	if "Page" in node_obj.member_of_names_list and not "CourseEventGroup" in group_obj.member_of_names_list:
Rachana's avatar
Rachana committed
57 58
		return HttpResponseRedirect(reverse('page', kwargs={'group_id': group_id}))
		# return (eval('page')(request, group_id))
59
	elif "File" in node_obj.member_of_names_list and not "CourseEventGroup" in group_obj.member_of_names_list :
Rachana's avatar
Rachana committed
60
		return HttpResponse(json.dumps(group_id))
Rachana's avatar
Rachana committed
61 62
		# elif get_member_of.name == 'File':
		# return(eval('file')(request, group_id))
63 64 65 66
	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
67 68
		return HttpResponseRedirect(reverse('group_dashboard', kwargs={'group_id': group_id}))
		# return(eval('group_dashboard')(request, group_id))   
makfire's avatar
makfire committed
69

70 71 72
@get_execution_time
# @staff_required
def delete_group(request,group_id):
73
    response_dict = {'success': False}
74 75 76 77 78 79
    group_obj = get_group_name_id(group_id, get_obj=True)
    del_s,del_msg = delete_node(group_obj._id, deletion_type=0)

    if trash_group._id not in group_obj.group_set:	
		group_obj.group_set.append(trash_group._id)
		group_obj.save(groupid=group_obj._id)
80 81
    if del_s:
        return HttpResponseRedirect(reverse('groupchange', kwargs={'group_id': 'home'}))
82

83
@get_execution_time
makfire's avatar
makfire committed
84
def delete_resource(request,group_id):
85 86 87
	# 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
88
	node_id = request.GET.getlist('node_id','')[0]
89 90 91 92 93 94 95
	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))
96

97
@get_execution_time
98
def restore_resource(request, group_id):
99
	response_dict = {'success': False}
100
	node_id = request.GET.getlist('node_id', '')[0]
101 102
	try:
		if node_id:
103

104 105 106 107 108 109
			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()]
110
				node_to_be_restore.status = u'PUBLISHED'
saurabh's avatar
saurabh committed
111
				node_to_be_restore.save(group_id)
112 113 114 115 116 117
				# 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))
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

@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()

144
	return HttpResponse(json.dumps(files_list))