def person_create_edit(request, group_id, app_id, app_set_id=None, app_set_instance_id=None, app_name=None): """ Creates/Modifies document of given person-type. """ auth = None if ObjectId.is_valid(group_id) is False : group_ins = node_collection.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 app = None if app_id is None: app = node_collection.one({'_type': "GSystemType", 'name': app_name}) if app: app_id = str(app._id) else: app = node_collection.one({'_id': ObjectId(app_id)}) app_name = app.name # app_name = "mis" app_set = "" app_collection_set = [] title = "" person_gst = None person_gs = None college_node = None college_id = None student_enrollment_code = u"" create_student_enrollment_code = False existing_colg = [] registration_date = None property_order_list = [] template = "" template_prefix = "mis" if request.user: if auth is None: auth = node_collection.one({'_type': 'Author', 'name': unicode(request.user.username)}) agency_type = auth.agency_type agency_type_node = node_collection.one({'_type': "GSystemType", 'name': agency_type}, {'collection_set': 1}) if agency_type_node: for eachset in agency_type_node.collection_set: app_collection_set.append(node_collection.one({"_id": eachset}, {'_id': 1, 'name': 1, 'type_of': 1})) # for eachset in app.collection_set: # app_collection_set.append(node_collection.one({"_id":eachset}, {'_id': 1, 'name': 1, 'type_of': 1})) college_node = node_collection.one({ "_id": ObjectId(group_id), "relation_set.group_of": {"$exists": True} }, { "relation_set.group_of": 1 }) if app_set_id: person_gst = node_collection.one({'_type': "GSystemType", '_id': ObjectId(app_set_id)}, {'name': 1, 'type_of': 1}) template = "ndf/" + person_gst.name.strip().lower().replace(' ', '_') + "_create_edit.html" title = person_gst.name person_gs = node_collection.collection.GSystem() person_gs.member_of.append(person_gst._id) if app_set_instance_id: person_gs = node_collection.one({'_type': "GSystem", '_id': ObjectId(app_set_instance_id)}) property_order_list = get_property_order_with_value(person_gs)#.property_order if request.method == "POST": if person_gst.name == "Student" and "_id" not in person_gs: create_student_enrollment_code = True # [A] Save person-node's base-field(s) is_changed = get_node_common_fields(request, person_gs, group_id, person_gst) if is_changed: # Remove this when publish button is setup on interface person_gs.status = u"PUBLISHED" person_gs.save(is_changed=is_changed,groupid=group_id) for each_rel in person_gs.relation_set: if each_rel and "officer_incharge_of" in each_rel: existing_colg = each_rel["officer_incharge_of"] if college_node: mis_admin = node_collection.one({ "_type": "Group", "name": "MIS_admin" }, { "_id": 1 } ) node_collection.collection.update({ "_id": person_gs._id }, { "$addToSet": {"group_set": mis_admin._id} }, upsert=False, multi=False ) # [B] Store AT and/or RT field(s) of given person-node (i.e., person_gs) for tab_details in property_order_list: for field_set in tab_details[1]: # Fetch only Attribute field(s) / Relation field(s) if '_id' in field_set: field_instance = node_collection.one({'_id': field_set['_id']}) fi_name = field_instance["name"] field_instance_type = type(field_instance) if field_instance_type in [AttributeType, RelationType]: field_data_type = field_set['data_type'] # Fetch field's value depending upon AT/RT and Parse fetched-value depending upon that field's data-type if field_instance_type == AttributeType: if "File" in field_instance["validators"]: # Special case: AttributeTypes that require file instance as it's value in which case file document's ObjectId is used user_id = request.user.id if fi_name in request.FILES: field_value = request.FILES[fi_name] else: field_value = "" # Below 0th index is used because that function returns tuple(ObjectId, bool-value) if field_value != '' and field_value != u'': file_name = person_gs.name + " -- " + field_instance["altnames"] content_org = "" tags = "" field_value = save_file(field_value, file_name, user_id, group_id, content_org, tags, access_policy="PRIVATE", count=0, first_object="", oid=True)[0] else: # Other AttributeTypes if fi_name in request.POST: field_value = request.POST[fi_name] # field_instance_type = "GAttribute" if fi_name in ["12_passing_year", "degree_passing_year"]: #, "registration_year"]: field_value = parse_template_data(field_data_type, field_value, date_format_string="%Y") elif fi_name in ["dob", "registration_date"]: field_value = parse_template_data(field_data_type, field_value, date_format_string="%d/%m/%Y") registration_date = field_value else: field_value = parse_template_data(field_data_type, field_value, date_format_string="%d/%m/%Y %H:%M") if field_value: person_gs_triple_instance = create_gattribute(person_gs._id, node_collection.collection.AttributeType(field_instance), field_value) else: if field_instance["object_cardinality"] > 1: field_value_list = request.POST.get(fi_name, "") if "[" in field_value_list and "]" in field_value_list: field_value_list = json.loads(field_value_list) else: field_value_list = request.POST.getlist(fi_name) else: field_value_list = request.POST.getlist(fi_name) if META_TYPE[3] in field_instance.member_of_names_list: # If Binary relationship found # [id, id, ...] # field_instance_type = "GRelation" for i, field_value in enumerate(field_value_list): field_value = parse_template_data(field_data_type, field_value, field_instance=field_instance, date_format_string="%m/%d/%Y %H:%M") field_value_list[i] = field_value else: # Relationship Other than Binary one found; e.g, Triadic # [[id, id, ...], [id, id, ...], ...] # field_instance_type = "GRelation" for i, field_value_inner_list in enumerate(field_value_list): for j, field_value in enumerate(field_value_inner_list): field_value = parse_template_data(field_data_type, field_value, field_instance=field_instance, date_format_string="%m/%d/%Y %H:%M") field_value_list[i][j] = field_value person_gs_triple_instance = create_grelation(person_gs._id, node_collection.collection.RelationType(field_instance), field_value_list) # Setting enrollment code for student node only while creating it if create_student_enrollment_code: # Create enrollment code for student node only while registering a new node for rel in college_node.relation_set: if rel and "group_of" in rel: college_id = rel["group_of"][0] student_enrollment_code = get_student_enrollment_code(college_id, person_gs._id, registration_date, ObjectId(group_id)) enrollment_code_at = node_collection.one({ "_type": "AttributeType", "name": "enrollment_code" }) try: ga_node = create_gattribute(person_gs._id, enrollment_code_at, student_enrollment_code) except Exception as e: print "\n StudentEnrollmentCreateError: " + str(e) + "!!!" # [C] Code to link GSystem Node and Author node via "has_login" relationship; # and Subscribe the Author node to College group if user "Program Officer" person_gs.reload() auth_node = None for attr in person_gs.attribute_set: if "email_id" in attr: if attr["email_id"]: auth_node = node_collection.one({'_type': "Author", 'email': attr["email_id"].lower()}) break if auth_node: has_login_rt = node_collection.one({'_type': "RelationType", 'name': "has_login"}) if has_login_rt: # Linking GSystem Node and Author node via "has_login" relationship; gr_node = create_grelation(person_gs._id, has_login_rt, auth_node._id) # Set author_agency field's value of author node as "Program Officer" # Required to identify at time of log-in in order to display # required modules defined for Program Officers under MIS GApp if auth_node.agency_type != u"Program Officer": auth_node.agency_type = u"Program Officer" auth_node.save(groupid=group_id) if "Program Officer" in person_gs.member_of_names_list: # If Person node (GSystem) is of Program Officer type # then only go for subscription college_id_list = [] # Fetch College's ObjectId to which Program Officer is assigned (via "officer_incharge_of") for rel in person_gs.relation_set: if "officer_incharge_of" in rel: if rel["officer_incharge_of"]: for college_id in rel["officer_incharge_of"]: if college_id not in college_id_list: college_id_list.append(college_id) break # break outer-loop (of relation_set) if college_id_list: # If College's ObjectId exists (list as PO might be assigned to more than one college) # Then prepare a list of their corresponding private group(s) (via "has_group") college_cur = node_collection.find( {'_id': {'$in': college_id_list}}, {'relation_set.has_group': 1} ) college_group_id_list = [] for college in college_cur: for rel in college.relation_set: if rel and "has_group" in rel: if rel["has_group"]: if rel["has_group"][0] not in college_group_id_list: college_group_id_list.append(rel["has_group"][0]) break # break inner-loop (college.relation_set) if college_group_id_list: # If college-group list exists # Then update their group_admin field (append PO's created_by) res = node_collection.collection.update( {'_id': {'$in': college_group_id_list}}, {'$addToSet': {'group_admin': auth_node.created_by}}, upsert=False, multi=True ) old_college_id_list = [] if existing_colg: if len(existing_colg) > len(college_id_list): for each_colg_id in existing_colg: if each_colg_id not in college_id_list: old_college_id_list.append(each_colg_id) old_college_cur = node_collection.find( {'_id': {'$in': old_college_id_list}}, {'relation_set.has_group': 1} ) old_college_group_id_list = [] for college in old_college_cur: for rel in college.relation_set: if rel and "has_group" in rel: if rel["has_group"]: if rel["has_group"][0] not in old_college_group_id_list: old_college_group_id_list.append(rel["has_group"][0]) break # break inner-loop (college.relation_set) if old_college_group_id_list: # If college-group list exists # Then update their group_admin field (remove PO's created_by) res = node_collection.collection.update( {'_id': {'$in': old_college_group_id_list}, '$or': [{'group_admin': auth_node.created_by}, {'author_set': auth_node.created_by}]}, {'$pull': {'group_admin': auth_node.created_by, 'author_set': auth_node.created_by}}, upsert=False, multi=True ) # The code below is commented as the college groups are PRIVATE. # for rel in person_gs.relation_set: # if rel and "officer_incharge_of" in rel: # pass # else: # node_collection.collection.update({'_id': auth_node._id}, # {'$set': {'agency_type': u"Other"}}, # upsert=False, multi=False # ) # Its purpose is to change the agency type back to Other auth_node.reload() # if person_gst.name != "Student": # return HttpResponseRedirect(reverse(app_name.lower()+":"+template_prefix+'_app_detail', kwargs={'group_id': group_id, "app_id":app_id, "app_set_id":app_set_id})) # else: return HttpResponseRedirect(reverse('mis:mis_app_instance_detail',kwargs={'group_id': group_id, "app_id":app_id, "app_set_id":app_set_id, "app_set_instance_id":unicode(person_gs._id)})) # return HttpResponseRedirect(reverse(app_name.lower()+":"+template_prefix+'_app_detail', kwargs={'group_id': group_id, "app_id":app_id, "app_set_id":app_set_id})) default_template = "ndf/person_create_edit.html" # default_template = "ndf/"+template_prefix+"_create_edit.html" context_variables = { 'groupid': group_id, 'group_id': group_id, 'app_id': app_id, 'app_name': app_name, 'app_collection_set': app_collection_set, 'app_set_id': app_set_id, 'title':title, 'property_order_list': property_order_list } if person_gst and person_gst.name in ["Voluntary Teacher", "Master Trainer"]: nussd_course_type = node_collection.one({'_type': "AttributeType", 'name': "nussd_course_type"}, {'_type': 1, '_id': 1, 'data_type': 1, 'complex_data_type': 1, 'name': 1, 'altnames': 1}) if nussd_course_type["data_type"] == "IS()": # Below code does little formatting, for example: # data_type: "IS()" complex_value: [u"ab", u"cd"] dt: # "IS(u'ab', u'cd')" dt = "IS(" for v in nussd_course_type.complex_data_type: dt = dt + "u'" + v + "'" + ", " dt = dt[:(dt.rfind(", "))] + ")" nussd_course_type["data_type"] = dt nussd_course_type["data_type"] = eval(nussd_course_type["data_type"]) nussd_course_type["value"] = None context_variables['nussd_course_type'] = nussd_course_type if app_set_instance_id: person_gs.get_neighbourhood(person_gs.member_of) if "trainer_teaches_course_in_college" in person_gs: l = [] for each_course_college in person_gs.trainer_teaches_course_in_college: # Fetch Course Type (i.e. nussd_course_type) ct = "" for each_attr in each_course_college[0].attribute_set: if "nussd_course_type" in each_attr and each_attr: ct = each_attr["nussd_course_type"] break univ_name = "" for each_rel in each_course_college[1].relation_set: if "college_affiliated_to" in each_rel and each_rel: univ = node_collection.find_one({"_id": {"$in": each_rel["college_affiliated_to"]}}) univ_name = univ.name if univ else "" l.append(( ct, each_course_college[0].name, each_course_college[1].name, each_course_college[0]._id.__str__(), each_course_college[1]._id.__str__(), univ_name )) if l: person_gs.trainer_teaches_course_in_college = l context_variables['node'] = person_gs try: return render_to_response([template, default_template], context_variables, context_instance = RequestContext(request) ) except TemplateDoesNotExist as tde: error_message = "\n PersonCreateEditViewError: This html template (" + str(tde) + ") does not exists !!!\n" raise Http404(error_message) except Exception as e: error_message = "\n PersonCreateEditViewError: " + str(e) + " !!!\n" raise Exception(error_message)
def person_create_edit(request, group_id, app_id, app_set_id=None, app_set_instance_id=None, app_name=None): """ Creates/Modifies document of given person-type. """ auth = None if ObjectId.is_valid(group_id) is False: group_ins = node_collection.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 app = None if app_id is None: app = node_collection.one({'_type': "GSystemType", 'name': app_name}) if app: app_id = str(app._id) else: app = node_collection.one({'_id': ObjectId(app_id)}) app_name = app.name # app_name = "mis" app_set = "" app_collection_set = [] title = "" person_gst = None person_gs = None college_node = None college_id = None student_enrollment_code = u"" create_student_enrollment_code = False existing_colg = [] registration_date = None property_order_list = [] template = "" template_prefix = "mis" if request.user: if auth is None: auth = node_collection.one({ '_type': 'Author', 'name': unicode(request.user.username) }) agency_type = auth.agency_type agency_type_node = node_collection.one( { '_type': "GSystemType", 'name': agency_type }, {'collection_set': 1}) if agency_type_node: for eachset in agency_type_node.collection_set: app_collection_set.append( node_collection.one({"_id": eachset}, { '_id': 1, 'name': 1, 'type_of': 1 })) # for eachset in app.collection_set: # app_collection_set.append(node_collection.one({"_id":eachset}, {'_id': 1, 'name': 1, 'type_of': 1})) college_node = node_collection.one( { "_id": ObjectId(group_id), "relation_set.group_of": { "$exists": True } }, {"relation_set.group_of": 1}) if app_set_id: person_gst = node_collection.one( { '_type': "GSystemType", '_id': ObjectId(app_set_id) }, { 'name': 1, 'type_of': 1 }) template = "ndf/" + person_gst.name.strip().lower().replace( ' ', '_') + "_create_edit.html" title = person_gst.name person_gs = node_collection.collection.GSystem() person_gs.member_of.append(person_gst._id) if app_set_instance_id: person_gs = node_collection.one({ '_type': "GSystem", '_id': ObjectId(app_set_instance_id) }) property_order_list = get_property_order_with_value( person_gs) #.property_order if request.method == "POST": if person_gst.name == "Student" and "_id" not in person_gs: create_student_enrollment_code = True # [A] Save person-node's base-field(s) is_changed = get_node_common_fields(request, person_gs, group_id, person_gst) if is_changed: # Remove this when publish button is setup on interface person_gs.status = u"PUBLISHED" person_gs.save(is_changed=is_changed, groupid=group_id) for each_rel in person_gs.relation_set: if each_rel and "officer_incharge_of" in each_rel: existing_colg = each_rel["officer_incharge_of"] if college_node: mis_admin = node_collection.one( { "_type": "Group", "name": "MIS_admin" }, {"_id": 1}) node_collection.collection.update( {"_id": person_gs._id}, {"$addToSet": { "group_set": mis_admin._id }}, upsert=False, multi=False) # [B] Store AT and/or RT field(s) of given person-node (i.e., person_gs) for tab_details in property_order_list: for field_set in tab_details[1]: # Fetch only Attribute field(s) / Relation field(s) if '_id' in field_set: field_instance = node_collection.one( {'_id': field_set['_id']}) fi_name = field_instance["name"] field_instance_type = type(field_instance) if field_instance_type in [AttributeType, RelationType]: field_data_type = field_set['data_type'] # Fetch field's value depending upon AT/RT and Parse fetched-value depending upon that field's data-type if field_instance_type == AttributeType: if "File" in field_instance["validators"]: # Special case: AttributeTypes that require file instance as it's value in which case file document's ObjectId is used user_id = request.user.id if fi_name in request.FILES: field_value = request.FILES[fi_name] else: field_value = "" # Below 0th index is used because that function returns tuple(ObjectId, bool-value) if field_value != '' and field_value != u'': file_name = person_gs.name + " -- " + field_instance[ "altnames"] content_org = "" tags = "" field_value = save_file( field_value, file_name, user_id, group_id, content_org, tags, access_policy="PRIVATE", count=0, first_object="", oid=True)[0] else: # Other AttributeTypes if fi_name in request.POST: field_value = request.POST[fi_name] # field_instance_type = "GAttribute" if fi_name in [ "12_passing_year", "degree_passing_year" ]: #, "registration_year"]: field_value = parse_template_data( field_data_type, field_value, date_format_string="%Y") elif fi_name in ["dob", "registration_date"]: field_value = parse_template_data( field_data_type, field_value, date_format_string="%d/%m/%Y") registration_date = field_value else: field_value = parse_template_data( field_data_type, field_value, date_format_string="%d/%m/%Y %H:%M") if field_value: person_gs_triple_instance = create_gattribute( person_gs._id, node_collection.collection.AttributeType( field_instance), field_value) else: if field_instance["object_cardinality"] > 1: field_value_list = request.POST.get( fi_name, "") if "[" in field_value_list and "]" in field_value_list: field_value_list = json.loads( field_value_list) else: field_value_list = request.POST.getlist( fi_name) else: field_value_list = request.POST.getlist( fi_name) if META_TYPE[ 3] in field_instance.member_of_names_list: # If Binary relationship found # [id, id, ...] # field_instance_type = "GRelation" for i, field_value in enumerate( field_value_list): field_value = parse_template_data( field_data_type, field_value, field_instance=field_instance, date_format_string="%m/%d/%Y %H:%M") field_value_list[i] = field_value else: # Relationship Other than Binary one found; e.g, Triadic # [[id, id, ...], [id, id, ...], ...] # field_instance_type = "GRelation" for i, field_value_inner_list in enumerate( field_value_list): for j, field_value in enumerate( field_value_inner_list): field_value = parse_template_data( field_data_type, field_value, field_instance=field_instance, date_format_string="%m/%d/%Y %H:%M" ) field_value_list[i][j] = field_value person_gs_triple_instance = create_grelation( person_gs._id, node_collection.collection.RelationType( field_instance), field_value_list) # Setting enrollment code for student node only while creating it if create_student_enrollment_code: # Create enrollment code for student node only while registering a new node for rel in college_node.relation_set: if rel and "group_of" in rel: college_id = rel["group_of"][0] student_enrollment_code = get_student_enrollment_code( college_id, person_gs._id, registration_date, ObjectId(group_id)) enrollment_code_at = node_collection.one({ "_type": "AttributeType", "name": "enrollment_code" }) try: ga_node = create_gattribute(person_gs._id, enrollment_code_at, student_enrollment_code) except Exception as e: print "\n StudentEnrollmentCreateError: " + str(e) + "!!!" # [C] Code to link GSystem Node and Author node via "has_login" relationship; # and Subscribe the Author node to College group if user "Program Officer" person_gs.reload() auth_node = None for attr in person_gs.attribute_set: if "email_id" in attr: if attr["email_id"]: auth_node = node_collection.one({ '_type': "Author", 'email': attr["email_id"].lower() }) break if auth_node: has_login_rt = node_collection.one({ '_type': "RelationType", 'name': "has_login" }) if has_login_rt: # Linking GSystem Node and Author node via "has_login" relationship; gr_node = create_grelation(person_gs._id, has_login_rt, auth_node._id) # Set author_agency field's value of author node as "Program Officer" # Required to identify at time of log-in in order to display # required modules defined for Program Officers under MIS GApp if auth_node.agency_type != u"Program Officer": auth_node.agency_type = u"Program Officer" auth_node.save(groupid=group_id) if "Program Officer" in person_gs.member_of_names_list: # If Person node (GSystem) is of Program Officer type # then only go for subscription college_id_list = [] # Fetch College's ObjectId to which Program Officer is assigned (via "officer_incharge_of") for rel in person_gs.relation_set: if "officer_incharge_of" in rel: if rel["officer_incharge_of"]: for college_id in rel["officer_incharge_of"]: if college_id not in college_id_list: college_id_list.append(college_id) break # break outer-loop (of relation_set) if college_id_list: # If College's ObjectId exists (list as PO might be assigned to more than one college) # Then prepare a list of their corresponding private group(s) (via "has_group") college_cur = node_collection.find( {'_id': { '$in': college_id_list }}, {'relation_set.has_group': 1}) college_group_id_list = [] for college in college_cur: for rel in college.relation_set: if rel and "has_group" in rel: if rel["has_group"]: if rel["has_group"][ 0] not in college_group_id_list: college_group_id_list.append( rel["has_group"][0]) break # break inner-loop (college.relation_set) if college_group_id_list: # If college-group list exists # Then update their group_admin field (append PO's created_by) res = node_collection.collection.update( {'_id': { '$in': college_group_id_list }}, { '$addToSet': { 'group_admin': auth_node.created_by } }, upsert=False, multi=True) old_college_id_list = [] if existing_colg: if len(existing_colg) > len(college_id_list): for each_colg_id in existing_colg: if each_colg_id not in college_id_list: old_college_id_list.append(each_colg_id) old_college_cur = node_collection.find( {'_id': { '$in': old_college_id_list }}, {'relation_set.has_group': 1}) old_college_group_id_list = [] for college in old_college_cur: for rel in college.relation_set: if rel and "has_group" in rel: if rel["has_group"]: if rel["has_group"][ 0] not in old_college_group_id_list: old_college_group_id_list.append( rel["has_group"][0]) break # break inner-loop (college.relation_set) if old_college_group_id_list: # If college-group list exists # Then update their group_admin field (remove PO's created_by) res = node_collection.collection.update( { '_id': { '$in': old_college_group_id_list }, '$or': [{ 'group_admin': auth_node.created_by }, { 'author_set': auth_node.created_by }] }, { '$pull': { 'group_admin': auth_node.created_by, 'author_set': auth_node.created_by } }, upsert=False, multi=True) # The code below is commented as the college groups are PRIVATE. # for rel in person_gs.relation_set: # if rel and "officer_incharge_of" in rel: # pass # else: # node_collection.collection.update({'_id': auth_node._id}, # {'$set': {'agency_type': u"Other"}}, # upsert=False, multi=False # ) # Its purpose is to change the agency type back to Other auth_node.reload() # if person_gst.name != "Student": # return HttpResponseRedirect(reverse(app_name.lower()+":"+template_prefix+'_app_detail', kwargs={'group_id': group_id, "app_id":app_id, "app_set_id":app_set_id})) # else: return HttpResponseRedirect( reverse('mis:mis_app_instance_detail', kwargs={ 'group_id': group_id, "app_id": app_id, "app_set_id": app_set_id, "app_set_instance_id": unicode(person_gs._id) })) # return HttpResponseRedirect(reverse(app_name.lower()+":"+template_prefix+'_app_detail', kwargs={'group_id': group_id, "app_id":app_id, "app_set_id":app_set_id})) default_template = "ndf/person_create_edit.html" # default_template = "ndf/"+template_prefix+"_create_edit.html" context_variables = { 'groupid': group_id, 'group_id': group_id, 'app_id': app_id, 'app_name': app_name, 'app_collection_set': app_collection_set, 'app_set_id': app_set_id, 'title': title, 'property_order_list': property_order_list } if person_gst and person_gst.name in [ "Voluntary Teacher", "Master Trainer" ]: nussd_course_type = node_collection.one( { '_type': "AttributeType", 'name': "nussd_course_type" }, { '_type': 1, '_id': 1, 'data_type': 1, 'complex_data_type': 1, 'name': 1, 'altnames': 1 }) if nussd_course_type["data_type"] == "IS()": # Below code does little formatting, for example: # data_type: "IS()" complex_value: [u"ab", u"cd"] dt: # "IS(u'ab', u'cd')" dt = "IS(" for v in nussd_course_type.complex_data_type: dt = dt + "u'" + v + "'" + ", " dt = dt[:(dt.rfind(", "))] + ")" nussd_course_type["data_type"] = dt nussd_course_type["data_type"] = eval(nussd_course_type["data_type"]) nussd_course_type["value"] = None context_variables['nussd_course_type'] = nussd_course_type if app_set_instance_id: person_gs.get_neighbourhood(person_gs.member_of) if "trainer_teaches_course_in_college" in person_gs: l = [] for each_course_college in person_gs.trainer_teaches_course_in_college: # Fetch Course Type (i.e. nussd_course_type) ct = "" for each_attr in each_course_college[0].attribute_set: if "nussd_course_type" in each_attr and each_attr: ct = each_attr["nussd_course_type"] break univ_name = "" for each_rel in each_course_college[1].relation_set: if "college_affiliated_to" in each_rel and each_rel: univ = node_collection.find_one({ "_id": { "$in": each_rel["college_affiliated_to"] } }) univ_name = univ.name if univ else "" l.append((ct, each_course_college[0].name, each_course_college[1].name, each_course_college[0]._id.__str__(), each_course_college[1]._id.__str__(), univ_name)) if l: person_gs.trainer_teaches_course_in_college = l context_variables['node'] = person_gs try: return render_to_response([template, default_template], context_variables, context_instance=RequestContext(request)) except TemplateDoesNotExist as tde: error_message = "\n PersonCreateEditViewError: This html template (" + str( tde) + ") does not exists !!!\n" raise Http404(error_message) except Exception as e: error_message = "\n PersonCreateEditViewError: " + str(e) + " !!!\n" raise Exception(error_message)
def parse_data_create_gsystem(json_file_path, file_name): json_file_content = "" try: print "\n file_name == ",file_name with open(json_file_path) as json_file: json_file_content = json_file.read() json_documents_list = json.loads(json_file_content) # Process data in proper format node = node_collection.collection.GSystem() node_keys = node.keys() node_structure = node.structure json_documents_list_spaces = json_documents_list json_documents_list = [] # Removes leading and trailing spaces from keys as well as values for json_document_spaces in json_documents_list_spaces: json_document = {} for key_spaces, value_spaces in json_document_spaces.iteritems(): json_document[key_spaces.strip().lower()] = value_spaces.strip() json_documents_list.append(json_document) except Exception as e: error_message = "\n While parsing the file ("+json_file_path+") got following error...\n " + str(e) log_list.append(error_message) print error_message raise error_message for i, json_document in enumerate(json_documents_list): try: if file_name == "QuizItem.csv": print "\n\n *******************" question_content = json_document['content'] question_content = question_content.split(' ') question_content = question_content[:4] question_content = ' '.join(question_content) json_document['name'] = question_content json_document['altnames'] = json_document['content'] group_id = ObjectId(json_document['group_id']) group_obj = node_collection.one({'_id': group_id}) if group_obj: group_id = group_obj._id else: group_id = home_grp._id user_id = int(json_document['user_id']) print "\n\n NAME ======= ", json_document['name'], group_id, user_id global node_repeated node_repeated = False n_name = "" if "first name" in json_document: n_name = json_document["first name"] + " " if json_document["middle name"]: n_name += json_document["middle name"] if json_document["last name"]: n_name += " " n_name += json_document["last name"] json_document["name"] = n_name.title() info_message = "\n ============ #"+ str(i+1) +" : Start of "+gsystem_type_name+"'s GSystem ("+json_document['name']+") creation/updation ============\n" log_list.append(info_message) parsed_json_document = {} attribute_relation_list = [] for key in json_document.iterkeys(): # print "\n key ",key parsed_key = key.lower() parsed_key = parsed_key.replace(" ", "_") if parsed_key in node_keys: if node_structure[parsed_key] == unicode: parsed_json_document[parsed_key] = unicode(json_document[key]) elif node_structure[parsed_key] == datetime.datetime: parsed_json_document[parsed_key] = datetime.datetime.strptime(json_document[key], "%d/%m/%Y") else: parsed_json_document[parsed_key] = json_document[key] else: parsed_json_document[key] = json_document[key] attribute_relation_list.append(key) info_message = "\n Creating "+gsystem_type_name+" ("+parsed_json_document["name"]+")..." log_list.append(info_message) print "\n HERE == " node = create_edit_gsystem(gsystem_type_id, gsystem_type_name, parsed_json_document, user_id) print "\n node created === ", node._id, " === ", node.name, node.altnames # print "attribute_relation_list == ",attribute_relation_list if node: if not attribute_relation_list: # Neither possible attribute fields, nor possible relations defined for this node info_message = "\n "+gsystem_type_name+" ("+node.name+"): Neither possible attribute fields, nor possible relations defined for this node !\n" log_list.append(info_message) continue gst_possible_attributes_dict = node.get_possible_attributes(gsystem_type_id) print "\n gsystem_type_id ===",gst_possible_attributes_dict relation_list = [] json_document['name'] = node.name # Write code for setting atrributes for key in attribute_relation_list: is_relation = True for attr_key, attr_value in gst_possible_attributes_dict.iteritems(): # print "\n\n attr_key === ", attr_key # print "\n\n altnames -- === ", attr_value['altnames'] if attr_value['altnames'] and key == attr_value['altnames'].lower() or key == attr_key.lower(): is_relation = False if json_document[key]: try: if attr_value['data_type'] == basestring: if u"\u2013" in json_document[key]: json_document[key] = json_document[key].replace(u"\u2013", "-") info_message = "\n For GAttribute parsing content | key: " + attr_key + " -- " + json_document[key] log_list.append(info_message) if attr_value['data_type'] == unicode: json_document[key] = unicode(json_document[key]) elif attr_value['data_type'] == bool: if json_document[key].lower() == "yes": json_document[key] = True elif json_document[key].lower() == "no": json_document[key] = False else: json_document[key] = None elif attr_value['data_type'] == datetime.datetime: # Use small-case altnames if key in ["dob", "date of birth", "date of registration"]: if json_document[key]: json_document[key] = datetime.datetime.strptime(json_document[key], "%d/%m/%Y") else: if json_document[key]: json_document[key] = datetime.datetime.strptime(json_document[key], "%Y") elif attr_value['data_type'] in [int, float, long]: if not json_document[key]: json_document[key] = 0 else: if attr_value['data_type'] == int: json_document[key] = int(json_document[key]) elif attr_value['data_type'] == float: json_document[key] = float(json_document[key]) else: json_document[key] = long(json_document[key]) elif type(attr_value['data_type']) == IS: for op in attr_value['data_type']._operands: if op.lower() == json_document[key].lower(): json_document[key] = op elif (attr_value['data_type'] in [list, dict]) or (type(attr_value['data_type']) in [list, dict]): if "," not in json_document[key]: # Necessary to inform perform_eval_type() that handle this value as list json_document[key] = "\"" + json_document[key] + "\", " else: formatted_value = "" for v in json_document[key].split(","): formatted_value += "\""+v.strip(" ")+"\", " json_document[key] = formatted_value perform_eval_type(key, json_document, "GSystem") subject_id = node._id attribute_type_node = None if attr_key in attr_type_dict: attribute_type_node = attr_type_dict[attr_key] else: attribute_type_node = node_collection.one({ '_type': "AttributeType", '$or': [{ 'name': {'$regex': "^" + attr_key + "$", '$options': 'i'} }, { 'altnames': {'$regex': "^" + attr_key + "$", '$options': 'i'} }] }) attr_type_dict[attr_key] = attribute_type_node object_value = json_document[key] ga_node = None info_message = "\n Creating GAttribute (" + node.name + " -- " + attribute_type_node.name + " -- " + str(json_document[key]) + ") ...\n" log_list.append(info_message) ga_node = create_gattribute(subject_id, attribute_type_node, object_value) except Exception as e: error_message = "\n While creating GAttribute (" + attr_key + ") for "+gsystem_type_name+"'s GSystem ("+json_document['name']+") got following error...\n " + str(e) + "\n" log_list.append(error_message) print error_message # Keep it! # To break outer for loop as key found break else: error_message = "\n DataNotFound: No data found for field ("+attr_key+") while creating GSystem (" + gsystem_type_name + " -- " + node.name + ") !!!\n" log_list.append(error_message) if is_relation: relation_list.append(key) if not relation_list: # No possible relations defined for this node info_message = "\n "+gsystem_type_name+" ("+node.name+"): No possible relations defined for this node !!!\n" log_list.append(info_message) else: gst_possible_relations_dict = node.get_possible_relations(gsystem_type_id) # Write code for setting relations for key in relation_list: is_relation = True for rel_key, rel_value in gst_possible_relations_dict.iteritems(): if key == rel_value['altnames'].lower() or key == rel_key.lower(): is_relation = False if json_document[key]: # Here semi-colon(';') is used instead of comma(',') # Beacuse one of the value may contain comma(',') which causes problem in finding required value in database try: if ";" not in json_document[key]: # Necessary to inform perform_eval_type() that handle this value as list json_document[key] = "\""+json_document[key]+"\", " else: formatted_value = "" for v in json_document[key].split(";"): formatted_value += "\""+v.strip(" ")+"\", " json_document[key] = formatted_value info_message = "\n For GRelation parsing content | key: " + rel_key + " -- " + json_document[key] log_list.append(info_message) perform_eval_type(key, json_document, "GSystem", "GSystem") # for right_subject_id in json_document[key]: subject_id = node._id # Here we are appending list of ObjectIds of GSystemType's type_of field # along with the ObjectId of GSystemType's itself (whose GSystem is getting created) # This is because some of the RelationType's are holding Base class's ObjectId # and not that of the Derived one's # Delibrately keeping GSystemType's ObjectId first in the list # And hence, used $in operator in the query! rel_subject_type = [] rel_subject_type.append(gsystem_type_id) if gsystem_type_node.type_of: rel_subject_type.extend(gsystem_type_node.type_of) relation_type_node = None if rel_key in rel_type_dict: relation_type_node = rel_type_dict[rel_key] else: relation_type_node = node_collection.one({ '_type': "RelationType", '$or': [{ 'name': {'$regex': "^" + rel_key + "$", '$options': 'i'} }, { 'altnames': {'$regex': "^" + rel_key + "$", '$options': 'i'} }], 'subject_type': {'$in': rel_subject_type} }) rel_type_dict[rel_key] = relation_type_node info_message = "\n Creating GRelation ("+node.name+" -- "+rel_key+" -- "+str(json_document[key])+") ...\n" log_list.append(info_message) gr_node = create_grelation(subject_id, relation_type_node, json_document[key]) except Exception as e: error_message = "\n While creating GRelation (" + rel_key + ") for "+gsystem_type_name+"'s GSystem ("+json_document['name']+") got following error...\n" + str(e) + "\n" log_list.append(error_message) pass if college_gst._id in relation_type_node.object_type: # Fetch college node's group id # Append it to node's group_set node_group_set = node.group_set is_group_set_changed = False # Iterate through each college # Find it's corresponding group's ObjectId # Append it to node's group_set for each in json_document[key]: each = ObjectId(each) each_str = str(each) if each_str in college_dict: college_group_id = college_dict[each_str] if college_group_id not in node_group_set: node_group_set.append(college_group_id) is_group_set_changed = True else: # If not found in college_dict # Then find and update college_dict college_node = node_collection.collection.aggregate([{ "$match": {"_id": each} }, { "$project": {"group_id": "$relation_set.has_group"} }]) college_node = college_node["result"] if college_node: college_node = college_node[0] college_group_id = college_node["group_id"] if college_group_id: college_group_id = college_group_id[0][0] college_dict[each_str] = college_group_id node_group_set.append(college_group_id) is_group_set_changed = True # Update node's group_set with updated list # if changed if is_group_set_changed: node_collection.collection.update({ "_id": subject_id }, { "$set": {"group_set": node_group_set} }, upsert=False, multi=False ) # To break outer for loop if key found break else: error_message = "\n DataNotFound: No data found for relation ("+rel_key+") while creating GSystem ("+gsystem_type_name+" -- "+node.name+") !!!\n" log_list.append(error_message) # print error_message break # Create enrollment code (Only for Student) if create_student_enrollment_code and not node_repeated: enrollment_code_at = node_collection.one({ "_type": "AttributeType", "name": "enrollment_code" }) node_exist = node_collection.one({"_id": node._id, "attribute_set.enrollment_code": {"$exists": True}}) if not node_exist: # It means enrollment_code is not set for given student node # Then set it try: college_id = None group_id = None for k, v in college_dict.items(): college_id = ObjectId(k) group_id = ObjectId(v) student_enrollment_code = get_student_enrollment_code(college_id, node._id, json_document["date of registration"], group_id) info_message = "\n Creating GAttribute (" + node.name + " -- " + enrollment_code_at.name + " -- " + str(student_enrollment_code) + ") ...\n" log_list.append(info_message) ga_node = create_gattribute(node._id, enrollment_code_at, student_enrollment_code) except Exception as e: error_message = "\n StudentEnrollmentCreateError: " + str(e) + "!!!" log_list.append(error_message) elif create_private_college_group: # Create a private group for respective college node node_exist = node_collection.one({"_id": node._id, "relation_set.has_group": {"$exists": True}}) if not node_exist: try: info_message = "\n Creating private group for given college (" + node.name + ") via RelationType (has_group)...\n" log_list.append(info_message) college_group, college_group_gr = create_college_group_and_setup_data(node) except Exception as e: error_message = "\n CollegeGroupCreateError: " + str(e) + "!!!" log_list.append(error_message) except Exception as e: error_message = "\n While creating "+gsystem_type_name+"'s GSystem ("+json_document['name']+") got following error...\n " + str(e) log_list.append(error_message) print error_message # Keep it! import sys print "\n ****\n" print 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno)