From d015986dfc12e3e2b64042db782bb8aab138682e Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Thu, 11 Jun 2015 21:03:00 +0300 Subject: [PATCH] added Campus create method --- SE_API/API.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/SE_API/API.py b/SE_API/API.py index 56c5583..d203ddc 100644 --- a/SE_API/API.py +++ b/SE_API/API.py @@ -1,3 +1,4 @@ + __author__ = 'sagi' import json from GithubAPI.GithubAPI import GitHubAPI_Keys @@ -20,6 +21,7 @@ from models.Campus import Campus #Validation Utils Libs from SE_API.Validation_Utils import * +from SE_API.Respones_Utils import * @@ -130,7 +132,7 @@ def oauth(oauth_token): print user_data["login"] - for u in resault.run(limit=5): + for u in resault.run(): print "Exists!!!" u.seToken = str(uuid.uuid4()) u.accessToken = oauth_token @@ -146,12 +148,58 @@ def oauth(oauth_token): else: tempEmail = user_data["email"] - user = User(username=user_data["login"], name=tempName, avatar_url=user_data["avatar_url"], email=tempEmail, isLecturer=False, accsessToken=oauth_token, seToken=str(uuid.uuid4())) + user = User(username=user_data["login"], name=tempName, avatar_url=user_data["avatar_url"], email=tempEmail, isLecturer=False, accessToken=oauth_token, seToken=str(uuid.uuid4())) db.put(user) db.save return cookieMonster(user.seToken) +@app.route('/api/Campuses/create/', methods=['POST']) +@auto.doc() +def create_campus(token): + """ + This call will create a new campus in the DB + :param token: user seToken + Payload + {'title': self.title, + 'email_ending': self.email_ending, + 'avatar_url': self.avatar_url + } + :return: + code 200 + """ + print "1\n" + if not request.data: + return Response(response=json.dumps({'message': 'Bad Request0'}), + status=400, + mimetype="application/json") + payload = json.loads(request.data) + if not is_lecturer(token): #todo: change to lecturer id + return Response(response=json.dumps({'message': 'Invalid token or not a lecturer!'}), + status=403, + mimetype="application/json") + + user = get_user_by_token(token) + + #todo: check legality + + + try: + campus = Campus(title=payload['title'], email_ending=payload['email_ending'], master_user_id=user.key().id(), avatar_url=payload['avatar_url']) + except Exception: + return Response(response=json.dumps({'message': 'Bad Request1'}), + status=400, + mimetype="application/json") + + db.put(campus) + db.save + return Response(response=json.dumps(campus.to_JSON()), + status=200, + mimetype="application/json") + + + + @app.route('/api/Campuses/', methods=['GET']) @auto.doc() def get_campuses(token):