+

Add subscribe method

Benjamin Renard [2014-01-18 19:14:09]
Add subscribe method
Filename
mycoserver/controllers.py
mycoserver/conv.py
mycoserver/db/__init__.py
diff --git a/mycoserver/controllers.py b/mycoserver/controllers.py
index b1e5b8c..a3f20a7 100644
--- a/mycoserver/controllers.py
+++ b/mycoserver/controllers.py
@@ -34,6 +34,26 @@ def login(req):
 	return wsgi_helpers.respond_json(req.ctx,login_data,headers=[('Access-Control-Allow-Origin','*')])

 @wsgify
+def subscribe(req):
+	params = req.params
+	log.debug(u'params = {}'.format(params))
+	inputs = {
+		'email': params.get('email'),
+		'name': params.get('name'),
+		'password': params.get('password'),
+		}
+	log.debug(u'inputs = {}'.format(inputs))
+	data, errors = conv.inputs_to_subscribe_data(inputs)
+	if errors is not None:
+		return wsgi_helpers.bad_request(req.ctx, comment=errors)
+
+	log.debug(u'data = {}'.format(data))
+
+	login_data=req.ctx.db.subscribe(data['email'],data['name'],data['password'])
+	return wsgi_helpers.respond_json(req.ctx,login_data,headers=[('Access-Control-Allow-Origin','*')])
+
+
+@wsgify
 def sync(req):
 	params = req.params
 	log.debug(u'params = {}'.format(params))
@@ -66,5 +86,6 @@ def make_router():
 	return router.make_router(
 		('GET', '^/$', home),
 		('GET', '^/login$', login),
+		('GET', '^/subscribe$', subscribe),
 		('GET', '^/sync$', sync),
 		)
diff --git a/mycoserver/conv.py b/mycoserver/conv.py
index 8518b6f..62f40a3 100644
--- a/mycoserver/conv.py
+++ b/mycoserver/conv.py
@@ -12,6 +12,16 @@ inputs_to_login_data = struct(
     drop_none_values=False,
     )

+inputs_to_subscribe_data = struct(
+    {
+        'email': pipe(cleanup_line, empty_to_none),
+        'name': pipe(cleanup_line, empty_to_none),
+        'password': pipe(cleanup_line, empty_to_none),
+    },
+    default='drop',
+    drop_none_values=False,
+    )
+
 inputs_to_sync_data = struct(
     {
         'email': pipe(cleanup_line, empty_to_none),
diff --git a/mycoserver/db/__init__.py b/mycoserver/db/__init__.py
index 9017456..72a4c7b 100644
--- a/mycoserver/db/__init__.py
+++ b/mycoserver/db/__init__.py
@@ -56,6 +56,20 @@ class DB(object):
 			return { 'loginerror': 'Utilisateur inconnu' }
 		return { 'loginerror': 'Erreur inconnu' }

+	def subscribe(self,email,name,password):
+		ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email))
+		log.debug(ret)
+		if ret[0][0]!=0:
+			return {'subscribeerror': u'Cette adresse mail est déjà associés a un compte !'}
+		else:
+			if self.do_sql("INSERT INTO users (email,name,password) VALUES ('%s','%s','%s')" % (email,name,password)):
+				return {
+					'email': email,
+					'name': name,
+					'password': password
+				}
+		return {'subscribeerror': u'Une erreur est survenue durant votre inscription :('}
+
 	def sync_group(self,email,groups):
 		db_groups=self.get_group(email)
 		json_group=json.dumps(groups)
ViewGit