summaryrefslogtreecommitdiff
path: root/gnowsys-ndf/gnowsys_ndf/ndf/views/batch.py
blob: c90530a5db8bf33644eff2dd13136e360cdeacd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.shortcuts import render_to_response  # render  uncomment when to use
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
import json

from mongokit import IS
try:
    from bson import ObjectId
except ImportError:  # old pymongo
    from pymongo.objectid import ObjectId

# from gnowsys_ndf.settings import GAPPS, MEDIA_ROOT
# from gnowsys_ndf.ndf.models import GSystemType, Node
from gnowsys_ndf.ndf.models import NodeJSONEncoder
from gnowsys_ndf.ndf.models import node_collection, triple_collection
from gnowsys_ndf.ndf.views.methods import create_grelation, get_execution_time, get_group_name_id

GST_BATCH = node_collection.one({"_type": "GSystemType", 'name': "Batch"})
app = GST_BATCH


@get_execution_time
def batch(request, group_id):
    """
   * Renders a list of all 'batches' available within the database.
    """
    group_name, group_id = get_group_name_id(group_id)
    # ins_objectid = ObjectId()
    # st_student = node_collection.one({'_type':'GSystemType','name':'Student'})
    # 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
    nussd_course_type_name = ""
    announced_course_name = ""

    if request.method == "POST":
        announced_course_name = request.POST.get("announced_course_name", "")
        nussd_course_type_name = request.POST.get("nussd_course_name", "")
        # colg_gst = node_collection.one({'_type': "GSystemType", 'name': 'College'})
        # req_colg_id = node_collection.one({'member_of': colg_gst._id, 'relation_set.has_group': ObjectId(group_id)})
        batch_coll = node_collection.find({'member_of': GST_BATCH._id, 'relation_set.has_course': ObjectId(announced_course_name)})
        # for each_batch in batch_coll:
        #   each_batch['batch_name_human_readble'] = (each_batch.name).replace('_',' ')
    else:
        batch_coll = node_collection.find({'member_of': GST_BATCH._id, 'relation_set.batch_in_group':ObjectId(group_id)})
    fetch_ATs = ["nussd_course_type"]
    req_ATs = []
    for each in fetch_ATs:
        each = node_collection.one({'_type': "AttributeType", 'name': each}, {'_type': 1, '_id': 1, 'data_type': 1, 'complex_data_type': 1, 'name': 1, 'altnames': 1})

        if each["data_type"] == "IS()":
            dt = "IS("
            for v in each.complex_data_type:
                dt = dt + "u'" + v + "'" + ", "
            dt = dt[:(dt.rfind(", "))] + ")"
            each["data_type"] = dt

        each["data_type"] = eval(each["data_type"])
        each["value"] = None
        req_ATs.append(each)
    # users_in_group = node_collection.one({'_id':ObjectId(group_id)}).author_set
    template = "ndf/batch.html"
    variable = RequestContext(request, {'batch_coll': batch_coll,
                            'appId': app._id,
                            'nussd_course_name_var': nussd_course_type_name,
                            'announced_course_name_var': announced_course_name,
                            'ATs': req_ATs, 'group_id': group_id,
                            'groupid': group_id, 'title': GST_BATCH.name,
                            'st_batch_id': GST_BATCH._id})
    return render_to_response(template, variable)


@get_execution_time
def new_create_and_edit(request, group_id, _id=None):
    # node = ""
    # count = ""
    batch = ""
    batch_count = 1
    ac = None
    nussd_course_name = ""

    if request.method == 'POST':
        batch_count = int(request.POST.get('batch_count', ''))

    st_student = node_collection.one({'_type': 'GSystemType', 'name': 'Student'})
    student_coll = node_collection.find(
        {'member_of': st_student._id, 'group_set': ObjectId(group_id)}
    )

    if _id:
        batch = node_collection.one(
            {'_id': ObjectId(_id)},
            {'relation_set.has_course': 1, 'name': 1}
        )

        for each in batch.relation_set:
            if "has_course" in each.keys():
                ac_id_of_batch = each["has_course"][0]

        ac = node_collection.one(
            {'_id': ObjectId(ac_id_of_batch), 'attribute_set.nussd_course_type': {'$exists': True}},
            {'attribute_set.nussd_course_type': 1}
        )

        if ac:
            for attr in ac.attribute_set:
                # if attr and attr.has_key("nussd_course_type"):
                if attr and "nussd_course_type" in attr:
                    nussd_course_name = attr["nussd_course_type"]
                    break

    fetch_ATs = ["nussd_course_type"]
    req_ATs = []
    for each in fetch_ATs:
        each = node_collection.one({'_type': "AttributeType", 'name': each}, {'_type': 1, '_id': 1, 'data_type': 1, 'complex_data_type': 1, 'name': 1, 'altnames': 1})

        if each["data_type"] == "IS()":
            dt = "IS("
            for v in each.complex_data_type:
                dt = dt + "u'" + v + "'" + ", "
            dt = dt[:(dt.rfind(", "))] + ")"
            each["data_type"] = dt

        each["data_type"] = eval(each["data_type"])
        each["value"] = None
        req_ATs.append(each)

    variable = RequestContext(request, {
        'group_id': group_id, 'groupid': group_id,
        'appId': app._id, 'title': GST_BATCH.name, 'ATs': req_ATs,
        'count': batch_count, 'batch_count': xrange(batch_count), 'st_batch_id': GST_BATCH._id, 
        'ac_node': ac,
        'student_count': student_coll.count(),
        'nussd_course_name': nussd_course_name,
        'node': batch
    })

    template = "ndf/new_create_batch.html"
    return render_to_response(template, variable)


@get_execution_time
def save_students_for_batches(request, group_id):
    '''
    This save method creates new  and update existing the batches
    '''
    edit_batch = []
    if request.method == 'POST':
        batch_user_list = request.POST.get('batch_user_list_dict', '')
        # print "\n\n batch_user_list_dict", batch_user_list
        batch_user_list = json.loads(batch_user_list)
        ac_id = request.POST.get('ac_id', '')
        # print "\n\n ac_id", ac_id
        # raise Exception("batch")
        for k, v in batch_user_list.items():
            save_btch(k, v, group_id, request, ac_id)
        return HttpResponseRedirect(reverse('batch', kwargs={'group_id': group_id}))


@get_execution_time
def save_batch(request, group_id):
# def save_batch(batch_name, user_list, group_id, request, ac_id):

    group_name, group_id = get_group_name_id(group_id)
    response_dict = {"success": False}
    # new_batch_node = None
    rt_has_batch_member = node_collection.one({'_type': 'RelationType', 'name': 'has_batch_member'})
    if request.is_ajax() and request.method == "POST":
        ac_id = request.POST.get("ac_id", '')
        batch_name = request.POST.get("batch_name", '')
        batch_id = request.POST.get("batch_id", '')
        user_list = request.POST.getlist("user_list[]", '')
        # create_new_batch = request.POST.get("create_new_batch", '')
        # response_dict["old_batches"] = find_batches_of_ac(ac_id)
        user_list = [ObjectId(each) for each in user_list]
        all_batches_in_grp = []
        if not batch_id:
            # b_node = node_collection.one({'member_of':GST_BATCH._id,'name':unicode(batch_name)})
            b_node = node_collection.collection.GSystem()
            b_node.member_of.append(GST_BATCH._id)
            b_node.created_by = int(request.user.id)
            b_node.group_set.append(ObjectId(group_id))
            b_node.name = batch_name
            b_node['altnames'] = batch_name.replace('_', ' ')

            b_node.contributors.append(int(request.user.id))
            b_node.modified_by = int(request.user.id)
            b_node.save()
            all_batches_in_grp.append(b_node._id)

            rt_group_has_batch = node_collection.one({'_type': 'RelationType', 'name': 'group_has_batch'})
            relation_coll = triple_collection.find({'_type': 'GRelation', 'relation_type.$id': rt_group_has_batch._id,'subject':ObjectId(group_id)})

            for each in relation_coll:
                all_batches_in_grp.append(each.right_subject)
                # to get all batches of the group
            rt_has_course = node_collection.one({'_type': 'RelationType', 'name': 'has_course'})

            create_grelation(ObjectId(group_id), rt_group_has_batch, all_batches_in_grp)
            create_grelation(b_node._id, rt_has_course, ObjectId(ac_id))
            response_dict['new_batch_created'] = True
            response_dict['new_batch_node_name'] = b_node.name
            response_dict['new_batch_node_id'] = str(b_node._id)
        else:
            response_dict['new_batch_created'] = False
            b_node = node_collection.one({'_id': ObjectId(batch_id)})
        if user_list:
            create_grelation(b_node._id, rt_has_batch_member, user_list)
        response_dict['success'] = True
        return HttpResponse(json.dumps(response_dict, cls=NodeJSONEncoder))


@get_execution_time
def detail(request, group_id, _id):
    student_coll = []
    node = node_collection.one({'_id':ObjectId(_id)})
    rt_has_batch_member = node_collection.one({'_type':'RelationType','name':'has_batch_member'})
    relation_coll = triple_collection.find({'_type':'GRelation','relation_type.$id':rt_has_batch_member._id,'subject':node._id,'status':u'PUBLISHED'})

    for each in relation_coll:
        n = node_collection.one({'_id':ObjectId(each.right_subject)})
        student_coll.append(n)
    template = "ndf/batch_detail.html"
    variable = RequestContext(request, {'node':node,'node_name_human_readable':(node.name).replace('_',' '), 'appId':app._id, 'groupid':group_id, 'group_id': group_id,'title':GST_BATCH.name, 'student_coll':student_coll})
    return render_to_response(template, variable)


@get_execution_time
def remove_stud_from_batch(request, group_id):
    group_name, group_id = get_group_name_id(group_id)
    if request.is_ajax() and request.method == "POST":
        batch_id = request.POST.get("batch_id", '')
        stud_id = request.POST.get("stud_id", '')
        rt_has_batch_member = node_collection.one({'_type':'RelationType','name':'has_batch_member'})
        grelation_node = triple_collection.one({'_type':'GRelation',
                        'relation_type.$id':rt_has_batch_member._id,
                        'subject':ObjectId(batch_id),'status':u'PUBLISHED',
                        'right_subject': ObjectId(stud_id)})
        rel_name = grelation_node.relation_type.name
        inv_rel_name = grelation_node.relation_type.inverse_name
        subj = grelation_node.subject
        right_subj = grelation_node.right_subject

        # Remove right-subject-node's ObjectId from the value
        # corresponding to subject-node's "relation-name" key
        # referenced in relation_set field
        res = node_collection.collection.update({
            '_id': subj,
            'relation_set.' + rel_name: {'$exists': True}
        }, {
            '$pull': {'relation_set.$.' + rel_name: right_subj}
        },
            upsert=False, multi=False
        )
        # Remove subject-node's ObjectId from the value corresponding
        # to right-subject-node's "inverse-relation-name" key
        # referenced in relation_set field
        res = node_collection.collection.update({
            '_id': right_subj,
            'relation_set.' + inv_rel_name: {'$exists': True}
        }, {
            '$pull': {'relation_set.$.' + inv_rel_name: subj}
        },
            upsert=False, multi=False
        )

        grelation_node.status = u"DELETED"
        grelation_node.save()
        status = "success"
        return HttpResponse(json.dumps(status, cls=NodeJSONEncoder))


@get_execution_time
def batch_detail(request, group_id):
    group_name, group_id = get_group_name_id(group_id)
    new_batch_node = None
    if request.is_ajax() and request.method == "GET":
        batch_id = request.GET.get("batch_id", '')

        student_coll = []
        node = node_collection.one({'_id':ObjectId(batch_id)})
        rt_has_batch_member = node_collection.one({'_type':'RelationType','name':'has_batch_member'})
        relation_coll = triple_collection.find({'_type':'GRelation','relation_type.$id':rt_has_batch_member._id,'subject':node._id,'status':u'PUBLISHED'})

        for each in relation_coll:
            n = node_collection.one({'_id':ObjectId(each.right_subject)})
            student_coll.append(n)
        return HttpResponse(json.dumps(student_coll, cls=NodeJSONEncoder))



@login_required
@get_execution_time
def delete_batch(request, group_id, _id):

    group_name, group_id = get_group_name_id(group_id)

    # if 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

    node = node_collection.one({'_id': ObjectId(_id)})
    left_relations = triple_collection.find({"_type": "GRelation", "subject": node._id})
    right_relations = triple_collection.find({"_type": "GRelation", "right_subject": node._id})
    attributes = triple_collection.find({"_type": "GAttribute", "subject": node._id})

    for eachobject in right_relations:
        # If given node is used in relationship with any other node (as right_subject)
        # Then this node's ObjectId must be removed from relation_set field of other node
        node_collection.collection.update(
            {'_id': eachobject.subject, 'relation_set.' + eachobject.relation_type.name: {'$exists': True}},
            {'$pull': {'relation_set.$.' + eachobject.relation_type.name: node._id}},
            upsert=False, multi=False
        )
        eachobject.delete()

    all_associates = list(left_relations) + list(attributes)
    # Deleting GAttributes and GRelations where given node is used as left subject
    for eachobject in all_associates:
        eachobject.delete()

    # Finally deleting given node
    node.delete()
    return HttpResponseRedirect(reverse('batch', kwargs={'group_id': group_id}))


@login_required
def get_possible_batches(request, group_id):
    '''
    Returns:
     * Batches of selected course
    '''
    group_name, group_id = get_group_name_id(group_id)
    response_dict = {"success": False}
    new_batch_node = None
    if request.is_ajax() and request.method == "POST":
        ac_id = request.POST.get("ac_id", '')
        create_new_batch = request.POST.get("create_new_batch", '')
        response_dict["success"] = True
        batch_user_list_dict = []
        list_of_members = []
        if ac_id:
            batch_cur = node_collection.find({'member_of': GST_BATCH._id,
                                'relation_set.has_course': ObjectId(ac_id)})
            for each_batch in batch_cur:
                each_batch_dict = {}
                if each_batch.relation_set:
                    for rel in each_batch.relation_set:
                        list_of_members = []
                        if rel and 'has_batch_member' in rel:
                            list_of_members.append(rel['has_batch_member'])
                            list_of_members.append(str(each_batch._id))
                            each_batch_dict[each_batch.name] = list_of_members
                batch_user_list_dict.append(each_batch_dict)
                # batch_user_list_dict.append(str(each_batch._id))
        # print "\n\nBatches----------", batch_user_list_dict
        response_dict["old_batches"] = batch_user_list_dict
        return HttpResponse(json.dumps(response_dict, cls=NodeJSONEncoder))