def get(cls, user_id=None): """Get user information.""" user_information = {} status_code = None user = User.query.filter_by(uuid=user_id).first() if user: user_information, _ = user_schema.dump(user) user_information['roles'], _ = basic_info_schema.dump(user.roles, many=True) status_code = 200 else: _, _, user_info = add_extra_user_info(g.current_user_token, user_id) if user_info.status_code != 200: return user_info.json(), user_info.status_code data = { 'createdAt': None, 'description': None, 'id': None, 'name': None, 'photo': None, 'modifiedAt': None, 'centerId': None, 'cohortId': None, 'roles': [] } data['id'] = user_info.json().get('id') data['email'] = user_info.json().get('email') data['name'] = (f"{user_info.json().get('first_name')} " f"{user_info.json().get('last_name')}") data['photo'] = user_info.json().get('picture') data['centerId'] = user_info.json().get('location').get('id') data['cohortId'] = user_info.json().get('cohort').get('id') user_information = data status_code = user_info.status_code location, _ = basic_info_schema.dump( Center.query.filter_by( uuid=user_information.pop('centerId')).first()) user_information['location'] = location cohort = Cohort.query.filter_by( uuid=user_information.pop('cohortId')).first() if cohort: cohort_serialized, _ = basic_info_schema.dump(cohort) user_information['cohort'] = cohort_serialized user_information['society'], _ = basic_info_schema.dump( cohort.society) user_information['roles'] = { role['name']: role['id'] for role in user_information['roles'] } return dict(data=user_information), status_code
def serialize_redmp(redemption): """To serialize and package redemptions.""" serial_data, _ = redemption_schema.dump(redemption) seriallized_user, _ = basic_info_schema.dump(redemption.user) serilaized_society, _ = basic_info_schema.dump(redemption.user.society) serial_data["user"] = seriallized_user serial_data["society"] = serilaized_society serial_data["center"], _ = basic_info_schema.dump(redemption.center) return serial_data
def put(self, redeem_id=None): """Complete Redemption Requests and mark them so.""" payload = request.get_json(silent=True) if not payload: return response_builder( dict(message="Data for editing must be provided", status="fail"), 400) if not redeem_id: return response_builder( dict(status="fail", message="RedemptionRequest id must be provided."), 400) redemp_request = self.RedemptionRequest.query.get(redeem_id) if not redemp_request: return response_builder( dict(data=None, status="fail", message="Resource does not exist."), 404) status = payload.get("status") if status == "completed": redemp_request.status = status CIO = self.Role.query.filter_by(name='cio').first() if CIO and CIO.users.all(): # TODO Add logging here email_payload = dict( sender=current_app.config["SENDER_CREDS"], subject="RedemptionRequest for {}".format( redemp_request.user.society.name), message= "Redemption Request on {} has been completed. Finance" " has wired the money to the reciepient.".format( redemp_request.name), recipients=([user.email for user in CIO.users] + [redemp_request.user.email])) self.email.send(current_app._get_current_object(), payload=email_payload, mail=self.mail) else: return response_builder( dict( status="Failed", message="Invalid status.", ), 400) redemp_request.save() mes = f"Redemption request status changed to {redemp_request.status}." serialized_redemption = serialize_redmp(redemp_request) serialized_completed_by, _ = basic_info_schema.dump(g.current_user) serialized_redemption["completedBy"] = serialized_completed_by return response_builder( dict(status="success", data=serialized_redemption, message=mes), 200)
def put(self): """Assign a cohort to a society. Return response (dict): key:status """ payload = request.get_json(silent=True) if not payload or not ('societyId' in payload and 'cohortId' in payload): return response_builder( dict(message="Error societyId and cohortId are required."), 400) society = self.Society.query.filter_by( uuid=payload.get('societyId')).first() if not society: return response_builder(dict(message="Error Invalid societyId."), 404) cohort = self.Cohort.query.filter_by( uuid=payload.get('cohortId')).first() if not cohort: return response_builder(dict(message="Error Invalid cohortId."), 404) if cohort.society_id == society.uuid: return response_builder(dict(message="Cohort already in society."), 409) society.cohorts.append(cohort) society.save() cohort_data = cohort_schema.dump(cohort).data cohort_meta_data = { 'society': basic_info_schema.dump(society).data, 'center': basic_info_schema.dump(cohort.center).data } cohort_data['meta'] = cohort_meta_data return response_builder( dict(message="Cohort added to society successfully", data=cohort_data), 200)
def put(self, redeem_id=None): """Complete Redemption Requests and mark them so.""" payload = request.get_json(silent=True) if not payload: return response_builder( dict(message="Data for editing must be provided", status="fail"), 400) if not redeem_id: return response_builder( dict(status="fail", message="RedemptionRequest id must be provided."), 400) redemp_request = RedemptionRequest.query.get(redeem_id) if not redemp_request: return response_builder( dict(data=None, status="fail", message="Resource does not exist."), 404) status = payload.get("status") if status == "completed": redemp_request.status = status send_email.delay( sender=current_app.config["SENDER_CREDS"], subject="RedemptionRequest for {}".format( redemp_request.user.society.name), message="Redemption Request on {} has been completed. Finance" " has wired the money to the reciepient.".format( redemp_request.name), recipients=[ redemp_request.user.email, current_app.config["CIO"] ]) else: return response_builder( dict( status="Failed", message="Invalid status.", ), 400) redemp_request.save() mes = f"Redemption request status changed to {redemp_request.status}." serialized_redemption = serialize_redmp(redemp_request) serialized_completed_by, _ = basic_info_schema.dump(g.current_user) serialized_redemption["completedBy"] = serialized_completed_by return response_builder( dict(status="success", data=serialized_redemption, message=mes), 200)
def post(cls): """Create Redemption Request.""" payload = request.get_json(silent=True) if not payload: return response_builder( dict(message="Redemption request must have data.", status="fail"), 400) result, errors = redemption_request_schema.load(payload) if errors: return response_builder(errors, 400) if result.get('value') > g.current_user.society.remaining_points: return response_builder( dict(message="Redemption request value exceeds your society's " "remaining points", status="fail"), 403) center = Center.query.filter_by(name=result.get('center')).first() if center: redemp_request = RedemptionRequest( name=result.get('name'), value=result.get('value'), description=result.get('description'), user=g.current_user, center=center, society=g.current_user.society) redemp_request.save() data, _ = redemption_schema.dump(redemp_request) data["center"], _ = basic_info_schema.dump(center) send_email.delay(sender=current_app.config["SENDER_CREDS"], subject="RedemptionRequest for {}".format( g.current_user.society.name), message="Redemption Request reason:{}." "Redemption Request value: {} points".format( redemp_request.name, redemp_request.value), recipients=[current_app.config["CIO"]]) return response_builder( dict( message="Redemption request created. Success Ops will be in" " touch soon.", status="success", data=serialize_redmp(redemp_request)), 201) else: return response_builder( dict(message= "Redemption request name, value and center required", status="fail"), 400)
def post(self): """Create Redemption Request.""" payload = request.get_json(silent=True) if not payload: return response_builder( dict(message="Redemption request must have data.", status="fail"), 400) result, errors = redemption_request_schema.load(payload) if errors: return response_builder(errors, 400) if result.get('value') > g.current_user.society.remaining_points: return response_builder( dict(message="Redemption request value exceeds your society's " "remaining points", status="fail"), 403) # bug fix this nigeria -> lagos if result.get('center') == 'nigeria': result['center'] = 'lagos' center = self.Center.query.filter_by(name=result.get('center')).first() if center: redemp_request = self.RedemptionRequest( name=result.get('name'), value=result.get('value'), description=result.get('description'), user=g.current_user, center=center, society=g.current_user.society) redemp_request.save() data, _ = redemption_schema.dump(redemp_request) data["center"], _ = basic_info_schema.dump(center) CIO = self.Role.query.filter_by(name='cio').first() if CIO and CIO.users.all(): # TODO Add logging here email_payload = dict( sender=g.current_user.email, subject="RedemptionRequest for {}".format( g.current_user.society.name), message="Redemption Request reason:{}." "Redemption Request value: {} points".format( redemp_request.name, redemp_request.value), recipients=[user.email for user in CIO.users]) self.email.send(current_app._get_current_object(), payload=email_payload, mail=self.mail) return response_builder( dict( message="Redemption request created. Success Ops will be in" " touch soon.", status="success", data=serialize_redmp(redemp_request)), 201) else: return response_builder( dict(message= "Redemption request name, value and center required", status="fail"), 400)
def put(self, redeem_id=None): """Approve or Reject Redemption requests.""" payload = request.get_json(silent=True) if not payload: return response_builder( dict(message="Data for editing must be provided", status="fail"), 400) result, errors = edit_redemption_request_schema.load(payload) if errors: return response_builder(errors, 400) if not redeem_id: return response_builder( dict(status="fail", message="RedemptionRequest id must be provided."), 400) redemp_request = self.RedemptionRequest.query.get(redeem_id) if not redemp_request: return response_builder( dict(data=None, status="fail", message="Resource does not exist."), 404) status = result.get("status") comment = result.get("comment") rejection_reason = result.get("rejection") or \ result.get('rejection_reason') if status == "approved": user = redemp_request.user society = self.Society.query.get(user.society_id) society.used_points = redemp_request redemp_request.status = status # Get the relevant Finance Center to respond on RedemptionRequest center_emails = {"kampala": "*****@*****.**"} finance_email = redemp_request.center.name.lower() + \ center_emails.get( redemp_request.center.name.lower(), "*****@*****.**" ) email_payload = dict( sender=current_app.config["SENDER_CREDS"], subject="RedemptionRequest for {}".format( redemp_request.user.society.name), message="Redemption Request on {} has been approved. Click " "the link below <a href='{}'>here</a> to view more " "details.".format( redemp_request.name, request.host_url + 'api/v1/societies/redeem/' + redeem_id), recipients=[finance_email]) self.email.send(current_app._get_current_object(), payload=email_payload, mail=self.mail) email_payload = dict( sender=current_app.config["SENDER_CREDS"], subject="RedemptionRequest for {}".format( redemp_request.user.society.name), message="Redemption Request on {} has been approved. Finance" " will be in touch.".format(redemp_request.name), recipients=[redemp_request.user.email]) self.email.send(current_app._get_current_object(), payload=email_payload, mail=self.mail) elif status == "rejected": redemp_request.status = status redemp_request.rejection = rejection_reason email_payload = dict( sender=current_app.config["SENDER_CREDS"], subject=f"Redemption Request for" f" {redemp_request.user.society.name}", message=f"This redemption request has been rejected for this" f" reason: {rejection_reason}", recipients=[redemp_request.user.email]) self.email.send(current_app._get_current_object(), payload=email_payload, mail=self.mail) elif comment: email_payload = dict( sender=current_app.config["SENDER_CREDS"], subject="More Info on RedemptionRequest for {}".format( redemp_request.user.society.name), message=comment, recipients=[redemp_request.user.email ]) # cover for requesting more information self.email.send(current_app._get_current_object(), payload=email_payload, mail=self.mail) else: return response_builder( dict( status="Failed", message="Invalid status.", ), 400) redemp_request.comment = comment or redemp_request.comment redemp_request.save() mes = f"Redemption request status changed to {status}." serialized_redemption = serialize_redmp(redemp_request) serialized_approved_by, _ = basic_info_schema.dump(g.current_user) serialized_redemption["approvedBy"] = serialized_approved_by return response_builder( dict(status="success", data=serialized_redemption, message=mes), 200)