Пример #1
0
 def post(self):
     """Add User ID to task as a favorite."""
     try:
         self.valid_args()
         data = json.loads(request.data)
         if (len(data.keys()) != 1) or ('task_id' not in data.keys()):
             raise AttributeError
         if current_user.is_anonymous():
             raise Unauthorized
         uid = current_user.id
         tasks = task_repo.get_task_favorited(uid, data['task_id'])
         if len(tasks) == 1:
             task = tasks[0]
         if len(tasks) == 0:
             task = task_repo.get_task(data['task_id'])
             if task is None:
                 raise NotFound
             if task.fav_user_ids is None:
                 task.fav_user_ids = [uid]
             else:
                 task.fav_user_ids.append(uid)
             task_repo.update(task)
             self._log_changes(None, task)
         return Response(json.dumps(task.dictize()), 200,
                         mimetype='application/json')
     except Exception as e:
         return error.format_exception(
             e,
             target=self.__class__.__name__.lower(),
             action='POST')
Пример #2
0
 def post(self):
     """Add User ID to task as a favorite."""
     try:
         self.valid_args()
         data = json.loads(request.data)
         if (len(data.keys()) != 1) or ('task_id' not in data.keys()):
             raise AttributeError
         if current_user.is_anonymous():
             raise Unauthorized
         uid = current_user.id
         tasks = task_repo.get_task_favorited(uid, data['task_id'])
         if len(tasks) == 1:
             task = tasks[0]
         if len(tasks) == 0:
             task = task_repo.get_task(data['task_id'])
             if task is None:
                 raise NotFound
             if task.fav_user_ids is None:
                 task.fav_user_ids = [uid]
             else:
                 task.fav_user_ids.append(uid)
             task_repo.update(task)
             self._log_changes(None, task)
         return Response(json.dumps(task.dictize()),
                         200,
                         mimetype='application/json')
     except Exception as e:
         return error.format_exception(
             e, target=self.__class__.__name__.lower(), action='POST')
Пример #3
0
 def delete(self, oid):
     """Delete User ID from task as a favorite."""
     try:
         if current_user.is_anonymous():
             raise abort(401)
         uid = current_user.id
         tasks = task_repo.get_task_favorited(uid, oid)
         if tasks == []:
             raise NotFound
         if len(tasks) == 1:
             task = tasks[0]
         idx = task.fav_user_ids.index(uid)
         task.fav_user_ids.pop(idx)
         task_repo.update(task)
         return Response(json.dumps(task.dictize()),
                         200,
                         mimetype='application/json')
     except Exception as e:
         return error.format_exception(
             e, target=self.__class__.__name__.lower(), action='DEL')
Пример #4
0
 def delete(self, oid):
     """Delete User ID from task as a favorite."""
     try:
         if current_user.is_anonymous():
             raise abort(401)
         uid = current_user.id
         tasks = task_repo.get_task_favorited(uid, oid)
         if tasks == []:
             raise NotFound
         if len(tasks) == 1:
             task = tasks[0]
         idx = task.fav_user_ids.index(uid)
         task.fav_user_ids.pop(idx)
         task_repo.update(task)
         return Response(json.dumps(task.dictize()), 200,
                         mimetype='application/json')
     except Exception as e:
         return error.format_exception(
             e,
             target=self.__class__.__name__.lower(),
             action='DEL')