+

Support lastChange field in JSON GroupList data

Benjamin Renard [2014-01-27 22:53:25]
Support lastChange field in JSON GroupList data
Filename
mycoserver/controllers.py
mycoserver/db/__init__.py
diff --git a/mycoserver/controllers.py b/mycoserver/controllers.py
index a3f20a7..ca16358 100644
--- a/mycoserver/controllers.py
+++ b/mycoserver/controllers.py
@@ -73,9 +73,11 @@ def sync(req):

 	login_data=req.ctx.db.login(data['email'],data['password'])
 	if 'email' in login_data:
+		log.debug(u'Login successful : sync groups')
 		ret=req.ctx.db.sync_group(data['email'],data['groups'])
 		return wsgi_helpers.respond_json(req.ctx,ret,headers=[('Access-Control-Allow-Origin','*')])
 	else:
+		log.debug(u'Login error : return login error')
 		return wsgi_helpers.respond_json(
                   req.ctx,
                   login_data,
diff --git a/mycoserver/db/__init__.py b/mycoserver/db/__init__.py
index 72a4c7b..ac4f8c4 100644
--- a/mycoserver/db/__init__.py
+++ b/mycoserver/db/__init__.py
@@ -74,15 +74,22 @@ class DB(object):
 		db_groups=self.get_group(email)
 		json_group=json.dumps(groups)
 		if db_groups!=False:
-			if db_groups=={}:
-				if groups=={}:
-					return {'groups': {}}
+			log.debug('Database groups : %s' % db_groups)
+			log.debug('Provided groups : %s' % groups)
+			if 'groups' not in db_groups or db_groups['groups']=={}:
+				log.debug('Database group is empty')
+				if 'groups' not in groups or groups['groups']=={}:
+					log.debug('Database and provided group are empty. Return empty')
+					return {'groups': {'groups': {}}}
 				else:
+					log.debug('Insert provided groups in database and return it')
 					if self.do_sql("INSERT INTO groups (email,groups) VALUES ('%s','%s')" % (email,json_group)):
 						return {'groups': groups}
-			elif groups=={}:
+			elif 'groups' not in groups or groups['groups']=={}:
+				log.debug('Provide group is empty. Return database groups')
 				return {'groups': db_groups}
 			else:
+				log.debug('Update database with provided group and return it')
 				if self.do_sql("UPDATE groups SET groups='%s' WHERE email='%s'" % (json_group,email)):
 					return {'groups': groups}
 		return {'syncerror': 'Erreur inconnu'}
@@ -93,6 +100,6 @@ class DB(object):
 			if len(ret)==1:
 				return json.loads(ret[0][0])
 			else:
-				return {}
+				return {'groups': {}}
 		else:
 			return False
ViewGit