def get(cls): claims = get_jwt_claims() tarefas = Tarefa.find_all(claims['id']) if (tarefas): return Tarefa_list_schema.dump(tarefas), 200 else: return {'msg':'Sem tarefas'}, 401
def put(cls, id: int): claims = get_jwt_claims() calendario = Calendario().find_by_id(id) if not calendario: return {'msg': 'Não foi encontrado o agendamento'}, 401 else: if 'title' in request.get_json(): calendario.title = request.get_json()['title'] if 'start' in request.get_json(): calendario.start = request.get_json()['start'] if 'end' in request.get_json(): calendario.end = request.get_json()['end'] if 'gps' in request.get_json(): calendario.gps = request.get_json()['gps'] if 'descricao' in request.get_json(): calendario.descricao = request.get_json()['descricao'] if 'users' in request.get_json(): calendario.users.clear() for user in request.get_json()['users']: calendario.users.append(User.find_by_id(user)) calendario.users.append(User.find_by_id(claims['id'])) if 'tarefas' in request.get_json(): calendario.tarefas.clear() for tarefa in request.get_json()['tarefas']: calendario.tarefas.append(Tarefa.find_by_id(tarefa)) calendario.save_to_db() return Calendario_schema.dump(calendario), 200
def get(cls, id: int): tarefa = Tarefa.find_by_id(id) claims = get_jwt_claims() if tarefa: documentosTarefas = Documento.find_by_tarefa(id) return Documento_list_schema.dump(documentosTarefas), 200 return {'msg': 'Tarefa não foi encontrado'}, 401
def get(cls, id: int): calen = Calendario.find_by_id(id) listDocuments = [] if calen: calenjson = Calendario_schema.dump(calen) print(calenjson) for i in calenjson['tarefas']: taref = Tarefa.find_by_id(i) documentosTarefas = Documento.find_by_tarefa(int(taref.idTarefa)) print( Documento_list_schema.dump(documentosTarefas)) print(taref.idTarefa) listDocuments.append(Documento_list_schema.dump(documentosTarefas)) listDocuments return listDocuments, 200 return {'msg' : 'Não foram encontrados registos'}, 401
def delete(cls, id: int): claims = get_jwt_claims() tarefa = Tarefa.find_by_id(id) if tarefa: if tarefa.idUser != claims['id']: return {"msg": "Não tem permissões para apagar a tarefa"}, 401 if len(tarefa.documentos) > 0: for doc in tarefa.documentos: try: os.remove("static/" + doc.localizacao) except: pass doc.delete_from_db() tarefa.delete_from_db() return {"msg": "Tarefa apagada com sucesso"}, 200 else: return {"msg": "Tarefa não foi encontrada"}, 401
def post(cls): claims = get_jwt_claims() if 'name' not in request.form or 'descricao' not in request.form: return {"msg": "Falta parametros"}, 401 tarefa = Tarefa() tarefa.idUser = claims['id'] tarefa.name = request.form["name"] tarefa.descricao = request.form["descricao"] if 'estado' in request.form: tarefa.estado = request.form["estado"] else: tarefa.estado = 0 i = 0 if 'usersTarefas'+ str(i) in request.form: while 'usersTarefas' + str(i) in request.form: tarefa.usersTarefas.append(User.find_by_id(request.form["usersTarefas"+str(i)])) i+=1 tarefa.save_to_db() i = 0 while 'documentos' + str(i) in request.files: file = request.files['documentos' + str(i)] filenameSplitted = file.filename.split(".") filename = str(time.time()) + "." + filenameSplitted[len(filenameSplitted) - 1] file.save(os.path.join("static/documents", filename)) Doc = Documento() Doc.idTarefa = tarefa.idTarefa Doc.localizacao = "documents/" + filename Doc.nome = file.filename[0:50] Doc.save_to_db() i += 1 return Tarefa_schema.dump(tarefa), 200
def post(cls): claims = get_jwt_claims() calendario = Calendario() calendario.title = request.get_json()['title'] calendario.start = request.get_json()['start'] calendario.end = request.get_json()['end'] calendario.gps = request.get_json()['gps'] calendario.descricao = request.get_json()['descricao'] if 'users' in request.get_json(): calendario.users.clear() for user in request.get_json()['users']: calendario.users.append(User.find_by_id(user)) calendario.users.append(User.find_by_id(claims['id'])) if 'tarefas' in request.get_json(): calendario.tarefas.clear() for tarefa in request.get_json()['tarefas']: calendario.tarefas.append(Tarefa.find_by_id(tarefa)) calendario.save_to_db() return Calendario_schema.dump(calendario), 200
def post(cls, id: int): tarefa = Tarefa.find_by_id(id) claims = get_jwt_claims() if tarefa: i = 0 while 'documentos' + str(i) in request.files: file = request.files['documentos' + str(i)] filenameSplitted = file.filename.split(".") filename = str(time.time()) + "." + filenameSplitted[len(filenameSplitted) - 1] file.save(os.path.join("static/documents", filename)) Doc = Documento() Doc.idTarefa = tarefa.idTarefa Doc.localizacao = "documents/" + filename Doc.nome = file.filename[0:50] Doc.save_to_db() i += 1 documentosTarefas = Documento.find_by_tarefa(id) return Documento_list_schema.dump(documentosTarefas), 200 return {'msg': 'Tarefa não foi encontrado'}, 401
def put(cls, id: int): tarefa = Tarefa.find_by_id(id) claims = get_jwt_claims() if tarefa: if tarefa.idUser != claims['id']: return {"msg": "Não tem permissões para editar a tarefa"}, 401 if 'name' in request.form: tarefa.name = request.form["name"] if 'descricao' in request.form: tarefa.descricao = request.form["descricao"] if 'estado' in request.form: tarefa.estado = request.form["estado"] i=0 tarefa.usersTarefas.clear() if 'usersTarefas' + str(i) in request.form: while 'usersTarefas' + str(i) in request.form: tarefa.usersTarefas.append(User.find_by_id(request.form["usersTarefas" + str(i)])) i += 1 tarefa.save_to_db() return {"msg": "Tarefa alterada com sucesso"}, 200 else: return {"msg": "Tarefa não foi encontrada"}, 401
def get(cls, id: int): tarefa = Tarefa.find_by_id(id) if tarefa: return Tarefa_schema.dump(tarefa), 200 else: return {"msg": "Tarefa não foi encontrada"}, 401