Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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))
Esempio n. 4
0
def send_inbox_message_update(sln_settings, message, service_identity=None):
    service_user = sln_settings.service_user
    sln_i_settings = get_solution_settings_or_identity_settings(
        sln_settings, service_identity)
    message_to = SolutionInboxMessageTO.fromModel(message, sln_settings,
                                                  sln_i_settings, True)
    data = serialize_complex_value(message_to, SolutionInboxMessageTO, False)
    channel.send_message(service_user,
                         u'solutions.common.messaging.update',
                         message=data,
                         service_identity=service_identity)
Esempio n. 5
0
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()
Esempio n. 6
0
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))
Esempio n. 7
0
def send_signup_update_messages(sln_settings, *messages):
    service_identity = ServiceIdentity.DEFAULT
    sln_i_settings = get_solution_settings_or_identity_settings(
        sln_settings, service_identity)

    sm_data = [{u'type': u'solutions.common.customer.signup.update'}]
    for message in messages:
        message_to = SolutionInboxMessageTO.fromModel(message, sln_settings,
                                                      sln_i_settings, True)
        sm_data.append({
            u'type':
            u'solutions.common.messaging.update',
            u'message':
            serialize_complex_value(message_to, SolutionInboxMessageTO, False)
        })

    channel.send_message(sln_settings.service_user,
                         sm_data,
                         service_identity=service_identity)
Esempio n. 8
0
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
Esempio n. 9
0
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