def post(self): """Handle post requests.""" post_author_key = self.request.get('receiver_key') sender_url_key = self.request.get('sender_key') post_key = self.request.get('entity_key') entity_type = self.request.get('entity_type') current_institution_key = ndb.Key( urlsafe=self.request.get('current_institution')) sender_inst_key = self.request.get( 'sender_institution_key') and ndb.Key( urlsafe=self.request.get('sender_institution_key')) post = ndb.Key(urlsafe=post_key).get() notification_message = post.create_notification_message( ndb.Key(urlsafe=sender_url_key), current_institution_key, sender_inst_key) subscribers = [subscriber.urlsafe() for subscriber in post.subscribers] user_is_author = post_author_key == sender_url_key for subscriber in subscribers: subscriber_is_sender = subscriber == sender_url_key if not (user_is_author and subscriber_is_sender) and not subscriber_is_sender: send_message_notification(receiver_key=subscriber, notification_type=entity_type, entity_key=post_key, message=notification_message)
def delete(self, user, post_urlsafe): """Handle DELETE Requests.""" """Get the post from the datastore.""" obj_key = ndb.Key(urlsafe=post_urlsafe) post = obj_key.get() is_admin = user.has_permission("remove_posts", post.institution.urlsafe()) is_author = user.has_permission("remove_post", post_urlsafe) Utils._assert(not is_admin and not is_author, "The user can not remove this post", NotAuthorizedException) post.delete(user) if (is_admin and not is_author): notification_message = post.create_notification_message( user_key=user.key, current_institution_key=user.current_institution, sender_institution_key=post.institution) send_message_notification(receiver_key=post.author.urlsafe(), notification_type='DELETED_POST', entity_key=post.key.urlsafe(), message=notification_message, entity=json.dumps( post.make(self.request.host)))
def post(self, user, post_key, comment_id=None, reply_id=None): """Handle POST Requests.""" """This method is only meant to give like in post, comment or reply and send notification.""" post = ndb.Key(urlsafe=post_key).get() Utils._assert(post.state == 'deleted', "This post has been deleted", EntityException) if comment_id: comment = post.like_comment(user, comment_id, reply_id) notification_type = 'LIKE_COMMENT' user_is_the_author = comment['author_key'] == user.key.urlsafe() if not user_is_the_author: receiver_key = comment['author_key'] notification_message = post.create_notification_message( user_key=user.key, current_institution_key=user.current_institution, sender_institution_key=post.institution) send_message_notification(receiver_key=receiver_key, notification_type=notification_type, entity_key=post_key, message=notification_message) else: post = post.like(user.key) entity_type = 'LIKE_POST' params = { 'receiver_key': post.author.urlsafe(), 'sender_key': user.key.urlsafe(), 'entity_key': post.key.urlsafe(), 'entity_type': entity_type, 'current_institution': user.current_institution.urlsafe(), 'sender_institution_key': post.institution.urlsafe() } enqueue_task('post-notification', params)
def post(self): """Send notifications to institution followers.""" sender_key = self.request.get('sender_key') entity_type = self.request.get('entity_type') entity_key = self.request.get('entity_key') current_institution_key = ndb.Key( urlsafe=self.request.get('current_institution')) entity = self.request.get('entity') if self.request.get( 'entity') else None inst_key = self.request.get('institution_key') institution = ndb.Key(urlsafe=inst_key).get() obj = ndb.Key( urlsafe=entity_key).get() if (entity_key) else institution notification_message = obj.create_notification_message( ndb.Key(urlsafe=sender_key), current_institution_key) for follower_key in institution.followers: follower = follower_key.get() is_active = follower.state == "active" if is_active and follower.key.urlsafe() != sender_key: send_message_notification(receiver_key=follower.key.urlsafe(), notification_type=entity_type, entity_key=entity_key or inst_key, message=notification_message, entity=entity)
def test_send_message_notification(self, taskqueue_add, create_entity): """Test send_message_notification method.""" sender = mocks.create_user() sender.photo_url = "photo-url" sender.put() receiver = mocks.create_user() institution = mocks.create_institution() current_institution = {"name": institution.name} post = mocks.create_post(receiver.key, institution.key) notification_type = "LIKE_POST" expected_message = { 'from': { 'name': sender.name.encode('utf8'), 'photo_url': sender.photo_url, 'institution_name': institution.name }, 'to': { 'institution_name': '' }, 'current_institution': current_institution } service_messages.send_message_notification( receiver_key=receiver.key.urlsafe(), notification_type=notification_type, entity_key=post.key.urlsafe(), message=expected_message) self.assertTrue(create_entity.called, "Should have called the create_entity method") self.assertTrue(taskqueue_add.called, "Should have called the add method from taskqueue")
def notify_admins(user): """Notify the admins about the user removal.""" for institution_key in user.institutions: admin_key = institution_key.get().admin notification_message = user.create_notification_message( user.key, institution_key) send_message_notification(receiver_key=admin_key.urlsafe(), notification_type='DELETED_USER', entity_key=institution_key.urlsafe(), message=notification_message)
def delete(self, user, url_string): """Delete member of specific institution.""" institution_key = ndb.Key(urlsafe=url_string) user.check_permission('remove_member', "User is not allowed to remove member", url_string) institution = institution_key.get() member_key = self.request.get('removeMember') member = ndb.Key(urlsafe=member_key) member = member.get() institution.remove_member(member) if member.state != 'inactive': notification_message = institution.create_notification_message( user.key, user.current_institution) send_message_notification(receiver_key=member.key.urlsafe(), notification_type='DELETE_MEMBER', entity_key=institution.key.urlsafe(), message=notification_message) justification = self.request.get('justification') subject = get_subject( 'LINK_REMOVAL') if member.state != 'inactive' else get_subject( 'INACTIVE_USER') email_sender = RemoveMemberEmailSender( **{ 'receiver': member.email[0], 'subject': subject, 'user_name': member.name, 'user_email': member.email[0], 'justification': justification, 'institution_name': institution.name, 'institution_admin': institution.admin.get().name, 'institution_email': institution.email, 'institution_key': institution.key.urlsafe(), 'html': 'remove_member_email.html' if member.state != 'inactive' else 'inactive_user_email.html' }) email_sender.send_email()
def notify_institution_removal(institution, remove_hierarchy, user, current_institution_key=None): """This method has two possibilities of flow depending on the remove_hierarchy's value. If it's true, the method send email and notification to all the hierarchy. Otherwise it send just to the first institution. Params: institution -- the current institution in the hierarchy whose admin will receive an email and a notification. remove_hierarchy -- string that works as a flag informing if the hierarchy. has been removed or not. user -- the user who made the request to remove the institution. """ subject = get_subject('INSTITUION_REMOVAL') body = """Lamentamos informar que a instituição %s foi removida pelo usuário %s """ % (institution.name, user.name) email_params = { "body": body, "subject": subject, "inst_key": institution.key.urlsafe() } email_sender = RemoveInstitutionEmailSender(**email_params) email_sender.send_email() user_has_to_receive_notification = institution.admin != user.key if user_has_to_receive_notification: notification_message = institution.create_notification_message( user_key=user.key, current_institution_key=current_institution_key, sender_institution_key=institution.key) send_message_notification(receiver_key=institution.admin.urlsafe(), notification_type='DELETED_INSTITUTION', entity_key=institution.key.urlsafe(), message=notification_message) if remove_hierarchy == "true": for child_key in institution.children_institutions: child = child_key.get() if child.state == "inactive": notify_institution_removal(child, remove_hierarchy, user, current_institution_key)
def post(self, user, post_key, comment_id): """Handle Post Comments requests.""" data = json.loads(self.request.body) reply_data = data['replyData'] post = ndb.Key(urlsafe=post_key).get() institution = post.institution.get() Utils._assert(institution.state == 'inactive', "The institution has been deleted", NotAuthorizedException) Utils._assert(post.state == 'deleted', "This post has been deleted", EntityException) reply = Comment.create(reply_data, user) post.reply_comment(reply, comment_id) notification_message = post.create_notification_message( user_key=user.key, current_institution_key=user.current_institution, sender_institution_key=post.institution ) notification_type = 'COMMENT' if (post.author != user.key): send_message_notification( receiver_key=post.author.urlsafe(), notification_type=notification_type, entity_key=post.key.urlsafe(), message=notification_message ) comment = post.get_comment(comment_id) notification_type = "REPLY_COMMENT" if (comment.get('author_key') != user.key.urlsafe()): send_message_notification( receiver_key=comment.get('author_key'), notification_type=notification_type, entity_key=post.key.urlsafe(), message=notification_message ) self.response.write(json.dumps(Utils.toJson(reply)))
def send_notification(self, current_institution, sender_key=None, receiver_key=None, notification_type=None, entity_key=None, message=None): """Method of send notification to invitee.""" sender_key = sender_key or self.sender_key if not receiver_key: active_invitee = User.get_active_user(self.invitee) receiver_key = active_invitee and active_invitee.key if receiver_key: notification_type = notification_type or 'INVITE' entity_key = entity_key or self.key.urlsafe() notification_message = message or self.create_notification_message( sender_key, current_institution) send_message_notification(receiver_key=receiver_key.urlsafe(), notification_type=notification_type, entity_key=entity_key, message=notification_message)
def delete(self, user, institution_key): """Handle DELETE Requests. This method delete an institution, which key is received as a paramater, from the user. In oposite of institution_members_handler's delete method, here the user requests to remove an intitution. There, the institution's admin makes the request. """ institution_key = ndb.Key(urlsafe=institution_key) institution = institution_key.get() user.remove_institution(institution_key) institution.remove_member(user) subject = get_subject('LINK_REMOVAL') message = """Lamentamos informar que %s removeu o vínculo com sua institutição %s """ % (user.name, institution.name) body = message + """ Equipe da Plataforma CIS """ admin = institution.admin.get() email_sender = LeaveInstitutionEmailSender(**{ 'receiver': admin.email, 'subject': subject, 'body': body }) email_sender.send_email() notification_message = user.create_notification_message( user.key, user.current_institution, sender_institution_key=institution.key) send_message_notification(receiver_key=admin.key.urlsafe(), notification_type='LEFT_INSTITUTION', entity_key=institution.key.urlsafe(), message=notification_message)
def send_notification(self): """ Method to send notification. """ send_message_notification(**self.format_notification())
def delete(self, user, institution_key, institution_link): """ Handle delete link between institutions. This handler remove the link between two institutions. If the parameter isParent is true, it means that the removal request has been made from a child institution, otherwise the request has been made by a parent institution. """ user.check_permission( 'remove_link', "User is not allowed to remove link between institutions", institution_key) is_parent = self.request.get('isParent') # If isParent is true, this attribute # holds the reference of the child intitution. institution = ndb.Key(urlsafe=institution_key).get() # If isParent is true, this attribute # holds the reference of the parent intitution. institution_link = ndb.Key(urlsafe=institution_link).get() Utils._assert(not type(institution) is Institution, "Key is not an institution", EntityException) Utils._assert(not type(institution_link) is Institution, "Key is not an institution", EntityException) Utils._assert(institution.state == 'inactive', "The institution has been deleted", NotAuthorizedException) Utils._assert(institution_link.state == 'inactive', "The institution has been deleted", NotAuthorizedException) institution.remove_link(institution_link, is_parent) admin = institution_link.admin if is_parent == "true": enqueue_task( 'remove-admin-permissions', { 'institution_key': institution.key.urlsafe(), 'parent_key': institution_link.key.urlsafe() }) email_sender = RequestLinkEmailSender( **{ 'institution_parent_name': institution_link.name, 'institution_parent_email': institution_link.institutional_email, 'institution_requested_key': institution_link.key.urlsafe(), 'institution_child_name': institution.name, 'institution_child_email': institution.institutional_email, 'subject': get_subject('REMOVED_LINK_EMAIL'), 'receiver': admin.get().email[0], 'html': 'removed_institutional_link.html' }) email_sender.send_email() else: enqueue_task( 'remove-admin-permissions', { 'institution_key': institution_link.key.urlsafe(), 'parent_key': institution.key.urlsafe() }) email_sender = RequestLinkEmailSender( **{ 'institution_parent_name': institution.name, 'institution_parent_email': institution.institutional_email, 'institution_requested_key': institution_link.key.urlsafe(), 'institution_child_name': institution_link.name, 'institution_child_email': institution_link.institutional_email, 'subject': get_subject('REMOVED_LINK_EMAIL'), 'receiver': admin.get().email[0], 'html': 'removed_institutional_link.html' }) email_sender.send_email() notification_type = 'REMOVE_INSTITUTION_LINK' notification_message = institution.create_notification_message( user_key=user.key, current_institution_key=user.current_institution, receiver_institution_key=institution_link.key, sender_institution_key=institution.key) send_message_notification(receiver_key=admin.urlsafe(), notification_type=notification_type, entity_key=institution_link.key.urlsafe(), message=notification_message)
def delete(self, user, institution_parent_urlsafe, institution_children_urlsafe): """ Handle delete children link between institutions. This handler remove the children link between two institutions. This handler is called by parent institution to remove link with the children. """ institution_parent = ndb.Key(urlsafe=institution_parent_urlsafe).get() institution_children = ndb.Key( urlsafe=institution_children_urlsafe).get() Utils._assert(not type(institution_parent) is Institution, "Key is not an institution", EntityException) Utils._assert(not type(institution_children) is Institution, "Key is not an institution", EntityException) user.check_permission( 'remove_link', "User is not allowed to remove link between institutions", institution_parent_urlsafe) # Remove child institution_parent.remove_child(institution_children.key) admin = institution_children.admin notification_type = 'REMOVE_INSTITUTION_LINK' notification_id = create_system_notification( institution_children.key, user.key.urlsafe(), ) enqueue_task( 'remove-admin-permissions', { 'institution_key': institution_children.key.urlsafe(), 'parent_key': institution_parent.key.urlsafe(), 'notification_id': notification_id }) email_sender = RequestLinkEmailSender( **{ 'institution_parent_name': institution_parent.name, 'institution_parent_email': institution_parent.institutional_email, 'institution_requested_key': institution_children.key.urlsafe(), 'institution_child_name': institution_children.name, 'institution_child_email': institution_children.institutional_email, 'subject': get_subject('REMOVED_LINK_EMAIL'), 'receiver': admin.get().email[0], 'html': 'removed_institutional_link.html' }) email_sender.send_email() notification_message = institution_parent.create_notification_message( user_key=user.key, current_institution_key=user.current_institution, receiver_institution_key=institution_children.key, sender_institution_key=institution_parent.key) send_message_notification( receiver_key=admin.urlsafe(), notification_type=notification_type, entity_key=institution_children.key.urlsafe(), message=notification_message)
def post(self, user): """Handle POST Requests.""" body = json.loads(self.request.body) post_data = body['post'] institution_key = post_data['institution'] institution = ndb.Key(urlsafe=institution_key).get() Utils._assert(not institution.is_active(), "This institution is not active", NotAuthorizedException) permission = get_permission(post_data) user.key.get().check_permission( permission, "You don't have permission to publish post.", institution_key) @ndb.transactional(xg=True, retries=10) def create_post(post_data, user, institution): created_post = PostFactory.create(post_data, user.key, institution.key) user.add_post(created_post) institution.add_post(created_post) return created_post post = create_post(post_data, user, institution) entity_type = PostFactory.get_type(post_data) params = { 'sender_key': user.key.urlsafe(), 'entity_key': post.key.urlsafe(), 'entity_type': entity_type, 'institution_key': post.institution.urlsafe(), 'current_institution': user.current_institution.urlsafe() } enqueue_task('notify-followers', params) if (post.shared_post): shared_post = post.shared_post.get() entity_type = 'SHARED_POST' params = { 'receiver_key': shared_post.author.urlsafe(), 'sender_key': user.key.urlsafe(), 'entity_key': shared_post.key.urlsafe(), 'entity_type': entity_type, 'current_institution': user.current_institution.urlsafe(), 'sender_institution_key': shared_post.institution.urlsafe() } enqueue_task('post-notification', params) elif post.shared_event: shared_event = post.shared_event.get() if shared_event.author_key != user.key: notification_message = post.create_notification_message( user_key=user.key, current_institution_key=user.current_institution, sender_institution_key=shared_event.institution_key) send_message_notification( receiver_key=shared_event.author_key.urlsafe(), notification_type='SHARED_EVENT', entity_key=post.key.urlsafe(), message=notification_message) self.response.write(json.dumps(post.make(self.request.host)))