Merge pull request #73 from sagidayan/API_Dev

Api dev
This commit is contained in:
Sagi Dayan 2015-06-24 22:43:52 +03:00
commit 73eef42871
7 changed files with 312 additions and 27 deletions

View file

@ -153,20 +153,72 @@ def get_campuses(token):
return forbidden("Invalid Token")
@campus_routes.route('/api/campuses/getCampusesByUser/<string:token>', methods=['GET'])
@auto.doc()
def getCampusesByUser(token):
"""
<span class="card-title">This Call will return an array of all Campuses of a certain User</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'
<br>
<br>
<b>Payload</b><br>
- NONE <br>
<br>
<br>
<b>Response</b>
<br>
200 - JSON Array, Example:<br>
[<br>
{
'title': 'JCE',<br>
'email_ending': '@post.jce.ac.il',<br>
'master_user_id': 123453433341, (User that created the campus)<br>
'avatar_url': 'http://some.domain.com/imagefile.jpg',<br>
'id' : 1234567890<br>
},<br>
....<br>
{<br>
...<br>
}req<br>
]<br>
<br>
403 - Invalid Token<br>
"""
user = get_user_by_token(token)
if user is None:
return bad_request("Bad user Token")
arr = []
for i in user['campuses_id_list']:
campus = Campus.get_by_id(int(i))
arr.append(dict(json.loads(campus.to_JSON())))
if len(arr) != 0:
return Response(response=json.dumps(arr),
status=200,
mimetype="application/json")
else:
return Response(response=[],
status=200,
mimetype="application/json")
#----------------------------------------------------------
# DELETE
#----------------------------------------------------------
@campus_routes.route('/api/campuses/deleteCampus/<string:token>/<string:campusid>', methods=['DELETE'])
@campus_routes.route('/api/campuses/deleteCampus/<string:token>/<string:campusId>', methods=['DELETE'])
@auto.doc()
def deleteCampus(token,campusid):
def deleteCampus(token,campusId):
"""
<span class="card-title">This Call will delete a specific campus</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'
- campusid: 'campusid'
- campusId: 1234567890
<br>
<br>
<b>Payload</b><br>
@ -192,7 +244,7 @@ def deleteCampus(token,campusid):
return forbidden("Invalid token or not a lecturer!")
user = get_user_by_token(token)
camp = Campus.get_by_id(int(campusid))
camp = Campus.get_by_id(int(campusId))
if camp is None:
return bad_request("no such campus")

View file

@ -50,9 +50,8 @@ def create_course(token):
{<br>
'courseName': 'Advance Math',<br>
'campusName': 'JCE',<br>
'startDate': {'year': 2015, 'month' : 4, 'day' : 3}<br>
'startDate': {'year': 2015, 'month' : 4, 'day' : 3},<br>
'endDate': {'year': 2016, 'month' : 5, 'day' : 14}<br>
'taskFlag': false<br>
}<br>
<br>
<br>
@ -75,7 +74,7 @@ def create_course(token):
try:
payload = json.loads(request.data)
except Exception as e:
return bad_request()
return bad_request("incorrect JSON format")
try:
start_date = datetime.date(payload['startDate']['year'],payload['startDate']['month'],payload['startDate']['day'])
@ -98,9 +97,16 @@ def create_course(token):
except Exception as e:
print e
return bad_request()
return bad_request(2)
#add user to course membersId list
course.membersId.append(str(user.key().id()))
db.put(course)
#add course to user course list
user.courses_id_list.append(str(course.key().id()))
db.put(user)
db.save
return Response(response=course.to_JSON(),
status=201,
@ -149,17 +155,72 @@ def createMessage(token):
return bad_request("here")
try:
msg = Message(courseName=payload['courseName'], message=payload['message'], msgDate=datetime.datetime.now())
msg = Message(groupId=payload['groupId'], message=payload['message'], msgDate=datetime.datetime.now(), master_id=user.key().id())
except Exception as e:
print e
return bad_request("there")
try:
msg['isProject'] = payload['isProject']
except Exception as e:
pass
db.save(msg)
db.save
return created()
#----------------------------------------------------------
# PUT
#----------------------------------------------------------
@course_routes.route('/api/courses/joinCourse/<string:token>/<string:courseId>', methods=["PUT"])
@auto.doc()
def joinCourse(token, courseId):
"""
<span class="card-title">This call will add the user (by token) to a specific course</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'<br>
- courseId: 123456789
<br>
<br>
<b>Payload</b><br>
- None <br>
<br>
<b>Response</b>
<br>
202 - Accepted
<br>
400 - Bad Request
<br>
403 - Invalid token or not a lecturer
"""
user = get_user_by_token(token)
if user is None:
return bad_request("Wrong user Token")
course = Course.get_by_id(int(courseId))
if course is None:
return bad_request("No such course")
if user.key().id() in course.membersId:
return no_content("User is already member in Project")
course.membersId.append(str(user.key().id()))
user.courses_id_list.append(str(course.key().id()))
db.put(course)
db.put(user)
db.save
return Response(response=course.to_JSON(),
status=202,
mimetype="application/json")
#----------------------------------------------------------
# GET
#----------------------------------------------------------
@ -211,6 +272,66 @@ def getCourseByCampusName(name):
status=200,
mimetype="application/json")
@course_routes.route('/api/courses/getCoursesByUser/<string:token>/<string:campusId>', methods=['GET'])
@auto.doc()
def getCampusesByUser(token,campusId):
"""
<span class="card-title">This Call will return an array of all Campuses of a certain User</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'<br>
- campusId: 1234354543<br>
<br>
<br>
<b>Payload</b><br>
- NONE <br>
<br>
<br>
<b>Response</b>
<br>
200 - JSON Array, Example:<br>
[<br>
{
'title': 'JCE',<br>
'email_ending': '@post.jce.ac.il',<br>
'master_user_id': 123453433341, (User that created the campus)<br>
'avatar_url': 'http://some.domain.com/imagefile.jpg',<br>
'id' : 1234567890<br>
},<br>
....<br>
{<br>
...<br>
}req<br>
]<br>
<br>
403 - Invalid Token<br>
"""
user = get_user_by_token(token)
if user is None:
return bad_request("Bad user Token")
campus = Campus.get_by_id(int(campusId))
if campus is None:
return bad_request("No such Campus")
arr = []
for i in user['courses_id_list']:
course = Course.get_by_id(int(i))
if course.courseName == campus.title:
arr.append(dict(json.loads(course.to_JSON())))
if len(arr) != 0:
return Response(response=json.dumps(arr),
status=200,
mimetype="application/json")
else:
return Response(response=[],
status=200,
mimetype="application/json")
@course_routes.route('/api/courses/getMessagesByCourseName/<string:name>', methods=["GET"])
@auto.doc()
def getMessagesByCourseName(name):
@ -286,7 +407,7 @@ def deleteCourse(token,courseid):
<span class="card-title">This Call will delete a specific Course</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'
- seToken: 'seToken'<br>
- courseid: 'courseid'
<br>
<br>

View file

@ -58,7 +58,10 @@ def create_project(token):
"""
if not request.data:
return bad_request()
try:
payload = json.loads(request.data)
except Exception as e:
return bad_request("invalid JSON format")
#if not is_lecturer(token): #todo: change to lecturer id
# return forbidden("Invalid token or not a lecturer!")
@ -68,6 +71,7 @@ def create_project(token):
#todo: check legality
try:
project = Project(projectName=payload['projectName'], courseName=payload['courseName'], master_id=user.key().id(), gitRepository=payload['gitRepository'], membersId=[token])
except Exception as e:
@ -75,6 +79,11 @@ def create_project(token):
return bad_request()
db.put(project)
#update user projects list
user.projects_id_list.append(str(project.key().id()))
db.put(user)
db.save
return Response(response=project.to_JSON(),
status=200,
@ -84,6 +93,50 @@ def create_project(token):
# PUT
#----------------------------------------------------------
@project_routes.route('/api/projects/joinProject/<string:token>/<string:projectId>', methods=["PUT"])
@auto.doc()
def joinProject(token, projectId):
"""
<span class="card-title">This call will add the user (by token) to a specific project</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'<br>
- projectId: 123456789
<br>
<br>
<b>Payload</b><br>
- None <br>
<br>
<b>Response</b>
<br>
202 - Accepted
<br>
400 - Bad Request
<br>
403 - Invalid token or not a lecturer
"""
user = get_user_by_token(token)
if user is None:
return bad_request("Wrong user Token")
project = Project.get_by_id(int(projectId))
if project is None:
return bad_request("No such Project")
if user.key().id() in project.membersId:
return no_content("User is already member in Project")
project.membersId.append(str(user.key().id()))
db.put(project)
db.save
return Response(response=project.to_JSON(),
status=202,
mimetype="application/json")
#----------------------------------------------------------
# GET
@ -138,21 +191,73 @@ def getProjectsByCourseName(name):
@project_routes.route('/api/projects/getProjectsByUser/<string:token>', methods=["GET"])
@auto.doc()
def getProjectsByUser(token):
"""
<span class="card-title">>This Call will return an array of all projects in a given course</span>
<br>
<b>Route Parameters</b><br>
- token: 'SEToken'
<br>
<br>
<b>Payload</b><br>
- NONE
<br>
<br>
<b>Response</b>
<br>
200 - JSON Example:<br>
<code>
{<br>
'projectName': 'Advance Math',<br>
'courseName': 'JCE',<br>
'grade': 98,<br>
'logo_url': 'http://location.domain.com/image.jpg',<br>
'gitRepository': 'http://location.git.com/somthing',<br>
'membersId': ['bob', 'dylan', 'quentin', 'terentino'],<br>
'id' : 1234567890<br>
}
</code>
<br>
"""
user = get_user_by_token(token)
if user is None:
return bad_request("Bad Token")
arr = []
for p in user['projects_id_list']:
project = Project.get_by_id(int(p))
arr.append(dict(json.loads(project.to_JSON())))
if len(arr) != 0:
return Response(response=json.dumps(arr),
status=200,
mimetype="application/json")
else:
return Response(response=[],
status=200,
mimetype="application/json")
#----------------------------------------------------------
# DELETE
#----------------------------------------------------------
@project_routes.route('/api/projects/deleteProject/<string:token>/<string:projectid>', methods=['DELETE'])
@project_routes.route('/api/projects/deleteProject/<string:token>/<string:projectId>', methods=['DELETE'])
@auto.doc()
def deleteProject(token,projectid):
def deleteProject(token,projectId):
"""
<span class="card-title">This Call will delete a specific Project</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'
- courseid: 'projectid'
- projectId: 'projectid'
<br>
<br>
<b>Payload</b><br>
@ -178,7 +283,9 @@ def deleteProject(token,projectid):
# return forbidden("Invalid token or not a lecturer!")
user = get_user_by_token(token)
p = Project.get_by_id(int(projectid))
if user is None:
return bad_request("Bad user Token")
p = Project.get_by_id(int(projectId))
if p is None:
return bad_request("no such Project")

View file

@ -269,9 +269,6 @@ def getUserByToken(token):
mimetype="application/json") # Real response!
return no_content("No User Found")

View file

@ -10,6 +10,7 @@ class Course(db.Model):
# projects = db.StringListProperty(required=True,default=[])
startDate = db.DateProperty(required=True)
endDate = db.DateProperty(required=True)
membersId = db.StringListProperty(required=True)
def to_JSON(self):
data = {'courseName' : self.courseName,
@ -26,6 +27,7 @@ class Course(db.Model):
'month': self.endDate.month,
'day': self.endDate.day,
},
'membersId' : self.membersId,
'id' : self.key().id()
}
return json.dumps(data)

View file

@ -4,13 +4,15 @@ __author__ = 'Aran'
from google.appengine.ext import db
class Message(db.Model):
courseName = db.StringProperty(required=True)
groupId = db.IntegerProperty(required=True)
message = db.StringProperty(required=True)
msgDate = db.DateTimeProperty(required=True)
master_id = db.IntegerProperty(required=True)
isProject = db.BooleanProperty(default=False)
def to_JSON(self):
data = {
'courseName' : self.courseName,
'groupId' : self.groupId,
'message' : self.message,
'date' : {
'year': self.msgDate.year,
@ -19,6 +21,8 @@ class Message(db.Model):
'hour': self.msgDate.hour,
'minute': self.msgDate.minute
},
'id' : self.key().id()
'id' : self.key().id(),
'master_id' : self.master_id,
'isProject' : self.isProject
}
return json.dumps(data)

View file

@ -15,6 +15,7 @@ class User(db.Model):
campusName = db.StringProperty(required=True, default=" ")
campuses_id_list = db.StringListProperty(default=[])
courses_id_list = db.StringListProperty(default=[])
projects_id_list = db.StringListProperty(default=[])
def to_JSON(self):
data = {'username' : self.username,
@ -25,6 +26,7 @@ class User(db.Model):
'isFirstLogin' : self.isFirstLogin,
'campusName': self.campusName,
'campuses_id_list': self.campuses_id_list,
'courses_id_list': self.courses_id_list
'courses_id_list': self.courses_id_list,
'projects_id_list': self.projects_id_list
}
return json.dumps(data)