Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
clinicaltrials
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
david Chavalarias
clinicaltrials
Commits
3f2554e9
Commit
3f2554e9
authored
Feb 23, 2017
by
Romain Loth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
esthetic (clarify comments and var names) + generalize passing db connection as arg
parent
9d295310
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
79 deletions
+127
-79
db.py
services/db.py
+100
-56
main.py
services/main.py
+24
-19
user.py
services/user.py
+2
-3
comex_user_shared_auth.js
static/js/comex_user_shared_auth.js
+1
-1
No files found.
services/db.py
View file @
3f2554e9
...
@@ -68,7 +68,7 @@ def connect_db(config=REALCONFIG):
...
@@ -68,7 +68,7 @@ def connect_db(config=REALCONFIG):
"""
"""
Simple connection
Simple connection
TODO decide if we'll use one or multiple (<= atm multiple
)
By default we use one new connection per function, but it can be passed to prevent that (in which case it should be closed at the end
)
"""
"""
return
connect
(
return
connect
(
host
=
config
[
'SQL_HOST'
],
host
=
config
[
'SQL_HOST'
],
...
@@ -79,10 +79,14 @@ def connect_db(config=REALCONFIG):
...
@@ -79,10 +79,14 @@ def connect_db(config=REALCONFIG):
charset
=
"utf8"
charset
=
"utf8"
)
)
def
doors_uid_to_luid
(
doors_uid
):
def
doors_uid_to_luid
(
doors_uid
,
cmx_db
=
None
):
"""
"""
Find corresponding luid
Find corresponding luid
"""
"""
if
cmx_db
:
db
=
cmx_db
else
:
db
=
connect_db
()
db
=
connect_db
()
db_c
=
db
.
cursor
()
db_c
=
db
.
cursor
()
...
@@ -95,19 +99,24 @@ def doors_uid_to_luid(doors_uid):
...
@@ -95,19 +99,24 @@ def doors_uid_to_luid(doors_uid):
luid
=
None
luid
=
None
if
n_rows
>
1
:
if
n_rows
>
1
:
if
not
cmx_db
:
db
.
close
()
db
.
close
()
raise
ValueError
(
"non unique doors_uid
%
s"
%
doors_uid
)
raise
ValueError
(
"non unique doors_uid
%
s"
%
doors_uid
)
elif
n_rows
==
1
:
elif
n_rows
==
1
:
luid
=
db_c
.
fetchone
()[
0
]
luid
=
db_c
.
fetchone
()[
0
]
if
not
cmx_db
:
db
.
close
()
db
.
close
()
return
luid
return
luid
def
email_exists
(
email
):
def
email_exists
(
email
,
cmx_db
=
None
):
"""
"""
Tests if there is already a user with this email
Tests if there is already a user with this email
"""
"""
if
cmx_db
:
db
=
cmx_db
else
:
db
=
connect_db
()
db
=
connect_db
()
db_c
=
db
.
cursor
()
db_c
=
db
.
cursor
()
...
@@ -119,6 +128,8 @@ def email_exists(email):
...
@@ -119,6 +128,8 @@ def email_exists(email):
n_rows
=
db_c
.
execute
(
stmt
)
n_rows
=
db_c
.
execute
(
stmt
)
exi_bool
=
(
n_rows
>=
1
)
exi_bool
=
(
n_rows
>=
1
)
if
not
cmx_db
:
db
.
close
()
db
.
close
()
return
exi_bool
return
exi_bool
...
@@ -270,12 +281,16 @@ def get_field_aggs(a_field,
...
@@ -270,12 +281,16 @@ def get_field_aggs(a_field,
return
agg_rows
return
agg_rows
def
rm_scholar
(
luid
):
def
rm_scholar
(
luid
,
cmx_db
=
None
):
"""
"""
Remove a scholar by id
Remove a scholar by id
(removals from sch_kw and sch_ht maps are triggered by cascade)
(removals from sch_kw and sch_ht maps are triggered by cascade)
"""
"""
if
cmx_db
:
db
=
cmx_db
else
:
db
=
connect_db
()
db
=
connect_db
()
db_c
=
db
.
cursor
()
db_c
=
db
.
cursor
()
stmt
=
'DELETE FROM scholars WHERE luid =
%
s'
%
str
(
luid
)
stmt
=
'DELETE FROM scholars WHERE luid =
%
s'
%
str
(
luid
)
...
@@ -283,10 +298,11 @@ def rm_scholar(luid):
...
@@ -283,10 +298,11 @@ def rm_scholar(luid):
dbresp
=
db_c
.
execute
(
stmt
)
dbresp
=
db_c
.
execute
(
stmt
)
db
.
commit
()
db
.
commit
()
mlog
(
'INFO'
,
'deleted user
%
i at his request'
%
int
(
luid
))
mlog
(
'INFO'
,
'deleted user
%
i at his request'
%
int
(
luid
))
if
not
cmx_db
:
db
.
close
()
db
.
close
()
def
get_full_scholar
(
uid
):
def
get_full_scholar
(
uid
,
cmx_db
=
None
):
"""
"""
uid : str
uid : str
local user id aka luid
local user id aka luid
...
@@ -295,9 +311,13 @@ def get_full_scholar(uid):
...
@@ -295,9 +311,13 @@ def get_full_scholar(uid):
=> Retrieves one line from *scholars* table, with joined optional concatenated *affiliations*, *keywords* and *linked_ids*
=> Retrieves one line from *scholars* table, with joined optional concatenated *affiliations*, *keywords* and *linked_ids*
=> Parse it all into a structured python user info dict
=> Parse it all into a structured python user info dict
=> NB: None if user doesn't exist in c
ome
x_db (but may exist in doors db)
=> NB: None if user doesn't exist in c
m
x_db (but may exist in doors db)
"""
"""
u_row
=
None
u_row
=
None
if
cmx_db
:
db
=
cmx_db
else
:
db
=
connect_db
()
db
=
connect_db
()
db_c
=
db
.
cursor
(
DictCursor
)
db_c
=
db
.
cursor
(
DictCursor
)
...
@@ -388,15 +408,20 @@ def get_full_scholar(uid):
...
@@ -388,15 +408,20 @@ def get_full_scholar(uid):
if
n_rows
>
1
:
if
n_rows
>
1
:
raise
IndexError
(
"DB one_usr_stmt returned
%
i rows instead of 1 for user
%
s"
%
(
n_rows
,
uid
))
raise
IndexError
(
"DB one_usr_stmt returned
%
i rows instead of 1 for user
%
s"
%
(
n_rows
,
uid
))
elif
n_rows
==
0
:
mlog
(
"WARNING"
,
"DB get_full_scholar attempt got no rows for:
%
s"
%
uid
)
urow_dict
=
db_c
.
fetchone
()
# we won't use the connect
if
not
cmx_db
:
db
.
close
()
db
.
close
()
# break with None if no results
if
urow_dict
is
None
:
mlog
(
"WARNING"
,
"DB get_full_scholar attempt got no rows for:
%
s"
%
uid
)
return
None
return
None
# normal case: we got exactly 1 user
# normal case <=> exactly one row
urow_dict
=
db_c
.
fetchone
()
db
.
close
()
# Exemple data in urow_dict
# Exemple data in urow_dict
# --------------------------
# --------------------------
...
@@ -406,15 +431,15 @@ def get_full_scholar(uid):
...
@@ -406,15 +431,15 @@ def get_full_scholar(uid):
# 'home_url': 'http://localhost/regcomex/', 'hon_title': 'Student',
# 'home_url': 'http://localhost/regcomex/', 'hon_title': 'Student',
# 'initials': 'JFK', 'interests_text': 'Blablabla',
# 'initials': 'JFK', 'interests_text': 'Blablabla',
# 'job_looking_date': '2019_09_28T22:00:00.000Z',
# 'job_looking_date': '2019_09_28T22:00:00.000Z',
# '
keywords': 'complex networks,complex systems,text mining,machine learning'
,
# '
hashtags': '#eccs15', 'hashtags_nb': 1
,
# 'keywords_nb': 4,
# 'keywords
': 'complex networks,complex systems,text mining,machine learning', 'keywords
_nb': 4,
# 'last_modified_date': '2016-12-07T15:56:09.721Z',
# 'last_modified_date': '2016-12-07T15:56:09.721Z',
# 'last_name': 'Kennedy',
# 'last_name': 'Kennedy',
# 'linked_ids': '
yoyo:42,foobar:XWING', 'linked_ids_nb': 2
,
# 'linked_ids': '
twitter:@jfk,yoyo:42,foobar:XWING', 'linked_ids_nb': 3
,
# 'middle_name': 'Fitzgerald',
# 'middle_name': 'Fitzgerald',
# 'org': 'Centre National de la Recherche Scientifique (CNRS)',
# 'org': 'Centre National de la Recherche Scientifique (CNRS)',
# 'org_city': 'Paris', 'org_type': 'public R&D org',
# 'org_city': 'Paris', 'org_type': 'public R&D org',
# 'pic_fname': '12345.jpg', 'pic_url': None, 'position': '
Engineer
',
# 'pic_fname': '12345.jpg', 'pic_url': None, 'position': '
Research Fellow
',
# 'record_status': None, 'team_lab': 'ISCPIF'}
# 'record_status': None, 'team_lab': 'ISCPIF'}
...
@@ -458,7 +483,7 @@ def get_full_scholar(uid):
...
@@ -458,7 +483,7 @@ def get_full_scholar(uid):
return
urow_dict
return
urow_dict
def
find_scholar
(
some_key
,
some_str_value
):
def
find_scholar
(
some_key
,
some_str_value
,
cmx_db
=
None
):
"""
"""
Get the luid of a scholar based on some str value
Get the luid of a scholar based on some str value
...
@@ -466,6 +491,10 @@ def find_scholar(some_key, some_str_value):
...
@@ -466,6 +491,10 @@ def find_scholar(some_key, some_str_value):
but this function doesn't check it !
but this function doesn't check it !
"""
"""
luid
=
None
luid
=
None
if
cmx_db
:
db
=
cmx_db
else
:
db
=
connect_db
()
db
=
connect_db
()
db_c
=
db
.
cursor
(
DictCursor
)
db_c
=
db
.
cursor
(
DictCursor
)
...
@@ -479,11 +508,14 @@ def find_scholar(some_key, some_str_value):
...
@@ -479,11 +508,14 @@ def find_scholar(some_key, some_str_value):
luid
=
first_row
[
'luid'
]
luid
=
first_row
[
'luid'
]
except
:
except
:
mlog
(
'WARNING'
,
'unsuccessful attempt to identify a scholar on key
%
s'
%
some_key
)
mlog
(
'WARNING'
,
'unsuccessful attempt to identify a scholar on key
%
s'
%
some_key
)
if
not
cmx_db
:
db
.
close
()
db
.
close
()
return
luid
return
luid
def
save_full_scholar
(
safe_recs
,
reg
_db
,
uactive
=
True
,
update_user
=
None
):
def
save_full_scholar
(
safe_recs
,
cmx
_db
,
uactive
=
True
,
update_user
=
None
):
"""
"""
For new registration:
For new registration:
-> add to *scholars* table, return new local uid
-> add to *scholars* table, return new local uid
...
@@ -544,7 +576,7 @@ def save_full_scholar(safe_recs, reg_db, uactive=True, update_user=None):
...
@@ -544,7 +576,7 @@ def save_full_scholar(safe_recs, reg_db, uactive=True, update_user=None):
db_tgtcols
.
append
(
'record_status'
)
db_tgtcols
.
append
(
'record_status'
)
db_qstrvals
.
append
(
'"active"'
)
db_qstrvals
.
append
(
'"active"'
)
reg_db_c
=
reg
_db
.
cursor
()
cmx_db_c
=
cmx
_db
.
cursor
()
if
not
update_user
:
if
not
update_user
:
# expected colnames "(doors_uid, last_modified_date, email, ...)"
# expected colnames "(doors_uid, last_modified_date, email, ...)"
...
@@ -569,16 +601,16 @@ def save_full_scholar(safe_recs, reg_db, uactive=True, update_user=None):
...
@@ -569,16 +601,16 @@ def save_full_scholar(safe_recs, reg_db, uactive=True, update_user=None):
mlog
(
"DEBUG"
,
"UPDATE"
if
update_user
else
"INSERT"
,
"SQL statement:"
,
full_statmt
)
mlog
(
"DEBUG"
,
"UPDATE"
if
update_user
else
"INSERT"
,
"SQL statement:"
,
full_statmt
)
reg
_db_c
.
execute
(
full_statmt
)
cmx
_db_c
.
execute
(
full_statmt
)
if
not
update_user
:
if
not
update_user
:
luid
=
reg
_db_c
.
lastrowid
luid
=
cmx
_db_c
.
lastrowid
else
:
else
:
luid
=
update_user
[
'luid'
]
luid
=
update_user
[
'luid'
]
reg
_db
.
commit
()
cmx
_db
.
commit
()
return
luid
return
luid
def
update_scholar_cols
(
selected_safe_recs
,
reg
_db
,
where_luid
=
None
):
def
update_scholar_cols
(
selected_safe_recs
,
cmx
_db
,
where_luid
=
None
):
"""
"""
For modification of selected columns:
For modification of selected columns:
-> *update* row with the values that are present and are real columns
-> *update* row with the values that are present and are real columns
...
@@ -612,7 +644,7 @@ def update_scholar_cols(selected_safe_recs, reg_db, where_luid=None):
...
@@ -612,7 +644,7 @@ def update_scholar_cols(selected_safe_recs, reg_db, where_luid=None):
db_tgtcols
.
append
(
colname
)
db_tgtcols
.
append
(
colname
)
db_qstrvals
.
append
(
quotedstrval
)
db_qstrvals
.
append
(
quotedstrval
)
reg_db_c
=
reg
_db
.
cursor
()
cmx_db_c
=
cmx
_db
.
cursor
()
set_full_str
=
','
.
join
([
db_tgtcols
[
i
]
+
'='
+
db_qstrvals
[
i
]
for
i
in
range
(
len
(
db_tgtcols
))])
set_full_str
=
','
.
join
([
db_tgtcols
[
i
]
+
'='
+
db_qstrvals
[
i
]
for
i
in
range
(
len
(
db_tgtcols
))])
# UPDATE: full_statement with formated values
# UPDATE: full_statement with formated values
...
@@ -620,35 +652,35 @@ def update_scholar_cols(selected_safe_recs, reg_db, where_luid=None):
...
@@ -620,35 +652,35 @@ def update_scholar_cols(selected_safe_recs, reg_db, where_luid=None):
set_full_str
,
set_full_str
,
where_luid
where_luid
)
)
reg
_db_c
.
execute
(
full_statmt
)
cmx
_db_c
.
execute
(
full_statmt
)
reg
_db
.
commit
()
cmx
_db
.
commit
()
return
where_luid
return
where_luid
def
save_pairs_sch_tok
(
pairings_list
,
c
ome
x_db
,
map_table
=
'sch_kw'
):
def
save_pairs_sch_tok
(
pairings_list
,
c
m
x_db
,
map_table
=
'sch_kw'
):
"""
"""
Simply save all pairings (luid, kwid) or (luid, htid) in the list
Simply save all pairings (luid, kwid) or (luid, htid) in the list
"""
"""
db_cursor
=
c
ome
x_db
.
cursor
()
db_cursor
=
c
m
x_db
.
cursor
()
for
id_pair
in
pairings_list
:
for
id_pair
in
pairings_list
:
db_cursor
.
execute
(
'INSERT INTO
%
s VALUES
%
s'
%
(
map_table
,
str
(
id_pair
)))
db_cursor
.
execute
(
'INSERT INTO
%
s VALUES
%
s'
%
(
map_table
,
str
(
id_pair
)))
c
ome
x_db
.
commit
()
c
m
x_db
.
commit
()
mlog
(
"DEBUG"
,
"
%
s: saved
%
s pair"
%
(
map_table
,
str
(
id_pair
)))
mlog
(
"DEBUG"
,
"
%
s: saved
%
s pair"
%
(
map_table
,
str
(
id_pair
)))
def
delete_pairs_sch_tok
(
uid
,
c
ome
x_db
,
map_table
=
'sch_kw'
):
def
delete_pairs_sch_tok
(
uid
,
c
m
x_db
,
map_table
=
'sch_kw'
):
"""
"""
Simply deletes all pairings (luid, *) in the table
Simply deletes all pairings (luid, *) in the table
"""
"""
if
map_table
not
in
[
'sch_kw'
,
'sch_ht'
]:
if
map_table
not
in
[
'sch_kw'
,
'sch_ht'
]:
raise
TypeError
(
'ERROR: Unknown map_table'
)
raise
TypeError
(
'ERROR: Unknown map_table'
)
db_cursor
=
c
ome
x_db
.
cursor
()
db_cursor
=
c
m
x_db
.
cursor
()
n
=
db_cursor
.
execute
(
'DELETE FROM
%
s WHERE uid = "
%
s"'
%
(
map_table
,
uid
))
n
=
db_cursor
.
execute
(
'DELETE FROM
%
s WHERE uid = "
%
s"'
%
(
map_table
,
uid
))
c
ome
x_db
.
commit
()
c
m
x_db
.
commit
()
mlog
(
"DEBUG"
,
"
%
s: DELETED
%
i pairings for
%
s"
%
(
map_table
,
n
,
str
(
uid
)))
mlog
(
"DEBUG"
,
"
%
s: DELETED
%
i pairings for
%
s"
%
(
map_table
,
n
,
str
(
uid
)))
def
get_or_create_tokitems
(
tok_list
,
c
ome
x_db
,
tok_table
=
'keywords'
):
def
get_or_create_tokitems
(
tok_list
,
c
m
x_db
,
tok_table
=
'keywords'
):
"""
"""
kw_str -> lookup/add to *keywords* table -> kw_id
kw_str -> lookup/add to *keywords* table -> kw_id
ht_str -> lookup/add to *hashtags* table -> ht_id
ht_str -> lookup/add to *hashtags* table -> ht_id
...
@@ -675,7 +707,7 @@ def get_or_create_tokitems(tok_list, comex_db, tok_table='keywords'):
...
@@ -675,7 +707,7 @@ def get_or_create_tokitems(tok_list, comex_db, tok_table='keywords'):
fill
[
'idc'
]
=
'htid'
fill
[
'idc'
]
=
'htid'
fill
[
'strc'
]
=
'htstr'
fill
[
'strc'
]
=
'htstr'
db_cursor
=
c
ome
x_db
.
cursor
()
db_cursor
=
c
m
x_db
.
cursor
()
found_ids
=
[]
found_ids
=
[]
for
tok_str
in
tok_list
:
for
tok_str
in
tok_list
:
...
@@ -695,7 +727,7 @@ def get_or_create_tokitems(tok_list, comex_db, tok_table='keywords'):
...
@@ -695,7 +727,7 @@ def get_or_create_tokitems(tok_list, comex_db, tok_table='keywords'):
# ex: INSERT INTO keywords(kwstr) VALUES ("complexity")
# ex: INSERT INTO keywords(kwstr) VALUES ("complexity")
db_cursor
.
execute
(
'INSERT INTO
%(tb)
s(
%(strc)
s) VALUES ("
%(q)
s")'
%
fill
)
db_cursor
.
execute
(
'INSERT INTO
%(tb)
s(
%(strc)
s) VALUES ("
%(q)
s")'
%
fill
)
c
ome
x_db
.
commit
()
c
m
x_db
.
commit
()
mlog
(
"INFO"
,
"Added '
%
s' to
%
s table"
%
(
tok_str
,
tok_table
))
mlog
(
"INFO"
,
"Added '
%
s' to
%
s table"
%
(
tok_str
,
tok_table
))
...
@@ -706,7 +738,7 @@ def get_or_create_tokitems(tok_list, comex_db, tok_table='keywords'):
...
@@ -706,7 +738,7 @@ def get_or_create_tokitems(tok_list, comex_db, tok_table='keywords'):
return
found_ids
return
found_ids
def
get_or_create_affiliation
(
org_info
,
c
ome
x_db
):
def
get_or_create_affiliation
(
org_info
,
c
m
x_db
):
"""
"""
(parent organization + lab) ---> lookup/add to *affiliations* table -> affid
(parent organization + lab) ---> lookup/add to *affiliations* table -> affid
...
@@ -744,7 +776,7 @@ def get_or_create_affiliation(org_info, comex_db):
...
@@ -744,7 +776,7 @@ def get_or_create_affiliation(org_info, comex_db):
else
:
else
:
db_constraints
.
append
(
"
%
s IS NULL"
%
colname
)
db_constraints
.
append
(
"
%
s IS NULL"
%
colname
)
db_cursor
=
c
ome
x_db
.
cursor
()
db_cursor
=
c
m
x_db
.
cursor
()
n_matched
=
db_cursor
.
execute
(
n_matched
=
db_cursor
.
execute
(
'SELECT affid FROM affiliations WHERE
%
s'
%
'SELECT affid FROM affiliations WHERE
%
s'
%
...
@@ -764,7 +796,7 @@ def get_or_create_affiliation(org_info, comex_db):
...
@@ -764,7 +796,7 @@ def get_or_create_affiliation(org_info, comex_db):
)
)
)
)
the_aff_id
=
db_cursor
.
lastrowid
the_aff_id
=
db_cursor
.
lastrowid
c
ome
x_db
.
commit
()
c
m
x_db
.
commit
()
mlog
(
"DEBUG"
,
"Added affiliation '
%
s'"
%
str
(
db_qstrvals
))
mlog
(
"DEBUG"
,
"Added affiliation '
%
s'"
%
str
(
db_qstrvals
))
else
:
else
:
raise
Exception
(
"ERROR: non-unique affiliation '
%
s'"
%
str
(
db_qstrvals
))
raise
Exception
(
"ERROR: non-unique affiliation '
%
s'"
%
str
(
db_qstrvals
))
...
@@ -775,31 +807,43 @@ def get_or_create_affiliation(org_info, comex_db):
...
@@ -775,31 +807,43 @@ def get_or_create_affiliation(org_info, comex_db):
# for users coming in from doors with no profile yet, we keep their doors infos (email, also name in the future)
# for users coming in from doors with no profile yet, we keep their doors infos (email, also name in the future)
def
save_doors_temp_user
(
doors_uid
,
doors_email
):
def
save_doors_temp_user
(
doors_uid
,
doors_email
,
cmx_db
=
None
):
if
cmx_db
:
db
=
cmx_db
else
:
db
=
connect_db
()
db
=
connect_db
()
db_c
=
db
.
cursor
()
db_c
=
db
.
cursor
()
stmt
=
"INSERT IGNORE INTO doors_temp_user(doors_uid, email) VALUES (
%
s,
%
s)"
stmt
=
"INSERT IGNORE INTO doors_temp_user(doors_uid, email) VALUES (
%
s,
%
s)"
db_c
.
execute
(
stmt
,
(
doors_uid
,
doors_email
))
db_c
.
execute
(
stmt
,
(
doors_uid
,
doors_email
))
db
.
commit
()
db
.
commit
()
if
not
cmx_db
:
db
.
close
()
db
.
close
()
def
get_doors_temp_user
(
doors_uid
):
def
get_doors_temp_user
(
doors_uid
,
cmx_db
=
None
):
info_row
=
None
info_row
=
None
if
cmx_db
:
db
=
cmx_db
else
:
db
=
connect_db
()
db
=
connect_db
()
db_c
=
db
.
cursor
(
DictCursor
)
db_c
=
db
.
cursor
(
DictCursor
)
db_c
.
execute
(
'''SELECT *
db_c
.
execute
(
'''SELECT *
FROM doors_temp_user
FROM doors_temp_user
WHERE doors_uid = "
%
s"'''
%
doors_uid
)
WHERE doors_uid = "
%
s"'''
%
doors_uid
)
info_row
=
db_c
.
fetchone
()
info_row
=
db_c
.
fetchone
()
if
not
cmx_db
:
db
.
close
()
db
.
close
()
return
info_row
return
info_row
def
rm_doors_temp_user
(
doors_uid
):
def
rm_doors_temp_user
(
doors_uid
,
cmx_db
=
None
):
if
cmx_db
:
db
=
cmx_db
else
:
db
=
connect_db
()
db
=
connect_db
()
db_c
=
db
.
cursor
()
db_c
=
db
.
cursor
()
db_c
.
execute
(
'''DELETE FROM doors_temp_user
db_c
.
execute
(
'''DELETE FROM doors_temp_user
WHERE doors_uid = "
%
s"'''
%
doors_uid
)
WHERE doors_uid = "
%
s"'''
%
doors_uid
)
db
.
commit
()
db
.
commit
()
if
not
cmx_db
:
db
.
close
()
db
.
close
()
...
...
services/main.py
View file @
3f2554e9
...
@@ -98,9 +98,9 @@ SOURCE_FIELDS = [
...
@@ -98,9 +98,9 @@ SOURCE_FIELDS = [
(
"hon_title"
,
True
,
None
),
(
"hon_title"
,
True
,
None
),
(
"interests_text"
,
True
,
None
),
(
"interests_text"
,
True
,
None
),
(
"gender"
,
False
,
None
),
# M|F
(
"gender"
,
False
,
None
),
# M|F
(
"job_looking_date"
,
True
,
"date"
),
# def null: not looking for a job
(
"job_looking_date"
,
True
,
"
s
date"
),
# def null: not looking for a job
(
"home_url"
,
True
,
"
url"
),
# scholar's homepage
(
"home_url"
,
True
,
"s
url"
),
# scholar's homepage
(
"pic_url"
,
True
,
"
url"
),
(
"pic_url"
,
True
,
"s
url"
),
(
"pic_file"
,
False
,
None
),
# saved separately
(
"pic_file"
,
False
,
None
),
# saved separately
# => for *scholars* table (optional)
# => for *scholars* table (optional)
...
@@ -132,13 +132,13 @@ def inject_doors_params():
...
@@ -132,13 +132,13 @@ def inject_doors_params():
-> 'doors_connect'
-> 'doors_connect'
(base_layout-rendered templates need it for login popup)
(base_layout-rendered templates need it for login popup)
"""
"""
if
'DOORS_PORT'
not
in
config
or
config
[
'DOORS_PORT'
]
in
[
'
'
,
'
80'
,
'443'
]:
if
'DOORS_PORT'
not
in
config
or
config
[
'DOORS_PORT'
]
in
[
'80'
,
'443'
]:
context_dict
=
dict
(
context_dict
=
dict
(
doors_connect
=
config
[
'DOORS_HOST'
]
doors_connect
=
config
[
'DOORS_HOST'
]
)
)
else
:
else
:
context_dict
=
dict
(
context_dict
=
dict
(
doors_connect
=
config
[
'DOORS_HOST'
]
doors_connect
=
config
[
'DOORS_HOST'
]
+
':'
+
config
[
'DOORS_PORT'
]
)
)
return
context_dict
return
context_dict
...
@@ -252,7 +252,8 @@ def user_api():
...
@@ -252,7 +252,8 @@ def user_api():
implemented "op" <=> verbs:
implemented "op" <=> verbs:
exists => bool
exists => bool
"""
"""
if
'op'
in
request
.
args
and
request
.
args
[
'op'
]
==
"exists"
:
if
'op'
in
request
.
args
:
if
request
.
args
[
'op'
]
==
"exists"
:
if
'email'
in
request
.
args
:
if
'email'
in
request
.
args
:
email
=
sanitize
(
request
.
args
[
'email'
])
email
=
sanitize
(
request
.
args
[
'email'
])
return
(
dumps
({
'exists'
:
db
.
email_exists
(
email
)}))
return
(
dumps
({
'exists'
:
db
.
email_exists
(
email
)}))
...
@@ -275,7 +276,7 @@ def login():
...
@@ -275,7 +276,7 @@ def login():
"login.html"
"login.html"
)
)
elif
request
.
method
==
'POST'
:
elif
request
.
method
==
'POST'
:
mlog
(
"DEBUG"
,
"
login
form received from "
+
request
.
path
+
", with keys:"
,
[
k
for
k
in
request
.
values
])
mlog
(
"DEBUG"
,
"
LOGIN:
form received from "
+
request
.
path
+
", with keys:"
,
[
k
for
k
in
request
.
values
])
# we used this custom header to mark ajax calls => called_as_api True
# we used this custom header to mark ajax calls => called_as_api True
x_req_with
=
request
.
headers
.
get
(
'X-Requested-With'
,
type
=
str
)
x_req_with
=
request
.
headers
.
get
(
'X-Requested-With'
,
type
=
str
)
...
@@ -309,10 +310,10 @@ def login():
...
@@ -309,10 +310,10 @@ def login():
try
:
try
:
doors_uid
=
doors_login
(
email
,
pwd
,
config
)
doors_uid
=
doors_login
(
email
,
pwd
,
config
)
except
Exception
as
err
:
except
Exception
as
err
:
mlog
(
"ERROR"
,
"error in doors_login request"
)
mlog
(
"ERROR"
,
"
LOGIN:
error in doors_login request"
)
raise
(
err
)
raise
(
err
)
mlog
(
"DEBUG"
,
"
doors_login
returned doors_uid '
%
s'"
%
doors_uid
)
mlog
(
"DEBUG"
,
"
user.doors_login()
returned doors_uid '
%
s'"
%
doors_uid
)
if
doors_uid
is
None
:
if
doors_uid
is
None
:
# break: can't doors_login
# break: can't doors_login
...
@@ -332,6 +333,7 @@ def login():
...
@@ -332,6 +333,7 @@ def login():
# normal user
# normal user
user
=
User
(
luid
)
user
=
User
(
luid
)
else
:
else
:
mlog
(
"DEBUG"
,
"LOGIN: encountered new doors id (
%
s), switching to empty user profile"
%
doors_uid
)
# user exists in doors but has no comex profile nor luid yet
# user exists in doors but has no comex profile nor luid yet
db
.
save_doors_temp_user
(
doors_uid
,
email
)
# preserve the email
db
.
save_doors_temp_user
(
doors_uid
,
email
)
# preserve the email
user
=
User
(
None
,
doors_uid
=
doors_uid
)
# get a user.empty
user
=
User
(
None
,
doors_uid
=
doors_uid
)
# get a user.empty
...
@@ -354,7 +356,7 @@ def login():
...
@@ -354,7 +356,7 @@ def login():
if
not
login_ok
:
if
not
login_ok
:
# break: failed to login_user()
# break: failed to login_user()
notok_message
=
"There was an unknown problem with the login."
notok_message
=
"
LOGIN
There was an unknown problem with the login."
if
called_as_api
:
if
called_as_api
:
# menubar login will prevent redirect
# menubar login will prevent redirect
return
(
nologin_message
,
404
)
return
(
nologin_message
,
404
)
...
@@ -373,7 +375,7 @@ def login():
...
@@ -373,7 +375,7 @@ def login():
elif
user
.
empty
:
elif
user
.
empty
:
mlog
(
'DEBUG'
,
"empty user redirected to profile"
)
mlog
(
'DEBUG'
,
"empty user redirected to profile"
)
# we go straight to
profile for the him
to create infos
# we go straight to
empty profile for the person
to create infos
return
(
redirect
(
url_for
(
'profile'
,
_external
=
True
)))
return
(
redirect
(
url_for
(
'profile'
,
_external
=
True
)))
# normal call, normal user
# normal call, normal user
...
@@ -392,7 +394,7 @@ def login():
...
@@ -392,7 +394,7 @@ def login():
# if relative
# if relative
if
next_url
[
0
]
==
'/'
:
if
next_url
[
0
]
==
'/'
:
next_url
=
url_for
(
'rootindex'
,
_external
=
True
)
+
next_url
[
1
:]
next_url
=
url_for
(
'rootindex'
,
_external
=
True
)
+
next_url
[
1
:]
mlog
(
"DEBUG"
,
"reabsoluted next_url:"
,
next_url
)
mlog
(
"DEBUG"
,
"
LOGIN:
reabsoluted next_url:"
,
next_url
)
return
(
redirect
(
next_url
))
return
(
redirect
(
next_url
))
else
:
else
:
...
@@ -572,7 +574,10 @@ def claim_profile():
...
@@ -572,7 +574,10 @@ def claim_profile():
luid
=
request
.
form
[
'return_user_luid'
]
luid
=
request
.
form
[
'return_user_luid'
]
return_user
=
User
(
luid
)
return_user
=
User
(
luid
)
name
=
return_user
.
info
.
get
(
'last_name'
)
+
', '
+
return_user
.
info
.
get
(
'first_name'
,
''
)
+
' '
+
return_user
.
info
.
get
(
'middle_name'
,
''
)
info
=
return_user
.
info
name
=
info
[
'last_name'
]
+
', '
+
info
[
'first_name'
]
if
info
[
'middle_name'
]:
name
+=
' '
+
info
[
'middle_name'
]
# we do our doors request here server-side to avoid MiM attack on result
# we do our doors request here server-side to avoid MiM attack on result
try
:
try
:
...
@@ -679,7 +684,7 @@ def register():
...
@@ -679,7 +684,7 @@ def register():
return
render_template
(
return
render_template
(
"thank_you.html"
,
"thank_you.html"
,
debug_records
=
(
clean_records
if
app
.
config
[
'DEBUG'
]
else
{}),
debug_records
=
(
clean_records
if
app
.
config
[
'DEBUG'
]
else
{}),
form_accepted
=
True
,
form_accepted
=
form_accepted
,
backend_error
=
False
,
backend_error
=
False
,
message
=
"""
message
=
"""
You can now visit elements of the members section:
You can now visit elements of the members section:
...
@@ -858,9 +863,9 @@ def sanitize(value, specific_type=None):
...
@@ -858,9 +863,9 @@ def sanitize(value, specific_type=None):
if
not
specific_type
:
if
not
specific_type
:
san_val
=
sub
(
r'[^\w@\.:,()# -]'
,
'_'
,
clean_val
)
san_val
=
sub
(
r'[^\w@\.:,()# -]'
,
'_'
,
clean_val
)
elif
specific_type
==
"url"
:
elif
specific_type
==
"
s
url"
:
san_val
=
sub
(
r'[^\w@\.: -/]'
,
'_'
,
clean_val
)
san_val
=
sub
(
r'[^\w@\.: -/]'
,
'_'
,
clean_val
)
elif
specific_type
==
"date"
:
elif
specific_type
==
"
s
date"
:
san_val
=
sub
(
r'[^0-9/-:]'
,
'_'
,
clean_val
)
san_val
=
sub
(
r'[^0-9/-:]'
,
'_'
,
clean_val
)
if
vtype
not
in
[
int
,
str
]:
if
vtype
not
in
[
int
,
str
]:
...
...
services/user.py
View file @
3f2554e9
...
@@ -79,8 +79,7 @@ class User(object):
...
@@ -79,8 +79,7 @@ class User(object):
doors but not in db)
doors but not in db)
=> no luid, but has doors_uid
=> no luid, but has doors_uid
This also causes trickier behaviour for get_id:
NB load_user() wants a *single id for both*,
ie load_user() wants a *single id for both*,
which is provided by self.get_id()
which is provided by self.get_id()
"""
"""
mlog
(
'DEBUG'
,
mlog
(
'DEBUG'
,
...
@@ -225,7 +224,6 @@ def doors_login(email, password, config=REALCONFIG):
...
@@ -225,7 +224,6 @@ def doors_login(email, password, config=REALCONFIG):
http_scheme
=
"https:"
http_scheme
=
"https:"
# (TODO generalize this logic)
if
config
[
'DOORS_PORT'
]
in
[
'80'
,
'443'
]:
if
config
[
'DOORS_PORT'
]
in
[
'80'
,
'443'
]:
# implicit port
# implicit port
doors_base_url
=
http_scheme
+
'//'
+
config
[
'DOORS_HOST'
]
doors_base_url
=
http_scheme
+
'//'
+
config
[
'DOORS_HOST'
]
...
@@ -276,6 +274,7 @@ def doors_register(email, password, name, config=REALCONFIG):
...
@@ -276,6 +274,7 @@ def doors_register(email, password, name, config=REALCONFIG):
# eg doors_response.content = b'{"status":"registration email sent",
# eg doors_response.content = b'{"status":"registration email sent",
# "email":"john@locke.com"}''
# "email":"john@locke.com"}''
answer
=
loads
(
doors_response
.
content
.
decode
())
answer
=
loads
(
doors_response
.
content
.
decode
())
mlog
(
"INFO"
,
"/api/register answer"
,
answer
)
return
answer
[
'userID'
]
return
answer
[
'userID'
]
else
:
else
:
return
None
return
None
static/js/comex_user_shared_auth.js
View file @
3f2554e9
...
@@ -71,7 +71,7 @@ cmxClt = (function(cC) {
...
@@ -71,7 +71,7 @@ cmxClt = (function(cC) {
// -> interaction elements (params, else default)
// -> interaction elements (params, else default)
var
emailId
,
duuidId
,
passId
,
pass2Id
,
captchaId
,
capcheckId
var
emailId
,
duuidId
,
passId
,
pass2Id
,
captchaId
,
capcheckId
console
.
info
(
'new AuthForm "'
+
auForm
.
id
+
'"[.type='
+
auForm
.
type
+
'] init params'
,
afParams
)
//
console.info('new AuthForm "'+auForm.id+'"[.type='+auForm.type+'] init params', afParams)
emailId
=
afParams
.
emailId
||
'email'
emailId
=
afParams
.
emailId
||
'email'
duuidId
=
afParams
.
duuidId
||
'doors_uid'
duuidId
=
afParams
.
duuidId
||
'doors_uid'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment