def getStaticFlow(request):
    from rogerthat.dal.service import get_service_menu_item_by_coordinates
    from rogerthat.rpc import users
    service_identity_user = add_slash_default(users.User(request.service))
    get_service_identity(service_identity_user)  # azzerts

    smd = get_service_menu_item_by_coordinates(service_identity_user, request.coords)
    if not smd:
        logging.info("No menu item found with coords %s" % request.coords)
        return None

    if smd.isBroadcastSettings:
        response = GetStaticFlowResponseTO()
        helper = FriendHelper.from_data_store(service_identity_user, FRIEND_TYPE_SERVICE)
        response.staticFlow = generate_broadcast_settings_static_flow(helper, users.get_current_user())
        return response

    if not smd.staticFlowKey:
        logging.info("Menu item %s doesn't reference a static flow" % request.coords)
        return None

    mfd = MessageFlowDesign.get(smd.staticFlowKey)
    static_flow = mfd.js_flow_definitions.get_by_hash(request.staticFlowHash)
    if not static_flow:
        logging.info("No static flow found on MFD '%s' with hash %s" % (mfd.name, request.staticFlowHash))
        return None

    response = GetStaticFlowResponseTO()
    response.staticFlow = static_flow.definition
    return response
def pressMenuItem(request):
    from rogerthat.bizz.service import press_menu_item
    from rogerthat.rpc import users
    service_identity_user = add_slash_default(users.User(request.service))
    get_service_identity(service_identity_user)  # azzerts
    mf_run_id = None if request.message_flow_run_id is MISSING else request.message_flow_run_id
    hashed_tag = None if request.hashed_tag is MISSING else request.hashed_tag
    timestamp = None if request.timestamp is MISSING else request.timestamp
    user = users.get_current_user()
    press_menu_item(user, service_identity_user, request.coords, request.context,
                    request.generation, mf_run_id, hashed_tag, timestamp)
def getMenuIcon(request):
    from rogerthat.dal.service import get_service_menu_item_by_coordinates
    from rogerthat.rpc import users
    service_identity_user = add_slash_default(users.User(request.service))
    get_service_identity(service_identity_user)  # azzerts
    smd = get_service_menu_item_by_coordinates(service_identity_user, request.coords)
    if not smd:
        return None
    response = GetMenuIconResponseTO()
    size = request.size
    icon, icon_hash = get_menu_icon(smd, service_identity_user, users.get_current_user())
    response.icon = unicode(base64.encodestring(render_menu_icon(icon, size)))
    response.iconHash = icon_hash
    return response
def get_menu(identifier):
    service_user = users.get_current_user()
    service_identity_user = create_service_identity_user(service_user, identifier)
    service_identity = get_service_identity(service_identity_user)
    dummy_translator = DummyTranslator(get_service_profile(service_user).defaultLanguage)
    sm = WebServiceMenuTO.fromServiceIdentity(service_identity, dummy_translator.default_language, dummy_translator)
    return sm
    def get(self, userCode, id_):
        download = self.request.get("download", "false") == "true"
        if not userCode or not id_:
            self.response.set_status(404)
            return
        pp = ProfilePointer.get(userCode)
        if not pp:
            self.response.set_status(404)
            return
        sid = get_service_interaction_def(pp.user, int(id_))
        short_link = get_service_interact_qr_code_url(sid)
        si = get_service_identity(sid.service_identity_user)
        if sid.qrTemplate:
            img = qrcode(short_link, str(sid.qrTemplate.blob) if sid.qrTemplate.blob else None, map(int, sid.qrTemplate.body_color), False)
        else:
            qrtemplate, color = get_default_qr_template_by_app_id(si.app_id)
            img = qrcode(short_link, qrtemplate.blob, color, False)
        if not img:
            self.response.set_status(404)
            return
        self.response.headers['Content-Type'] = "image/png"
        safe_service_name = safe_file_name(si.name)
        safe_poketag_name = safe_file_name(sid.tag)

        self.response.headers['Content-Disposition'] = "%s; filename=qr-%s-%s.png" % ("attachment" if download else "inline", safe_poketag_name, safe_service_name)
        self.response.headers['Cache-Control'] = "public, max-age=31536000"  # Cache for 1 year
        self.response.out.write(img)
def revoke_role(service_identity_user, user, role):
    admin = False
    service_identity = get_service_identity(service_identity_user)
    if not service_identity:
        raise ServiceIdentityDoesNotExistException(get_identity_from_service_identity_user(service_identity_user))
    if isinstance(role, unicode):
        admin = True
        azzert(role in ROLES)  # builtin
    else:
        azzert(role.service_user == service_identity.service_user)
        role = u'sr:%s' % role.role_id

    def trans():
        user_profile = get_user_profile(user, False)
        user_profile.revoke_role(service_identity_user, role)
        user_profile.put()
        if admin:
            deferred.defer(drop_sessions_of_user, user, _transactional=True)

    if db.is_in_transaction():
        trans()
    else:
        db.run_in_transaction(trans)

    if admin:
        _send_admin_role_updates(service_identity_user)
def grant_role(service_identity_user, user, role):
    admin = False
    service_identity = get_service_identity(service_identity_user)
    if not service_identity:
        raise ServiceIdentityDoesNotExistException(get_identity_from_service_identity_user(service_identity_user))
    if isinstance(role, unicode):
        admin = True
        azzert(role in ROLES)  # builtin
    else:
        azzert(role.service_user == service_identity.service_user)
        role = u'sr:%s' % role.role_id

    def trans():
        user_profile = get_user_profile(user, False)
        bizz_check(user_profile, "User %s does not exist" % user.email())
        user_profile.grant_role(service_identity_user, role)
        user_profile.put()

    if db.is_in_transaction():
        trans()
    else:
        db.run_in_transaction(trans)

    if admin:
        _send_admin_role_updates(service_identity_user)
def has_role(service_identity, user, role):
    if isinstance(service_identity, users.User):
        service_identity_user = service_identity
        service_identity = get_service_identity(service_identity_user)
        azzert(service_identity, "Service identity %s not found" % service_identity_user)
    else:
        service_identity_user = service_identity.service_identity_user

    if isinstance(user, users.User):
        user_profile = get_user_profile(user, True)
    else:
        user_profile = user
        user = user_profile.user

    if isinstance(role, unicode):
        # Admin role, just fallback on UserProfile.has_role
        return user_profile.has_role(service_identity_user, role)

    azzert(role.service_user == service_identity.service_user)
    role = u'sr:%s' % role.role_id
    # check on the service identity itself
    if user_profile.has_role(service_identity_user, role):
        return True
    if service_identity.is_default:
        return False
    # check on the default service identity
    return user_profile.has_role(create_service_identity_user(service_identity.service_user, ServiceIdentity.DEFAULT),
                                 role)
def del_user_data(email, user_data_keys, service_identity=None, app_id=None):
    service_identity_user = get_and_validate_service_identity_user(users.get_current_user(), service_identity)
    if app_id is None:
        app_id = get_service_identity(service_identity_user).app_id
    set_user_data(service_identity_user,
                  create_app_user(users.User(email), app_id),
                  json.dumps({key: None for key in user_data_keys}))
    def post(self):
        uuid = self.request.get('uuid')
        major = self.request.get('major')
        minor = self.request.get('minor')
        tag = self.request.get('tag')
        service = self.request.get('service')
        identity = self.request.get('identity')

        if not (uuid and major and minor and service and identity):
            return self.redirect(result='Please supply all parameters.')

        logging.info("New beacon added uuid: %s major: %s minor: %s for: %s/%s" % (uuid, major, minor, service, identity))
        try:
            service_user = users.User(service)
            service_identity_user = create_service_identity_user(service_user, identity)
            si = get_service_identity(service_identity_user)
            if not si:
                self.redirect(result='Service not found.')
                return
            name = "%s|%s" % (major, minor)

            added = add_new_beacon(uuid, name, tag, service_identity_user)
            if added:
                self.redirect(result='Added beacon with name: %s for: %s' % (name, service_identity_user))
            else:
                self.redirect(result='Beacon already exists: %s for: %s' % (name, service_identity_user))
        except Exception, e:
            logging.warn(str(e), exc_info=1)
            self.redirect(result=str(e))
    def from_model(cls, model, base_url, statistics=None):
        # type: (NewsItem, unicode, NewsItemInternalStatistics) -> NewsItemTO
        from rogerthat.dal.service import get_service_identity
        si = get_service_identity(model.sender)
        buttons = model.buttons.values() if model.buttons else []

        if not statistics:
            statistics = NewsItemInternalStatistics()

        feed_names = []
        if model.feeds:
            for feed in model.feeds.values():
                feed_name = NewsFeedNameTO.from_model(feed)
                feed_names.append(feed_name)

        # set the target audience
        if model.target_audience_enabled:
            target_audience = NewsTargetAudienceTO()
            target_audience.min_age = model.target_audience_min_age
            target_audience.max_age = model.target_audience_max_age
            target_audience.gender = model.target_audience_gender
            target_audience.connected_users_only = model.connected_users_only
        else:
            target_audience = None
        sender_email = model.sender.email()
        sender_name = si.name if si else u""
        sender_avatar_id = si.avatarId if si else -1
        return cls(model.id, sender_email, sender_name, sender_avatar_id, model.title, model.message,
                   model.image_url(base_url), model.broadcast_type, statistics.total_reached,
                   statistics.users_that_rogered, buttons, model.qr_code_content, model.qr_code_caption, model.version,
                   model.timestamp, model.flags, model.type, model.sticky, model.sticky_until, model.app_ids,
                   True if statistics.users_that_rogered else False, model.scheduled_at, model.published,
                   statistics.details, statistics.total_action, statistics.total_followed, target_audience,
                   model.role_ids, model.tags, feed_names)
Exemple #12
0
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 _handle_service_interaction_based_invitation(self, entry_point, code, language, su, user_agent, version):
        match = re.match("^/q/s/(.+)/(\\d+)$", su.full)
        if not match:
            self.response.out.write(_TMPL % su.full)
        else:
            user_code = match.group(1)

            service_profile = get_service_profile_via_user_code(user_code)

            if not service_profile:  # Profile of usercode not found. Sorry we can't help you :(
                logging.info("Profile of usercode not found. Sorry we can't help you :(")
                self.response.out.write(_TMPL % "/")
                return

            service_user = service_profile.user

            if entry_point == "S":  # It's a QR-Code
                logging.info("It's a QR-Code")
                sid = int(match.group(2))
                service_interaction_def = get_service_interaction_def(service_user, int(sid))
                service_identity_user = create_service_identity_user(service_user, service_interaction_def.service_identity)
                service_identity = get_service_identity(service_identity_user)
                self._handle_after_qr_code_scan(user_agent, service_identity, language, sid)
                return

            if entry_point == "M":
                logging.info("It's a Message")
            else:
                logging.warn("Expected entry_point 'M' but got '%s'." % entry_point)

            # Get service identity by sid
            sid = int(match.group(2))
            service_interaction_def = get_service_interaction_def(service_user, int(sid))
            service_identity_user = create_service_identity_user(service_user, service_interaction_def.service_identity)
            service_identity = get_service_identity(service_identity_user)
            supported_platform = determine_if_platform_supports_rogerthat_by_user_agent(user_agent)
            if (version and version == "web") or not supported_platform:
                logging.info("Unsupported platform ===> Show sorry page with install instructions for android & iphone")

                variables = {'profile': service_identity,
                             'payload': urllib.urlencode([("chl", "%s/S/%s" % (get_server_settings().baseUrl, code))]),
                             'app_name': get_app_name_by_id(service_identity.app_id)}
                # Unsupported platform ===> Show sorry page with install instructions for android & iphone
                self.response.out.write(render('sorry_step2', language, variables, 'web'))
            else:
                fallback_url = "%s/%s/%s?v=web" % (get_server_settings().baseUrl, entry_point, code)
                redirect_to_app(self, user_agent.lower(), service_identity.app_id, su.full, fallback_url)
def add_new_beacon_discovery(app_user, beacon_uuid, beacon_name):
    '''A human user has discovered a beacon'''
    beacon_uuid = beacon_uuid.lower()
    beacon = get_beacon(beacon_uuid, beacon_name)
    if not beacon:
        logging.warn("Beacon not found: {uuid: %s, name: %s}", beacon_uuid, beacon_name)
        return None, None

    keyy = BeaconDiscovery.create_key(app_user, beacon.service_identity_user)
    beaconDiscovery = BeaconDiscovery.get(keyy)
    if beaconDiscovery:
        return beacon.service_identity_user, beacon.tag

    BeaconDiscovery(key=keyy, creation_time=now()).put()

    si = get_service_identity(beacon.service_identity_user)
    if not si:
        logging.error("Service identity not found for service_identity_user: %s" % beacon.service_identity_user)
    elif not si.beacon_auto_invite:
        logging.info("Beacon detected but %s does not allow auto invite {user %s, uuid: %s, name: %s}",
                     beacon.service_identity_user, app_user, beacon_uuid, beacon_name)
    elif get_app_id_from_app_user(app_user) not in si.appIds:
        logging.info("Beacon detected but %s does not contain app_id of user %s: {uuid: %s, name: %s}",
                     beacon.service_identity_user, app_user, beacon_uuid, beacon_name)
        return None, None
    elif get_friend_serviceidentity_connection(app_user, beacon.service_identity_user):
        logging.info("Beacon detected but %s and %s were already friends: {uuid: %s, name: %s}",
                     app_user, beacon.service_identity_user, beacon_uuid, beacon_name)
    else:
        # Fake a deleted connection between service and human user to be able to show the service's avatar on the phone
        friend_map = get_friends_map(app_user)
        friend_map.generation += 1
        friend_map.version += 1
        friend_map.put()

        friend_detail = FriendDetails().addNew(remove_slash_default(beacon.service_identity_user),
                                               si.name, si.avatarId, type_=FriendDetail.TYPE_SERVICE)
        friend_detail.relationVersion = -1

        from rogerthat.bizz.job.update_friends import _do_update_friend_request
        _do_update_friend_request(app_user, friend_detail, UpdateFriendRequestTO.STATUS_ADD, friend_map,
                                  extra_conversion_kwargs=dict(existence=FriendTO.FRIEND_EXISTENCE_DELETED,
                                                               includeServiceDetails=False))

        user_profile = get_user_profile(app_user)
        app_name = get_app_name_by_id(user_profile.app_id)
        m = localize(user_profile.language, "_automatic_detection_invitation", app_name=app_name, service_name=si.name)
        _send_invitation_message_from_service_to_user(app_user, beacon.service_identity_user, m,
                                                      si.descriptionBranding, user_profile.language,
                                                      ORIGIN_BEACON_DETECTED, BEACON_DISCOVERY)

        message = "Beacon detected at %s (%s) with tag: %s" % (si.name.encode('utf8'),
                                                               remove_slash_default(si.service_identity_user).email().encode('utf8'),
                                                               beacon.tag.encode('utf8'))

        server_settings = get_server_settings()
        xmpp.send_message(server_settings.xmppInfoMembers, message, message_type=xmpp.MESSAGE_TYPE_CHAT)

    return beacon.service_identity_user, beacon.tag
 def from_model(cls, model, base_url, statistics):
     # type: (NewsItem, unicode, NewsItemInternalStatistics) -> BaseNewsItemTO
     from rogerthat.dal.service import get_service_identity
     si = get_service_identity(model.sender)
     return cls(model.id, si.service_identity_user.email(), si.name, si.avatarId, model.title, model.message,
                model.image_url(base_url), model.broadcast_type, statistics.total_reached,
                statistics.users_that_rogered, model.buttons, model.qr_code_content, model.qr_code_caption,
                model.version, model.timestamp, model.flags, model.type)
def _validate_app_broadcast(service_identity_user, app_ids, message):
    si = get_service_identity(service_identity_user)
    azzert(si)
    for app_id in app_ids:
        azzert(app_id in si.appIds)

    bizz_check(message and message.strip(), 'Message should not be empty')
    return si
    def test_grant_role(self):
        john_profile = get_user_profile(self.john, True)
        jane_profile = get_user_profile(self.jane, True)
        si = get_service_identity(self.service_identity_user)
        self.assertTrue(john_profile.has_role(self.service_identity_user, roles.ROLE_ADMIN))
        self.assertTrue(has_role(si, john_profile, roles.ROLE_ADMIN))

        self.assertFalse(jane_profile.has_role(self.service_identity_user, roles.ROLE_ADMIN))
        self.assertFalse(has_role(si, jane_profile, roles.ROLE_ADMIN))
def _match_roles_of_item(app_user, news_item):
    if not news_item.has_roles():
        return True

    service_user = get_service_user_from_service_identity_user(news_item.sender)
    role_ids = news_item.role_ids
    roles = [role for role in get_service_roles_by_ids(service_user, role_ids) if role is not None]
    user_profile = get_user_profile(app_user)
    service_identity = get_service_identity(news_item.sender)
    return any([has_role(service_identity, user_profile, role) for role in roles])
def get_profile_info(user, cached=True, skip_warning=False):
    '''Returns a ProfileInfo (UserProfile or ServiceIdentity)'''
    if "/" in user.email():
        return get_service_identity(user) if cached else get_service_identity_not_cached(user)
    profile = _get_profile(user) if cached else _get_profile_not_cached(user)
    if not profile or isinstance(profile, UserProfile):
        return profile
    if not skip_warning:
        logging.warn("Implicit retrieving default ServiceIdentity for %s\n%s" % (user.email(), get_python_stack_trace(short=True)))
    return get_default_service_identity(user) if cached else get_default_service_identity_not_cached(user)
 def _ensure_data(self):
     if self.has_data:
         return
     else:
         if self.is_service:
             self._service_profile = get_service_profile(self.service_user)
             self._profile_info = get_service_identity(self.service_identity_user)
         else:
             self._profile_info = db.get(UserProfile.createKey(self.user))
         self.has_data = True
 def trans():
     id_default = get_service_identity(svc_id_user_default)
     id_default.qualifiedIdentifier = u'qual1'
     id_default.menuBranding = u"MENUBR"
     id_default.descriptionBranding = u"DESCBR"
     id_default.homeBrandingHash = u"HOMEBR"
     id_default.mainPhoneNumber = u"123456123123"
     id_default.defaultAppId = app_ids[0]
     id_default.appIds = app_ids
     id_default.put()
     return id_default
    def testServiceIdentityCacheSerialization2(self):
        name = u'xxxx'
        svc_email = u'*****@*****.**'
        svc_user = users.User(svc_email)
        _, svc_identity = create_service_profile(svc_user, name)
        self.assert_(svc_identity.name == name)
        self.assert_(svc_identity.is_default)
        self.assert_(svc_identity.service_user.email() == svc_email)
        self.assert_(svc_identity.isServiceIdentity)

        svc_id1 = get_profile_info(svc_user)
        self.assert_(svc_id1.name == name)
        self.assert_(svc_id1.is_default)
        self.assert_(svc_id1.service_user.email() == svc_email)
        self.assert_(svc_id1.isServiceIdentity)

        svc_id2 = get_profile_info(svc_user)
        self.assert_(svc_id2.name == name)
        self.assert_(svc_id2.is_default)
        self.assert_(svc_id2.service_user.email() == svc_email)
        self.assert_(svc_id2.isServiceIdentity)

        rpc.wait_for_rpcs()
        flush_request_cache()

        svc_id3 = get_service_identity(add_slash_default(svc_user))
        self.assert_(svc_id3.name == name)
        self.assert_(svc_id3.is_default)
        self.assert_(svc_id3.service_user.email() == svc_email)
        self.assert_(svc_id3.isServiceIdentity)

        rpc.wait_for_rpcs()
        flush_request_cache()

        svc_id4 = get_service_identity(add_slash_default(svc_user))
        self.assert_(svc_id4.name == name)
        self.assert_(svc_id4.is_default)
        self.assert_(svc_id4.service_user.email() == svc_email)
        self.assert_(svc_id4.isServiceIdentity)

        self.assertRaises(AssertionError, get_service_identity, svc_user)
    def get(self):
        identifier = self.request.GET.get('identifier')
        azzert(identifier)
        cursor = self.request.GET.get('cursor')

        si = get_service_identity(create_service_identity_user(users.get_current_user(), identifier))

        params = {'service_identity': identifier,
                  'service_identity_name': si.name,
                  'container_id': 'serviceUsersContainer_%s' % md5_hex(identifier),
                  'cursor': cursor}
        self.response.out.write(render('service_users', [DEFAULT_LANGUAGE], params, 'web'))
    def test_app_data(self):
        expected_data = {'key1': 1, 'key2': 2}
        set_app_data(self.service_identity_user, json.dumps(expected_data))

        si = get_service_identity(self.service_identity_user)
        self.assertDictEqual(expected_data, si.serviceData.to_json_dict())

        new_data = {'key3': 3}
        set_app_data(self.service_identity_user, json.dumps(new_data))

        expected_data.update(new_data)
        si = get_service_identity(self.service_identity_user)
        self.assertDictEqual(expected_data, si.serviceData.to_json_dict())

        new_data = {'key1': None, 'key2': 'B', 'key3': 'C', 'key4': 'D'}
        set_app_data(self.service_identity_user, json.dumps(new_data))

        expected_data.update(new_data)
        del expected_data['key1']
        si = get_service_identity(self.service_identity_user)
        self.assertDictEqual(expected_data, si.serviceData.to_json_dict())
def get_identity(service_identity=None):
    """
    Args:
        service_identity (unicode)
    Returns:
        ServiceIdentityDetailsTO
    """
    service_user = users.get_current_user()
    service_identity_user = get_and_validate_service_identity_user(service_user, service_identity)
    service_identity = get_service_identity(service_identity_user)
    service_profile = get_service_profile(service_user)
    return ServiceIdentityDetailsTO.fromServiceIdentity(service_identity, service_profile)
    def get_default_app_id(self, user_hash, identity):
        index = ProfileHashIndex.get(ProfileHashIndex.create_key(user_hash))
        if not index:
            logging.debug('No profile found with user_hash %s', user_hash)
            return None

        si_user = create_service_identity_user(index.user, identity)
        si = get_service_identity(si_user)
        if not si:
            logging.debug('Service identity not found: %s', si_user.email())
            return None
        return si.app_id
def _convert_to_member_users(service_identity_user, members):
    from rogerthat.bizz.service import validate_is_friend_or_supports_app_id
    mm = None
    if members != MISSING:
        mm = []
        si = get_service_identity(service_identity_user)
        for m in members:
            if isinstance(m, MemberTO) and m.app_id != MISSING:
                validate_is_friend_or_supports_app_id(si, m.app_id, m.app_user)
                mm.append(m.app_user)
            else:
                mm.append(create_app_user_by_email(m, si.app_id))
    return mm
Exemple #28
0
def get_app_statistics(service_identity):
    service_user = users.get_current_user()
    if not service_identity or service_identity == MISSING:
        service_identity = ServiceIdentity.DEFAULT
    service_identity_user = create_service_identity_user(service_user, service_identity)
    si = get_service_identity(service_identity_user)
    default_app = get_app(si.defaultAppId)
    customer = get_customer(service_user)
    available_apps = [a.app_id for a in get_available_apps_for_customer(customer, default_app.demo)]
    if not available_apps:
        # Customer does not exist or has no app ids
        available_apps = sorted(si.appIds)
    return app.get_app_statistics(available_apps)
def _create_news_item(message,
                      service_identity,
                      service_user,
                      broadcast_type,
                      urls=None):
    if service_identity:
        service_identity_user = create_service_identity_user(
            service_user, service_identity)
        si = get_service_identity(service_identity_user)
    else:
        si = get_default_service_identity(service_user)
        service_identity_user = si.service_identity_user
    if '\n' in message:
        split = message.splitlines()
        title = limit_string(split[0], NewsItem.MAX_TITLE_LENGTH)
        message = '\n'.join(split[1:])
    else:
        title = limit_string(message, NewsItem.MAX_TITLE_LENGTH)
    news_buttons = []
    if urls:
        for url in urls:
            if url.get('url') and url.get('name'):
                id_ = u'url'
                caption = u'%s' % url['name']
                action = u'%s' % url['url']
                if len(caption) > NewsItem.MAX_BUTTON_CAPTION_LENGTH:
                    sln_settings = get_solution_settings(service_user)
                    caption = translate(sln_settings.main_language,
                                        SOLUTION_COMMON, u'read_more')
                news_buttons.append(NewsActionButtonTO(id_, caption, action))
                break

    app_ids = [
        app_id for app_id in si.appIds if app_id != App.APP_ID_OSA_LOYALTY
    ]
    return put_news(service_identity_user,
                    sticky=False,
                    sticky_until=0,
                    title=title,
                    message=message,
                    image=None,
                    news_type=NewsItem.TYPE_NORMAL,
                    broadcast_type=broadcast_type,
                    news_buttons=news_buttons,
                    qr_code_content=None,
                    qr_code_caption=None,
                    app_ids=app_ids,
                    news_id=None,
                    accept_missing=True)
    def trans():
        app = get_app(app_id)
        to_put = [app]

        app.admin_services = data.admin_services
        app.name = data.name
        app.type = data.type
        app.main_service = data.main_service
        app.facebook_app_id = data.facebook_app_id
        app.ios_app_id = data.facebook_app_secret
        app.android_app_id = data.android_app_id
        app.dashboard_email_address = data.dashboard_email_address
        app.contact_email_address = data.contact_email_address
        app.user_regex = data.user_regex
        app.demo = data.demo
        app.beta = data.beta
        app.secure = data.secure
        app.chat_enabled = data.chat_enabled
        app.mdp_client_id = data.mdp_client_id
        app.mdp_client_secret = data.mdp_client_secret
        app.owncloud_base_uri = data.owncloud_base_uri
        app.owncloud_admin_username = data.owncloud_admin_username
        app.owncloud_admin_password = data.owncloud_admin_password

        if set(app.embedded_apps).symmetric_difference(data.embedded_apps):
            deferred.defer(send_update_all_embedded_apps, app_id, _countdown=2, _transactional=True)
        app.embedded_apps = data.embedded_apps

        old_auto_connected_services = {acs.service_identity_email for acs in app.auto_connected_services}
        app.auto_connected_services = AutoConnectedServices()
        for acs in data.auto_connected_services:
            service_identity_user = add_slash_default(users.User(acs.service_identity_email))
            if service_identity_user in identities_to_patch:
                si = get_service_identity(service_identity_user)
                si.appIds.append(app_id)
                to_put.append(si)
            acs.service_identity_email = service_identity_user.email()
            app.auto_connected_services.add(acs)

        new_acs = [acs for acs in app.auto_connected_services
                   if acs.service_identity_email not in old_auto_connected_services]
        if new_acs:
            logging.info('There are new auto-connected services for %s: %s', app_id,
                         [acs.service_identity_email for acs in new_acs])
        for acs in new_acs:
            deferred.defer(connect_auto_connected_service, app_id, acs, _transactional=True)
        put_and_invalidate_cache(*to_put)
        return app
def get_group_info(service_identity_user, broadcast_type=None, app_ids=None, news_item=None, has_locations=False):
    check_broadcast_type = None
    check_app_ids = []

    if news_item:
        check_broadcast_type = broadcast_type if broadcast_type else news_item.broadcast_type
        check_app_ids = news_item.app_ids
        if app_ids:
            for app_id in app_ids:
                if app_id not in check_app_ids:
                    check_app_ids.append(app_id)

    else:
        if broadcast_type:
            check_broadcast_type = broadcast_type
        if app_ids:
            check_app_ids = app_ids

    logging.debug('debugging_news get_group_info check_broadcast_type:%s check_app_ids:%s', check_broadcast_type, check_app_ids)
    if not check_broadcast_type or not check_app_ids:
        return [], []


    service_user = get_service_user_from_service_identity_user(service_identity_user)
    group_types = get_group_types_for_service(service_user, check_broadcast_type)
    logging.debug('debugging_news get_group_info group_types:%s', group_types)
    if not group_types:
        return [], []

    if has_locations:
        group_types.append(NewsGroup.TYPE_LOCATION)

    si = get_service_identity(service_identity_user)
    group_ids = []
    for group_type in group_types:
        if group_type in (NewsGroup.TYPE_PROMOTIONS, NewsGroup.TYPE_PRESS):
            app_ids = []
            for app_id in check_app_ids:
                ns = NewsStream.create_key(app_id).get()
                if ns and not ns.should_create_groups:
                    app_ids.append(app_id)
        else:
            app_ids = [si.app_id]
        group_ids.extend(get_group_ids_for_type(group_type, si.app_id, app_ids))

    logging.debug('debugging_news get_group_info group_ids:%s', group_ids)
    return group_types, group_ids
def re_index_news_item(news_item):
    if not news_item:
        return None
    the_index = search.Index(name=NEWS_INDEX)
    news_id = str(news_item.id)
    the_index.delete([news_id])

    if not news_item.published:
        return None

    if news_item.deleted:
        return None

    si = get_service_identity(news_item.sender)
    if not si:
        return None

    sc = SearchConfig.get(SearchConfig.create_key(news_item.sender))
    service_visible = bool(sc and sc.enabled)
    if not service_visible:
        return None

    timestamp = news_item.scheduled_at if news_item.scheduled_at else news_item.timestamp

    fields = [search.AtomField(name='id', value=news_id),
              search.TextField(name='app_ids', value=" ".join(news_item.app_ids)),
              search.TextField(name='sender_name', value=si.name),
              search.NumberField(name='timestamp', value=timestamp)]

    if news_item.type == NewsItem.TYPE_NORMAL:
        if news_item.title:
            fields.append(search.TextField(name='title', value=news_item.title))
        if news_item.message:
            fields.append(search.TextField(name='message', value=news_item.message))
    elif news_item.type == NewsItem.TYPE_QR_CODE:
        if news_item.qr_code_caption:
            fields.append(search.TextField(name='title', value=news_item.qr_code_caption))
        if news_item.qr_code_content:
            fields.append(search.TextField(name='message', value=news_item.qr_code_content))
    else:
        return None

    m_doc = search.Document(doc_id=news_id, fields=fields)
    the_index.put(m_doc)

    return m_doc
def userScanned(request):
    from rogerthat.rpc import users
    app_id = users.get_current_app_id()
    app_user = create_app_user(users.User(request.email), request.app_id)
    service_identity_user = add_slash_default(users.User(request.service_email))

    if app_id == request.app_id:
        should_make_friends = True
    else:
        si = get_service_identity(service_identity_user)
        bizz_check(si, "ServiceIdentity %s not found" % service_identity_user)
        should_make_friends = request.app_id in si.appIds

    if should_make_friends:
        try_or_defer(makeFriends, app_user, service_identity_user, original_invitee=None, servicetag=None, origin=None,
                     notify_invitee=False, notify_invitor=False, user_data=None)

    return UserScannedResponseTO()
Exemple #34
0
def set_service_enabled(customer_id):
    customer = Customer.get_by_id(customer_id)
    if not customer.service_email:
        raise NoSubscriptionException(customer)

    service_user = users.User(customer.service_email)
    service_identity_user = create_service_identity_user(service_user)
    si = get_service_identity(service_identity_user)
    sln_settings = get_solution_settings(service_user)
    sln_settings.service_disabled = False
    customer.service_disabled_at = 0
    customer.disabled_reason = u''
    customer.disabled_reason_int = 0
    customer.subscription_cancel_pending_date = 0
    # restore app ids
    customer.app_ids = si.sorted_app_ids
    customer.default_app_id = si.app_id
    db.put([customer, sln_settings])

    rogerthat_re_enable_service(service_user)
Exemple #35
0
def put_news_item(service_identity_user,
                  title,
                  message,
                  broadcast_type,
                  sponsored,
                  image,
                  action_button,
                  order_items,
                  news_type,
                  qr_code_caption,
                  app_ids,
                  scheduled_at,
                  news_id=None,
                  broadcast_on_facebook=False,
                  broadcast_on_twitter=False,
                  facebook_access_token=None,
                  target_audience=None,
                  role_ids=None,
                  host=None):
    """
    Creates a news item first then processes the payment if necessary (not necessary for non-promoted posts).
    If the payment was unsuccessful it will be retried in a deferred task.

    Args:
        service_identity_user (users.User)
        title (unicode)
        message (unicode)
        broadcast_type (unicode)
        sponsored (bool)
        image (unicode)
        action_button (NewsActionButtonTO)
        order_items (list of OrderItemTO)
        news_type (int)
        qr_code_caption (unicode)
        app_ids (list of unicode)
        scheduled_at (long)
        news_id (long): id of the news item to update. When not provided a new news item will be created.
        broadcast_on_facebook (bool)
        broadcast_on_twitter (bool)
        facebook_access_token (unicode): user or page access token
        target_audience (NewsTargetAudienceTO)
        role_ids (list of long) the list of role ids to filter sending the news to their members
        host (unicode): host of the api request (used for social media apps)

    Returns:
        news_item (NewsBroadcastItemTO)
    """
    if not order_items or order_items is MISSING:
        order_items = []
    if news_type == NewsItem.TYPE_QR_CODE:
        sln_settings = get_solution_settings(
            get_service_user_from_service_identity_user(service_identity_user))
        azzert(SolutionModule.LOYALTY in sln_settings.modules)
    sponsored_until = None
    should_save_coupon = news_type == NewsItem.TYPE_QR_CODE and not news_id
    sponsored_app_ids = set()
    extra_app_ids = []
    si = get_service_identity(service_identity_user)
    for order_item in reversed(order_items):
        if order_item.product == Product.PRODUCT_NEWS_PROMOTION and sponsored:
            azzert(order_item.app_id)
            azzert(order_item.app_id not in sponsored_app_ids)
            sponsored_app_ids.add(order_item.app_id)
            order_item.count = get_sponsored_news_count_in_app(
                service_identity_user, order_item.app_id).count
        elif order_item.product == Product.PRODUCT_EXTRA_CITY:
            azzert(order_item.app_id)
            azzert(order_item.app_id not in extra_app_ids)
            extra_app_ids.append(order_item.app_id)
            if order_item.app_id in si.appIds:
                order_items.remove(order_item)
        else:
            raise BusinessException('Invalid product %s' % order_item.product)

    if not news_id and not app_ids:
        raise BusinessException(
            'Please select at least one app to publish this news in')
    if sponsored:
        sponsored_until_date = datetime.datetime.utcnow() + datetime.timedelta(
            days=SPONSOR_DAYS)
        sponsored_until = long(sponsored_until_date.strftime('%s'))
        # for sponsored news that is free in certain apps no order item is given, so add it here
        sponsored_counts = get_sponsored_news_count(service_identity_user,
                                                    app_ids)
        for sponsored_count in sponsored_counts:
            if sponsored_count.remaining_free != 0 and sponsored_count.app_id in app_ids:
                sponsored_app_ids.add(sponsored_count.app_id)
        app_ids = list(sponsored_app_ids)

    service_user, identity = get_service_identity_tuple(service_identity_user)
    default_app = get_app(si.defaultAppId)
    if App.APP_ID_ROGERTHAT in si.appIds and App.APP_ID_ROGERTHAT not in app_ids:
        app_ids.append(App.APP_ID_ROGERTHAT)
    if default_app.demo and App.APP_ID_ROGERTHAT in app_ids:
        app_ids.remove(App.APP_ID_ROGERTHAT)
    kwargs = {
        'sticky': sponsored,
        'sticky_until': sponsored_until,
        'message': message,
        'broadcast_type': broadcast_type,
        'service_identity': identity,
        'news_id': news_id,
        'app_ids': app_ids,
        'image': image,
        'scheduled_at': scheduled_at,
        'target_audience': target_audience,
        'role_ids': role_ids
    }
    if not news_id:
        kwargs['news_type'] = news_type

    if news_type == NewsItem.TYPE_QR_CODE:
        if should_save_coupon:

            def trans():
                coupon = NewsCoupon(
                    parent=NewsCoupon.create_parent_key(service_identity_user),
                    content=qr_code_caption)
                coupon.put()
                return coupon

            coupon = db.run_in_transaction(trans)
            kwargs['qr_code_content'] = u'%s' % json.dumps({'c': coupon.id})
        kwargs['qr_code_caption'] = qr_code_caption
    elif news_type == NewsItem.TYPE_NORMAL:
        kwargs.update({
            'action_buttons': [action_button] if action_button else [],
            'title':
            title
        })
    else:
        raise BusinessException('Invalid news type')
    for key, value in kwargs.items():
        if value is MISSING:
            del kwargs[key]

    with users.set_user(service_user):
        try:

            def trans():
                news_item = news.publish(accept_missing=True, **kwargs)
                if should_save_coupon:
                    _save_coupon_news_id(news_item.id, coupon)
                elif news_type == NewsItem.TYPE_QR_CODE and qr_code_caption is not MISSING and qr_code_caption and news_id:
                    news_coupon = NewsCoupon.get_by_news_id(
                        service_identity_user, news_id)
                    if news_coupon:
                        news_coupon.content = qr_code_caption
                        news_coupon.put()
                    else:
                        logging.warn(
                            'Not updating qr_code_caption for non-existing coupon for news with id %d',
                            news_id)
                if order_items:
                    create_and_pay_news_order(service_user, news_item.id,
                                              order_items)
                return news_item

            news_item = run_in_xg_transaction(trans)
            if broadcast_on_facebook or broadcast_on_twitter:
                if scheduled_at is not MISSING and scheduled_at > 0:
                    schedule_post_to_social_media(service_user, host,
                                                  broadcast_on_facebook,
                                                  broadcast_on_twitter,
                                                  facebook_access_token,
                                                  news_item.id, scheduled_at)
                else:
                    post_to_social_media(service_user, broadcast_on_facebook,
                                         broadcast_on_twitter,
                                         facebook_access_token, news_item.id)

            return NewsBroadcastItemTO.from_news_item_to(
                news_item, broadcast_on_facebook, broadcast_on_twitter)
        except:
            if should_save_coupon:
                db.delete_async(coupon)
            raise
Exemple #36
0
    def post(self):
        # this url is used in the beacon configurator app to override the uuid, major and minor
        from rogerthat.pages.shortner import get_short_url_by_code
        url = self.request.POST.get("url", None)
        signature_client = self.request.POST.get("signature", None)
        if not (url and signature_client):
            logging.error("not all params given")
            self.abort(500)
            return

        signature_client = signature_client.upper()
        logging.info("validate beacon app url: %s and signature: %s", url,
                     signature_client)
        solution_server_settings = get_solution_server_settings()
        signature_server = md5_hex(
            solution_server_settings.shop_beacons_app_secret %
            (url, url)).upper()
        logging.info("signature server: %s", signature_server)
        if not (url and signature_client == signature_server):
            logging.error("signature did not match")
            self.abort(500)
            return

        m = re.match("^(HTTP|http)(S|s)?://(.*)/(M|S)/(.*)$", url)
        if not m:
            logging.error("invalid url")
            self.abort(500)
            return

        _, _, _, _, code = m.groups()
        su = get_short_url_by_code(code)
        logging.info("validate short url: %s", su.full)
        if not su.full.startswith("/q/s/"):
            logging.error("short url does not start with /q/s")
            self.abort(500)
            return

        match = re.match("^/q/s/(.+)/(\\d+)$", su.full)
        if not match:
            logging.error("user_code not found in url")
            self.abort(500)
            return

        user_code = match.group(1)
        logging.info("validating user code: %s", user_code)
        sid = match.group(2)
        logging.info("validating sid: %s", sid)
        pp = ProfilePointer.get_by_key_name(user_code)
        if not pp:
            logging.error("ProfilePointer not found")
            self.abort(500)
            return

        sid = get_service_interaction_def(pp.user, int(sid))
        if not sid:
            logging.error("sid not found")
            self.abort(500)
            return

        si = get_service_identity(sid.service_identity_user)

        if not si:
            logging.error("service_identity not found")
            self.abort(500)
            return

        def trans():
            beacon = Beacon.all().ancestor(parent_key(si.service_user)).get()
            if beacon:
                return beacon.uuid, beacon.name
            app = App.get(App.create_key(si.app_id))
            app.beacon_last_minor = app.beacon_last_minor + 1
            name = "%s|%s" % (app.beacon_major, app.beacon_last_minor)
            logging.info("add_new_beacon: %s", name)
            if not add_new_beacon(app.beacon_uuid, name, u'Autoconnect',
                                  si.service_identity_user):
                raise Exception("Beacon already exists")

            app.put()
            return app.beacon_uuid, name

        xg_on = db.create_transaction_options(xg=True)
        beacon_uuid, beacon_name = db.run_in_transaction_options(xg_on, trans)

        major, minor = beacon_name.split("|")
        logging.info("Auto connecting beacon %s to service %s", beacon_name,
                     si.service_identity_user)

        outfile = StringIO()
        s_ushort(outfile, int(major))
        s_ushort(outfile, int(minor))
        id_ = base64.b64encode(outfile.getvalue())

        self.response.headers['Content-Type'] = 'text/json'
        self.response.write(
            json.dumps({
                "uuid": beacon_uuid,
                "major": int(major),
                "minor": int(minor),
                "email": si.qualifiedIdentifier,
                "name": si.name,
                "id": id_
            }))