Пример #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)
Пример #2
0
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)
Пример #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))
Пример #4
0
def count_unread_solution_inbox_messages(service_user, service_identity):
    service_identity_user = create_service_identity_user_wo_default(
        service_user, service_identity)
    ancestor = parent_key_unsafe(service_identity_user, SOLUTION_COMMON)
    qry = SolutionInboxMessage.all(keys_only=True).ancestor(ancestor).filter(
        'parent_message_key =', None)
    qry.filter('deleted =', False)
    qry.filter('trashed =', False)
    qry.filter('read =', False)
    qry.filter('starred =', False)
    return qry.count(None)
Пример #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()
Пример #6
0
def get_solution_inbox_messages(service_user,
                                service_identity,
                                count,
                                name,
                                cursor=None):
    service_identity_user = create_service_identity_user_wo_default(
        service_user, service_identity)
    ancestor = parent_key_unsafe(service_identity_user, SOLUTION_COMMON)
    qry = SolutionInboxMessage.all().with_cursor(cursor).ancestor(
        ancestor).filter('parent_message_key =', None)
    qry.filter('deleted =', False)
    if name == SolutionInboxMessage.INBOX_NAME_UNREAD:
        qry.filter('trashed =', False)
        qry.filter('read =', False)
        qry.filter('starred =', False)
    elif name == SolutionInboxMessage.INBOX_NAME_STARRED:
        qry.filter('trashed =', False)
        qry.filter('starred =', True)
    elif name == SolutionInboxMessage.INBOX_NAME_READ:
        qry.filter('trashed =', False)
        qry.filter('read =', True)
        qry.filter('starred =', False)
    elif name == SolutionInboxMessage.INBOX_NAME_TRASH:
        qry.filter('trashed =', True)
    else:
        return None
    qry.order('-last_timestamp')
    messages = qry.fetch(count)
    cursor_ = qry.cursor()
    has_more = False
    if len(messages) != 0:
        qry.with_cursor(cursor_)
        if len(qry.fetch(1)) > 0:
            has_more = True

    return unicode(cursor_), messages, has_more
Пример #7
0
def update_statistic():
    # Completely rebuilds statistics on run.

    current_date = now()
    broadcast_count_dict = {}
    for news_item in get_news_of_last_month():
        service_email = get_service_user_from_service_identity_user(
            news_item.sender).email()
        if service_email not in broadcast_count_dict:
            broadcast_count_dict[service_email] = 1
        else:
            broadcast_count_dict[service_email] += 1

    future_event_count_dict = {}

    for future_event_key in Event.get_future_event_keys(current_date):
        service_email = future_event_key.parent().name()
        if service_email not in future_event_count_dict:
            future_event_count_dict[service_email] = 1
        else:
            future_event_count_dict[service_email] += 1

    static_content_count_dict = {}

    for static_content_key in SolutionStaticContent.get_all_keys():
        service_email = static_content_key.parent().name()
        if service_email not in static_content_count_dict:
            static_content_count_dict[service_email] = 1
        else:
            static_content_count_dict[service_email] += 1

    unanswered_question_count_dict = {}

    # find the oldest, unanswered question per customer and add it to the statistics
    for unanswered_question in SolutionInboxMessage.get_all_unanswered_questions(
            5):
        service_email = unanswered_question.service_user.email()
        if unanswered_question.question_asked_timestamp != 0:
            if not service_email in unanswered_question_count_dict:
                unanswered_question_count_dict[
                    service_email] = unanswered_question.question_asked_timestamp
            elif unanswered_question.question_asked_timestamp < unanswered_question_count_dict[
                    service_email]:
                unanswered_question_count_dict[
                    service_email] = unanswered_question.question_asked_timestamp

    # dict with as keys the app id from the city, value the statistics of this city.
    statistics = {}
    for customer in Customer.all():
        if len(customer.app_ids) != 0:
            service_email = customer.service_email
            if customer.app_id not in statistics:
                stats = AssociationStatistic(key_name=customer.app_id)
                stats.generated_on = current_date
                statistics[customer.app_id] = stats
            else:
                stats = statistics[customer.app_id]
            if not service_email:
                logging.error(
                    u'Association customer %s(%d) has no service_email!',
                    customer.name, customer.id)
                continue
            stats.customer_emails.append(service_email)

            if service_email in broadcast_count_dict:
                stats.broadcasts_last_month.append(
                    broadcast_count_dict[customer.service_email])
            else:
                stats.broadcasts_last_month.append(0)

            if service_email in future_event_count_dict:
                stats.future_events_count.append(
                    future_event_count_dict[service_email])
            else:
                stats.future_events_count.append(0)

            if service_email in static_content_count_dict:
                stats.static_content_count.append(
                    static_content_count_dict[service_email])
            else:
                stats.static_content_count.append(0)

            if service_email in unanswered_question_count_dict:
                stats.last_unanswered_questions_timestamps.append(
                    unanswered_question_count_dict[service_email])
            else:
                stats.last_unanswered_questions_timestamps.append(0)

    for chunk in chunks(statistics.values(), 200):
        db.put(chunk)