Merge pull request #91 from sagidayan/API_Dev

fix function
This commit is contained in:
Sagi Dayan 2015-06-29 19:22:44 +03:00
commit 27ee77deed
3 changed files with 109 additions and 56 deletions

View file

@ -7,8 +7,11 @@ from GithubAPI.GithubAPI import GitHubAPI_Keys
from google.appengine.ext import db
import requests
import datetime
import thread
import threading
from flask import Flask, request, render_template, redirect, abort, Response
from flask import Flask, request, render_template, redirect, abort, Response, g
from flask.ext.github import GitHub
from flask.ext.cors import CORS, cross_origin
@ -58,6 +61,7 @@ def create_project(token):
<br>
403 - Invalid token or not a lecturer
"""
if not request.data:
return bad_request()
try:
@ -76,17 +80,40 @@ def create_project(token):
print e
return bad_request()
db.put(project)
try:
project.logo_url = payload['logo_url']
except Exception as e:
print e
pass
# def updateProjectInfo(response):
#
# projectDic = dict(json.loads(response.data))
# project = Project.get_by_id(int(projectDic['id']))
# project.info = json.dumps(get_github_data(project.gitRepository))
# db.put(project)
# db.save
# return response
db.put(project)
#update user projects list
user.projects_id_list.append(str(project.key().id()))
print "finish project"
db.put(user)
db.save
t1 = threading.Thread(target=updateProjectInfo,args=(project.key().id(),))
t1.start()
# thread.start_new_thread(updateProjectInfo, (project.key().id(),))
return Response(response=project.to_JSON(),
status=200,
mimetype="application/json")
#----------------------------------------------------------
# PUT
#----------------------------------------------------------
@ -312,4 +339,28 @@ def deleteProject(token,projectId):
@project_routes.route('/api/projects/help')
def documentation():
return auto.html()
return auto.html()
# def after_this_request(func):
# if not hasattr(g, 'call_after_request'):
# g.call_after_request = []
# g.call_after_request.append(func)
# return func
#
#
# @project_routes.after_request
# def per_request_callbacks(response):
# for func in getattr(g, 'call_after_request', ()):
# response = func(response)
# return response
#
def updateProjectInfo(projectId):
print "in updateProjectInfo"
import time
project = Project.get_by_id(int(projectId))
project.info = json.dumps(get_github_data(project.gitRepository))
db.put(project)
db.save
return

View file

@ -89,59 +89,59 @@ def updateUser(token):
db.save
return ok("User updated")
@user_routes.route('/api/users/addUserToCourse/<string:token>', methods=["PUT"])
@auto.doc()
def addUserToCourse(token):
"""
<span class="card-title">>This Call will add a course to user course list</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'
<br>
<br>
<b>Payload</b><br>
- JSON Object, Example: <br>
{<br>
'courseId': 1234567890<br>
}<br>
<br>
<b>Response</b>
<br>
200 - User updated
<br>
400 - Bad Request
"""
if not request.data:
return bad_request()
try:
payload = json.loads(request.data)
except Exception as e:
return bad_request()
#check user exists
user = get_user_by_token(token)
if user is None:
return bad_request("Not a user!")
#check course Exists
course = Course.get_by_id(payload['coursesId'])
if course is None:
return bad_request("No such Course!")
try:
user.courses_id_list.append(payload['coursesId'])
except Exception as e:
print e
return bad_request()
db.put(user)
db.save
return Response(response=user.to_JSON(),
status=200,
mimetype="application/json") # Real response!
# @user_routes.route('/api/users/addUserToCourse/<string:token>', methods=["PUT"])
# @auto.doc()
# def addUserToCourse(token):
# """
# <span class="card-title">>This Call will add a course to user course list</span>
# <br>
# <b>Route Parameters</b><br>
# - seToken: 'seToken'
# <br>
# <br>
# <b>Payload</b><br>
# - JSON Object, Example: <br>
# {<br>
# 'courseId': 1234567890<br>
# }<br>
# <br>
# <b>Response</b>
# <br>
# 200 - User updated
# <br>
# 400 - Bad Request
# """
#
# if not request.data:
# return bad_request()
#
# try:
# payload = json.loads(request.data)
# except Exception as e:
# return bad_request()
#
# #check user exists
# user = get_user_by_token(token)
# if user is None:
# return bad_request("Not a user!")
#
# #check course Exists
# course = Course.get_by_id(payload['coursesId'])
# if course is None:
# return bad_request("No such Course!")
#
# try:
# user.courses_id_list.append(payload['coursesId'])
# except Exception as e:
# print e
# return bad_request()
#
# db.put(user)
# db.save
# return Response(response=user.to_JSON(),
# status=200,
# mimetype="application/json") # Real response!
#

View file

@ -11,6 +11,7 @@ class Project(db.Model):
logo_url = db.StringProperty(required=False)
gitRepository = db.StringProperty(required=True)
membersId = db.StringListProperty(required=True)
info = db.TextProperty(required=False, default="{}")
def to_JSON(self):
data = {'projectName' : self.projectName,
@ -20,6 +21,7 @@ class Project(db.Model):
'logo_url' : self.logo_url,
'gitRepository' : self.gitRepository,
'membersId' : self.membersId,
'info': json.loads(self.info),
'id' : self.key().id()
}
return json.dumps(data)