def update_apple_push_device_token(mobile, token):
    if mobile.type in Mobile.IOS_TYPES:
        token.decode("hex")  # just check whether is nicely hex encoded

    if mobile.pushId == token:
        pass  # prevent unnecessary datastore accesses

    old_mobiles = list(get_mobiles_by_ios_push_id(token))
    user = mobile.user
    def trans(mobile_key, user):
        mobile, profile = db.get((mobile_key, get_user_profile_key(user)))
        mobile.pushId = token
        db.put_async(mobile)
        if not profile.mobiles:
            profile.mobiles = MobileDetails()
        if mobile.account in profile.mobiles:
            profile.mobiles[mobile.account].pushId = token
        else:
            profile.mobiles.addNew(mobile.account, mobile.type, token, get_app_id_from_app_user(user))
        for old_mobile in old_mobiles:
            if mobile_key != old_mobile.key():
                if mobile.user == old_mobile.user:
                    _mark_mobile_for_delete(old_mobile)
                    profile.mobiles.remove(old_mobile.account)
                else:
                    deferred.defer(mark_mobile_for_delete, old_mobile.user, old_mobile.key(), _transactional=True)
        profile.put()
    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans, mobile.key(), user)
def add_auto_connected_services(app_id, services, auto_connect_now=True):
    def trans():
        app = get_app(app_id)
        to_be_put = [app]

        si_users = [add_slash_default(users.User(acs.service_identity_email)) for acs in services]
        service_identities = get_service_identities_by_service_identity_users(si_users)
        for si, acs in zip(service_identities, services):
            if not si:
                raise ServiceWithEmailDoesNotExistsException(acs.service_identity_email)
            if app_id not in si.appIds:
                si.appIds.append(app_id)
                to_be_put.append(si)

            acs.service_identity_email = si.user.email()
            app.auto_connected_services.add(acs)

        put_and_invalidate_cache(*to_be_put)

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

    if auto_connect_now:
        logging.info('There are new auto-connected services for %s: %s', app_id, [acs.service_identity_email
                                                                                  for acs in services])
        for acs in services:
            deferred.defer(connect_auto_connected_service, app_id, acs)
Beispiel #3
0
def _3000_revoke_roles(job_key):
    phase = MigrateServiceJob.PHASE_3000_REVOKE_ROLES
    next_phase = MigrateServiceJob.PHASE_4000_DISCONNECT_FRIENDS

    # Validate that the job still exists
    job = _get_job(job_key, phase)

    # Do the work
    _log_progress(job)

    try:
        si_user_email = job.from_service_user.email()
        profiles = UserProfile.list_by_service_role_email(si_user_email)
        for p in profiles:
            def trans():
                job = db.get(job_key)

                for si_email, granted_roles in p.grants.iteritems():
                    if si_email.startswith(si_user_email + '/'):
                        si_user = users.User(si_email)
                        for role in granted_roles:
                            p.revoke_role(si_user, role)
                            job.add_service_grant(si_user, p.user, role)
                put_and_invalidate_cache(p, job)

            xg_on = db.create_transaction_options(xg=True)
            db.run_in_transaction_options(xg_on, trans)
    except BusinessException as e:
        logging.exception("Caught BusinessException")
        azzert(False, "Caught BusinessException: %s" % e)

    # Set the next phase
    _set_job_in_next_phase(job_key, phase, next_phase)
def start_admin_debugging(app_user, timeout):
    mobiles = list(get_user_active_mobiles(app_user))
    azzert(len(mobiles) == 1)

    m = mobiles[0]
    if m.type in (Mobile.TYPE_IPHONE_HTTP_APNS_KICK,
                  Mobile.TYPE_IPHONE_HTTP_XMPP_KICK,
                  Mobile.TYPE_WINDOWS_PHONE) \
              or (m.is_android and m.pushId):
        # No XMPP account needs to be created
        settings = get_server_settings()
        jid = base64.b64encode(encrypt_value(md5(settings.secret), users.get_current_user().email().encode('utf8')))
        password = None
        type_ = CurrentlyForwardingLogs.TYPE_GAE_CHANNEL_API
    else:
        account = generate_account(u'dbg_%s' % uuid.uuid4().get_hex())
        if not APPSCALE:
            create_jabber_account(account, None)
        jid = account.account
        password = account.password
        type_ = CurrentlyForwardingLogs.TYPE_XMPP

    def trans():
        debug_request = StartDebuggingRequest(key=StartDebuggingRequest.create_key(app_user, jid),
                                              timestamp=now())
        db.put_async(debug_request)
        deferred.defer(stop_debugging, app_user, jid, debug_request=debug_request, notify_user=False,
                       _countdown=timeout * 60, _transactional=True, _queue=SCHEDULED_QUEUE)
        return start_log_forwarding(app_user, jid, xmpp_target_password=password, type_=type_)

    xg_on = db.create_transaction_options(xg=True)
    return db.run_in_transaction_options(xg_on, trans)
def update_service_avatar(service_user, image):
    img = images.Image(image)
    img.im_feeling_lucky()
    img.execute_transforms()
    if img.height != img.width:
        devation = float(img.width) / float(img.height)
        if devation < 0.95 or devation > 1.05:
            from rogerthat.bizz.service import AvatarImageNotSquareException
            logging.debug("Avatar Size: %sx%s" % (img.width, img.height))
            raise AvatarImageNotSquareException()
    img = images.Image(image)
    img.resize(150, 150)
    image = img.execute_transforms(images.PNG, 100)
    if is_trial_service(service_user):
        image = add_trial_service_overlay(image)
    def trans():
        service_profile = get_service_profile(service_user)
        avatar = get_avatar_by_id(service_profile.avatarId)
        if not avatar:
            avatar = Avatar(user=service_profile.user)
        update_avatar_profile(service_profile, avatar, image)
        service_profile.version += 1
        service_profile.put()

        from rogerthat.bizz.job.update_friends import schedule_update_all_friends_of_service_user
        schedule_update_all_friends_of_service_user(service_profile)
    xg_on = db.create_transaction_options(xg=True)
    return db.run_in_transaction_options(xg_on, trans)
Beispiel #6
0
def add_auto_connected_services(app_id, services, auto_connect_now=True):
    def trans():
        app = get_app(app_id)
        to_be_put = [app]

        si_users = [add_slash_default(users.User(acs.service_identity_email)) for acs in services]
        service_identities = get_service_identities_by_service_identity_users(si_users)
        for si, acs in zip(service_identities, services):
            if not si:
                raise ServiceWithEmailDoesNotExistsException(acs.service_identity_email)
            if app_id not in si.appIds:
                si.appIds.append(app_id)
                to_be_put.append(si)

            acs.service_identity_email = si.user.email()
            app.auto_connected_services.add(acs)

        put_and_invalidate_cache(*to_be_put)

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

    if auto_connect_now:
        for acs in services:
            logging.info("There is a new auto-connected service for %s: %s", app_id, acs.service_identity_email)
            run_job(get_user_profile_keys_by_app_id, [app_id],
                    hookup_with_default_services.run_for_auto_connected_service, [acs, None])
Beispiel #7
0
def take():
    quiz = db(db.quiz.id == request.get_vars['id']).select()[0]
    activeQ = db(db.question.id == quiz.questions[int(request.get_vars['q']) -
                                                  1]).select()[0]
    status = ""
    userid = auth.user.id
    results = []
    if userid == quiz.author_id:
        owner = True
        if not quiz.active:
            db(db.quiz.id == request.get_vars['id']).update(active=True)
    else:
        owner = False
        if not quiz.active:
            redirect(URL('default', 'index'))
        if request.vars.submitAnswer:
            if activeQ.active:
                options = googledb.create_transaction_options(
                    propagation=googledb.ALLOWED)
                googledb.run_in_transaction_options(
                    options, submitGuess,
                    quiz.questions[int(request.get_vars['q']) - 1],
                    unicode(userid), int(request.vars.answer))
                response.flash = T("Answer submitted!")
            else:
                response.flash = T("Answer not submitted - quiz not active")
    return dict(quiz=quiz,
                question=activeQ,
                qnum=request.get_vars['q'],
                owner=owner,
                results=results)
def ensure_in_transaction(func, *args, **kwargs):
    """ Runs the specified method in a transaction, if the current thread is
    not currently running in a transaction already.

    However, if we're running as part of the remote-api service, do
    *not* run in a transaction, since remote-api does not support
    transactions well (in particular, you can't do any queries while
    inside a transaction).  The remote-api shell marks itself in the
    SERVER_SOFTWARE environment variable; other remote-api users
    should do similarly.

    Arguments:
       func: the function to run in transcation
       *args, **kwargs: the args/kwargs to pass to func, with the
          exception of:
       xg_on: if True allow XG transactions (which are disallowed by default)
    """
    if 'xg_on' in kwargs:
        xg_on = kwargs['xg_on']
        del kwargs['xg_on']
    else:
        xg_on = None

    if db.is_in_transaction() or 'remote' in os.environ["SERVER_SOFTWARE"]:
        return func(*args, **kwargs)

    if xg_on is not None:
        options = db.create_transaction_options(xg=xg_on)
        return db.run_in_transaction_options(options, func, *args, **kwargs)
    else:
        return db.run_in_transaction(func, *args, **kwargs)
Beispiel #9
0
def _create_restaurant_invite(service_user, service_identity, invitee, message, app_id):
    def trans():
        # 1: Check if invitee has been send from service in the last month
        sln_settings = get_solution_settings(service_user)
        db_key = RestaurantInvite.create_key(service_user, service_identity, invitee, sln_settings.solution)
        old_invite = db.get(db_key)
        t = time.time()
        # 2: Store in datastore
        if old_invite:
            if old_invite.status == RestaurantInvite.STATUS_ACCEPTED:
                return
            if not old_invite.epoch < t - 30 * 24 * 60 * 60:
                return
            else:
                old_invite.epoch = t
                old_invite.put()
        else:
            invite = RestaurantInvite(key=db_key)
            invite.epoch = t
            invite.put()
        # 3: Do invite
        deferred.defer(_restaurant_invite, service_user, service_identity, invitee, message, unicode(db_key), sln_settings, app_id,
                       _transactional=True)
    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Beispiel #10
0
    def post(self):
        make_empty = lambda x: None if x is None or x.strip() == '' else x

        def trans():
            account = get_account_by_session_cookie(self.request.cookies)
            account.dateTimezone = self.request.get('timezone')
            account.dailyResultsExport = make_empty(self.request.get("dailyResultsExport"))
            db.put_async(account)
            daily_results_export = get_daily_result_export_by_account(account)
            if account.dailyResultsExport:
                if not daily_results_export:
                    timezone = pytz.timezone(account.dateTimezone)
                    tmp = datetime.datetime.fromtimestamp(time.time(), tz=timezone) + datetime.timedelta(1)
                    tomorrow = datetime.datetime(tmp.year, tmp.month, tmp.day, tzinfo=timezone)
                    epoch = datetime.datetime.fromtimestamp(0, tz=pytz.timezone('UTC'))
                    delta = tomorrow - epoch
                    timestamp = int(delta.total_seconds())
                    DailyResultExport(key_name='daily_result_export', parent=account, nextExport=timestamp).put()
            else:
                if daily_results_export:
                    daily_results_export.delete()

        xg_on = db.create_transaction_options(xg=True)
        db.run_in_transaction_options(xg_on, trans)
        self.redirect('/')
Beispiel #11
0
def _move_loyalty_slides_to_cloudstorage(ls_key):
    def trans():
        ls = db.get(ls_key)
        _copy_gcs_file(ls)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
def reactivate_user_profile(profile, app_user, owncloud_password=None, tos_version=None,
                            consent_push_notifications_shown=False):

    def trans():

        models_to_restore = list()
        archives_to_delete = list()

        archives_to_delete.append(profile)
        if isinstance(profile, UserProfileArchive):
            new_user_profile = profile.archive(UserProfile)
        else:
            new_user_profile = profile.archive(FacebookUserProfile)
        models_to_restore.append(new_user_profile)

        if owncloud_password and not new_user_profile.owncloud_password:
            new_user_profile.owncloud_password = owncloud_password
        if tos_version:
            new_user_profile.tos_version = tos_version
        if consent_push_notifications_shown:
            new_user_profile.consent_push_notifications_shown = True
        new_user_profile.invalidateCache()

        db.put(models_to_restore)
        db.delete(archives_to_delete)
        deferred.defer(restoreUserDataAfterReactivate, app_user, _transactional=True)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
def _finishup_mobile_registration(mobile, invitor_code, invitor_secret, ipaddress, ms_key):
    mobile_user = mobile.user
    app_settings = get_app_settings(get_app_id_from_app_user(mobile_user))
    user_profile = get_user_profile(mobile_user)
    server_settings = get_server_settings()

    def trans():  # Operates on 2 entity groups
        email = get_human_user_from_app_user(mobile_user).email()
        for _, static_email in chunks(server_settings.staticPinCodes, 2):
            if email == static_email:
                break
        else:
            deferred.defer(send_welcome_message, mobile_user, _transactional=True, _countdown=5)

        mobile_settings = db.get(ms_key)
        request = UpdateSettingsRequestTO()
        request.settings = SettingsTO.fromDBSettings(app_settings, user_profile, mobile_settings)
        updateSettings(update_settings_response_handler, logError, mobile_user, request=request)

        deferred.defer(_finishup_mobile_registration_step2, mobile.key(), invitor_code, invitor_secret, ipaddress,
                       mobile_settings.majorVersion, mobile_settings.minorVersion, _transactional=True,
                       _queue=FAST_QUEUE)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
def _finishup_mobile_registration_step2(mobile_key, invitor_code, invitor_secret, ipaddress, majorVersion,
                                        minorVersion):
    mobile = db.get(mobile_key)
    mobile_user = mobile.user
    server_settings = get_server_settings()

    def trans():  # Operates on 2 entity groups
        hookup_with_default_services.schedule(mobile_user, ipaddress)
        deferred.defer(sync_payment_database, mobile_user, _transactional=True)

        if invitor_code and invitor_secret:
            pp = ProfilePointer.get(invitor_code)
            if not pp:
                logging.error("User with userCode %s not found!" % invitor_code)
            else:
                deferred.defer(ack_invitation_by_invitation_secret, mobile_user, pp.user, invitor_secret,
                               _transactional=True, _countdown=10)

        elif invitor_code:
            for ysaaa_hash, static_email in chunks(server_settings.ysaaaMapping, 2):
                if invitor_code == ysaaa_hash:
                    service_user = users.User(static_email)
                    makeFriends(service_user, mobile_user, original_invitee=None, servicetag=None, origin=ORIGIN_YSAAA)
                    break
            else:
                azzert(False, u"ysaaa registration received but not found mapping")

        for _, static_email in chunks(server_settings.staticPinCodes, 2):
            if mobile_user.email() == static_email:
                break
        else:
            deferred.defer(send_messages_after_registration, mobile_key, _transactional=True)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Beispiel #15
0
def _redeem_city_wide_lottery_visits(service_user, sln_cwl_key, now_):
    def trans():
        models_to_put = []
        sln_cwl = db.get(sln_cwl_key)
        slls = db.get(
            SolutionCityWideLotteryStatistics.create_key(sln_cwl.app_id))
        if slls:
            sln_cwl.count = slls.count
            sln_cwl.app_users = slls.app_users
            models_to_put.append(sln_cwl)

            slls.count = []
            slls.app_users = []
            models_to_put.append(slls)

        for s in SolutionCityWideLotteryVisit.load(sln_cwl.app_id):
            s.redeemed = True
            s.redeemed_timestamp = now_
            models_to_put.append(s)

        if models_to_put:
            put_and_invalidate_cache(*models_to_put)

        send_message(service_user, u"solutions.common.loyalty.points.update")

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Beispiel #16
0
 def add_article(self, blog, text, keywords, nextPostTime):
 
     helper = flickr.flickr_helper.FlickrHelper()
     helper.fetch(keywords)
     
     
     pageUrl = ''
     imageUrl = ''
     
     if helper.id != '':
         pageUrl = helper.get_page_url()
         imageUrl = helper.get_image_url()
         
         
     
     # run_in_transaction_optionsによるトランザクション
     params = {'blogId': blog.id,
               'text': text,
               'nextPostTime': nextPostTime,
               'pageUrl': pageUrl,
               'imageUrl': imageUrl
              }
     
     
     # ★以下のニ行をアンコメントすることで、トランザクションを使わなくなる★
     #self.run_xg_transaction(params)
     #return
     
     xg_options = db.create_transaction_options(xg=True, retries=3)
     db.run_in_transaction_options(xg_options,
                                   self.run_xg_transaction,
                                   params)
def get_friend_location(app_user, friend, target=0):
    myFriendMap = get_friends_map(app_user)
    if not friend.email() in myFriendMap.friendDetails:
        logging.warning("%s is not in %s his/her friendMap anymore. Ignoring getFriendLocation request.", friend, app_user)
        return
    friendDetail = myFriendMap.friendDetails[friend.email()]
    if not friendDetail.sharesLocation:
        return
    friend_profile = get_user_profile(friend)
    if not friend_profile.mobiles:
        return
    request = GetLocationRequestTO()
    request.friend = get_human_user_from_app_user(app_user).email()
    request.high_prio = False
    if target == 0:
        request.target = GetLocationRequestTO.TARGET_MOBILE if users.get_current_mobile() else GetLocationRequestTO.TARGET_WEB
    else:
        request.target = target
    xg_on = db.create_transaction_options(xg=True)
    def trans():
        capi_calls = getLocation(get_location_response_handler, get_location_response_error_handler, friend, request=request, DO_NOT_SAVE_RPCCALL_OBJECTS=True)
        lr = LocationRequest(parent=parent_key(friend), key_name=app_user.email(), timestamp=now())
        db.put_async(lr)
        for capi_call in capi_calls:
            capi_call.lr = lr.key()
            capi_call.target = target
        db.put(capi_calls)
        deferred.defer(_cancel_location_request, lr, None, target, None, _countdown=17 * 60, _transactional=True, _queue=SCHEDULED_QUEUE)
    db.run_in_transaction_options(xg_on, trans)
def start_service_location_tracking(service_identity_user, app_user, until, distance_filter):
    from rogerthat.bizz.friends import areFriends
    service_profile_info, human_profile_info = get_profile_infos([service_identity_user, app_user])
    if not areFriends(service_profile_info, human_profile_info):
        raise CanOnlyTrackServiceSubscriptionsException()
    def trans():
        slt = get_current_tracker(app_user, service_identity_user)
        if slt:
            return slt.encrypted_key()
        key = ServiceLocationTracker.create_key(app_user, str(uuid.uuid4()))
        slt = ServiceLocationTracker(key=key, creation_time=now(), until=until, enabled=True,
                               service_identity_user=service_identity_user)
        slt.put()
        request = TrackLocationRequestTO()
        request.high_prio = True
        request.friend = remove_slash_default(service_identity_user).email()
        request.target = GetLocationRequestTO.TARGET_SERVICE_LOCATION_TRACKER
        request.until = until
        request.distance_filter = distance_filter
        for capi_call in trackLocation(track_location_response_handler, track_location_response_error_handler, app_user,
                                       request=request, DO_NOT_SAVE_RPCCALL_OBJECTS=True):
            capi_call.tracker = key
            capi_call.put()
        return slt.encrypted_key()
    xg_on = db.create_transaction_options(xg=True)
    return db.run_in_transaction_options(xg_on, trans)
def _create_default_calendar(sln_settings_key):
    sln_settings = db.get(sln_settings_key)

    if not sln_settings:
        return

    if SolutionModule.AGENDA not in sln_settings.modules:
        return

    if not sln_settings.default_calendar:

        def trans():
            sc = SolutionCalendar(parent=parent_key(sln_settings.service_user,
                                                    sln_settings.solution),
                                  name="Default",
                                  deleted=False)
            sc.put()
            sln_settings.default_calendar = sc.calendar_id
            put_and_invalidate_cache(sln_settings)
            return sc

        logging.debug('Creating default calendar for: %s',
                      sln_settings.service_user)
        xg_on = db.create_transaction_options(xg=True)
        db.run_in_transaction_options(xg_on, trans)
    def wrapped(*args, **kwargs):
        if is_options:
            options = _clone_options(args[0])
            args = args[1:]
        else:
            options = dict(_default_options)
        if is_retries:
            retries = args[0]
            args = args[1:]
        else:
            retries = options['retries']
        options['retries'] = 0
        if options.get('propagation') is None and _temp_transaction_options.propagation:
            options['propagation'] = db.ALLOWED
        options = db.create_transaction_options(**options)

        if db.is_in_transaction():
            return _orig_run_in_transaction_options(options, *args, **kwargs)

        if not retries:
            retries = 3
        if APPSCALE:
            retries += 3

        def run(transaction_guid):
            max_tries = retries + 1
            count = 0
            while count < max_tries:
                count += 1
                start = time.time()
                try:
                    return _orig_run_in_transaction_options(options, *args, **kwargs)
                except (TransactionFailedError, db.Timeout) as e:
                    if isinstance(e, db.Timeout) and type(e) != db.Timeout:
                        raise e  # only retrying in case of db.Timeout exceptions, not subclasses
                    if count == max_tries:
                        raise e
                    transactions.post_transaction_actions.reset(transaction_guid)
                    logging.info("%s: %s. Retrying... (%s)", e.__class__.__name__, e.message, count)
                    sleep_time = 1.1 - (time.time() - start)
                    if sleep_time > 0:
                        logging.info("Sleeping %s seconds ....", sleep_time)
                        time.sleep(sleep_time)

        from rogerthat.utils import transactions
        if db.is_in_transaction():
            transaction_guid = transactions.post_transaction_actions.get_current_transaction_guid()
        else:
            transaction_guid = str(uuid.uuid4())
            transactions.post_transaction_actions.set_current_transaction_guid(transaction_guid)
        try:
            r = run(transaction_guid)
        except:
            transactions.post_transaction_actions.finalize(success=False, transaction_guid=transaction_guid)
            raise
        try:
            transactions.post_transaction_actions.finalize(success=True, transaction_guid=transaction_guid)
        except:
            logging.error("Caught exception in rpc.transaction_done", exc_info=1, _suppress=False)
        return r
Beispiel #21
0
 def get(self):
   key = self.request.get('key')
   amount = self.request.get('amount')
   xg = self.request.get('xg')
   if xg is not None and xg == 'true':
     try:
       xg_on = db.create_transaction_options(xg=True)
       db.run_in_transaction_options(xg_on,
         self.increment_counters, key, int(amount))
       counter1 = Counter.get_by_key_name(key)
       counter2 = Counter.get_by_key_name(key + '_backup')
       status = {
         'success' : True,
         'counter' : counter1.counter,
         'backup' : counter2.counter
       }
     except Exception:
       counter1 = Counter.get_by_key_name(key)
       counter2 = Counter.get_by_key_name(key + '_backup')
       status = {
         'success' : False,
         'counter' : counter1.counter,
         'backup' : counter2.counter
       }
   else:
     try:
       db.run_in_transaction(self.increment_counter, key, int(amount))
       counter = Counter.get_by_key_name(key)
       status = { 'success' : True, 'counter' : counter.counter }
     except Exception:
       counter = Counter.get_by_key_name(key)
       status = { 'success' : False, 'counter' : counter.counter }
   self.response.headers['Content-Type'] = "application/json"
   self.response.out.write(json.dumps(status))
Beispiel #22
0
 def get(self):
     xg_on = db.create_transaction_options(xg=True)
     output = "Nothing to process."
     time_cursor = None
     try:
         query_results = db.GqlQuery("SELECT latest_processed_time FROM ProcessTimeCursor ORDER BY cursor_added_at DESC LIMIT 1")
     except Exception, e:
         pass
def _get_and_save_facebook_avatar(app_user, fb_id, profile_or_key, avatar_or_key, retry_count=0):
    if retry_count == 5:
        logging.debug("Reached max retry count. Giving up trying to get the facebook avatar for %s.", app_user)
        return None

    avatar = db.get(avatar_or_key) if isinstance(avatar_or_key, db.Key) else avatar_or_key
    if avatar.picture:
        logging.debug("In the mean time, there already is an avatar set for %s. Stop retrying...", app_user)
        return avatar

    profile_or_key_is_key = isinstance(profile_or_key, db.Key)
    try:
        url = 'https://graph.facebook.com/%s/picture' % fb_id
        response = urlfetch.fetch(url, deadline=60)
        if response.status_code == 404:
            logging.warn('Facebook avatar not found. Giving up trying to get the facebook avatar for %s', app_user)
            return None
        if response.status_code != 200:
            logging.warn('Recieved code %s from facebook while fetching avatar. Retrying... \n%s', response.code,
                         response.content)
            profile_key = profile_or_key if profile_or_key_is_key else profile_or_key.key()
            deferred.defer(_get_and_save_facebook_avatar, app_user, fb_id, profile_key, avatar.key(), retry_count + 1,
                           _countdown=5)
            return None
        image = response.content

        def update_avatar_and_profile(profile_or_key_is_key):
            avatar = db.get(avatar_or_key) if isinstance(avatar_or_key, db.Key) else avatar_or_key
            if avatar.picture:
                logging.debug("In the mean time, there already is an avatar set for %s. Stopping...", app_user)
                return None, None
            avatar.picture = image
            avatar.put()

            profile = db.get(profile_or_key) if profile_or_key_is_key else profile_or_key
            _calculateAndSetAvatarHash(profile, image)
            if profile_or_key_is_key:
                profile.put()
            return avatar

        if profile_or_key_is_key:
            xg_on = db.create_transaction_options(xg=True)
            avatar = db.run_in_transaction_options(xg_on, update_avatar_and_profile, profile_or_key_is_key)
        else:
            avatar = update_avatar_and_profile(profile_or_key_is_key)

    except Exception as e:
        avatar.put()  # put empty to get avatar id.

        if isinstance(e, DeadlineExceededError) or isinstance(e, HTTPException) and e.message and 'deadline' in e.message.lower():
            logging.debug("Timeout while retrieving facebook avatar for %s. Retrying...", app_user)
            profile_key = profile_or_key if profile_or_key_is_key else profile_or_key.key()
            deferred.defer(_get_and_save_facebook_avatar, app_user, fb_id, profile_key, avatar.key(), retry_count + 1,
                           _countdown=5)
        else:
            logging.exception("Failed to retrieve facebook avatar for %s.", app_user)

    return avatar
def _edit_profile(app_user, name, image, access_token, birthdate, gender, has_birthdate, has_gender, current_mobile):
    from rogerthat.bizz.profile import update_avatar_profile, couple_facebook_id_with_profile, schedule_re_index

    def trans(image):
        changed_properties = []
        user_profile = get_user_profile(app_user)
        if name is not MISSING:
            if user_profile.name != name:
                changed_properties.append(u"name")
            user_profile.name = name

        # has_birthdate and has_gender are used starting from 1.0.999.A and 1.0.137.i
        if has_birthdate is not MISSING and has_gender is not MISSING:
            if has_birthdate is True:
                user_profile.birthdate = birthdate
                user_profile.birth_day = UserProfile.get_birth_day_int(birthdate)
            if has_gender is True:
                user_profile.gender = gender
        else:
            # birthdate and gender are only used without has_gender and has_birthdate in 1.0.998.A
            if birthdate is not MISSING and gender is not MISSING:
                if birthdate == 0 and gender == 0:
                    pass  # user pressed save in 1.0.998.A without setting gender and birthdate
                else:
                    user_profile.birthdate = birthdate
                    user_profile.birth_day = UserProfile.get_birth_day_int(birthdate)
                    if gender != 0:
                        user_profile.gender = gender

        if image:
            avatar = get_avatar_by_id(user_profile.avatarId)
            if not avatar:
                avatar = Avatar(user=user_profile.user)

            image = base64.b64decode(str(image))
            img = Image(image)
            if img.width > 150 or img.height > 150:
                logging.info('Resizing avatar from %sx%s to 150x150', img.width, img.height)
                img.resize(150, 150)
                image = img.execute_transforms(img.format, 100)

            update_avatar_profile(user_profile, avatar, image)
            changed_properties.append(u"avatar")
        user_profile.version += 1
        user_profile.put()

        from rogerthat.bizz.profile import update_mobiles, update_friends
        update_mobiles(user_profile.user, user_profile, current_mobile)  # update myIdentity
        schedule_re_index(app_user)
        if changed_properties:  # Not necessary when only birth date or gender were updated.
            update_friends(user_profile)  # notify my friends.

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

    if access_token:
        couple_facebook_id_with_profile(app_user, access_token)
def logErrorBizz(request, user=None, install_id=None, session=None, shop=False):
    if not shouldLogClientError(request, user, install_id):
        logging.warn('Ignoring logError request for %s:\n%s\n\n%s',
                     user or install_id, request.description, request.errorMessage)
        return

    def do_in_trans():
        m = hashlib.sha256()
        m.update(request.mobicageVersion.encode('utf-8') if request.mobicageVersion else "")
        m.update("-")
        m.update(str(request.platform))
        m.update("-")
        m.update(request.platformVersion.encode('utf-8') if request.platformVersion else "")
        m.update("-")
        m.update(request.errorMessage.encode('utf-8') if request.errorMessage else "")
        m.update("-")
        m.update(request.description.encode('utf-8') if request.description else "")
        key = m.hexdigest()
        me = MobicageError.get_by_key_name(key)
        if not me:
            me = MobicageError(key_name=key)
            me.mobicageVersion = request.mobicageVersion
            me.platform = request.platform
            me.platformVersion = request.platformVersion
            me.errorMessage = request.errorMessage
            me.description = request.description
            me.occurenceCount = 1
        else:
            me.occurenceCount += 1
        me.put()
        ce = ClientError(parent=me)
        ce.user = user

        if session and session.user != user:
            if session.shop:
                ce.userStr = u"%s (%s via shop)" % (user, session.user)
            else:
                ce.userStr = u"%s (%s)" % (user, session.user)
        elif user:
            if shop:
                ce.userStr = u"%s (via shop)" % user
            else:
                ce.userStr = u"%s" % user
        else:
            ce.userStr = None

        ce.installId = install_id
        ce.timestamp = request.timestamp / 1000 if request.timestamp > now() * 10 else request.timestamp
        ce.timestamp = min(now(), ce.timestamp)  # don't accept timestamps in the future
        ce.put()

        installation = Installation.get_by_key_name(install_id) if install_id else None
        if installation:
            InstallationLog(parent=installation, timestamp=now(), description="ClientError occurred").put()

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, do_in_trans)
def _friend_trial_service(user, service):
    logging.info("Hooking up %s with %s" % (user, service))
    service_identity_user = create_service_identity_user(service, ServiceIdentity.DEFAULT)
    def trans():
        makeFriends(service_identity_user, user, user, None, notify_invitee=True, origin=ORIGIN_USER_INVITE)
        grant_role(service_identity_user, user, roles.ROLE_ADMIN)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
def _send_notification_about_failed_location_fix(user, friend, friend_mobile_key_name, target, error_status):
    '''
    @param user: The user who sent the location request.
    @param friend: The user who failed to execute the location request.
    @param friend_mobile_key_name: The key name of the friend's Mobile model.
    @param target: The reason of the location request. One of GetLocationRequestTO.TARGET_*.
    @param error_status: The reason of the failed location request. One of GetLocationErrorTO.STATUS_*.
    '''
    friend_profile, user_profile = get_profile_infos([friend, user], expected_types=[UserProfile, UserProfile])
    app_name = get_app_name_by_id(user_profile.app_id)
    friend_msg = None
    user_reason_msg = None
    if error_status in (GetLocationErrorTO.STATUS_AUTHORIZATION_DENIED,
                        GetLocationErrorTO.STATUS_AUTHORIZATION_ONLY_WHEN_IN_USE):
        if error_status == GetLocationErrorTO.STATUS_AUTHORIZATION_DENIED:
            friend_msg = localize(friend_profile.language, "_location_services_denied",
                                  name=user_profile.name, app_name=app_name)
            user_reason_msg = localize(user_profile.language, "_friend_denied_location_services",
                                       name=friend_profile.name, app_name=app_name)
        elif error_status == GetLocationErrorTO.STATUS_AUTHORIZATION_ONLY_WHEN_IN_USE:
            friend_msg = localize(friend_profile.language, "_location_services_denied",
                                  name=user_profile.name, app_name=app_name)
            user_reason_msg = localize(user_profile.language, "_friend_denied_location_services",
                                       name=friend_profile.name, app_name=app_name)

        if friend_msg:
            friend_mobile = Mobile.get_by_key_name(friend_mobile_key_name)
            if friend_mobile.is_ios and friend_mobile.osVersion:
                if friend_mobile.osVersion.startswith('7'):
                    friend_msg += "\n\n" + localize(friend_profile.language, "_enable_location_services_ios7",
                                                    app_name=app_name)
                elif friend_mobile.osVersion.startswith('8'):
                    friend_msg += "\n\n" + localize(friend_profile.language, "_enable_location_services_ios8",
                                                    app_name=app_name)

    if target == GetLocationRequestTO.TARGET_MOBILE:
        user_msg = localize(user_profile.language,
                            "We could not determine the location of %(name)s.",
                            name=friend_profile.name)
    elif target == GetLocationRequestTO.TARGET_MOBILE_FIRST_REQUEST_AFTER_GRANT:
        user_msg = localize(user_profile.language,
                            "%(name)s accepted your location sharing request. Unfortunately we could not determine his/her location at this moment.",
                            name=friend_profile.name)
    else:
        logging.error("Don't know what to do in _send_notification_about_failed_location_fix.\n\nLocals:\n%s" % locals())
        return

    if user_reason_msg:
        user_msg = u"%s (%s)" % (user_msg, user_reason_msg)

    if user_msg and not friend_msg:
        user_msg = u"%s\n\n%s" % (user_msg, localize(user_profile.language, "Please try again later."))

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, dashboardNotification, user, user_msg)
    if friend_msg:
        db.run_in_transaction_options(xg_on, dashboardNotification, friend, friend_msg)
def _cleanup(job_guid):
    def trans():
        qry = _worker_qry(job_guid)
        if qry.fetch(1):
            deferred.defer(_cleanup, job_guid, _queue=HIGH_LOAD_CONTROLLER_QUEUE, _countdown=5, _transactional=True)
        else:
            db.delete(RebuildRolesJob.create_key(job_guid))
    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
def _cancel_location_request(location_request, mobile_key_name, target, error_status):
    xg_on = db.create_transaction_options(xg=True)
    def trans():
        location_request_from_ds = db.get(location_request.key())
        if location_request_from_ds and location_request.timestamp == location_request_from_ds.timestamp:
            deferred.defer(_send_notification_about_failed_location_fix, location_request.user, location_request.friend,
                           mobile_key_name, target, error_status, _transactional=True)
            location_request.delete()
    db.run_in_transaction_options(xg_on, trans)
Beispiel #30
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)
Beispiel #31
0
def _migrate_ancestor_models(job, from_ancestor_key):
    def trans():
        old_models = db.GqlQuery("SELECT * WHERE ANCESTOR IS KEY('%s')" % str(from_ancestor_key)).fetch(None)
        if old_models:
            _migrate_models(job, old_models)
            return True
        return False
    xg_on = db.create_transaction_options(xg=True)
    while db.run_in_transaction_options(xg_on, trans):
        pass
def _run_update_embedded_js_for_all_users(db_keys):

    def run():
        return db.get(db_keys)

    xg_on = db.create_transaction_options(xg=True)
    js_embedding_models = db.run_in_transaction_options(xg_on, run)

    request = UpdateJSEmbeddingRequestTO.fromDBJSEmbedding(js_embedding_models)
    run_job(get_active_mobiles_keys, [], _update_embedded_js_for_user, [request])
def run_for_auto_connected_service(app_user_or_key, acs, ipaddress=None, countdown=0):
    app_user = app_user_or_key if isinstance(app_user_or_key, users.User) else users.User(app_user_or_key.name())

    def trans():
        if acs.local:
            deferred.defer(run_local_services, app_user, ipaddress, acs, _transactional=True, _countdown=countdown)
        else:
            hookup(app_user, acs)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Beispiel #34
0
def save_out_of_office_message(service_user, service_identity, message):
    def trans():
        sln_settings = get_solution_settings(service_user)
        sln_i_settings = get_solution_settings_or_identity_settings(
            sln_settings, service_identity)
        sln_i_settings.holiday_out_of_office_message = db.Text(message)
        sln_settings.updates_pending = True
        put_and_invalidate_cache(sln_settings, sln_i_settings)
        return sln_settings

    xg_on = db.create_transaction_options(xg=True)
    sln_settings = db.run_in_transaction_options(xg_on, trans)
    broadcast_updates_pending(sln_settings)
Beispiel #35
0
def find_prospects(app_id,
                   postal_codes,
                   sw_lat,
                   sw_lon,
                   ne_lat,
                   ne_lon,
                   city_name,
                   check_phone_number,
                   radius=200):
    logging.info(
        'Finding prospects for %s',
        dict(app_id=app_id,
             postal_codes=postal_codes,
             south_west=(sw_lat, sw_lon),
             north_east=(ne_lat, ne_lon),
             check_phone_number=check_phone_number,
             city_name=city_name))

    google_maps_key = get_server_settings().googleMapsKey
    shop_app_key = ShopApp.create_key(app_id)

    def trans():
        app = get_app_by_id(app_id)
        azzert(app)

        shop_app = db.get(shop_app_key)
        if not shop_app:
            shop_app = ShopApp(key=shop_app_key)

        shop_app.name = app.name
        shop_app.searched_south_west_bounds.append(db.GeoPt(sw_lat, sw_lon))
        shop_app.searched_north_east_bounds.append(db.GeoPt(ne_lat, ne_lon))
        shop_app.postal_codes = postal_codes
        shop_app.put()

        deferred.defer(_start_building_grid,
                       google_maps_key,
                       app_id,
                       postal_codes,
                       radius,
                       sw_lat,
                       sw_lon,
                       ne_lat,
                       ne_lon,
                       city_name,
                       check_phone_number,
                       _transactional=True,
                       _queue=HIGH_LOAD_WORKER_QUEUE)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Beispiel #36
0
def deploy_translation(service_user):

    def trans():
        to_put = set()

        service_profile = get_service_profile(service_user)
        if not service_profile.editableTranslationSet:
            logging.error("Deploy translation error - no editable translation found for svc %s" % service_user.email())
            return

        # 1. Archive old active translation set
        if service_profile.activeTranslationSet:
            old_active_translation_set = ServiceTranslationSet.get(service_profile.activeTranslationSet)
            old_active_translation_set.status = ServiceTranslationSet.ARCHIVED
            to_put.add(old_active_translation_set)

        # 2. Promote old editable translation set to new active
        service_profile.activeTranslationSet = service_profile.editableTranslationSet
        to_put.add(service_profile)
        new_active_translation_set = ServiceTranslationSet.get(service_profile.activeTranslationSet)
        new_active_translation_set.status = ServiceTranslationSet.ACTIVE
        to_put.add(new_active_translation_set)

        # 3. Create new editable translation set
        new_editable_translation_set = ServiceTranslationSet.create_editable_set(service_user)
        new_editable_translation_set.latest_export_timestamp = new_active_translation_set.latest_export_timestamp
        service_profile.editableTranslationSet = str(new_editable_translation_set.key())
        to_put.add(new_editable_translation_set)

        # 4. Copy existing translations to new
        branding_translations_dict = None
        for tr in ServiceTranslation.all().ancestor(new_active_translation_set).fetch(None):
            translation_dict = tr.translation_dict
            if tr.translation_type == ServiceTranslation.BRANDING_CONTENT:
                branding_translations_dict = translation_dict
            to_put.add(ServiceTranslation.create(new_editable_translation_set, tr.translation_type, translation_dict))

        # 5. Store all in db
        put_and_invalidate_cache(*to_put)

        return service_profile, branding_translations_dict

    xg_on = db.create_transaction_options(xg=True)
    service_profile, branding_translations_dict = db.run_in_transaction_options(xg_on, trans)

    if len(service_profile.supportedLanguages) > 1:
        if branding_translations_dict:
            deferred.defer(_translate_all_app_brandings, service_user, branding_translations_dict)
        deferred.defer(_translate_all_message_flows, service_user)
        deferred.defer(_update_i18n_search_configs, service_user)
    deferred.defer(_populate_new_editable_set, service_user)
Beispiel #37
0
def submitAnswer():
    activeQ = db(db.question.id == int(request.vars.qId)).select()[0]
    if activeQ.active:
        options = googledb.create_transaction_options(
            propagation=googledb.ALLOWED)
        googledb.run_in_transaction_options(options, submitGuess,
                                            int(request.vars.qId),
                                            unicode(auth.user.id),
                                            int(request.vars.guess))
        result = "Submitted Guess: %c" % (
            chr(ord('a') + int(request.vars.guess)))
    else:
        result = "Question not active - try again in a bit!"
    return result
Beispiel #38
0
def _cleanup_session(session_key):

    def trans():
        session = db.get(session_key)
        if session.account == None:
            pass
        elif session.timeout > now():
            return

        session.active = False
        session.put()

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Beispiel #39
0
def messaging_flow_member_result(sik, id_, **kwargs):
    account = get_account_by_sik(sik)
    if not account:
        return

    def trans():
        rp = RunnerProcess.get_by_key_name(id_, parent=account)
        if rp:
            logging.warning("Skipping duplicate messaging.flow_member_result")
            return
        RunnerProcess(key_name=id_, parent=account).put()
        deferred.defer(_flow_member_result, account, kwargs.copy(), _transactional=True)
    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
def add_broadcast_test_person(service_user, test_user):

    def trans():
        service_profile = get_service_profile(service_user, False)
        user_profile = get_user_profile(test_user, False)
        bizz_check(user_profile, u"No user found with e-mail '%s'" % test_user.email())
        bizz_check(not test_user in service_profile.broadcastTestPersons,
                   u"Duplicate test person: %s" % test_user.email())
        service_profile.broadcastTestPersons.append(test_user)
        service_profile.put()
        return user_profile

    xg_on = db.create_transaction_options(xg=True)
    return db.run_in_transaction_options(xg_on, trans)
def save_branding_settings(service_user, branding_settings_to):
    """
    Args:
        service_user (users.User)
        branding_settings_to (BrandingSettingsTO)
    """
    def trans():
        branding_settings, sln_settings, loyalty_settings = db.get(
            (SolutionBrandingSettings.create_key(service_user),
             SolutionSettings.create_key(service_user),
             SolutionLoyaltySettings.create_key(service_user)))

        if not branding_settings:
            branding_settings = SolutionBrandingSettings(
                key=SolutionBrandingSettings.create_key(service_user))
        branding_settings_to.color_scheme = u'light'
        branding_settings_to.background_color = SolutionBrandingSettings.default_background_color(
            branding_settings_to.color_scheme)
        branding_settings_to.text_color = SolutionBrandingSettings.default_text_color(
            branding_settings_to.color_scheme)

        branding_settings.background_color = branding_settings_to.background_color
        branding_settings.color_scheme = branding_settings_to.color_scheme
        branding_settings.menu_item_color = branding_settings_to.menu_item_color
        branding_settings.text_color = branding_settings_to.text_color
        branding_settings.show_identity_name = branding_settings_to.show_identity_name
        branding_settings.show_avatar = branding_settings_to.show_avatar
        branding_settings.modification_time = now()

        sln_settings.events_branding_hash = None
        sln_settings.updates_pending = True

        to_be_put = [branding_settings, sln_settings]

        if loyalty_settings:
            loyalty_settings.loyalty_branding_hash = None
            to_be_put.append(loyalty_settings)
        sgps = SolutionGroupPurchaseSettings.get(
            SolutionGroupPurchaseSettings.create_key(service_user,
                                                     sln_settings.solution))
        if sgps:
            sgps.branding_hash = None
            to_be_put.append(sgps)

        put_and_invalidate_cache(*to_be_put)
        on_trans_committed(broadcast_updates_pending, sln_settings)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
 def post(self):
     if users.get_current_user() is None:
         raise HTTPUnauthorized(
             "You must be logged in to create a new election.")
     params = ProcessParams(request=self.request,
                            optional_params=["start_ts", "end_ts"],
                            required_params=["title", "candidates"])
     if (not isinstance(params["candidates"], list)
             or len(params["candidates"]) < 2):
         raise HTTPBadRequest("At least two candidates are required.")
     # NOTE: Cross-group transcations requires using the high-replication
     # datastore. Add the --high_replication command line flag to
     # dev_appserver.py in the development environment.
     db.run_in_transaction_options(db.create_transaction_options(xg=True),
                                   self.CreateElectionAndCandidates, params)
Beispiel #43
0
def check_i18n_status_of_message_flows(service_user):
    from rogerthat.bizz.service.mfd import render_xml_for_message_flow_design

    def trans():
        translator = get_translator(service_user)
        mfds = get_multilanguage_message_flow_designs_by_status(service_user, MessageFlowDesign.STATUS_VALID)
        for mfd in mfds:
            render_xml_for_message_flow_design(mfd, translator, dict())

        put_and_invalidate_cache(*mfds)

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

    channel.send_message(service_user, u'rogerthat.mfd.changes')
Beispiel #44
0
def load_loyalty_settings():
    service_user = users.get_current_user()
    session_ = users.get_current_session()
    service_identity = session_.service_identity

    def trans():
        sln_l_settings = SolutionLoyaltySettings.get_by_user(service_user)
        if is_default_service_identity(service_identity):
            sln_li_settings = sln_l_settings
        else:
            sln_li_settings = SolutionLoyaltyIdentitySettings.get_by_user(service_user, service_identity)
        return sln_l_settings, sln_li_settings
    xg_on = db.create_transaction_options(xg=True)
    sln_l_settings, sln_li_settings = db.run_in_transaction_options(xg_on, trans)
    return LoyaltySettingsTO.fromSolutionLoyaltySettingsObject(sln_l_settings, sln_li_settings) if sln_l_settings else None
Beispiel #45
0
def delete_event(service_user, event_id):
    def trans():
        settings = get_solution_settings(service_user)
        event = get_event_by_id(service_user, settings.solution, event_id)
        if event:
            if event.source == Event.SOURCE_CMS:
                event.delete()
            else:
                event.deleted = True
                event.put()
            settings.updates_pending = True
            put_and_invalidate_cache(settings)
        return settings

    xg_on = db.create_transaction_options(xg=True)
    settings = db.run_in_transaction_options(xg_on, trans)
    broadcast_updates_pending(settings)
Beispiel #46
0
def load_specific_loyalty_settings(loyalty_type):
    service_user = users.get_current_user()
    session_ = users.get_current_session()
    service_identity = session_.service_identity
    sln_settings = get_solution_settings(service_user)
    if SolutionModule.HIDDEN_CITY_WIDE_LOTTERY in sln_settings.modules:
        loyalty_type = None

    def trans():
        sln_l_settings = SolutionLoyaltySettings.get_by_user(service_user)
        if is_default_service_identity(service_identity):
            sln_li_settings = sln_l_settings
        else:
            sln_li_settings = SolutionLoyaltyIdentitySettings.get_by_user(service_user, service_identity)
        return sln_l_settings, sln_li_settings
    xg_on = db.create_transaction_options(xg=True)
    sln_l_settings, sln_li_settings = db.run_in_transaction_options(xg_on, trans)
    return LoyaltySettingsTO.fromSolutionLoyaltySettingsObject(sln_l_settings, sln_li_settings, loyalty_type) if sln_l_settings else None
Beispiel #47
0
  def get(self):
    key = self.request.get('key')
    amount = self.request.get('amount')
    xg = self.request.get('xg')
    if xg is not None and xg == 'true':
      try:
        xg_on = db.create_transaction_options(xg=True)
        db.run_in_transaction_options(xg_on,
          self.increment_counters, key, int(amount))
        counter1_future = db.get_async(db.Key.from_path('Counter', key))
        counter2_future = db.get_async(db.Key.from_path('Counter', 
          key + '_backup'))

        counter1 = counter1_future.get_result()
        counter2 = counter2_future.get_result()
        status = {
          'success' : True,
          'counter' : counter1.counter,
          'backup' : counter2.counter
        }
      except Exception:
        counter1_future = db.get_async(db.Key.from_path('Counter', key))
        counter2_future = db.get_async(db.Key.from_path('Counter',
          key + '_backup'))

        counter1 = counter1_future.get_result()
        counter2 = counter2_future.get_result()
        status = {
          'success' : False,
          'counter' : counter1.counter,
          'backup' : counter2.counter
        }
    else:
      try:
        db.run_in_transaction(self.increment_counter, key, int(amount))
        counter_future = db.get_async(db.Key.from_path('Counter', key))
        counter = counter_future.get_result()
        status = { 'success' : True, 'counter' : counter.counter }
      except Exception:
        counter_future = db.get_async(db.Key.from_path('Counter', key))
        counter = counter_future.get_result()
        status = { 'success' : False, 'counter' : counter.counter }
    self.response.headers['Content-Type'] = "application/json"
    self.response.out.write(json.dumps(status))
Beispiel #48
0
def save_menu(service_user, menu):
    def trans():
        sln_settings = get_solution_settings(service_user)

        m = get_restaurant_menu(service_user, sln_settings.solution)
        if not m:
            m = RestaurantMenu(key=RestaurantMenu.create_key(
                service_user, sln_settings.solution))
        m.is_default = False
        m.predescription = menu.predescription
        m.postdescription = menu.postdescription
        m.categories = MenuCategories()
        category_names = list()
        for c in menu.categories:
            if c.name in category_names:
                raise BusinessException(
                    common_translate(sln_settings.main_language,
                                     SOLUTION_COMMON,
                                     "category_duplicate_name",
                                     name=c.name))
            if c.id == MISSING:
                c.id = str(uuid.uuid4()).decode('UTF-8')
            category_names.append(c.name)
            item_names = list()
            for i in c.items:
                if i.name in item_names:
                    raise BusinessException(
                        common_translate(sln_settings.main_language,
                                         SOLUTION_COMMON,
                                         "product_duplicate_name",
                                         name=i.name))
                if i.id == MISSING:
                    i.id = str(uuid.uuid4()).decode('UTF-8')
                item_names.append(i.name)
            m.categories.add(c)

        sln_settings.updates_pending = True
        put_and_invalidate_cache(m, sln_settings)
        return sln_settings

    xg_on = db.create_transaction_options(xg=True)
    sln_settings = db.run_in_transaction_options(xg_on, trans)
    broadcast_updates_pending(sln_settings)
Beispiel #49
0
def delete_group_purchase(service_user, service_identity, group_purchase_id):
    service_identity_user = create_service_identity_user_wo_default(
        service_user, service_identity)

    def txn():
        sln_settings = get_solution_settings(service_user)
        m = SolutionGroupPurchase.get_by_id(
            group_purchase_id,
            parent_key_unsafe(service_identity_user, sln_settings.solution))
        azzert(service_user == m.service_user)
        m.deleted = True
        m.put()

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, txn)

    send_message(service_user,
                 u"solutions.common.group_purchase.update",
                 service_identity=service_identity)
def _provision_without_publish(sln_settings_key):
    service_user = users.User(sln_settings_key.parent().name())
    service_profile = get_service_profile(service_user)
    if not service_profile or service_profile.solution != SOLUTION_FLEX:
        return

    with users.set_user(service_user):
        default_lang = get_default_language()
        sln_settings = get_and_complete_solution_settings(
            service_user, SOLUTION_FLEX)
        put_avatar_if_needed(service_user)
        # Force update branding settings
        branding_settings = SolutionBrandingSettings.get(
            SolutionBrandingSettings.create_key(service_user))
        if not branding_settings:
            return

        if branding_settings.color_scheme == u'dark':
            branding_settings.menu_item_color = SolutionBrandingSettings.default_menu_item_color(
                u'light')

        branding_settings.modification_time = now()
        branding_settings.color_scheme = u'light'
        branding_settings.background_color = SolutionBrandingSettings.default_background_color(
            branding_settings.color_scheme)
        branding_settings.text_color = SolutionBrandingSettings.default_text_color(
            branding_settings.color_scheme)
        branding_settings.show_avatar = False
        branding_settings.put()
        main_branding = get_and_store_main_branding(service_user)

        populate_identity(sln_settings, main_branding.branding_key)

        for i, label in enumerate(['About', 'History', 'Call', 'Recommend']):
            system.put_reserved_menu_item_label(
                i, translate(sln_settings.main_language, SOLUTION_COMMON,
                             label))

        xg_on = db.create_transaction_options(xg=True)
        allow_transaction_propagation(db.run_in_transaction_options, xg_on,
                                      provision_all_modules, sln_settings,
                                      DEFAULT_COORDS, main_branding,
                                      default_lang)
Beispiel #51
0
def delete_holiday(service_user, service_identity, holiday):
    def trans():
        sln_settings = get_solution_settings(service_user)
        sln_i_settings = get_solution_settings_or_identity_settings(
            sln_settings, service_identity)
        for i in range(len(sln_i_settings.holidays) / 2):
            if sln_i_settings.holidays[2 * i] == holiday.start:
                del sln_i_settings.holidays[2 * i:2 * i + 2]
                break
        else:
            raise InvalidHolidayException('holiday-not-found')

        sln_settings.updates_pending = True
        put_and_invalidate_cache(sln_settings, sln_i_settings)
        return sln_settings

    xg_on = db.create_transaction_options(xg=True)
    sln_settings = db.run_in_transaction_options(xg_on, trans)
    broadcast_updates_pending(sln_settings)
Beispiel #52
0
    def post(self):
        key = self.request.get('key')

        def txn():
            rec = URLStorage.get(key)
            if rec:
                rec.topic = self.get_topic(rec.url)

                img = self.get_snapshot(rec.url)
                ss = SnapshotStorage(url=rec.url)
                ss.snapshot = db.Blob(img)
                ss.put()
                rec.snapshot = '/img/blob_snapshot?key=%s' % ss.key()

                rec.title = self.get_title(rec.url)
                rec.put()

        db.run_in_transaction_options(db.create_transaction_options(xg=True),
                                      txn)
Beispiel #53
0
def get_google_calendars(service_user, calendar_id):
    sln_settings = get_solution_settings(service_user)

    def trans():
        sc = SolutionCalendar.get_by_id(
            calendar_id, parent_key(service_user, sln_settings.solution))
        if not sc:
            return None, None
        return sc.get_google_credentials(), sc.google_calendar_ids

    xg_on = db.create_transaction_options(xg=True)
    google_credentials, google_calendar_ids = db.run_in_transaction_options(
        xg_on, trans)

    result = SolutionGoogleCalendarStatusTO()
    result.enabled = False
    result.calendars = []
    if google_credentials:
        logging.debug("access_token_expired: %s",
                      google_credentials.access_token_expired)
        if google_credentials.access_token_expired:
            return result

        http_auth = google_credentials.authorize(httplib2.Http())
        calendar_service = build('calendar', 'v3', http=http_auth)
        try:
            result.enabled = True
            calendar_list = calendar_service.calendarList().list().execute(
                http=http_auth)
            if calendar_list['items']:
                for c in calendar_list['items']:
                    calendar_to = SolutionGoogleCalendarTO()
                    calendar_to.key = c["id"]
                    calendar_to.label = c["summary"]
                    calendar_to.enabled = calendar_to.key in google_calendar_ids
                    result.calendars.append(calendar_to)
        except:
            result.enabled = False
            result.calendars = []
            logging.warn(u"Error while loading calendars", exc_info=1)

    return result
Beispiel #54
0
def _worker_city_wide_lottery(sln_cwl_lottery_key):
    tmp_sln_cwl = db.get(sln_cwl_lottery_key)
    service_user = get_service_user_for_city(tmp_sln_cwl.app_id)

    if not service_user:
        raise Exception(
            "Failed to do city wide lottery service_user not found for app: %s",
            tmp_sln_cwl.app_id)

    def trans():
        sln_cwl = db.get(sln_cwl_lottery_key)
        logging.info("city wide lottery loot: %s", sln_cwl.app_id)
        sln_settings = db.get(SolutionSettings.create_key(service_user))

        if SolutionModule.HIDDEN_CITY_WIDE_LOTTERY in sln_settings.modules:
            now_tz = int(
                time.mktime(
                    datetime.fromtimestamp(
                        now(),
                        pytz.timezone(sln_settings.timezone)).timetuple()))
            logging.debug("sln_cwl.end_timestamp: %s", sln_cwl.end_timestamp)
            logging.debug("end: %s", now_tz)
            seconds_before = sln_cwl.end_timestamp - now_tz
            if seconds_before < 0:
                seconds_before = 0
            logging.debug(
                "_schedule_loot_city_wide_lottery seconds_before: %s",
                seconds_before)
            deferred.defer(_pick_city_wide_lottery_winner,
                           service_user,
                           sln_cwl_lottery_key,
                           _countdown=seconds_before,
                           _queue=SCHEDULED_QUEUE,
                           _transactional=True)

        else:
            sln_cwl.deleted = True
        sln_cwl.schedule_loot_time = sln_cwl.schedule_loot_time * -1
        sln_cwl.put()

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Beispiel #55
0
def _worker(sln_loyalty_lottery_key):
    def trans():
        sln_loyalty_lottery = db.get(sln_loyalty_lottery_key)
        service_user = sln_loyalty_lottery.service_user
        logging.info("loyalty lottery loot: %s", service_user)

        sls_key = SolutionLoyaltySettings.create_key(service_user)
        sln_settings_key = SolutionSettings.create_key(service_user)
        sln_loyalty_settings, sln_settings = db.get(
            [sls_key, sln_settings_key])

        if SolutionModule.LOYALTY in sln_settings.modules:
            if sln_loyalty_settings.loyalty_type != SolutionLoyaltySettings.LOYALTY_TYPE_LOTTERY:
                sln_loyalty_lottery.deleted = True
            else:
                now_tz = int(
                    time.mktime(
                        datetime.fromtimestamp(
                            now(),
                            pytz.timezone(sln_settings.timezone)).timetuple()))
                logging.debug("sln_loyalty_lottery.end_timestamp: %s",
                              sln_loyalty_lottery.end_timestamp)
                logging.debug("end: %s", now_tz)
                seconds_before = sln_loyalty_lottery.end_timestamp - now_tz
                if seconds_before < 0:
                    seconds_before = 0
                logging.debug("_schedule_loot_lottery seconds_before: %s",
                              seconds_before)
                deferred.defer(_pick_winner,
                               service_user,
                               sln_loyalty_lottery_key,
                               _countdown=seconds_before,
                               _queue=SCHEDULED_QUEUE,
                               _transactional=True)

        else:
            sln_loyalty_lottery.deleted = True
        sln_loyalty_lottery.schedule_loot_time = sln_loyalty_lottery.schedule_loot_time * -1
        sln_loyalty_lottery.put()

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Beispiel #56
0
def put_loyalty_settings(loyalty_type, loyalty_settings, loyalty_website):
    service_user = users.get_current_user()
    try:
        def trans(loyalty_type):
            sln_settings = get_solution_settings(service_user)
            if SolutionModule.HIDDEN_CITY_WIDE_LOTTERY in sln_settings.modules:
                loyalty_type = SolutionLoyaltySettings.LOYALTY_TYPE_CITY_WIDE_LOTTERY
            sln_settings.updates_pending = True

            sln_loyalty_settings = SolutionLoyaltySettings.get_by_user(service_user)

            if sln_loyalty_settings.loyalty_type != loyalty_type:
                sln_loyalty_settings.branding_key = None
                sln_settings.loyalty_branding_hash = None
                sln_loyalty_settings.loyalty_type = loyalty_type

            if sln_loyalty_settings.website != loyalty_website:
                sln_loyalty_settings.modification_time = now()

            sln_loyalty_settings.website = loyalty_website

            if loyalty_type == SolutionLoyaltySettings.LOYALTY_TYPE_REVENUE_DISCOUNT:
                sln_loyalty_settings.x_visits = loyalty_settings.x_visits
                sln_loyalty_settings.x_discount = loyalty_settings.x_discount
            elif loyalty_type == SolutionLoyaltySettings.LOYALTY_TYPE_STAMPS:
                sln_loyalty_settings.x_stamps = loyalty_settings.x_stamps
                sln_loyalty_settings.stamps_type = loyalty_settings.stamps_type
                sln_loyalty_settings.stamps_winnings = loyalty_settings.stamps_winnings
                sln_loyalty_settings.stamps_auto_redeem = loyalty_settings.stamps_auto_redeem

            put_and_invalidate_cache(sln_loyalty_settings, sln_settings)
            return sln_settings

        xg_on = db.create_transaction_options(xg=True)
        sln_settings = db.run_in_transaction_options(xg_on, trans, loyalty_type)
        broadcast_updates_pending(sln_settings)

        send_message(service_user, u"solutions.common.loyalty.settings.update")
        return RETURNSTATUS_TO_SUCCESS
    except BusinessException, e:
        return ReturnStatusTO.create(False, e.message)
Beispiel #57
0
def run_in_transaction(function, xg=False, *args, **kwargs):
    """Runs specified function in a transaction.
    If called inside a transaction, the function is executed without creating a new transaction.

    Args:
        function: a function to be run inside the transaction on all remaining arguments
        xg: set to true to allow cross-group transactions (high replication datastore only)
        *args: Positional arguments for function.
        **kwargs: Keyword arguments for function.

    Returns:
        The function's return value, if any
    """
    if db.is_in_transaction():
        return function(*args, **kwargs)
    elif xg:
        transaction_options = db.create_transaction_options(xg=True)
        return db.run_in_transaction_options(transaction_options, function,
                                             *args, **kwargs)
    else:
        return db.run_in_transaction(function, *args, **kwargs)
Beispiel #58
0
    def _prepareService(self, email):
        service_user = users.User(unicode(email))

        def trans():
            service_profile, service_identity = create_service_profile(
                service_user, u's1')
            service_profile.supportedLanguages = ["en", "fr", "nl"]

            service_profile_attrs = [
                "aboutMenuItemLabel", "messagesMenuItemLabel",
                "shareMenuItemLabel", "callMenuItemLabel"
            ]

            for attr in service_profile_attrs:
                setattr(service_profile, attr, attr)
            service_profile.put()

            service_identity_attrs = [
                "name", "qualifiedIdentifier", "description",
                "descriptionBranding", "menuBranding", "mainPhoneNumber",
                "callMenuItemConfirmation"
            ]
            for attr in service_identity_attrs:
                setattr(service_identity, attr, attr)
                service_identity.put()
            return service_profile

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

        # Create some menu items
        for coords in [[1, 0, 1], [1, 1, 1], [1, 2, 1], [1, 3, 1]]:
            create_menu_item(service_user, "3d", "000000",
                             'x'.join(map(str, coords)), "tag", coords,
                             "screenBranding", None, False, False, [])

        # Translate service/identity properties
        translate_service_strings(service_user)

        return service_user, service_profile
Beispiel #59
0
def save_menu_name(service_user, name):
    name = name.strip()
    bizz_check(name, "Name can not be empty")

    def trans():
        sln_settings = get_solution_settings(service_user)
        sln_settings.updates_pending = True

        menu = get_restaurant_menu(service_user, sln_settings.solution)
        menu.name = name

        put_and_invalidate_cache(sln_settings, menu)

        return sln_settings

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

    channel.send_message(sln_settings.service_user,
                         'solutions.common.menu.name_updated',
                         name=name)
    broadcast_updates_pending(sln_settings)
Beispiel #60
0
def create_new_location(service_user, name, broadcast_to_users=None):
    sln_settings = get_solution_settings(service_user)
    if sln_settings.identities:
        service_identity = int(max(sln_settings.identities)) + 1
    else:
        service_identity = 1
    service_identity = u"%s" % service_identity

    def trans():
        sln_i_settings_key = SolutionIdentitySettings.create_key(
            service_user, service_identity)
        sln_i_settings = SolutionIdentitySettings(key=sln_i_settings_key)
        sln_i_settings.name = name
        sln_i_settings.phone_number = sln_settings.phone_number
        sln_i_settings.qualified_identifier = sln_settings.qualified_identifier
        sln_i_settings.description = sln_settings.description
        sln_i_settings.opening_hours = sln_settings.opening_hours
        sln_i_settings.address = sln_settings.address
        sln_i_settings.location = sln_settings.location
        sln_i_settings.search_keywords = sln_settings.search_keywords
        sln_i_settings.inbox_forwarders = []
        sln_i_settings.inbox_connector_qrcode = None
        sln_i_settings.inbox_mail_forwarders = []
        sln_i_settings.inbox_email_reminders_enabled = False
        sln_i_settings.holidays = []
        sln_i_settings.holiday_out_of_office_message = common_translate(
            sln_settings.main_language, SOLUTION_COMMON,
            'holiday-out-of-office')
        sln_i_settings.put()

        if not sln_settings.identities:
            sln_settings.identities = []
        sln_settings.identities.append(service_identity)
        sln_settings.put()
        on_trans_committed(_create_service_identity, service_user,
                           sln_i_settings, broadcast_to_users)

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