Skip to content

Commit

Permalink
save templates on mongodb initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
lazarocosta committed May 1, 2020
1 parent 6d06d46 commit 8501175
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 130 deletions.
11 changes: 9 additions & 2 deletions src/GCF/decorators/OntologyClass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import datetime
import importlib
import json
from marshmallow_jsonschema import JSONSchema


def decorator_schema(cls):
def getSchema(self):
json_schema = JSONSchema()
return json_schema.dump(self)
setattr(cls, 'getSchema', getSchema)
return cls


def ontology_class(cls):
Expand Down
5 changes: 3 additions & 2 deletions src/Models/CRM/v5_0_2/NodeEntities/E1_CRM_Entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
UniqueIdProperty,
db,
)
from src.GCF.decorators.OntologyClass import ontology_class
from src.GCF.decorators.OntologyClass import ontology_class, decorator_schema
from src.Models.CRM.v5_0_2.NodeProperties.has_value import has_value
from src.Models.CRM.v5_0_2.NodeProperties.P1_is_identified_by import P1_is_identified_by
from src.Models.CRM.v5_0_2.NodeProperties.P2_has_type import P2_has_type
Expand All @@ -21,7 +21,7 @@
)
from src.Models.DataObject.v0_0_2.SuperClass import SuperClass


@decorator_schema
class E1_CRM_EntitySchema(Schema):
uid = fields.String()
name = fields.String(required=True)
Expand Down Expand Up @@ -81,3 +81,4 @@ def __init__(self, schema=None, *args, **kwargs):
schema = E1_CRM_EntitySchema()

SuperClass.__init__(self, schema)

21 changes: 2 additions & 19 deletions src/Models/DataObject/v0_0_2/DataObject.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

from marshmallow import Schema, fields
from neomodel import StringProperty, StructuredNode, UniqueIdProperty
from src.GCF.decorators.OntologyClass import decorator_schema
from src.Models.DataObject.v0_0_2.SuperClass import SuperClass


@decorator_schema
class DataObjectSchema(Schema):
uid = fields.String()
name = fields.String(required=True)
Expand All @@ -22,21 +23,3 @@ def __init__(self, schema=None, *args, **kwargs):




var = {
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"PersonNameSchema": {
"properties": {
"name": {"title": "name", "type": "string"},
"person_name": {"title": "person_name", "type": "string"},
"stringValue": {"title": "stringValue", "type": "string"},
"uid": {"title": "uid", "type": "string"},
},
"required": ["name", "person_name", "stringValue"],
"type": "object",
"additionalProperties": False,
}
},
"$ref": "#/definitions/PersonNameSchema",
}
23 changes: 14 additions & 9 deletions src/Models/DataObject/v0_0_2/SuperClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,20 @@ def __get_schema_with_template_aux(self, definitions, json_template, new_json_sc
else:
entity_name = list(json_template.keys())[0]
current_entity = entity_name + "Schema"
entity = {
'type': definitions[current_entity]['type'],
'properties': {},
'additionalProperties': definitions[current_entity]['additionalProperties'],
}
if 'required' in definitions[current_entity].keys():
entity['required'] = definitions[current_entity]['required']

new_json_schema['definitions'][current_entity] = entity
entity = {}
get_entity = new_json_schema["definitions"].get(current_entity, None)
if get_entity is not None:
entity = new_json_schema["definitions"][current_entity]
else:
entity = {
'type': definitions[current_entity]['type'],
'properties': {},
'additionalProperties': definitions[current_entity]['additionalProperties'],
}
if 'required' in definitions[current_entity].keys():
entity['required'] = definitions[current_entity]['required']

new_json_schema['definitions'][current_entity] = entity

properties = definitions[current_entity]['properties']

Expand Down
74 changes: 49 additions & 25 deletions src/Routes/mongo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from datetime import datetime
from src.Utils.Utils import find_name_of_class_schema_in_project


from pymongo import MongoClient
Expand All @@ -8,13 +9,7 @@
db = client.mydatabase
date_now = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")

# def read_file(path_file):
# file = open(path_file, "r")
# file_to_json = json.loads(file.read())
# print(file_to_json)
# return file_to_json
#
#

# def insert_into_mongo(content):
# result = {}
# if isinstance(content, type([])):
Expand All @@ -28,6 +23,19 @@
# cursor = db.reviews.find({})
# for document in cursor:
# print(document)
def populate_template_collection(objectjson):

for object in objectjson:
class_name = object["className"]

class_in_project = find_name_of_class_schema_in_project(class_name)
if class_in_project is None:
print(class_name + " did't found")
template = object["template"]
schema = class_in_project().getSchema()
schema_str = json.dumps(schema)
db.defaultTemplate.insert_one({"class_name": class_name, "template": template, "schema": schema_str})
return


def update_data_in_mongo(uid, data):
Expand All @@ -42,42 +50,55 @@ def update_data_in_mongo(uid, data):
print("updated")


def update_template_in_mongo(classes_name, template):
template_str = json.dumps(template)
def update_schema_in_mongo(classes_name, schema):
schema_str = json.dumps(schema)
date = datetime.now()
date_str = date.strftime("%Y-%m-%d, %H:%M:%S")
message = ""
if db.template.count_documents({'classes_name': {'$in': classes_name}}) == 0:
result = get_schema_from_mongo(classes_name)
if result is None:
message = "was inserted new template"
print(message)
db.template.insert_one({'classes_name': classes_name, "timestamp": date_str, "template": template_str})
db.template.insert_one({'classes_name': classes_name, "timestamp": date_str, "schema": schema_str})
return message
elif result['match'] < len(classes_name):
db.template.insert_one({'classes_name': classes_name, "timestamp": date_str, "schema": schema_str})
message = "inserted new template"
else:
db.data.find_one_and_replace({"_id": result['_id']}, {'classes_name': classes_name, "timestamp": date_str, "schema": schema})
message = "updated template"
return message


def get_schema_from_mongo(classes_name):
if db.template.count_documents({'classes_name': {'$in': classes_name}}) == 0:
return None
else:
result = db.template.find({'classes_name': {'$in': classes_name}})

elements_match_records = {
'_id': "",
'match': 0,
'template': {}
'schema': {}
}
for document in result:
merged = set(classes_name) & set(document['classes_name'])
if len(merged) > elements_match_records["match"]:
elements_match_records['match'] = len(merged)
elements_match_records['template'] = document
elements_match_records['schema'] = document
elements_match_records['_id'] = document['_id']
return elements_match_records

if elements_match_records['match'] < len(classes_name):
db.template.insert_one({'classes_name': classes_name, "timestamp": date_str, "template": template_str})
elements_match_records['match'] = len(classes_name)
elements_match_records['template'] = template
message = "inserted new template"
print(message)
else:
db.data.find_one_and_replace({"_id": elements_match_records['_id']}, {'classes_name': classes_name, "timestamp": date_str, "template": template})
message = "updated template"
print(message)
return message

def get_record_from_collection(uid, collection):
record = db[collection].find_one({"uid": uid})
return record


def add_record_to_collection(uid, data, collection):
date = datetime.now()
date_str = date.strftime("%Y-%m-%d, %H:%M:%S")
db[collection].insert_one({"uid": uid, "data": data, "timestamp": date_str})
return


def get_all_records_from_collection(collection):
Expand All @@ -92,6 +113,8 @@ def delete_collection(collection):


delete_collection("data")
delete_collection("template")

# unique = datetime.now()
# #update_data(5552,"aaa")
# class_name = ['ola', '1', '2', '3']
Expand All @@ -106,3 +129,4 @@ def delete_collection(collection):
#
# print("get all records")
# get_all_records_from_collection("template")
#get_all_records_from_collection("data")
Loading

0 comments on commit 8501175

Please sign in to comment.