From b1f1400628686498c1c2c640ff4d9ae149d55f7a Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Mon, 29 Jun 2015 19:21:18 +0300 Subject: [PATCH] fix function --- SE_API/ProjectRoutes.py | 57 +++++++++++++++++++-- SE_API/UserRoutes.py | 106 ++++++++++++++++++++-------------------- models/Project.py | 2 + 3 files changed, 109 insertions(+), 56 deletions(-) diff --git a/SE_API/ProjectRoutes.py b/SE_API/ProjectRoutes.py index 59127c9..686c267 100644 --- a/SE_API/ProjectRoutes.py +++ b/SE_API/ProjectRoutes.py @@ -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):
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() \ No newline at end of file + 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 \ No newline at end of file diff --git a/SE_API/UserRoutes.py b/SE_API/UserRoutes.py index a21e997..90715da 100644 --- a/SE_API/UserRoutes.py +++ b/SE_API/UserRoutes.py @@ -89,59 +89,59 @@ def updateUser(token): db.save return ok("User updated") -@user_routes.route('/api/users/addUserToCourse/', methods=["PUT"]) -@auto.doc() -def addUserToCourse(token): - """ - >This Call will add a course to user course list -
- Route Parameters
- - seToken: 'seToken' -
-
- Payload
- - JSON Object, Example:
- {
- 'courseId': 1234567890
- }
-
- Response -
- 200 - User updated -
- 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/', methods=["PUT"]) +# @auto.doc() +# def addUserToCourse(token): +# """ +# >This Call will add a course to user course list +#
+# Route Parameters
+# - seToken: 'seToken' +#
+#
+# Payload
+# - JSON Object, Example:
+# {
+# 'courseId': 1234567890
+# }
+#
+# Response +#
+# 200 - User updated +#
+# 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! +# diff --git a/models/Project.py b/models/Project.py index 042c082..a9de13f 100644 --- a/models/Project.py +++ b/models/Project.py @@ -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)