def post(self, schedule_id): if request.json is not None: try: name = request.json['name'] priority = int(request.json['priority']) goal = request.json.get('goal',None) result = request.json.get('result',None) except KeyError: return DiaryBuilder.create_error_response(400, 'Missing keys in payload') except ValueError: return DiaryBuilder.create_error_response(400, 'Duration must be integer') else: return DiaryBuilder.create_error_response(415, 'please json') task = Task( name=name, priority=priority, goal=goal, result=result ) s_task = ScheduleTask( schedule_id=schedule_id, task=task ) try: db.session.add(s_task) db.session.commit() except IntegrityError: return DiaryBuilder.create_error_response(409, 'Task already exists') else: return Response(status=201)
def post(self, schedule_id): data = request.get_json() if data is not None: try: name = request.json['name'] value = float(request.json['value']) except KeyError: return DiaryBuilder.create_error_response( 400, 'Invalid json payload') except ValueError: return DiaryBuilder.create_error_response( 400, 'Value must be number') else: return DiaryBuilder.create_error_response(415, 'please json') item = Item(name=name, value=value) s_item = ScheduleItem(schedule_id=schedule_id, item=item) try: db.session.add(s_item) db.session.commit() except IntegrityError: return DiaryBuilder.create_error_response(409, 'Item already exists') else: return Response(status=201)
def patch(self, schedule_id, item_id): if request.json is not None: try: name = request.json['name'] value = float(request.json['value']) except KeyError: return DiaryBuilder.create_error_response( 400, 'Invalid json payload') except ValueError: return DiaryBuilder.create_error_response( 400, 'Value must be number') else: return DiaryBuilder.create_error_response(415, 'please json') query = Item.query.filter_by(id=item_id).first() if query is None: return DiaryBuilder.create_error_response(404, 'Item does not exist') query.name = name query.value = value try: db.session.add(query) db.session.commit() except IntegrityError: return DiaryBuilder.create_error_response(409, 'Item already exists') else: Response(status=204)
def post(self, schedule_id): if request.json is not None: try: name = request.json['name'] duration = int(request.json['duration']) note = request.json.get('note', None) except KeyError: return DiaryBuilder.create_error_response( 400, 'Missing keys in payload') except ValueError: return DiaryBuilder.create_error_response( 400, 'Duration must be integer') else: return DiaryBuilder.create_error_response(415, 'please json') event = Event(name=name, duration=duration, note=note) s_event = ScheduleEvent(schedule_id=schedule_id, event=event) try: db.session.add(s_event) db.session.commit() except IntegrityError: return DiaryBuilder.create_error_response(409, 'Event already exists') else: return Response(status=201)
def patch(self, schedule_id, task_id): if request.json is not None: try: name = request.json['name'] priority = int(request.json['priority']) goal = request.json.get('goal', None) result = request.json.get('result', None) except KeyError: return DiaryBuilder.create_error_response( 400, 'Missing keys in payload') except ValueError: return DiaryBuilder.create_error_response( 400, 'Duration must be integer') else: return DiaryBuilder.create_error_response(415, 'please json') task_item = Task.query.filter_by(id=task_id).first() if task_item is None: return DiaryBuilder.create_error_response(404, 'Task does not exist') task_item.name = name task_item.priority = priority task_item.goal = goal task_item.result = result try: db.session.commit() except IntegrityError: return DiaryBuilder.create_error_response(409, 'Task already exists') else: return Response(status=204)
def delete(self, schedule_id): query = Schedule.query.filter_by(id=schedule_id).first() if query is None: return DiaryBuilder.create_error_response( 404, 'Schedule does not exist') db.session.delete(query) db.session.commit() return Response(status=204)
def delete(self, schedule_id, task_id): task_item = Task.query.filter_by(id=task_id).first() if task_item is None: return DiaryBuilder.create_error_response(404, 'Task does not exist') db.session.delete(task_item) db.session.commit() return Response(status=204)
def post(self): if request.json is not None: try: name = request.json.get('name', None) start_time = parser.parse(request.json['start_time']) end_time = parser.parse(request.json['end_time']) except KeyError: return DiaryBuilder.create_error_response(400, 'Invalid JSON payload') except ValueError: return DiaryBuilder.create_error_response(400, 'Invalid time format in payload') else: return DiaryBuilder.create_error_response(415, 'Json payload please') schedule = Schedule( name=name, start_time=start_time, end_time=end_time, ) db.session.add(schedule) db.session.commit() DiaryBuilder.create_error_response(409, 'Schedule already exists') return Response(status=201)
def get(self): body = DiaryBuilder() body.add_namespace() body.add_control('self', url_for('.schedulecollection')) body.add_control('profile','/profiles/schedule/') body.add_control_add_schedule() query_results = Schedule.query.all() items = [] for schedule_item in query_results: item_dict = MasonBuilder() item_dict['name'] = schedule_item.name item_dict['id'] = schedule_item.id item_dict['start_time'] = schedule_item.start_time.strftime(TIME_FORMAT) item_dict['end_time'] = schedule_item.end_time.strftime(TIME_FORMAT) item_dict.add_control('self', url_for('.scheduleresource', schedule_id=schedule_item.id)) items.append(item_dict) body['items'] = items return Response(json.dumps(body, indent=4), status=200, mimetype=MIMETYPE)
def put(self, schedule_id): if request.json is not None: try: name = request.json.get('name', None) start_time = datetime.strptime(request.json['start_time'], TIME_FORMAT) end_time = datetime.strptime(request.json['end_time'], TIME_FORMAT) except KeyError: return DiaryBuilder.create_error_response( 400, 'Invalid JSON payload') except ValueError: return DiaryBuilder.create_error_response( 400, 'Invalid time format in payload') else: query = Schedule.query.filter_by(id=schedule_id).first() if query is None: return DiaryBuilder.create_error_response( 404, 'Schedule does not exit') query.name = name query.start_time = start_time query.end_time = end_time try: db.session.commit() except IntegrityError: return DiaryBuilder.create_error_response( 409, 'Schedule already exists') else: return Response(status=204) else: DiaryBuilder.create_error_response(415, 'Json payload please')
def get(self, schedule_id): schedule_item = Schedule.query.filter_by(id=schedule_id).first() if schedule_item is None: return DiaryBuilder.create_error_response(404, 'Schedule not found') body = DiaryBuilder() body['start_time'] = schedule_item.start_time.strftime(TIME_FORMAT) body['end_time'] = schedule_item.end_time.strftime(TIME_FORMAT) body['name'] = schedule_item.name body.add_control('self', url_for('.scheduleresource', schedule_id=schedule_id)) body.add_control('collection', url_for('.schedulecollection')) body.add_control('profile', '/profiles/schedule/') body.add_control_edit_schedule(schedule_id) body.add_control_delete_schedule(schedule_id) items_dict = DiaryBuilder() items_dict.add_control( 'item', url_for('.itemcollection', schedule_id=schedule_id)) items_dict.add_control( 'task', url_for('.taskcollection', schedule_id=schedule_id)) items_dict.add_control( 'event', url_for('.eventcollection', schedule_id=schedule_id)) body['items'] = [items_dict] return Response(json.dumps(body, indent=4), status=200, mimetype=MIMETYPE)
def get(self, schedule_id): query = ScheduleTask.query.filter_by(schedule_id=schedule_id).all() if len(query) == 0: return DiaryBuilder.create_error_response(404, 'Schedule does not exist or has no tasks') body = DiaryBuilder() body.add_namespace() body.add_control('self', url_for('.taskcollection', schedule_id=schedule_id)) body.add_control('collection', url_for('.scheduleresource',schedule_id=schedule_id)) body.add_control('profile','/profiles/task/') body.add_control_add_task(schedule_id) body.add_control_items_in(schedule_id) body.add_control_events_in(schedule_id) body.add_control_all_schedules() items = [] for task_item in query: task_item = task_item.task item_dict = MasonBuilder( name=task_item.name, priority=task_item.priority, goal=task_item.goal, result=task_item.result, id=task_item.id ) item_dict.add_control( 'self',url_for('.taskresource',schedule_id=schedule_id,task_id=task_item.id)) item_dict.add_control('profile','/profiles/task/') items.append(item_dict) body['items'] = items return Response(json.dumps(body, indent=4),status=200, mimetype=MIMETYPE)
def get(self, schedule_id): query = ScheduleItem.query.filter_by(schedule_id=schedule_id).all() if len(query) == 0: return DiaryBuilder.create_error_response( 404, 'Schedule does not exist or has no items') body = DiaryBuilder() body.add_namespace() body.add_control('self', url_for('.itemcollection', schedule_id=schedule_id)) body.add_control('collection', url_for('.scheduleresource', schedule_id=schedule_id)) body.add_control('profile', '/profiles/item/') body.add_control_add_item(schedule_id) body.add_control_events_in(schedule_id) body.add_control_tasks_in(schedule_id) body.add_control_all_schedules() items = [] for item in query: item = item.item item_dict = MasonBuilder(name=item.name, value=item.value, id=item.id) item_dict.add_control( 'self', url_for('.itemresource', schedule_id=schedule_id, item_id=item.id)) item_dict.add_control('profile', '/profiles/item/') items.append(item_dict) body['items'] = items return Response(json.dumps(body, indent=4), status=200, mimetype=MIMETYPE)
def get(self, schedule_id, item_id): query = Item.query.filter_by(id=item_id).first() if query is None: return DiaryBuilder.create_error_response(404, 'Item does not exist') body = DiaryBuilder() body['name'] = query.name body['value'] = query.value body.add_namespace() body.add_control( 'self', url_for('.itemresource', schedule_id=schedule_id, item_id=item_id)) body.add_control('collection', url_for('.itemcollection', schedule_id=schedule_id)) body.add_control('profile', '/profiles/item/') body.add_control_edit_item(schedule_id, item_id) body.add_control_add_item(schedule_id) body.add_control_delete_item(schedule_id, item_id) return Response(json.dumps(body, indent=4), status=200, mimetype=MIMETYPE)
def get(self, schedule_id, task_id): task_item = Task.query.filter_by(id=task_id).first() if task_item is None: return DiaryBuilder.create_error_response(404, 'Task does not exist') body = DiaryBuilder() body['name'] = task_item.name body['priority'] = task_item.priority body['goal'] = task_item.goal body['result'] = task_item.result body.add_namespace() body.add_control( 'self', url_for('.taskresource', schedule_id=schedule_id, task_id=task_id)) body.add_control('profile', '/profiles/task/') body.add_control('collection', url_for('.taskcollection', schedule_id=schedule_id)) body.add_control_add_task(schedule_id) body.add_control_edit_task(schedule_id, task_id) body.add_control_delete_task(schedule_id, task_id) return Response(json.dumps(body, indent=4), mimetype=MIMETYPE)