imageDashboard.py 12 KB
Newer Older
1
''' -- imports from installed packages -- '''
krishna31's avatar
krishna31 committed
2
from django.http import HttpResponseRedirect
3
from django.http import HttpResponse
krishna31's avatar
krishna31 committed
4
from django.core.urlresolvers import reverse
5 6 7 8 9 10 11
from django.shortcuts import render_to_response, render
from django.template import RequestContext

try:
    from bson import ObjectId
except ImportError:  # old pymongo
    from pymongo.objectid import ObjectId
krishna31's avatar
krishna31 committed
12
from gnowsys_ndf.ndf.models import File
13

krishna31's avatar
krishna31 committed
14
''' -- imports from application folders/files -- '''
15
from gnowsys_ndf.settings import META_TYPE, GAPPS, MEDIA_ROOT
16
from gnowsys_ndf.ndf.models import node_collection
admin's avatar
admin committed
17
from gnowsys_ndf.ndf.views.methods import get_node_common_fields,create_grelation_list,get_execution_time
18
from gnowsys_ndf.ndf.views.methods import get_node_metadata
19
from gnowsys_ndf.ndf.management.commands.data_entry import create_gattribute
20
from gnowsys_ndf.ndf.views.methods import get_node_metadata, get_node_common_fields, create_gattribute, get_page, get_execution_time,set_all_urls,get_group_name_id 
21 22 23
gapp_mt = node_collection.one({'_type': "MetaType", 'name': META_TYPE[0]})
GST_IMAGE = node_collection.one({'member_of': gapp_mt._id, 'name': GAPPS[3]})

krishna31's avatar
krishna31 committed
24

admin's avatar
admin committed
25
@get_execution_time
26
def imageDashboard(request, group_id, image_id=None):
27 28 29
    '''
    fetching image acording to group name
    '''
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    # ins_objectid  = ObjectId()
    # if ins_objectid.is_valid(group_id) is False :
    #     group_ins = node_collection.find_one({'_type': "Group", "name": group_id})
    #     auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #     if group_ins:
    #         group_id = str(group_ins._id)
    #     else :
    #         auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #         if auth :
    #             group_id = str(auth._id)
    # else :
    #     pass
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

47
    if image_id is None:
48
        image_ins = node_collection.find_one({'_type': "GSystemType", "name": "Image"})
49 50
        if image_ins:
            image_id = str(image_ins._id)
51
    img_col = node_collection.find({'_type': 'File', 'member_of': {'$all': [ObjectId(image_id)]}, 'group_set': {'$all': [ObjectId(group_id)]}})
krishna31's avatar
krishna31 committed
52
    template = "ndf/ImageDashboard.html"
53
    already_uploaded=request.GET.getlist('var',"")
Anuja G's avatar
Anuja G committed
54
    variable = RequestContext(request, {'imageCollection': img_col,'already_uploaded':already_uploaded,'groupid':group_id,'group_id':group_id })
krishna31's avatar
krishna31 committed
55
    return render_to_response(template, variable)
56

admin's avatar
admin committed
57
@get_execution_time
Anuja G's avatar
Anuja G committed
58
def getImageThumbnail(request, group_id, _id):
59 60 61
    '''
    this funciton can be called to get thumbnail of image throw url
    '''
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    # ins_objectid = ObjectId()
    # if ins_objectid.is_valid(group_id) is False :
    #     group_ins = node_collection.find_one({'_type': "Group","name": group_id})
    #     auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #     if group_ins:
    #         group_id = str(group_ins._id)
    #     else :
    #         auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #         if auth :
    #             group_id = str(auth._id)
    # else :
    #     pass
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

79 80
    img_obj = node_collection.one({"_type": u"File", "_id": ObjectId(_id)})

81
    if img_obj is not None:
82
        # getting latest uploaded pic's _id
83
        img_fs = img_obj.fs_file_ids[2]
84

85 86
        if (img_obj.fs.files.exists(img_fs)):
            f = img_obj.fs.files.get(ObjectId(img_fs))
87
            return HttpResponse(f.read(),content_type=f.content_type)
88 89
    else:
        return HttpResponse("")
krishna31's avatar
krishna31 committed
90
        
admin's avatar
admin committed
91
@get_execution_time    
Anuja G's avatar
Anuja G committed
92
def getFullImage(request, group_id, _id, file_name = ""):
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    # ins_objectid  = ObjectId()
    # if ins_objectid.is_valid(group_id) is False :
    #     group_ins = node_collection.find_one({'_type': "Group", "name": group_id})
    #     auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #     if group_ins:
    #         group_id = str(group_ins._id)
    #     else :
    #         auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #         if auth :
    #             group_id = str(auth._id)
    # else :
    #     pass
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

110
    img_obj = node_collection.one({"_id": ObjectId(_id)})
111 112 113 114 115 116 117 118
    if img_obj is not None:
        if (img_obj.fs.files.exists(img_obj.fs_file_ids[0])):
            f = img_obj.fs.files.get(ObjectId(img_obj.fs_file_ids[0]))
            return HttpResponse(f.read(), content_type=f.content_type)
        else:
            return HttpResponse("")
    else:
        return HttpResponse("")
119

admin's avatar
admin committed
120
@get_execution_time
Anuja G's avatar
Anuja G committed
121
def get_mid_size_img(request, group_id, _id):
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    # ins_objectid  = ObjectId()
    # if ins_objectid.is_valid(group_id) is False :
    #     group_ins = node_collection.find_one({'_type': "Group","name": group_id})
    #     auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #     if group_ins:
    #         group_id = str(group_ins._id)
    #     else :
    #         auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #         if auth :
    #             group_id = str(auth._id)
    # else :
    #     pass
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

139
    img_obj = node_collection.one({"_id": ObjectId(_id)})
140 141
    try:
        f = img_obj.fs.files.get(ObjectId(img_obj.fs_file_ids[2]))
142
        return HttpResponse(f.read(), content_type=f.content_type)
143 144 145 146
    except IndexError:
        f = img_obj.fs.files.get(ObjectId(img_obj.fs_file_ids[0]))
        return HttpResponse(f.read(), content_type=f.content_type)
        
admin's avatar
admin committed
147
@get_execution_time
Anuja G's avatar
Anuja G committed
148
def image_search(request,group_id):
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    # ins_objectid  = ObjectId()
    # if ins_objectid.is_valid(group_id) is False :
    #     group_ins = node_collection.find_one({'_type': "Group","name": group_id})
    #     auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #     if group_ins:
    #         group_id = str(group_ins._id)
    #     else :
    #         auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #         if auth :
    #             group_id = str(auth._id)
    # else :
    #     pass
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

166
    imgcol = node_collection.find({"_type": "File", 'mime_type': {'$regex': 'image'}})
167 168
    if request.method=="GET":
        keyword=request.GET.get("search","")
169
        img_search=node_collection.find({'$and':[{'mime_type':{'$regex': 'image'}},{'$or':[{'name':{'$regex':keyword}},{'tags':{'$regex':keyword}}]}]})
170
        template="ndf/file_search.html"
Anuja G's avatar
Anuja G committed
171
        variable=RequestContext(request,{'file_collection':img_search,'view_name':'image_search','groupid':group_id,'group_id':group_id})
172
        return render_to_response(template,variable)
krishna31's avatar
krishna31 committed
173

admin's avatar
admin committed
174
@get_execution_time
Anuja G's avatar
Anuja G committed
175
def image_detail(request, group_id, _id):
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    # ins_objectid  = ObjectId()
    # if ins_objectid.is_valid(group_id) is False :
    #     group_ins = node_collection.find_one({'_type': "Group","name": group_id})
    #     auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #     if group_ins:
    #         group_id = str(group_ins._id)
    #     else :
    #         auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #         if auth :
    #             group_id = str(auth._id)
    # else :
    #     pass
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)

193
    img_node = node_collection.one({"_id": ObjectId(_id)})
sunnychaudhari's avatar
sunnychaudhari committed
194 195 196 197 198 199 200 201 202

    # First get the navigation list till topic from theme map
    nav_l=request.GET.get('nav_li','')
    breadcrumbs_list = []
    nav_li = ""

    if nav_l:
      nav_li = nav_l

203 204
    if img_node._type == "GSystemType":
	return imageDashboard(request, group_id, _id)
205
    img_node.get_neighbourhood(img_node.member_of)
206

207
    imageCollection = node_collection.find({'member_of': {'$all': [ObjectId(GST_IMAGE._id)]}, 
208 209 210 211 212 213 214 215 216 217 218 219
                                              '_type': 'File','fs_file_ids': {'$ne': []}, 
                                              'group_set': {'$all': [ObjectId(group_id)]},
                                              '$or': [
                                                {'access_policy': u"PUBLIC"},
                                                {'$and': [
                                                  {'access_policy': u"PRIVATE"}, 
                                                  {'created_by': request.user.id}
                                                  ]
                                                }
                                              ]
                                            }).sort("last_update", -1)

krishna31's avatar
krishna31 committed
220 221
    return render_to_response("ndf/image_detail.html",
                                  { 'node': img_node,
sunnychaudhari's avatar
sunnychaudhari committed
222
                                    'group_id': group_id, 'nav_list':nav_li,
223
                                    'groupid':group_id, 'imageCollection': imageCollection
krishna31's avatar
krishna31 committed
224 225 226
                                  },
                                  context_instance = RequestContext(request)
        )
227

admin's avatar
admin committed
228
@get_execution_time
Anuja G's avatar
Anuja G committed
229
def image_edit(request,group_id,_id):
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    # ins_objectid  = ObjectId()
    # if ins_objectid.is_valid(group_id) is False :
    #     group_ins = node_collection.find_one({'_type': "Group","name": group_id})
    #     auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #     if group_ins:
    #         group_id = str(group_ins._id)
    #     else :
    #         auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username) })
    #         if auth :
    #             group_id = str(auth._id)
    # else :
    #     pass
    try:
        group_id = ObjectId(group_id)
    except:
        group_name, group_id = get_group_name_id(group_id)
        
247
    group_obj = node_collection.one({'_id': ObjectId(group_id)})
248
    img_node = node_collection.one({"_id": ObjectId(_id)})
249 250 251
    ce_id = request.GET.get('course_event_id')
    res = request.GET.get('res')

252
    title = GST_IMAGE.name
krishna31's avatar
krishna31 committed
253
    if request.method == "POST":
254 255
        # get_node_common_fields(request, img_node, group_id, GST_IMAGE)
        img_node.save(is_changed=get_node_common_fields(request, img_node, group_id, GST_IMAGE))
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if "CourseEventGroup" not in group_obj.member_of_names_list:
            get_node_metadata(request,img_node)
            teaches_list = request.POST.get('teaches_list','') # get the teaches list 
            if teaches_list !='':
                teaches_list=teaches_list.split(",")
            create_grelation_list(img_node._id,"teaches",teaches_list)
            assesses_list = request.POST.get('assesses_list','')
            if assesses_list !='':
                assesses_list=assesses_list.split(",")

            create_grelation_list(img_node._id,"assesses",assesses_list)

            return HttpResponseRedirect(reverse('image_detail', kwargs={'group_id': group_id, '_id': img_node._id}))
        else:
            url = "/"+ group_id +"/?selected="+str(img_node._id)+"#view_page"
            return HttpResponseRedirect(url)
krishna31's avatar
krishna31 committed
272
    else:
273
        img_node.get_neighbourhood(img_node.member_of)
krishna31's avatar
krishna31 committed
274
        return render_to_response("ndf/image_edit.html",
275
                                  {'node': img_node, 'title': title,
Anuja G's avatar
Anuja G committed
276
                                    'group_id': group_id,
277 278 279
                                    'groupid': group_id,
                                    'ce_id':ce_id,
                                    'res': res
krishna31's avatar
krishna31 committed
280 281 282
                                },
                                  context_instance=RequestContext(request)
                              )