def link_institution(self, user_id, institution_id): user = User.get(user_id=user_id) if self.limit_institution_number(user_id=user_id): crud_inst = CRUDInstitution() inst = crud_inst.get_institutions(institution_id=institution_id) user_inst = UserInstitution.get(user_id=user_id, institution_id=institution_id) if not user_inst is None: user_inst.status = "PENDING" else: user_inst = UserInstitution(user_id=user_id, institution_id=institution_id, status="PENDING", event_date=datetime.today()) commit() inst_data = crud_inst.get_institution( institution_id=institution_id, export_json=False) inst_data["status"] = "PENDING" result = inst_data else: result = { "message": "USUARIO NAO PODE SE VINCULAR EM MAIS DE 1 INSTITUICAO" } return result
def linked_users_institution(): inst = CRUDInstitution() if request.method == "GET": inst_id = request.args.get("institution_id", type=int) type = request.args.get("type", type=str) application.logger.info("/link-institution-users/ method: %s" % (request.method)) application.logger.info("params: %s" % (request.args)) application.logger.info("headers") application.logger.info(request.headers) if type is None: users = inst.list_linked_users(institution_id=inst_id) else: users = inst.list_linked_users(institution_id=inst_id, type=type) application.logger.info("result: %s" % (users)) users_json = json.dumps(users) resp = flask.Response(users_json) resp.headers['Access-Control-Allow-Origin'] = '*' #if len(users) > 0 : return resp, 200 #else: # return resp, 204 elif request.method == "POST": content = request.json user_id = content["user_id"] institution_id = content["institution_id"] application.logger.info("/link-institution-users/ method - %s" % (request.method)) application.logger.info("content %s" % (content)) result = inst.approve_user(institution_id=institution_id, user_id=user_id) application.logger.info("result %s" % (result)) resp = flask.Response(json.dumps(result)) resp.headers['Access-Control-Allow-Origin'] = '*' return resp, 200
def unlink_institution(self, user_id, institution_id): user_inst = UserInstitution.get(user_id=user_id, institution_id=institution_id) user_inst.status = "REMOVED_BY_USER" commit() crud_inst = CRUDInstitution() inst_data = crud_inst.get_institution(institution_id=institution_id, export_json=False) inst_data["status"] = "REMOVED_BY_USER" return inst_data
def delete_linked_users_institution(): inst = CRUDInstitution() if request.method == "POST": content = request.json application.logger.info("/link-institution-users/ method - %s" % (request.method)) application.logger.info("HEADERS ------ %s" % (request.headers)) application.logger.info("body %s" % (request.json)) result = inst.remove_user(institution_id=content["institution_id"], user_id=content["user_id"]) application.logger.info("result %s" % (result)) resp = flask.Response(json.dumps(result)) resp.headers['Access-Control-Allow-Origin'] = '*' return resp, 200
def list_linked_institution(self, user_id, status=["PENDING", "APPROVED"]): user_insts = select(user_inst for user_inst in UserInstitution if user_inst.user_id.user_id == user_id and user_inst.status in status) crud_inst = CRUDInstitution() result = {} for user_inst in user_insts: inst_data = crud_inst.get_institution( institution_id=user_inst.institution_id.institution_id, export_json=False) if user_inst.status in ["PENDING", "APPROVED"]: inst_data["status"] = user_inst.status result = inst_data return result
def get_promotion(self, promotion_id): inst_ctrl = CRUDInstitution() promotion = Promotion.get(promotion_id=promotion_id) promotion_dict = { "promotion_id": promotion.promotion_id, "title": promotion.title, "description": promotion.description, "link": promotion.link, "date_initial": promotion.date_initial.strftime("%d/%m/%Y"), "date_end": promotion.date_end.strftime("%d/%m/%Y") } inst = inst_ctrl.get_institution( institution_id=promotion.institution_id.institution_id, export_json=False) promotion_dict.update(inst) return promotion_dict
def institutions(): inst = CRUDInstitution() if request.method == "POST": content = request.json #try: result = inst.create_institution( city_id=content["city-id"], institution_type_id=content["institution-type"], name=content["name"], email=content["email"], site=content["site"], street=content["street"], number=content["number"], complement=content["complement"], district=content["district"], phone=content["phone"], postal_code=content["postal-code"]) #except KeyError as ex: # return json.dumps({"message": "Field not found %s" % (str(ex))}), 401 resp = flask.Response(result) resp.headers['Access-Control-Allow-Origin'] = '*' return resp, 201 elif request.method == "GET": result = inst.list_institution( query=request.args.get("query", type=str)) resp = flask.Response(result) resp.headers['Access-Control-Allow-Origin'] = '*' return resp, 200 elif request.method == "PUT": content = request.json result = inst.alter_instution(**content) resp = flask.Response(result) resp.headers['Access-Control-Allow-Origin'] = '*' return resp, 200 elif request.method == "DELETE": content = request.json try: result = inst.delete_institution( institution_id=content["institution-id"]) resp = flask.Response(json.dumps(result)) resp.headers['Access-Control-Allow-Origin'] = '*' return resp, 200 except Exception as ex: return "", 400
def institution_type(): inst = CRUDInstitution() if request.method == "POST": content = request.json result = inst.add_institution_type(content["description"]) resp = flask.Response(result) resp.headers['Access-Control-Allow-Origin'] = '*' return resp, 201 elif request.method == "GET": result = inst.list_institution_type() resp = flask.Response(result) resp.headers['Access-Control-Allow-Origin'] = '*' return resp, 200 elif request.method == "PUT": content = request.json result = inst.update_institution_type( id=content["id"], description=content["description"]) return "", 200 elif request.method == "DELETE": content = request.json inst.remove_institution_type(content["id"]) return "", 200