def post(self): session = Session() # get news information provided in the request news_data = request.get_json(force=True) #create database model new_news = News(title=news_data['title'], content=news_data['content'], source=news_data['source'], published_at=news_data['published_at'], external_link=news_data['external_link'], state_abbr=news_data['state_abbr'], city_name=news_data['city_name']) # add new news entity to database session.add(new_news) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(new_news) } return jsonify(response)
def put(self, id): '''Altera uma resposta específica''' session = Session() given_reply = session.query(Reply).filter_by(id=id) if given_reply.scalar() is None: namespace.abort(404) reply = given_reply.one() # user and report cant be changed for datafield in request.args: if (datafield.lower() != 'user') and (datafield.lower() != 'report'): setattr(reply, datafield, request.args[datafield]) session.add(reply) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(reply) } return jsonify(response)
def put(self, id): '''Atualiza os dados de uma noticia''' session = Session() # get news determined_news = session.query(News).filter_by(id=id) #check if news exists if determined_news.scalar() is None: namespace.abort(404) # update news data with given values news = determined_news.one() for datafield in request.args: setattr(news, datafield, request.args[datafield]) session.add(news) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(news) } return jsonify(response)
def post(self): '''Cria um novo usuário''' session = Session() # get user data provided in the request userdata = request.get_json(force=True) # create database model new_user = User(name=userdata['name'], email=userdata['email'], phone=userdata['phone'], passhash=userdata['passhash'], salt=userdata['salt'], birthday=userdata['birthday'], zip_code=userdata['zip_code'], state_abbr=userdata['state_abbr'], city_name=userdata['city_name'], city_number=userdata['city_number'], area=userdata['area']) # add new user to database session.add(new_user) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(new_user) } return jsonify(response)
def put(self, id): '''Atualiza os dados de um usuário''' session = Session() # look up given user given_user = session.query(User).filter_by(id=id) # check if given user exists if given_user.scalar() is None: namespace.abort(404) # update user data with given values user = given_user.one() for datafield in request.args: setattr(user, datafield, request.args[datafield]) session.add(user) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(user) } return jsonify(response)
def put(self, id): '''Atualiza os dados de um privilégio''' session = Session() # look up given privilege given_privilege = session.query(Privilege).filter_by(identifier=id) # check if given privilege exists if given_privilege.scalar() is None: namespace.abort(404) # update privilege entry with given values privilege = given_privilege.one() for datafield in request.args: setattr(privilege, datafield, request.args[datafield]) session.add(privilege) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(privilege) } return jsonify(response)
def post(self): '''Cria uma nova denúncia''' session = Session() # get report data provided in the request reportdata = request.get_json(force=True) # create database model new_report = Report(state_abbr=reportdata['state_abbr'], city_name=reportdata['city_name'], area=reportdata['area'], geolatitude=reportdata['geolatitude'], geolongitude=reportdata['geolongitude'], description=reportdata['description']) # check if given user exists user_id = reportdata['user'] given_user = session.query(User).filter_by(id=user_id) if given_user.scalar() is None: response = { "status": 404, "message": "Not Found", "error": True, "response": "User not found" } return jsonify(response) # create and attach attachments for attachment in reportdata['attachments']: attach = Attachment(attachment_addr=attachment) attach.report = new_report attach.user = given_user.one() session.add(attach) # attach given user new_report.user = given_user.one() # add new report to database session.add(new_report) session.commit() attachs = [] for item in new_report.attachments: attachs.append(dictionarize(item)) # respond request response = { "status": 200, "message": "Success", "error": False, "response": { "report": dictionarize(new_report), "attachments": attachs } } return jsonify(response)
def put(self, id): '''Atualiza os dados de uma flag''' session = Session() # look up given flag given_flag = session.query(Flag).filter_by(identifier=id) # check if given flag exists if given_flag.scalar() is None: namespace.abort(404) # update flag entry with given values flag = given_flag.one() for datafield in request.args: if datafield.lower() != 'privileges': setattr(flag, datafield, request.args[datafield]) flag = session.query(Flag).filter_by(identifier=id).one() granted = [] if 'privileges' in request.args: # get list of privileges req_privileges = request.args['privileges'].split(",") grantable = [] for priv in req_privileges: # check if requested privilege exists item = session.query(Privilege).filter_by(identifier=priv) if item.scalar() is not None: # check if privilege can be granted to users privilege = item.one() if privilege.assignable is not False: grantable.append(privilege) # assign grantable privileges to flag flag.privileges = grantable for privilege in grantable: granted.append(privilege.identifier) session.add(flag) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": { "flag": dictionarize(flag), "privileges": granted } } return jsonify(response)
def put(self, id): '''Atualiza os dados de uma notificação''' session = Session() # look up given flag given_notification = session.query(Notification).filter_by(id=id) # check if given notification exists if given_notification.scalar() is None: namespace.abort(404) # update notification entry with given values notification = given_notification.one() for datafield in request.args: if datafield != 'user_id': setattr(notification, datafield, request.args[datafield]) if 'user_id' in request.args: # check if given user exists user = session.query(User).filter_by(id=request.args['user_id']) if user.scalar() is None: response = { "status": 404, "message": "Not Found", "error": True, "response": "User has not been found" } return jsonify(response) notification.user = user.one() session.add(notification) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(notification) } return jsonify(response)
def post(self): '''Cria uma nova resposta''' session = Session() # get reply data provided in the request replydata = request.get_json(force=True) # create database model new_reply = Reply(content=replydata['content']) # check if given user and report exists user_id = replydata['user'] given_user = session.query(User).filter_by(id=user_id) report_id = replydata['report'] given_report = session.query(Report).filter_by(id=report_id) if (given_user.scalar() is None) or (given_report.scalar() is None): response = { "status": 404, "message": "Not Found", "error": True, "response": "User or Report not found" } return jsonify(response) # attach user and report to reply new_reply.user = given_user.one() new_reply.report = given_report.one() # add new reply to database session.add(new_reply) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(new_reply) } return jsonify(response)
def post(self): '''Cria um novo privilégio''' session = Session() # get privilege information provided in the request privilege_data = request.get_json(force=True) # create database model new_privilege = Privilege(identifier=privilege_data['identifier'], assignable=privilege_data['assignable']) # add new privilege entity to database session.add(new_privilege) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(new_privilege) } return jsonify(response)
def post(self): '''Cria uma nova notificação''' session = Session() # get notification information provided in the request notif_data = request.get_json(force=True) # check if given user exists user_id = notif_data['user_id'] user = session.query(User).filter_by(id=user_id) if user.scalar() is None: response = { "status": 404, "message": "Not Found", "error": True, "response": "User has not been found" } return jsonify(response) # create database model new_notification = Notification(content=notif_data['content'], notiftype=notif_data['notiftype']) new_notification.user = user.one() # add new notification entity to database session.add(new_notification) session.commit() # respond request response = { "status": 200, "message": "Success", "error": False, "response": dictionarize(new_notification) } return jsonify(response)
def put(self, id): '''Atualiza os dados de uma denúncia''' session = Session() # look up given report given_report = session.query(Report).filter_by(id=id) # check if given report exists if given_report.scalar() is None: namespace.abort(404) # update report data with given values report = given_report.one() for datafield in request.args: setattr(report, datafield, request.args[datafield]) session.add(report) session.commit() attachs = [] for item in report.attachments: attachs.append(dictionarize(item)) # respond request response = { "status": 200, "message": "Success", "error": False, "response": { "report": dictionarize(report), "attachments": attachs } } return jsonify(response)
def post(self): '''Cria uma nova flag''' session = Session() # get flag information provided in the request flag_data = request.get_json(force=True) # check if flag already exists flag_id = flag_data['identifier'] flag_scalar = session.query(Flag).filter_by( identifier=flag_id).scalar() if flag_scalar is not None: response = { "status": 409, "message": "Conflict", "error": True, "response": "Flag already exists" } return jsonify(response) # create database model new_flag = Flag( identifier=flag_id, description=flag_data['description'], title=flag_data['title'], ) if 'privileges' in flag_data: # get list of privileges req_privileges = flag_data['privileges'] grantable = [] for priv in req_privileges: # check if requested privilege exists item = session.query(Privilege).filter_by(identifier=priv) if item.scalar() is not None: # check if privilege can be granted to users privilege = item.one() if privilege.assignable is not False: grantable.append(privilege) # assign grantable privileges to flag new_flag.privileges = grantable # add new flag entity to database session.add(new_flag) session.commit() # generate a list of granted privilege identifiers granted = [] for privilege in grantable: granted.append(privilege.identifier) # respond request response = { "status": 200, "message": "Success", "error": False, "response": { "flag": dictionarize(new_flag), "privileges": granted } } return jsonify(response)