Add subscribe method
Benjamin Renard

Benjamin Renard commited on 2014-01-18 19:14:09
Showing 3 changed files, with 45 additions and 0 deletions.

... ...
@@ -33,6 +33,26 @@ def login(req):
33 33
 	login_data=req.ctx.db.login(data['email'],data['password'])
34 34
 	return wsgi_helpers.respond_json(req.ctx,login_data,headers=[('Access-Control-Allow-Origin','*')])
35 35
 
36
+@wsgify
37
+def subscribe(req):
38
+	params = req.params
39
+	log.debug(u'params = {}'.format(params))
40
+	inputs = {
41
+		'email': params.get('email'),
42
+		'name': params.get('name'),
43
+		'password': params.get('password'),
44
+		}
45
+	log.debug(u'inputs = {}'.format(inputs))
46
+	data, errors = conv.inputs_to_subscribe_data(inputs)
47
+	if errors is not None:
48
+		return wsgi_helpers.bad_request(req.ctx, comment=errors)
49
+
50
+	log.debug(u'data = {}'.format(data))
51
+
52
+	login_data=req.ctx.db.subscribe(data['email'],data['name'],data['password'])
53
+	return wsgi_helpers.respond_json(req.ctx,login_data,headers=[('Access-Control-Allow-Origin','*')])
54
+
55
+
36 56
 @wsgify
37 57
 def sync(req):
38 58
 	params = req.params
... ...
@@ -66,5 +86,6 @@ def make_router():
66 86
 	return router.make_router(
67 87
 		('GET', '^/$', home),
68 88
 		('GET', '^/login$', login),
89
+		('GET', '^/subscribe$', subscribe),
69 90
 		('GET', '^/sync$', sync),
70 91
 		)
... ...
@@ -12,6 +12,16 @@ inputs_to_login_data = struct(
12 12
     drop_none_values=False,
13 13
     )
14 14
 
15
+inputs_to_subscribe_data = struct(
16
+    {
17
+        'email': pipe(cleanup_line, empty_to_none),
18
+        'name': pipe(cleanup_line, empty_to_none),
19
+        'password': pipe(cleanup_line, empty_to_none),
20
+    },
21
+    default='drop',
22
+    drop_none_values=False,
23
+    )
24
+
15 25
 inputs_to_sync_data = struct(
16 26
     {
17 27
         'email': pipe(cleanup_line, empty_to_none),
... ...
@@ -56,6 +56,20 @@ class DB(object):
56 56
 			return { 'loginerror': 'Utilisateur inconnu' }
57 57
 		return { 'loginerror': 'Erreur inconnu' }
58 58
 
59
+	def subscribe(self,email,name,password):
60
+		ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email))
61
+		log.debug(ret)
62
+		if ret[0][0]!=0:
63
+			return {'subscribeerror': u'Cette adresse mail est déjà associés a un compte !'}
64
+		else:
65
+			if self.do_sql("INSERT INTO users (email,name,password) VALUES ('%s','%s','%s')" % (email,name,password)):
66
+				return {
67
+					'email': email,
68
+					'name': name,
69
+					'password': password
70
+				}
71
+		return {'subscribeerror': u'Une erreur est survenue durant votre inscription :('}
72
+
59 73
 	def sync_group(self,email,groups):
60 74
 		db_groups=self.get_group(email)
61 75
 		json_group=json.dumps(groups)
62 76