def test_roles_matching(self): role_names = ['employee', 'manager', 'g.manager'] service_profile = create_service_profile(self.user, u'test name')[0] service_profile.broadcastTypes.append(self._get_broadcast_type(111)) service_profile.put() with set_current_user(self.user, skip_create_session=True): role_ids = [system.put_role(name, ServiceRole.TYPE_MANAGED) for name in role_names] si_user = create_service_identity_user(self.user, ServiceIdentity.DEFAULT) app_user_with_roles = create_app_user_by_email(u'*****@*****.**') app_user_without_roles = create_app_user_by_email(u'*****@*****.**') app_user_half_roles = create_app_user_by_email(u'*****@*****.**') create_user_profile(app_user_with_roles, u'ROLES') create_user_profile(app_user_without_roles, u'NO ROLES') create_user_profile(app_user_half_roles, u'1/2 ROLES') # now the service user has some roles, assign it to the user for role_id in role_ids: system.add_role_member(role_id, MemberTO.from_user(app_user_with_roles)) system.add_role_member(role_ids[1], MemberTO.from_user(app_user_half_roles)) news_item_with_roles = self._create_news_item(111, role_ids=role_ids) news_item_without_roles = self._create_news_item(111) self.assertTrue(match_roles_of_item(si_user, app_user_with_roles, news_item_without_roles)) self.assertTrue(match_roles_of_item(si_user, app_user_without_roles, news_item_without_roles)) self.assertTrue(match_roles_of_item(si_user, app_user_half_roles, news_item_without_roles)) self.assertTrue(match_roles_of_item(si_user, app_user_with_roles, news_item_with_roles)) self.assertTrue(match_roles_of_item(si_user, app_user_half_roles, news_item_with_roles)) self.assertFalse(match_roles_of_item(si_user, app_user_without_roles, news_item_with_roles))
def broadcast_group_purchase(service_user, service_identity, group_purchase_id, message): service_identity_user = create_service_identity_user_wo_default( service_user, service_identity) sln_settings = get_solution_settings(service_user) sgp = SolutionGroupPurchase.get_by_id( group_purchase_id, parent_key_unsafe(service_identity_user, sln_settings.solution)) sln_main_branding = get_solution_main_branding(service_user) branding = sln_main_branding.branding_key if sln_main_branding else None members = dict() for e in sgp.subscriptions: if e.sender: member = MemberTO() member.alert_flags = Message.ALERT_FLAG_VIBRATE member.member = e.sender.email member.app_id = e.sender.app_id members["%s:%s" % (member.member, member.app_id)] = member if members: messaging.send(parent_key=None, parent_message_key=None, message=message, answers=[], flags=Message.FLAG_ALLOW_DISMISS, members=members.values(), branding=branding, tag=None, service_identity=service_identity)
def send_expired_voucher_message(voucher_key, sln_settings, days): voucher = SolutionCityVoucher.get(voucher_key) if not voucher or days in voucher.expiration_reminders_sent: return language = sln_settings.main_language service_user = sln_settings.service_user branding = get_solution_main_branding(service_user).branding_key if voucher.owner: activation_date = format_timestamp(voucher.activation_date, sln_settings, format='medium') message = common_translate(language, SOLUTION_COMMON, u'voucher_expiration_message', days=days, date=activation_date) with users.set_user(sln_settings.service_user): member = MemberTO() member.alert_flags = Message.ALERT_FLAG_VIBRATE member.member = voucher.owner.email() member.app_id = voucher.app_id messaging.send(parent_key=None, parent_message_key=None, message=message, answers=[], flags=Message.FLAG_ALLOW_DISMISS, members=[member], branding=branding, tag=None) voucher.expiration_reminders_sent.append(days) db.put(voucher)
def send_message_for_pharmacy_order(service_user, order_key, order_status, message): from solutions.common.bizz.messaging import send_inbox_forwarders_message azzert(order_status in SolutionPharmacyOrder.ORDER_STATUSES) def txn(): m = SolutionPharmacyOrder.get(order_key) azzert(service_user == m.service_user) if m.status == SolutionPharmacyOrder.STATUS_RECEIVED and m.status != order_status: m.status = order_status m.put() return m order = db.run_in_transaction(txn) sm_data = [] sm_data.append({u"type": u"solutions.common.pharmacy_orders.update"}) sln_settings = get_solution_settings(service_user) if message: if order.solution_inbox_message_key: sim_parent, _ = add_solution_inbox_message( service_user, order.solution_inbox_message_key, True, None, now(), message, mark_as_unread=False, mark_as_read=True) send_inbox_forwarders_message(service_user, sim_parent.service_identity, None, message, { 'if_name': sim_parent.sender.name, 'if_email': sim_parent.sender.email }, message_key=sim_parent.solution_inbox_message_key, reply_enabled=sim_parent.reply_enabled) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, order.service_identity) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(sim_parent, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) else: sln_main_branding = get_solution_main_branding(service_user) branding = sln_main_branding.branding_key if sln_main_branding else None member = MemberTO() member.alert_flags = Message.ALERT_FLAG_VIBRATE member.member = order.sender.email member.app_id = order.sender.app_id messaging.send(parent_key=None, parent_message_key=None, message=message, answers=[], flags=Message.FLAG_ALLOW_DISMISS, members=[member], branding=branding, tag=None, service_identity=order.service_identity) elif order.solution_inbox_message_key: sim_parent = SolutionInboxMessage.get(order.solution_inbox_message_key) if not sim_parent.read: sim_parent.read = True sim_parent.put() deferred.defer(update_user_data_admins, service_user, order.service_identity) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, order.service_identity) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(sim_parent, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) send_message(service_user, sm_data, service_identity=order.service_identity)
def delete_sandwich_order(service_user, service_identity, sandwich_id, message): from solutions.common.bizz.messaging import send_inbox_forwarders_message sln_settings = get_solution_settings(service_user) def txn(): m = SandwichOrder.get_by_order_id(service_user, service_identity, sln_settings.solution, sandwich_id) azzert(service_user == m.service_user) m.deleted = True m.put() return m xg_on = db.create_transaction_options(xg=True) sandwich_order = db.run_in_transaction_options(xg_on, txn) sm_data = [{u"type": u"solutions.common.sandwich.orders.deleted", u'sandwich_id': sandwich_id}] if message: if sandwich_order.solution_inbox_message_key: sim_parent, _ = add_solution_inbox_message(service_user, sandwich_order.solution_inbox_message_key, True, None, now(), message, mark_as_unread=False, mark_as_read=True, mark_as_trashed=True) send_inbox_forwarders_message(service_user, sim_parent.service_identity, None, message, { 'if_name': sim_parent.sender.name, 'if_email':sim_parent.sender.email }, message_key=sim_parent.solution_inbox_message_key, reply_enabled=sim_parent.reply_enabled) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, sandwich_order.service_identity) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(sim_parent, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) else: branding = get_solution_main_branding(service_user).branding_key member = MemberTO() member.alert_flags = Message.ALERT_FLAG_VIBRATE member.member = sandwich_order.sender.email member.app_id = sandwich_order.sender.app_id messaging.send(parent_key=None, parent_message_key=None, message=message, answers=[], flags=Message.FLAG_ALLOW_DISMISS, members=[member], branding=branding, tag=None, service_identity=sandwich_order.service_identity) elif sandwich_order.solution_inbox_message_key: sim_parent = SolutionInboxMessage.get(sandwich_order.solution_inbox_message_key) if not sim_parent.trashed and not sim_parent.deleted: sim_parent.trashed = True sim_parent.put() deferred.defer(update_user_data_admins, service_user, sandwich_order.service_identity) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, sandwich_order.service_identity) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(sim_parent, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) send_message(service_user, sm_data, service_identity=sandwich_order.service_identity)
def common_new_chat_message(parent_message_key, message_key, sender, message, answers, timestamp, tag, service_identity, attachments): if tag and tag.startswith(POKE_TAG_INBOX_FORWARDING_REPLY): info = json.loads(tag[len(POKE_TAG_INBOX_FORWARDING_REPLY):]) message_key = info['message_key'] sim_parent = SolutionInboxMessage.get(reconstruct_key(db.Key(message_key))) if sim_parent.awaiting_first_message: sim_parent.awaiting_first_message = False sim_parent.put() else: service_user = sim_parent.service_user sln_settings = get_solution_settings(service_user) sent_by_service = True if sim_parent.sender.email == sender.email and sim_parent.sender.app_id == sender.app_id: sent_by_service = False picture_attachments = [] video_attachments = [] for a in attachments: if a.content_type.startswith("image"): picture_attachments.append(a.download_url) if a.content_type.startswith("video"): video_attachments.append(a.download_url) if sent_by_service: sim_parent, _ = add_solution_inbox_message(service_user, message_key, sent_by_service, [sender], timestamp, message, picture_attachments, video_attachments, mark_as_unread=False, mark_as_read=True) else: sim_parent, _ = add_solution_inbox_message(service_user, message_key, sent_by_service, [sender], timestamp, message, picture_attachments, video_attachments) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, service_identity) send_message(service_user, u"solutions.common.messaging.update", service_identity=service_identity, message=serialize_complex_value(SolutionInboxMessageTO.fromModel(sim_parent, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)) member_sender_user = create_app_user_by_email(sim_parent.sender.email, sim_parent.sender.app_id) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, service_identity) members = [MemberTO.from_user(users.User(f)) for f in sln_i_settings.inbox_forwarders] if member_sender_user.email() not in sln_i_settings.inbox_forwarders: members.append(MemberTO.from_user(member_sender_user)) users.set_user(service_user) try: messaging.add_chat_members(sim_parent.message_key, members) finally: users.clear_user() else: raise NotImplementedError()
def _send_order_confirmation(service_user, lang, message_flow_run_id, member, steps, end_id, end_message_flow_id, parent_message_key, tag, result_key, flush_id, flush_message_flow_id, service_identity, user_details, order_details): if now() - steps[-1].acknowledged_timestamp < 10: alert_flags = Message.ALERT_FLAG_SILENT else: alert_flags = Message.ALERT_FLAG_VIBRATE users.set_user(service_user) try: messaging.send( parent_key=parent_message_key, parent_message_key=parent_message_key, message=u'%s\n\n%s:\n%s' % (translate(lang, SOLUTION_COMMON, u'order_complete_will_notify'), translate(lang, SOLUTION_COMMON, u'usage-detail'), order_details), answers=list(), flags=Message.FLAG_ALLOW_DISMISS | Message.FLAG_AUTO_LOCK, members=[ MemberTO.from_user(user_details[0].toAppUser(), alert_flags) ], branding=get_solution_main_branding(service_user).branding_key, tag=None, service_identity=service_identity, alert_flags=alert_flags, step_id=u'order_complete') finally: users.clear_user()
def test_form(service_user, form_id, testers): # type: (users.User, int, list[users.User]) -> None form = get_form(form_id, service_user) request = TestFormRequestTO(id=form_id, version=form.version) to_put = [] prof = get_service_profile(service_user) branding = prof.broadcastBranding caption = localize(prof.defaultLanguage, 'forms.test_form') answers = [AnswerTO(id_=u'test', caption=caption, action=u'form://%d?version=%d&test=true' % (form_id, form.version))] flags = Message.FLAG_AUTO_LOCK alert_flags = Message.ALERT_FLAG_VIBRATE tag = None message = localize(prof.defaultLanguage, 'forms.click_button_to_test') for user_profile in get_profile_infos(testers): mobiles = db.get([get_mobile_key_by_account(mobile_detail.account) for mobile_detail in user_profile.mobiles]) has_other = False android_mobile = None for mobile in mobiles: if mobile.is_android: android_mobile = mobile else: has_other = True if has_other: messaging.send(None, None, message, answers, flags, [MemberTO.from_user(user_profile.user)], branding, tag, alert_flags=alert_flags) else: title = localize(prof.defaultLanguage, 'forms.test_form_x', title=form.title) body = localize(prof.defaultLanguage, 'forms.click_to_test_your_form') kwargs = {CAPI_KEYWORD_PUSH_DATA: TestFormNotification(title, body, form.id, form.version)} to_put.extend(testForm(test_form_response_handler, logError, user_profile.user, request=request, MOBILE_ACCOUNT=android_mobile, DO_NOT_SAVE_RPCCALL_OBJECTS=True, **kwargs)) db.put(to_put)
def reply_sandwich_order(service_user, service_identity, sandwich_id, message=unicode): from solutions.common.bizz.messaging import send_inbox_forwarders_message sln_settings = get_solution_settings(service_user) sandwich_order = SandwichOrder.get_by_order_id(service_user, service_identity, sln_settings.solution, sandwich_id) azzert(service_user == sandwich_order.service_user) sandwich_order.status = SandwichOrder.STATUS_REPLIED sandwich_order.put() sm_data = [] sm_data.append({u"type": u"solutions.common.sandwich.orders.update"}) if sandwich_order.solution_inbox_message_key: sim_parent, _ = add_solution_inbox_message(service_user, sandwich_order.solution_inbox_message_key, True, None, now(), message, mark_as_unread=False, mark_as_read=True) send_inbox_forwarders_message(service_user, sim_parent.service_identity, None, message, { 'if_name': sim_parent.sender.name, 'if_email':sim_parent.sender.email }, message_key=sim_parent.solution_inbox_message_key, reply_enabled=sim_parent.reply_enabled) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, sandwich_order.service_identity) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(sim_parent, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) else: sln_main_branding = get_solution_main_branding(service_user) branding = sln_main_branding.branding_key if sln_main_branding else None member = MemberTO() member.alert_flags = Message.ALERT_FLAG_VIBRATE member.member = sandwich_order.sender.email member.app_id = sandwich_order.sender.app_id messaging.send(parent_key=None, parent_message_key=None, message=message, answers=[], flags=Message.FLAG_ALLOW_DISMISS, members=[member], branding=branding, tag=None, service_identity=sandwich_order.service_identity) send_message(service_user, sm_data, service_identity=sandwich_order.service_identity)
def add_to_chat(parent_message_key): messaging.add_chat_members(parent_message_key, [MemberTO.from_user(app_user)])
def rm_from_chat(parent_message_key): messaging.delete_chat_members(parent_message_key, [MemberTO.from_user(app_user)])
title=sgp.title, reason=e.message) except: logging.error("Failure when adding new group_purchase subscription", exc_info=1) r.result = None r.error = u"An unknown error occurred" sgp = SolutionGroupPurchase.get_by_id( group_purchase_id, parent_key_unsafe(service_identity_user, sln_settings.solution)) message = translate(sln_settings.main_language, SOLUTION_COMMON, 'group-subscription-failure-unknown-title', title=sgp.title) member = MemberTO() member.alert_flags = Message.ALERT_FLAG_VIBRATE member.member = user_details[0].email member.app_id = user_details[0].app_id main_branding = get_solution_main_branding(service_user) messaging.send(parent_key=None, parent_message_key=None, message=message, answers=[], flags=Message.FLAG_ALLOW_DISMISS, members=[member], branding=main_branding.branding_key, tag=None, service_identity=service_identity) return r