From 8f9d704c9a35713003b1b6191f4d29c297e8dc67 Mon Sep 17 00:00:00 2001 From: Mathieu Benoit Date: Sun, 1 Apr 2018 13:55:19 -0400 Subject: [PATCH] [#83] Login: add incremental information when connect from third-party --- src/web/handlers.py | 17 ++++++------ src/web/py_class/db.py | 60 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/web/handlers.py b/src/web/handlers.py index 30647463..0c912685 100644 --- a/src/web/handlers.py +++ b/src/web/handlers.py @@ -205,8 +205,9 @@ def get(self): # use this email to associate user = self._db.get_user(email=email, force_email_no_password=True) if user: - user["google_id"] = google_id - self._db.update_user(user) + self._db.add_missing_info_user(user, google_id=google_id, verified_email=verified_email, + given_name=given_name, family_name=family_name, + locale=locale) else: user = self._db.create_user(username, email=email, google_id=google_id, verified_email=verified_email, name=name, given_name=given_name, @@ -281,8 +282,8 @@ def get(self): # use this email to associate user = self._db.get_user(email=email, force_email_no_password=True) if user: - user["facebook_id"] = facebook_id - self._db.update_user(user) + self._db.add_missing_info_user(user, facebook_id=facebook_id, given_name=given_name, + family_name=family_name, locale=locale) else: user = self._db.create_user(username, name=name, given_name=given_name, family_name=family_name, locale=locale, email=email, facebook_id=facebook_id) @@ -356,8 +357,8 @@ def get(self): # use this email to associate user = self._db.get_user(email=email, force_email_no_password=True) if user: - user["twitter_id"] = twitter_id - self._db.update_user(user) + self._db.add_missing_info_user(user, twitter_id=twitter_id, verified_email=verified_email, + locale=locale) else: user = self._db.create_user(username, email=email, name=name, verified_email=verified_email, locale=locale, twitter_id=twitter_id) @@ -647,8 +648,8 @@ def post(self): return # Update password - current_user["password"] = self._db.generate_password(password) - self._db.update_user(current_user) + updated_password = self._db.generate_password(password) + self._db.add_missing_info_user(current_user, password=updated_password) # TODO Need to validate insertion data = {"status": "Password added."} diff --git a/src/web/py_class/db.py b/src/web/py_class/db.py index 9afdf920..055afe42 100644 --- a/src/web/py_class/db.py +++ b/src/web/py_class/db.py @@ -55,16 +55,6 @@ def create_user(self, username, email=None, password=None, google_id=None, faceb # TODO let user create it and not automatic create empty_character = [{ - "habilites": [ - {} - ], - "technique_maitre": [], - "rituel": [], - "xp_naissance": 6, - "xp_autre": 0, - "character_id": "0f173f629ef64fb190bfcef7b7f85697", - "date_modify": 1520689829.9900929928, - "date_creation": 1520689829.9900929928 }] data = {"email": email, "username": username, "name": name, "given_name": given_name, @@ -76,6 +66,54 @@ def create_user(self, username, email=None, password=None, google_id=None, faceb eid = self._db_user.insert(data) return self._db_user.get(eid=eid) + def add_missing_info_user(self, obj_user, password=None, google_id=None, facebook_id=None, twitter_id=None, + name=None, given_name=None, family_name=None, verified_email=False, locale=None, + postal_code=None): + has_update = False + + if password and not obj_user.get("password"): + obj_user["password"] = password + has_update = True + + if google_id and not obj_user.get("google_id"): + obj_user["google_id"] = google_id + has_update = True + + if facebook_id and not obj_user.get("facebook_id"): + obj_user["facebook_id"] = facebook_id + has_update = True + + if twitter_id and not obj_user.get("twitter_id"): + obj_user["twitter_id"] = twitter_id + has_update = True + + if name and not obj_user.get("name"): + obj_user["name"] = name + has_update = True + + if given_name and not obj_user.get("given_name"): + obj_user["given_name"] = given_name + has_update = True + + if family_name and not obj_user.get("family_name"): + obj_user["family_name"] = family_name + has_update = True + + if verified_email and not obj_user.get("verified_email"): + obj_user["verified_email"] = verified_email + has_update = True + + if locale and not obj_user.get("locale"): + obj_user["locale"] = locale + has_update = True + + if postal_code and not obj_user.get("postal_code"): + obj_user["postal_code"] = postal_code + has_update = True + + if has_update: + self.update_user(obj_user) + def get_all_user(self, user_id=None): if not user_id: # get all user list @@ -168,7 +206,7 @@ def transform(element): # TODO validate fields in data if delete_character_by_id: del lst_character[i] - else: + elif character_data: lst_character[i] = character_data # update last modify date character_data["date_modify"] = datetime.datetime.utcnow().timestamp()