Ejemplo n.º 1
0
def organization_create_edit(request, group_id, app_id, app_set_id=None, app_set_instance_id=None, app_name=None):
  """
  Creates/Modifies document of given organization-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
  group_name, group_id = get_group_name_id(group_id)
  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)},{'_id':1, 'name':1})
  app_name = app.name

  # app_name = "mis"
  app_set = ""
  app_collection_set = []
  title = ""

  organization_gst = None
  organization_gs = 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}))

  if app_set_id:
    organization_gst = node_collection.one({'_type': "GSystemType", '_id': ObjectId(app_set_id)}, {'name': 1, 'type_of': 1})
    template = "ndf/organization_create_edit.html"
    title = organization_gst.name
    organization_gs = node_collection.collection.GSystem()
    organization_gs.member_of.append(organization_gst._id)

  if app_set_instance_id:
    organization_gs = node_collection.one({'_type': "GSystem", '_id': ObjectId(app_set_instance_id)})
  property_order_list = get_property_order_with_value(organization_gs)#.property_order
  if request.method == "POST":
    # [A] Save organization-node's base-field(s)
    is_changed = get_node_common_fields(request, organization_gs, group_id, organization_gst)

    if is_changed:
      # Remove this when publish button is setup on interface
      organization_gs.status = u"PUBLISHED"

    organization_gs.save(is_changed=is_changed,groupid=group_id)

    # [B] Store AT and/or RT field(s) of given organization-node (i.e., organization_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']})
          field_instance_type = type(field_instance)

          if field_instance_type in [AttributeType, RelationType]:
            if field_instance["name"] == "attendees":
              continue

            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
                
                if field_instance["name"] in request.FILES:
                  field_value = request.FILES[field_instance["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 = organization_gs.name + " -- " + field_instance["altnames"]
                  content_org = ""
                  tags = ""
                  field_value = save_file(field_value, file_name, request.user.id, group_id, content_org, tags, access_policy="PRIVATE", count=0, first_object="", oid=True)[0]

              else:
                # Other AttributeTypes 
                field_value = request.POST[field_instance["name"]]

              # field_instance_type = "GAttribute"
              if field_instance["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 field_instance["name"] in ["dob", "registration_date"]:
                field_value = parse_template_data(field_data_type, field_value, date_format_string="%d/%m/%Y")
              else:
                field_value = parse_template_data(field_data_type, field_value, date_format_string="%d/%m/%Y %H:%M")

              if field_value:
                organization_gs_triple_instance = create_gattribute(organization_gs._id, node_collection.collection.AttributeType(field_instance), field_value)

            else:
              if field_instance["object_cardinality"] > 1:
                field_value_list = request.POST.get(field_instance["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(field_instance["name"])

              else:
                field_value_list = request.POST.getlist(field_instance["name"])

              # 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

              organization_gs_triple_instance = create_grelation(organization_gs._id, node_collection.collection.RelationType(field_instance), field_value_list)

    # [C] Create private group only for College GSystems
    if "College" in organization_gs.member_of_names_list:
      # Create a group for respective college node
      college_group, college_group_gr = create_college_group_and_setup_data(organization_gs)

    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/"+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 app_set_instance_id:
    #   organization_gs.get_neighbourhood(organization_gs.member_of)
    #   context_variables['node'] = organization_gs
    context_variables['node_id'] = organization_gs._id
    context_variables['node_name'] = organization_gs.name

  try:
    return render_to_response(template,
                              context_variables,
                              context_instance = RequestContext(request)
                            )

  except TemplateDoesNotExist as tde:
    error_message = "\n OrganizationCreateEditViewError: This html template (" + str(tde) + ") does not exists !!!\n"
    raise Http404(error_message)

  except Exception as e:
    error_message = "\n OrganizationCreateEditViewError: " + str(e) + " !!!\n"
    raise Exception(error_message)
Ejemplo n.º 2
0
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)