From 275b6684595ed47d44701a39627645d7efb07d49 Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Sun, 2 Aug 2015 14:42:41 +0300 Subject: [PATCH 1/2] added getTaskById --- SE_API/TaskRoutes.py | 129 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 22 deletions(-) diff --git a/SE_API/TaskRoutes.py b/SE_API/TaskRoutes.py index c587901..e4c0296 100644 --- a/SE_API/TaskRoutes.py +++ b/SE_API/TaskRoutes.py @@ -143,7 +143,79 @@ def create_task(token): status=200, mimetype="application/json") +@task_routes.route('/api/tasks/submitTask///', methods=['POST']) +@auto.doc() +def submitTask(token, taskId, ownerId): + """ + This call will create a new Task in the DB +
+ Route Parameters
+ - seToken: 'seToken' +
+
+ Payload
+ - JSON Object, Example:
+ {
+ "title":"task1",
+ "courseId":1234567890,
+ "description":"pls fddfsdfdsk",
+ "dueDate":{"year":2010,
+ "month":2,
+ "day":4
+ }, + "isPersonal":true,
+ "components":[
+ {
+ "type" : "should be type1",
+ "label" : "should be label1",
+ "isMandatory" : true,
+ "order" : 1
+ },
+ {
+ "type" : "should be type2",
+ "label" : "should be label2",
+ "isMandatory" : true,
+ "order" : 2
+ },
+ {
+ "type" : "should be type3",
+ "label" : "should be label3",
+ "isMandatory" : false,
+ "order" : 3
+ }
+ ]
+} +
+
+ Response +
+ 201 - Created +
+ 400 - Bad Request +
+ 403 - Invalid token or not a lecturer + """ + if not request.data: + return bad_request("no payload") + payload = json.loads(request.data) + user = get_user_by_token(token) + + task = Task.get_by_id(int(taskId)) + + #create components + for c in payload['components']: + try: + component = TaskComponent(taskId=task.key().id(), userId=(int(ownerId)), type=c['type'], label=c['label'], isMandatory=c['isMandatory'], order=c['order']) + except Exception as e: + print e + return bad_request("Bad component") + db.put(component) + db.save + + return Response(response=task.to_JSON(), + status=200, + mimetype="application/json") #---------------------------------------------------------- # PUT #---------------------------------------------------------- @@ -267,7 +339,7 @@ def getAllFutureCampusTasks(token, courseId): for t in query.run(): taskDic =dict(json.loads(t.to_JSON())) #add a key 'forSortDate' for sorting dates - taskTime = datetime.datetime(taskDic['dueDate']['year'], taskDic['dueDate']['month'], taskDic['dueDate']['day']) + taskTime = datetime.date(taskDic['dueDate']['year'], taskDic['dueDate']['month'], taskDic['dueDate']['day']) if taskTime >= datetime.date.today(): taskDic['forSortDate'] = taskTime arr.append(taskDic) @@ -335,7 +407,7 @@ def getAllFutureTasks(token): for t in query.run(): taskDic =dict(json.loads(t.to_JSON())) #add a key 'forSortDate' for sorting dates - taskTime = datetime.datetime(taskDic['dueDate']['year'], taskDic['dueDate']['month'], taskDic['dueDate']['day']) + taskTime = datetime.date(taskDic['dueDate']['year'], taskDic['dueDate']['month'], taskDic['dueDate']['day']) if taskTime >= datetime.date.today(): taskDic['forSortDate'] = taskTime arr.append(taskDic) @@ -558,15 +630,16 @@ def getAllUserTasks(token): -@task_routes.route('/api/tasks/getUserTaskById//', methods=["GET"]) +@task_routes.route('/api/tasks/getTaskById///', methods=["GET"]) @auto.doc() -def getUserTaskById(token, taskId): +def getTaskById(token, taskId, ownerId): """ >This Call will return an array of all components for a given task
Route Parameters
- SeToken: token
- taskId: 1234567890 + - ownerId: 123456789

Payload
@@ -606,33 +679,45 @@ def getUserTaskById(token, taskId): if task is None: return bad_request("Bad Task id") + task = json.loads(task.to_JSON()) + task['components'] = [] + task['grade'] = {} taskCompQuery = TaskComponent.all() - taskCompQuery.filter("taskId = ", task.key().id()) + taskCompQuery.filter("taskId = ", taskId) - if task.isPersonal: - taskCompQuery.filter("userId = ", user.key().id()) - else: - taskCompQuery.filter("userId = ", user.key().id())#TODO: fix to project + taskCompQuery.filter("userId = ", ownerId) + # if task.isPersonal: + # taskCompQuery.filter("userId = ", user.key().id()) + # else: + # taskCompQuery.filter("userId = ", user.key().id())#TODO: fix to project #check if never created a personalized task and if so, create it if taskCompQuery.count() == 0: - taskCompQuery = TaskComponent.all().filter("taskId = ", task.key().id()).filter("userId = ", -1) - for tc in taskCompQuery.run(): - tcNew = TaskComponent(taskId=tc.taskId, userId=user.key().id(), type=tc.type, label=tc.label, isMandatory=tc.isMandatory, order=tc.order) - db.put(tcNew) + print "here" + taskCompQuery = TaskComponent.all().filter("taskId =", int(taskId)).filter("userId =", -1) + print "query count is: ", taskCompQuery.count() + for tc in taskCompQuery.run(): + task['components'].append(dict(json.loads(tc.to_JSON()))) - grade = TaskGrade(grade=0, taskId=task.key().id(), userId=user.key().id()) - db.put(grade) + # for tc in taskCompQuery.run(): + # tcNew = TaskComponent(taskId=tc.taskId, userId=user.key().id(), type=tc.type, label=tc.label, isMandatory=tc.isMandatory, order=tc.order) + # db.put(tcNew) + + # grade = TaskGrade(grade=0, taskId=task.key().id(), userId=user.key().id()) + # db.put(grade) + grade = TaskGrade.all().filter("taskId = ", taskId).filter("userId = ", ownerId) + gradeFound = False + for g in grade.run(): + task['grade'] = g + gradeFound = True + if not gradeFound: + task['grade'] = {'taskId': taskId, 'userId': ownerId, 'grade': None} - - - - - - db.save - return no_content() + return Response(response=json.dumps(task), + status=200, + mimetype="application/json") From ad3a34d6ee99f8bd511df8833929ba90dfe8326f Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Sun, 2 Aug 2015 14:55:39 +0300 Subject: [PATCH 2/2] Changed task route in app js. task fill gets real task --- templates/js/app.js | 4 +- .../js/controllers/newTasksController.js | 4 +- templates/js/controllers/taskController.js | 66 +++++++++++-------- templates/js/services/apiService.js | 9 +++ 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/templates/js/app.js b/templates/js/app.js index cb5558a..1bec6f8 100644 --- a/templates/js/app.js +++ b/templates/js/app.js @@ -48,11 +48,11 @@ app.config(['$routeProvider', '$locationProvider', templateUrl: 'templates/views/newTask.html', controller: 'newTasksController' }) - .when('/tasks/overview/:taskId/:submitterId', { + .when('/tasks/overview/:taskId/:submitterId/:gId', { templateUrl: 'templates/views/task.html', controller: 'taskController' }) - .when('/tasks/fill/:taskId', { + .when('/tasks/fill/:taskId/:gId', { templateUrl: 'templates/views/task.html', controller: 'taskController' }) diff --git a/templates/js/controllers/newTasksController.js b/templates/js/controllers/newTasksController.js index e634291..c348a31 100644 --- a/templates/js/controllers/newTasksController.js +++ b/templates/js/controllers/newTasksController.js @@ -51,7 +51,7 @@ angular.module('SeHub').controller('newTasksController', ['$scope', 'apiService' console.error(err); }) } else { - alert('Fill All Shit!'); + alert('Please Fill All Task info & At least one component'); } } @@ -64,6 +64,8 @@ angular.module('SeHub').controller('newTasksController', ['$scope', 'apiService' return false; if (!$scope.task.date) return false; + if ($scope.task.components.length < 1) + return false; return true; } diff --git a/templates/js/controllers/taskController.js b/templates/js/controllers/taskController.js index 4133f05..979bfe2 100644 --- a/templates/js/controllers/taskController.js +++ b/templates/js/controllers/taskController.js @@ -6,12 +6,22 @@ angular.module('SeHub') var taskId = $routeParams.taskId; var submitterId = $routeParams.submitterId; + var token = $cookies['com.sehub.www']; + var groupId = $routeParams.gId; + apiService.getTaskById(token, taskId, groupId).success(function(data){ + $scope.task = data; + $scope.dateInit($scope.task.dueDate); + }).error(function(err){ + console.error('Error: ', err); + }) if (submitterId) { //In This Case we Only Want to show The Content of the Submitter $scope.readOnly = true; + } else { //In This Case We Need An Empty Task To Be Able To Fill It $scope.readOnly = false; + apiService.getTaskById(token, taskId, groupId); } $scope.dateInit = function(date) { @@ -81,35 +91,35 @@ angular.module('SeHub') = Mock Data = =================================*/ - $scope.task = { - "title": "task1", - "courseId": 1234567890, - "description": "one line\nsecondline\nthirdline", - "dueDate": { - "year": 2010, - "month": 2, - "day": 4 - }, - "isPersonal": true, - "components": [{ - "type": "radiobuttons", - "label": "pick One|this|orthis|MaybeThis", - "isMandatory": true, - "order": 1 - }, { - "type": "checkbox", - "label": "tick Me", - "isMandatory": true, - "order": 2 - }, { - "type": "textarea", - "label": "fill shit", - "isMandatory": false, - "order": 3 - }] - }; + // $scope.task = { + // "title": "task1", + // "courseId": 1234567890, + // "description": "one line\nsecondline\nthirdline", + // "dueDate": { + // "year": 2010, + // "month": 2, + // "day": 4 + // }, + // "isPersonal": true, + // "components": [{ + // "type": "radiobuttons", + // "label": "pick One|this|orthis|MaybeThis", + // "isMandatory": true, + // "order": 1 + // }, { + // "type": "checkbox", + // "label": "tick Me", + // "isMandatory": true, + // "order": 2 + // }, { + // "type": "textarea", + // "label": "fill shit", + // "isMandatory": false, + // "order": 3 + // }] + // }; - $scope.dateInit($scope.task.dueDate); + $scope.dueTime = function() { if (!$scope.task.date || $scope.task.date === '') diff --git a/templates/js/services/apiService.js b/templates/js/services/apiService.js index 505ed15..8e042fb 100644 --- a/templates/js/services/apiService.js +++ b/templates/js/services/apiService.js @@ -245,6 +245,15 @@ service.factory('apiService', ['$http', function($http) { url: url }; return $http(req); + }, + getTaskById: function(token, taskId, ownerId){ + var url = (DEBUG ? "http://localhost:8080" : "http://se-hub.appspot.com") + "/api/tasks/getTaskById/" + token + "/" + taskId + "/" + ownerId; + var req = { + method: 'GET', + url: url + }; + return $http(req); + } }; }]); \ No newline at end of file