Skip to content

Commit

Permalink
factorizing models parts + more on projects models
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienParis committed Jul 12, 2018
1 parent debf9ed commit e533d86
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 149 deletions.
90 changes: 90 additions & 0 deletions solidata_api/_models/models_generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- encoding: utf-8 -*-

"""
_models/models_generic.py
- provides the models for all api routes
"""

from log_config import log, pformat

log.debug("... loading models_generic.py ...")


from flask_restplus import fields

### import data serializers
from solidata_api._serializers.schema_logs import *
from solidata_api._serializers.schema_generic import *
from solidata_api._serializers.schema_projects import *



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

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



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

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

collaborators = fields.List(
collaborator,
description = "List of collaborators on this document",
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,
) :

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

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

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

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

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

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

return doc_log
79 changes: 54 additions & 25 deletions solidata_api/_models/models_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from solidata_api._serializers.schema_generic import *
from solidata_api._serializers.schema_projects import *

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

### create models from serializers
# nested models : https://github.com/noirbizarre/flask-restplus/issues/8
Expand All @@ -32,24 +34,34 @@ class Project_infos :

def __init__(self, ns_) :

### SELF MODULES
self.basic_infos = fields.Nested(
ns_.model('Project_public_data', project_basics )
)

self.modifications = fields.List(
fields.Nested(
ns_.model('Modifications', modification )
),
default = []
)
self.project_log = fields.Nested(
ns_.model('Project_log', {
'created_at' : created_at,
# 'login_count' : count,
'modified_log' : self.modifications
})
)
### 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_)


### IN / complete data to enter in DB
Expand All @@ -58,17 +70,34 @@ def __init__(self, ns_) :
'infos' : self.basic_infos,
'log' : self.project_log ,

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

### datasets
'dm_t' : oid,
'ds_i' : fields.List(oid),
'dc_' : fields.List(oid),
'rec_' : fields.List(oid),
})

### OUT / complete data to enter in DB
self.mod_complete_out = ns_.model('Project_out', {

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


})
# self.mod_complete_out = ns_.model('Project_out', {

# 'infos' : self.basic_infos,
# 'log' : self.project_log ,

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

# ### datasets
# 'datamodel' : oid,
# 'dataset_inputs' : fields.List(oid),
# 'correspondance_dicts' : fields.List(oid),
# 'recipes' : fields.List(oid),

# })


@property
Expand Down
86 changes: 43 additions & 43 deletions solidata_api/_models/models_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from solidata_api._serializers.schema_generic import *
from solidata_api._serializers.schema_users import *

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

### create models from serializers
# nested models : https://github.com/noirbizarre/flask-restplus/issues/8
Expand All @@ -33,7 +35,7 @@ def __init__(self, ns_):
@property
def model(self):
return self.mod


class AnonymousUser :
"""
Expand Down Expand Up @@ -145,27 +147,45 @@ def __init__(self, ns_) :
ns_.model('User_profiles', user_profiles)
)

self.modifications = fields.List(
fields.Nested(
ns_.model('Modifications', modification )
),
default = []
)
self.user_log = fields.Nested(
ns_.model("User_log", {
'created_at' : created_at,
'login_count' : count,
'modified_log' : self.modifications
# self.modifications = fields.List(
# fields.Nested(
# ns_.model('Modifications', modification )
# ),
# default = []
# )
# self.user_log = fields.Nested(
# ns_.model("User_log", {
# 'created_at' : created_at,
# 'login_count' : count,
# 'modified_log' : self.modifications
# })
# )

self.user_log = create_model_modif_log(ns_, "User_log", schema=modification, include_counts=True, counts_name='login_count', include_created_by=False)

### favorites
self.fav_ = fields.Nested(
ns_.model("User_fav", {
'oid' : oid,
'doc_categ' : doc_categ,
'added_at' : created_at,
})
)
self.favorites = fields.List(
self.fav_ ,
description = "list of user's favorite documents",
attribute = "favorites",
default = []
)


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

'infos' : self.basic_infos,
'profile' : self.profiles,
'log' : self.user_log ,
'infos' : self.basic_infos,
'profile' : self.profiles,
'log' : self.user_log ,
'favorites' : self.favorites,

'auth': fields.Nested(
ns_.model('User_authorizations', user_auth_in )
Expand All @@ -184,9 +204,10 @@ def __init__(self, ns_) :
### OUT / complete data to enter in DB
self.mod_complete_out = ns_.model('User_out', {

'infos' : self.basic_infos,
'profile' : self.profiles,
'log' : self.user_log ,
'infos' : self.basic_infos,
'profile' : self.profiles,
'log' : self.user_log ,
'favorites' : self.favorites,

'auth' : fields.Nested(
ns_.model('User_authorizations', user_auth_out )
Expand All @@ -207,9 +228,10 @@ def __init__(self, ns_) :
### OUT / for access tokens
self.mod_access = ns_.model('User_access', {

'infos' : self.basic_infos,
# 'log' : self.user_log,
# 'profile' : self.profiles,
'infos' : self.basic_infos,
# 'log' : self.user_log,
# 'profile' : self.profiles,
# 'favorites' : self.favorites,

'auth' : fields.Nested(
ns_.model('User_authorizations', user_auth_out )
Expand All @@ -236,25 +258,3 @@ def model_complete_out(self):
def model_access(self):
return self.mod_access

# class User_in :

# def __init__(self, ns_) :

# self.mod = ns_.model('User', {
# 'infos': fields.Nested(
# ns_.model('User_public_data', user_basics )
# ),
# 'auth': fields.Nested(
# ns_.model('User_authorizations', user_auth )
# ),
# 'preferences': fields.Nested(
# ns_.model('User_preferences', user_preferences_in )
# ),
# 'datasets': fields.Nested(
# ns_.model('User_datasets', user_datasets_in )
# ),
# })

# @property
# def model(self):
# return self.mod
16 changes: 16 additions & 0 deletions solidata_api/_serializers/schema_datamodels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- encoding: utf-8 -*-

"""
schema_datamodels.py
- provides the model for DATAMODEL definition in DB and Flask-Restplus
"""

from log_config import log, pformat

log.debug("... loading schema_datamodels.py ...")

from flask_restplus import fields

from .schema_generic import *
from .schema_logs import *
from .schema_users import *
Loading

0 comments on commit e533d86

Please sign in to comment.