diff --git a/SE_API/API.py b/SE_API/API.py index 04286ba..f90b38f 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 * @@ -152,7 +154,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 @@ -174,6 +176,52 @@ def oauth(oauth_token): 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): 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 diff --git a/SE_API/Validation_Utils.py b/SE_API/Validation_Utils.py index f9754da..9226d61 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): emailSuffix = str(email).split('@')[1] message = mail.EmailMessage(sender="SE-Hub Support ", 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,