Skip to content

Commit

Permalink
factorizing create_model functions (beginning)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienParis committed Jul 18, 2018
1 parent e533d86 commit 5b653f0
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 320 deletions.
50 changes: 27 additions & 23 deletions solidata_api/_core/queries_db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,35 @@

### declaring collections as app variables

mongo_users = mongo.db[ app.config["MONGO_COLL_USERS"] ]
mongo_licences = mongo.db[ app.config["MONGO_COLL_LICENCES"] ]
mongo_projects = mongo.db[ app.config["MONGO_COLL_PROJECTS"] ]
mongo_datamodels = mongo.db[ app.config["MONGO_COLL_DATAMODELS"] ]
mongo_datamodels_fields = mongo.db[ app.config["MONGO_COLL_DATAMODELS_FIELDS"] ]
mongo_connectors = mongo.db[ app.config["MONGO_COLL_CONNECTORS"] ]
mongo_datasets_inputs = mongo.db[ app.config["MONGO_COLL_DATASETS_INPUTS"] ]
mongo_datasets_outputs = mongo.db[ app.config["MONGO_COLL_DATASETS_OUTPUTS"] ]
mongo_recipes = mongo.db[ app.config["MONGO_COLL_RECIPES"] ]
mongo_corr_dicts = mongo.db[ app.config["MONGO_COLL_CORR_DICTS"] ]

mongo_jwt_blacklist = mongo.db[ app.config["MONGO_COLL_JWT_BLACKLIST"] ]
mongo_users = mongo.db[ app.config["MONGO_COLL_USERS"] ]
mongo_projects = mongo.db[ app.config["MONGO_COLL_PROJECTS"] ]
mongo_datamodels_templates = mongo.db[ app.config["MONGO_COLL_DATAMODELS_TEMPLATES"] ]
mongo_datamodels_fields = mongo.db[ app.config["MONGO_COLL_DATAMODELS_FIELDS"] ]
# mongo_connectors = mongo.db[ app.config["MONGO_COLL_CONNECTORS"] ]
mongo_datasets_inputs = mongo.db[ app.config["MONGO_COLL_DATASETS_INPUTS"] ]
mongo_recipes = mongo.db[ app.config["MONGO_COLL_RECIPES"] ]
# mongo_corr_dicts = mongo.db[ app.config["MONGO_COLL_CORR_DICTS"] ]
mongo_datasets_outputs = mongo.db[ app.config["MONGO_COLL_DATASETS_OUTPUTS"] ]

mongo_jwt_blacklist = mongo.db[ app.config["MONGO_COLL_JWT_BLACKLIST"] ]


# mongo_licences = mongo.db[ app.config["MONGO_COLL_LICENCES"] ]


db = {
"mongo_users" : mongo_users,
"mongo_licences" : mongo_licences,
"mongo_projects" : mongo_projects,
"mongo_datamodels" : mongo_datamodels,
"mongo_datamodels_fields" : mongo_datamodels_fields,
"mongo_connectors" : mongo_connectors,
"mongo_datasets_inputs" : mongo_datasets_inputs,
"mongo_datasets_outputs" : mongo_datasets_outputs,
"mongo_recipes" : mongo_recipes,
"mongo_corr_dicts" : mongo_corr_dicts,
"mongo_jwt_blacklist" : mongo_jwt_blacklist,
"mongo_users" : mongo_users,
"mongo_projects" : mongo_projects,
"mongo_datamodels_templates" : mongo_datamodels_templates,
"mongo_datamodels_fields" : mongo_datamodels_fields,
# "mongo_connectors" : mongo_connectors,
"mongo_datasets_inputs" : mongo_datasets_inputs,
"mongo_recipes" : mongo_recipes,
# "mongo_corr_dicts" : mongo_corr_dicts,

"mongo_datasets_outputs" : mongo_datasets_outputs,
# "mongo_licences" : mongo_licences,
"mongo_jwt_blacklist" : mongo_jwt_blacklist,
}

def select_collection(coll_name):
Expand Down
9 changes: 7 additions & 2 deletions solidata_api/_core/utils/app_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

from datetime import datetime, timedelta

def create_modif_log( doc, action, field="log", nested_field="modified_log", dt=datetime.utcnow(), by=None ) :
def create_modif_log( doc,
action,
field="log",
nested_field="modified_log",
dt=datetime.utcnow(),
by=None ) :
"""
Create a simple dict for modif_log
and insert it into document
Expand All @@ -14,7 +19,7 @@ def create_modif_log( doc, action, field="log", nested_field="modified_log", dt=

### add author of modif
if by != None :
modif["by"] = by
modif["modif_by"] = by

doc[field][nested_field].insert(0, modif)

Expand Down
130 changes: 104 additions & 26 deletions solidata_api/_models/models_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,150 @@



### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
### MODEL / BASIC INFOS
### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
def create_model_basic_infos(ns_, model_name,
schema=doc_basics,
schema = doc_basics,
is_user_infos = False,
) :

"""
"""

schema = doc_basics

if is_user_infos == True :
schema = user_basics

basic_infos = fields.Nested(
ns_.model( model_name , doc_basics )
ns_.model( model_name , schema )
)
return basic_infos



### MODEL TEAM
### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
### MODEL / TEAM
### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
def create_model_team(ns_, model_name="Collaborator"):


"""
"""

collaborator = fields.Nested(
ns_.model( model_name, {
'user_oid' : oid,
'auth_edit' : edit_auth
'oid_usr' : oid_usr,
'edit_auth' : edit_auth,
'added_at' : added_at,
'added_by' : oid_usr,
})
)

collaborators = fields.List(
collaborator,
description = "List of collaborators on this document",
description = "List of {}s on this document".format(model_name),
default = []
)
)

return collaborators


### MODIFICATIONS LOG
def create_model_modif_log(ns_, model_name,
schema = modification_full,
include_counts = False,
counts_name = "counts",
include_created_by = True,
include_is_running = False,

### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
### MODEL / DATASETS
### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
def create_model_datasets(ns_, model_name = "Datasets" ,
include_fav = False,
display_fullname = False ,
schema_list = ["prj", "dmt", "dmf", "dsi", "rec", "dso"],
) :
"""
"""

datasets_dict = {}

for schema in schema_list :

model_dataset = {
'oid_usr' : oid_dict[schema]["field"],
'added_at' : added_at,
'added_by' : oid_usr,
}

if include_fav == True :
model_dataset["is_fav"] = is_fav

dataset = fields.Nested(
ns_.model( schema.title(), model_dataset )
)

dataset_list = fields.List(
dataset,
description = "List of {}s on this document".format(oid_dict[schema]["fullname"]),
default = []
)

datasets_dict[schema] = dataset_list

datasets = fields.Nested(
ns_.model( model_name, datasets_dict )
)

return datasets



### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
### MODEL / MODIFICATIONS LOG
### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
def create_model_modif_log(ns_, model_name = "Modification",
schema = modification_full,
) :

"""
"""

### create the list of modifications
modifications = fields.List(
fields.Nested(
ns_.model('Modifications', schema )
ns_.model( model_name, schema )
),
description = "List of the modifications on this document",
description = "List of the {}s on this document".format(model_name),
default = []
)

return modifications


### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
### MODEL / SPECS
### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
def create_model_specs(ns_, model_name = "Specs",
include_counts = False,
counts_name = "counts",
include_created_by = True,
include_is_running = False,
include_is_loaded = False,
) :
"""
"""

log_base = {
specs_base = {
'created_at' : created_at,
'modified_log' : modifications
}

if include_created_by == True :
log_base['created_by'] = oid
specs_base['created_by'] = oid_usr

if include_counts == True :
log_base[ counts_name ] = count
specs_base[ counts_name ] = count

if include_is_running == True :
log_base[ "is_running" ] = is_running
specs_base[ "is_running" ] = is_running

### compile the document's log
doc_log = fields.Nested(
ns_.model( model_name, log_base )
doc_specs = fields.Nested(
ns_.model( model_name, specs_base )
)

return doc_log
return doc_specs
60 changes: 19 additions & 41 deletions solidata_api/_models/models_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from solidata_api._serializers.schema_projects import *

### import generic models functions
from solidata_api._models.models_generic import *
from solidata_api._models.models_generic import *

### create models from serializers
# nested models : https://github.com/noirbizarre/flask-restplus/issues/8
Expand All @@ -35,50 +35,28 @@ class Project_infos :
def __init__(self, ns_) :

### SELF MODULES
# self.basic_infos = fields.Nested(
# ns_.model('Project_infos', doc_basics )
# )
self.basic_infos = create_model_basic_infos(ns_, "Project_infos")

# self.modifications = fields.List(
# fields.Nested(
# ns_.model('Modifications_by', modification_full )
# ),
# default = []
# )
# self.project_log = fields.Nested(
# ns_.model('Project_log', {
# 'created_at' : created_at,
# 'modified_log' : self.modifications
# })
# )

self.project_log = create_model_modif_log(ns_, "Project_log", include_is_running=True)

# self.collaborator = fields.Nested(
# ns_.model('Collaborator', {
# 'user_oid' : oid,
# 'auth_edit' : edit_auth
# })
# )
self.collaborators = create_model_team(ns_)

self.basic_infos = create_model_basic_infos(ns_, "Project_infos")
self.modif_log = create_model_modif_log(ns_, "Project_modif_log")
self.specs = create_model_specs(ns_, include_is_running=True)
self.collaborators = create_model_team(ns_)
self.datasets = create_model_datasets(ns_, schema_list=["dmt","dsi","rec","dso"])

### IN / complete data to enter in DB
self.mod_complete_in = ns_.model('Project_in', {
self.mod_complete_in = ns_.model('Project_in', {

'infos' : self.basic_infos,
'log' : self.project_log ,
'infos' : self.basic_infos,
'specs' : self.specs ,
'modif_log' : self.modif_log ,

### team and edition levels
# 'proj_team' : fields.List(self.collaborator) ,
'proj_team' : self.collaborators ,
'prj_team' : self.collaborators ,

### datasets
'dm_t' : oid,
'ds_i' : fields.List(oid),
'dc_' : fields.List(oid),
'rec_' : fields.List(oid),
"datasets" : self.datasets ,
# 'dmt' : oid_dmt,
# 'dsi' : fields.List(oid_dsi),
# 'rec' : fields.List(oid_rec),
# 'dso' : fields.List(oid_dso),
})

### OUT / complete data to enter in DB
Expand All @@ -104,8 +82,8 @@ def __init__(self, ns_) :
def model_complete_in(self):
return self.mod_complete_in

@property
def model_complete_out(self):
return self.mod_complete_out
# @property
# def model_complete_out(self):
# return self.mod_complete_out


Loading

0 comments on commit 5b653f0

Please sign in to comment.