From e1b6f496861e92e183cf16b081e0ef95f149b87e Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Thu, 11 Jun 2015 20:59:40 +0300 Subject: [PATCH 1/4] added response utils --- SE_API/Respones_Utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 SE_API/Respones_Utils.py diff --git a/SE_API/Respones_Utils.py b/SE_API/Respones_Utils.py new file mode 100644 index 0000000..a7d7b17 --- /dev/null +++ b/SE_API/Respones_Utils.py @@ -0,0 +1,15 @@ +__author__ = 'Aran' + +from flask import Response +import json + +def bad_request(): + return Response(response=json.dumps({'message': 'Bad Request'}), + status=400, + mimetype="application/json") + + +def forbidden(message={'message': 'Forbidden'}): + return Response(response=json.dumps(message), + status=403, + mimetype="application/json") \ No newline at end of file From dcb83987f88786d2c8466026b16c16a05007b75a Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Thu, 11 Jun 2015 21:01:05 +0300 Subject: [PATCH 2/4] -var name change in campus -default value for Campus properties --- models/Campus.py | 4 ++-- models/User.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/models/Campus.py b/models/Campus.py index 4859409..f4e9cef 100644 --- a/models/Campus.py +++ b/models/Campus.py @@ -11,12 +11,12 @@ class Campus(db.Model): master_user_id = db.IntegerProperty(required=True) def to_JSON(self): - dick = {'title': self.title, + data = {'title': self.title, 'email_ending': self.email_ending, 'master_user_id': self.master_user_id, 'avatar_url': self.avatar_url } - return json.dumps(dick) + return json.dumps(data) """ diff --git a/models/User.py b/models/User.py index ed0e2c1..c336d36 100644 --- a/models/User.py +++ b/models/User.py @@ -12,8 +12,8 @@ class User(db.Model): seToken = db.StringProperty(required=True) avatar_url = db.StringProperty(required=True) isFirstLogin = db.BooleanProperty(default=True) - campuses_id_list = db.StringListProperty() - classes_id_list = db.StringListProperty() + campuses_id_list = db.StringListProperty(default=[]) + classes_id_list = db.StringListProperty(default=[]) def to_JSON(self): data = {'username' : self.username, From 628aad5e0369789297e523c5dd920eed584dcf78 Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Thu, 11 Jun 2015 21:01:58 +0300 Subject: [PATCH 3/4] added 2 methods (is_lecturer, get_user_by_token) --- SE_API/Validation_Utils.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/SE_API/Validation_Utils.py b/SE_API/Validation_Utils.py index 836e4d1..12c35d9 100644 --- a/SE_API/Validation_Utils.py +++ b/SE_API/Validation_Utils.py @@ -4,14 +4,27 @@ from models.User import User from google.appengine.api import mail -def is_user_token_valid(token): +def get_user_by_token(token): query = User.all() query.filter("seToken = ", token) - for u in query.run(): + for u in query.run(limit = 1): + return u + return None + +def is_user_token_valid(token): + user = get_user_by_token(token) + if user is not None: return True return False +def is_lecturer(token): + user = get_user_by_token(token) + if user is None: + return False + return user.isLecturer + + def send_validation_email(token, email, name): message = mail.EmailMessage(sender="SE-Hub Support ", subject="SE-Hub Activate Account") From d015986dfc12e3e2b64042db782bb8aab138682e Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Thu, 11 Jun 2015 21:03:00 +0300 Subject: [PATCH 4/4] 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):