def new_inbox_message(sln_settings, message, parent_chat_key=None, service_identity=None, **kwargs): service_identity = service_identity or ServiceIdentity.DEFAULT service_user = sln_settings.service_user language = sln_settings.main_language si = get_service_identity( create_service_identity_user(service_user, service_identity)) user_details = UserDetailsTO.create(service_user.email(), si.name, language, si.avatarUrl, si.app_id) if not parent_chat_key: category = kwargs.get('category') category_key = kwargs.get('category_key') reply_enabled = kwargs.get('reply_enabled', False) message = create_solution_inbox_message(service_user, service_identity, category, category_key, True, [user_details], now(), message, reply_enabled) else: message, _ = add_solution_inbox_message(service_user, parent_chat_key, False, [user_details], now(), message, **kwargs) return message
def _repair_order_received(service_user, 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): from solutions.common.bizz.messaging import send_inbox_forwarders_message logging.info("_flow_member_result_repair_order: \n %s" % steps) remarks = _get_value(steps[0], u'message_remarks') if steps[1].answer_id == u"positive": picture_url = _get_value(steps[1], u'message_picture') else: picture_url = None sln_settings = get_solution_settings(service_user) logging.info("Saving repair order from %s" % user_details[0].email) service_identity_user = create_service_identity_user_wo_default(service_user, service_identity) o = SolutionRepairOrder(parent=parent_key_unsafe(service_identity_user, sln_settings.solution)) o.description = remarks o.sender = SolutionUser.fromTO(user_details[0]) o.timestamp = steps[1].received_timestamp o.status = SolutionRepairOrder.STATUS_RECEIVED o.picture_url = picture_url o.user = user_details[0].toAppUser() if user_details else None msg = common_translate(sln_settings.main_language, SOLUTION_COMMON, 'if-repair-order-received', remarks=remarks) message = create_solution_inbox_message(service_user, service_identity, SolutionInboxMessage.CATEGORY_REPAIR, None, False, user_details, steps[1].received_timestamp, msg, True, [picture_url] if picture_url else []) o.solution_inbox_message_key = message.solution_inbox_message_key o.put() message.category_key = unicode(o.key()) message.put() sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, service_identity) sm_data = [] sm_data.append({u"type": u"solutions.common.repair_orders.update"}) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(message, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) send_message(service_user, sm_data, service_identity=service_identity) attachments = [] if picture_url: att = AttachmentTO() att.content_type = AttachmentTO.CONTENT_TYPE_IMG_JPG att.download_url = picture_url att.name = common_translate(sln_settings.main_language, SOLUTION_COMMON, u'picture') att.size = 0 attachments = [att] app_user = user_details[0].toAppUser() send_inbox_forwarders_message(service_user, service_identity, app_user, msg, { 'if_name': user_details[0].name, 'if_email':user_details[0].email }, message_key=message.solution_inbox_message_key, attachments=attachments, reply_enabled=message.reply_enabled)
def _send_event_notification(sln_settings, service_user, service_identity, user_details, event, event_guest): from solutions.common.bizz.messaging import send_inbox_forwarders_message status = translate(sln_settings.main_language, SOLUTION_COMMON, event_guest.status_str) status_message = translate(sln_settings.main_language, SOLUTION_COMMON, u'events_status_notification', status=status, event=event.title) create_chat = True if event_guest.chat_key: create_chat = SolutionInboxMessage.get(event_guest.chat_key) is None if create_chat: event_key = unicode(event.key()) message = create_solution_inbox_message( service_user, service_identity, SolutionInboxMessage.CATEGORY_AGENDA, event_key, False, user_details, now(), status_message, True) event_guest.chat_key = message.solution_inbox_message_key event_guest.put() app_user = user_details[0].toAppUser() else: message, _ = add_solution_inbox_message(service_user, event_guest.chat_key, False, user_details, now(), status_message) app_user = None send_inbox_forwarders_message( service_user, service_identity, app_user, status_message, { 'if_name': user_details[0].name, 'if_email': user_details[0].email }, message_key=message.solution_inbox_message_key, reply_enabled=message.reply_enabled, send_reminder=False) # show as last message sln_i_settings = get_solution_settings_or_identity_settings( sln_settings, service_identity) message_to = SolutionInboxMessageTO.fromModel(message, sln_settings, sln_i_settings, True) send_message(service_user, u'solutions.common.messaging.update', service_identity=service_identity, message=serialize_complex_value(message_to, SolutionInboxMessageTO, False))
def bulk_invite_result(service_user, service_identity, tag, email, result, user_details): if not tag: logging.exception("Expected tag in bulk_invite_result") return if tag in (SERVICE_AUTOCONNECT_INVITE_TAG, APP_BROADCAST_TAG): return try: key = db.Key(tag) except db.BadKeyError: logging.info('Tag is no db.Key: %s. Ignoring...', tag) return def trans(): invite = db.get(reconstruct_key(key)) if not invite: logging.error("Invite object not found in datastore") return save_message = False if "accepted" == result: invite.status = RestaurantInvite.STATUS_ACCEPTED save_message = True else: invite.status = RestaurantInvite.STATUS_REJECTED invite.put() return save_message save_message = run_in_xg_transaction(trans) if save_message: now_ = now() sln_settings = get_solution_settings(service_user) msg = common_translate(sln_settings.main_language, SOLUTION_COMMON, 'if-accepted-invitation', if_name=user_details[0].name, if_email=user_details[0].email) message = create_solution_inbox_message( service_user, service_identity, SolutionInboxMessage.CATEGORY_BULK_INVITE, None, False, user_details, now_, msg, False) app_user = create_app_user_by_email(user_details[0].email, user_details[0].app_id) send_inbox_forwarders_message(service_user, service_identity, app_user, msg, { 'if_name': user_details[0].name, 'if_email': user_details[0].email }, message_key=message.solution_inbox_message_key, reply_enabled=message.reply_enabled, send_reminder=False) 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(message, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False))
def trans(): sln_settings = get_solution_settings(service_user) order_settings = get_solution_order_settings(sln_settings) lang = sln_settings.main_language comment = None phone = None takeaway_time = None if order_type == ORDER_TYPE_SIMPLE: details = get_extended_details_from_tag( _get_value(steps[0], u'message_details')) if steps[1].answer_id == u"positive": picture_url = _get_value(steps[1], u'message_picture') att = AttachmentTO() att.content_type = AttachmentTO.CONTENT_TYPE_IMG_JPG att.download_url = picture_url att.name = translate(lang, SOLUTION_COMMON, u'picture') att.size = 0 attachments = [att] else: picture_url = None attachments = [] phone = _get_value(steps[2], u'message_phone') msg = common_translate(lang, SOLUTION_COMMON, 'if-order-received') % { 'remarks': details, 'phone_number': phone } elif order_type == ORDER_TYPE_ADVANCED: with closing(StringIO()) as order: timezone_offset = datetime.datetime.now( pytz.timezone( sln_settings.timezone)).utcoffset().total_seconds() has_order_items = False for step in steps: if step.step_id == u'message_phone': phone = step.get_value() elif step.step_id == u'message_comment': comment = step.get_value() elif step.step_id == u'message_advanced_order': step_value = step.display_value.encode('utf-8') if step_value: has_order_items = True order.write(step_value) elif step.step_id == u'message_takeaway_time': takeaway_time = int(step.get_value() - timezone_offset) picture_url = None attachments = [] if comment: if has_order_items: order.write('\n\n') c = '%s: %s' % (common_translate( lang, SOLUTION_COMMON, 'reservation-comment'), comment) order.write( c.encode('utf-8') if isinstance(c, unicode) else c) details = get_extended_details_from_tag( order.getvalue().decode('utf-8')) takeaway_datetime = datetime.datetime.fromtimestamp( takeaway_time, tz=get_timezone(sln_settings.timezone)) takeaway_time_str = format_datetime(takeaway_datetime, locale=lang, format='d/M/yyyy H:mm') msg = '%s:\n%s\n%s: %s\n%s: %s' % ( common_translate(lang, SOLUTION_COMMON, 'order_received'), details, common_translate(lang, SOLUTION_COMMON, 'phone_number'), phone, common_translate(lang, SOLUTION_COMMON, 'takeaway_time'), takeaway_time_str) else: raise BusinessException('Unsupported order type %s', order_type) if not order_settings.manual_confirmation: # Waiting for follow-up message deferred.defer(_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, details, _transactional=db.is_in_transaction()) service_identity_user = create_service_identity_user_wo_default( service_user, service_identity) o = SolutionOrder( parent=parent_key_unsafe(service_identity_user, SOLUTION_COMMON)) o.description = details o.phone_number = phone o.sender = SolutionUser.fromTO(user_details[0]) o.timestamp = now() o.status = SolutionOrder.STATUS_RECEIVED o.picture_url = picture_url o.takeaway_time = takeaway_time o.user = user_details[0].toAppUser() if user_details else None message = create_solution_inbox_message( service_user, service_identity, SolutionInboxMessage.CATEGORY_ORDER, None, False, user_details, steps[2].received_timestamp, msg, True, [picture_url] if picture_url else []) o.solution_inbox_message_key = message.solution_inbox_message_key o.put() message.category_key = unicode(o.key()) message.put() sln_i_settings = get_solution_settings_or_identity_settings( sln_settings, service_identity) sm_data = [{ u"type": u"solutions.common.orders.update" }, { u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value( SolutionInboxMessageTO.fromModel(message, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False) }] send_message(service_user, sm_data, service_identity=service_identity) app_user = user_details[0].toAppUser() send_inbox_forwarders_message( service_user, service_identity, app_user, msg, dict(if_name=user_details[0].name, if_email=user_details[0].email), message_key=message.solution_inbox_message_key, attachments=attachments, reply_enabled=message.reply_enabled)
def process_sandwich_order(service_user, service_identity, user_details, type_, topping, customizations, remark, takeaway_time, parent_message_key): from solutions.common.bizz.messaging import send_inbox_forwarders_message # Calculate price type_id = int(type_.split('_')[-1]) topping_id = int(topping.split('_')[-1]) if topping else None option_ids = [int(c.split('_')[-1]) for c in customizations] if customizations else [] logging.info("type: %s", type_id) logging.info("topping: %s", topping_id) logging.info("options: %s", option_ids) sandwich_type = SandwichType.get_by_type_id(service_user, SOLUTION_FLEX, type_id) sandwich_topping = SandwichTopping.get_by_topping_id(service_user, SOLUTION_FLEX, topping_id) if topping_id else None sandwich_customizations = SandwichOption.get_by_option_ids(service_user, SOLUTION_FLEX, option_ids) logging.info("type: %s", sandwich_type) logging.info("topping: %s", sandwich_topping) logging.info("options: %s", sandwich_customizations) total_price = sum([sandwich_type.price, sandwich_topping.price if sandwich_topping else 0] + [sc.price for sc in sandwich_customizations]) # Save order to datastore now_ = now() sln_settings = get_solution_settings(service_user) timezone_offset = datetime.now(pytz.timezone(sln_settings.timezone)).utcoffset().total_seconds() lang = sln_settings.main_language customizations = [''] customizations.extend([sw.description for sw in sandwich_customizations]) msg = common_translate(lang, SOLUTION_COMMON, 'if-sandwich-order-received', sandwich_type=sandwich_type.description, topping=sandwich_topping.description if sandwich_topping else u"", customizations=u"\n - ".join(customizations), remarks=remark or "") if takeaway_time: takeaway_time = long(takeaway_time - timezone_offset) takeaway_time_str = format_datetime( datetime.fromtimestamp(takeaway_time, tz=get_timezone(sln_settings.timezone)), format='short', locale=lang) msg = '%s\n%s: %s' % (msg, common_translate(lang, SOLUTION_COMMON, 'takeaway_time'), takeaway_time_str) create_inbox_message = True service_identity_user = create_service_identity_user_wo_default(service_user, service_identity) order = SandwichOrder.get_or_insert(key_name=parent_message_key, parent=parent_key_unsafe(service_identity_user, SOLUTION_FLEX)) if order.solution_inbox_message_key: create_inbox_message = False order.sender = SolutionUser.fromTO(user_details[0]) order.order_time = now_ order.price = total_price order.type_id = type_id order.topping_id = topping_id order.option_ids = option_ids order.remark = remark order.status = SandwichOrder.STATUS_RECEIVED order.takeaway_time = takeaway_time sm_data = [{u"type": u"solutions.common.sandwich.orders.update"}] if create_inbox_message: message = create_solution_inbox_message(service_user, service_identity, SolutionInboxMessage.CATEGORY_SANDWICH_BAR, None, False, user_details, now_, msg, True) order.solution_inbox_message_key = message.solution_inbox_message_key order.put() message.category_key = unicode(order.key()) message.put() sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, service_identity) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(message, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) app_user = create_app_user_by_email(user_details[0].email, user_details[0].app_id) send_inbox_forwarders_message(service_user, service_identity, app_user, msg, { 'if_name': user_details[0].name, 'if_email':user_details[0].email }, message_key=message.solution_inbox_message_key, reply_enabled=message.reply_enabled) send_message(service_user, sm_data, service_identity=service_identity) if SolutionModule.LOYALTY in sln_settings.modules: deferred.defer(update_user_data_admins, service_user, service_identity)
def trans(): sm_data = [] if sln_loyalty_lottery.winner_timestamp != 0: logging.debug("loyalty lottery loot: update winner %s", sln_loyalty_lottery.winner) sim_parent, _ = add_solution_inbox_message( service_user, sln_loyalty_lottery.solution_inbox_message_key, True, None, now_, msg_sorry, mark_as_read=True) if sim_parent.message_key_by_tag: message_key_by_tag = json.loads(sim_parent.message_key_by_tag) if message_key_by_tag.get(u"loyalty_lottery_loot", None): deferred.defer(_messaging_seal, service_user, message_key_by_tag[u"loyalty_lottery_loot"], sim_parent.message_key, sim_parent.message_key, 1, _transactional=True) send_inbox_forwarders_message( service_user, service_identity, None, msg_sorry, { 'if_name': user_detail.name, 'if_email': user_detail.email }, message_key=sim_parent.solution_inbox_message_key, reply_enabled=sim_parent.reply_enabled, send_reminder=False) deferred.defer(send_email_to_user_for_loyalty_update, service_user, service_identity, sln_loyalty_lottery.winner, msg_sorry, False) 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) }) logging.debug("loyalty lottery loot: new winner %s", winner) sim_parent = create_solution_inbox_message( service_user, service_identity, SolutionInboxMessage.CATEGORY_LOYALTY, unicode(sln_loyalty_lottery_key), True, [user_detail], now_, msg_ok, True, mark_as_read=True) sln_loyalty_lottery.solution_inbox_message_key = sim_parent.solution_inbox_message_key if sln_loyalty_lottery.winner: if not sln_loyalty_lottery.skip_winners: sln_loyalty_lottery.skip_winners = [] sln_loyalty_lottery.skip_winners.append(sln_loyalty_lottery.winner) sln_loyalty_lottery.pending = False sln_loyalty_lottery.winner = winner sln_loyalty_lottery.winner_info = SolutionUser.fromTO( user_detail) if user_detail else None sln_loyalty_lottery.winner_timestamp = now_ sln_loyalty_lottery.put() send_inbox_forwarders_message( service_user, service_identity, None, msg_ok, { 'if_name': user_detail.name, 'if_email': user_detail.email }, message_key=sim_parent.solution_inbox_message_key, reply_enabled=sim_parent.reply_enabled, send_reminder=False, answers=[btn], store_tag=u"loyalty_lottery_loot", flags=message_flags) deferred.defer(send_email_to_user_for_loyalty_update, service_user, service_identity, sln_loyalty_lottery.winner, msg_ok, False, sim_parent.solution_inbox_message_key) 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) }) sm_data.append({u"type": u"solutions.common.loyalty.lottery.update"}) send_message(service_user, sm_data, service_identity=service_identity) deferred.defer(_continue, service_user, service_identity, sln_loyalty_lottery_key, _transactional=True)
def appointment_asked(service_user, 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): from solutions.common.bizz.messaging import _get_step_with_id, send_inbox_forwarders_message logging.info("_flow_member_result_appointment: \n %s" % steps) day = _get_step_with_id(steps, 'message_day') if not day: logging.error("Did not find step with id 'day'. Can not process message_flow_member_result with tag %s" % tag) return None phone = _get_step_with_id(steps, 'message_phone') if not phone: logging.error("Did not find step with id 'phone'. Can not process message_flow_member_result with tag %s" % tag) return None reason = _get_step_with_id(steps, 'message_reason') if not reason: logging.error("Did not find step with id 'reason'. Can not process message_flow_member_result with tag %s" % tag) return None logging.info("Saving appointment from %s" % user_details[0].email) sln_settings = get_solution_settings(service_user) days_str = [] language = sln_settings.main_language or DEFAULT_LANGUAGE for timeframe_id in day.form_result.result.values: sawt = SolutionAppointmentWeekdayTimeframe.get_by_id(int(timeframe_id), parent_key(service_user, sln_settings.solution)) if sawt: days_str.append(sawt.label(language)) msg_appointment = """%s: %s %s: %s %s: %s""" % (common_translate(language, SOLUTION_COMMON , 'days'), "\n".join(days_str), common_translate(language, SOLUTION_COMMON , 'phone'), phone.form_result.result.value, common_translate(language, SOLUTION_COMMON , 'reason'), reason.form_result.result.value) msg = common_translate(language, SOLUTION_COMMON, 'if-appointment-received', appointment=msg_appointment) message = create_solution_inbox_message(service_user, service_identity, SolutionInboxMessage.CATEGORY_APPOINTMENT, None, False, user_details, reason.acknowledged_timestamp, msg, True) app_user = create_app_user_by_email(user_details[0].email, user_details[0].app_id) send_inbox_forwarders_message(service_user, service_identity, app_user, msg, { 'if_name': user_details[0].name, 'if_email':user_details[0].email }, message_key=message.solution_inbox_message_key, reply_enabled=message.reply_enabled) 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(message, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)) return None
def pharmacy_order_received(service_user, 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): from solutions.common.bizz.messaging import send_inbox_forwarders_message sln_settings = get_solution_settings(service_user) logging.info("_flow_member_result_pharmacy_order: \n %s" % steps) if "button_button_yes" == steps[0].answer_id: picture_url = _get_value(steps[1], u'message_photo_upload_prescription') description = None if u"positive" == steps[2].answer_id: remarks = _get_value(steps[2], u'message_remarks_box') else: remarks = "" else: if u"positive" == steps[1].answer_id: picture_url = _get_value(steps[1], u'message_photo_upload_box') description = None if u"positive" == steps[2].answer_id: remarks = _get_value(steps[2], u'message_remarks_box') else: remarks = "" else: picture_url = None description = _get_value(steps[2], u'message_describe_box') if u"positive" == steps[3].answer_id: remarks = _get_value(steps[3], u'message_remarks_box') else: remarks = "" logging.info("Saving pharmacy order from %s" % user_details[0].email) app_user = create_app_user_by_email(user_details[0].email, user_details[0].app_id) service_identity_user = create_service_identity_user_wo_default(service_user, service_identity) o = SolutionPharmacyOrder(parent=parent_key_unsafe(service_identity_user, SOLUTION_COMMON)) o.description = description o.remarks = remarks o.sender = SolutionUser.fromTO(user_details[0]) o.timestamp = steps[2].received_timestamp o.status = SolutionPharmacyOrder.STATUS_RECEIVED o.picture_url = picture_url o.user = app_user msg = translate(sln_settings.main_language, SOLUTION_COMMON, 'if-order-received', remarks=remarks, phone_number="") message = create_solution_inbox_message(service_user, service_identity, SolutionInboxMessage.CATEGORY_PHARMACY_ORDER, None, False, user_details, steps[2].received_timestamp, msg, True, [picture_url] if picture_url else []) o.solution_inbox_message_key = message.solution_inbox_message_key o.put() message.category_key = unicode(o.key()) message.put() sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, service_identity) sm_data = [] sm_data.append({u"type": u"solutions.common.pharmacy_orders.update"}) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(message, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) send_message(service_user, sm_data, service_identity=service_identity) if picture_url: att = AttachmentTO() att.content_type = AttachmentTO.CONTENT_TYPE_IMG_JPG att.download_url = picture_url att.name = translate(sln_settings.main_language, SOLUTION_COMMON, u'picture') att.size = 0 send_inbox_forwarders_message(service_user, service_identity, app_user, msg, { 'if_name': user_details[0].name, 'if_email': user_details[0].email }, message_key=message.solution_inbox_message_key, attachments=[att], reply_enabled=message.reply_enabled) else: send_inbox_forwarders_message(service_user, service_identity, app_user, msg, { 'if_name': user_details[0].name, 'if_email': user_details[0].email }, message_key=message.solution_inbox_message_key, reply_enabled=message.reply_enabled) return None