def validate_session(secret, logged_in_as): from rogerthat.utils.service import get_service_identity_tuple session = db.run_in_transaction(Session.get_by_key_name, secret) if not session or session.deleted: return None, None if session.timeout < now(): session.deleted = True session.put() return None, None if session.timeout < now() + (SESSION_TIMEOUT / 2): deferred.defer(_update_session_timeout, secret) if logged_in_as: if logged_in_as == session.user.email(): return session, session.user if not (session.shop or session.has_access(logged_in_as)): logging.info("Dropping session because '%s' was not found in %s", logged_in_as, session.service_users_blob) session.delete() return None, None else: service_identity_user = users.User(logged_in_as) service_user, _ = get_service_identity_tuple(service_identity_user) return session, service_user if session.service_identity_user: service_user, _ = get_service_identity_tuple(session.service_identity_user) return session, service_user return session, session.user
def unsubscribe_reminder(email, data, reason): def parse_data(email, data): app_user_email = create_app_user_by_email(email).email() user = users.User(app_user_email) data = base64.decodestring(data) data = decrypt(user, data) data = json.loads(data) azzert(data["d"] == calculate_secure_url_digest(data)) return data, user try: data, app_user = parse_data(email, data) except: logging.exception("Could not decipher url!") return DEACTIVATE_ACCOUNT_DOES_NOT_EXITS is_success = unsubscribe_from_reminder_email(app_user) if is_success: ActivationLog(timestamp=now(), email=app_user.email(), mobile=None, description="Unsubscribed from reminder email | %s" % reason).put() return DEACTIVATE_SUCCESS else: ActivationLog(timestamp=now(), email=app_user.email(), mobile=None, description="Unsubscribed from reminder email not existing account | %s" % reason).put() return DEACTIVATE_ACCOUNT_DOES_NOT_EXITS
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 get(self): settings = get_server_settings() secret = self.request.headers.get("X-Nuntiuz-Secret", None) if secret != settings.jabberSecret: logging.error("Received unauthenticated apple certificate request, ignoring ...") return app_id = self.request.get("id") if not app_id: return app = get_app_by_id(app_id) if not app: return if app.apple_push_cert_valid_until < now() + 30 * DAY: send_mail(settings.dashboardEmail, settings.supportWorkers, "The APN cert of %s is about to expire" % app_id, "The APN cert of %s is valid until %s GMT" % (app_id, time.ctime(app.apple_push_cert_valid_until))) if app.apple_push_cert_valid_until < now() + 15 * DAY: logging.error("The APN cert of %s is valid until %s GMT" % (app_id, time.ctime(app.apple_push_cert_valid_until))) result = json.dumps(dict(cert=app.apple_push_cert, key=app.apple_push_key, valid_until=app.apple_push_cert_valid_until)) self.response.headers['Content-Type'] = 'application/binary' _, data = encrypt_for_jabber_cloud(secret, result) self.response.write(data)
def test_news_item_target_audience(self): today = date.today() date_old = today + relativedelta(years=-40) date_ancient = today + relativedelta(years=-500) date_young = today + relativedelta(years=-10) empty_profile = UserProfile(birthdate=None, gender=None) profile_old_male = UserProfile(birthdate=get_epoch_from_datetime(date_old), gender=UserProfile.GENDER_MALE) profile_old_female = UserProfile(birthdate=get_epoch_from_datetime(date_old), gender=UserProfile.GENDER_FEMALE) profile_ancient_female = UserProfile(birthdate=get_epoch_from_datetime(date_ancient), gender=UserProfile.GENDER_FEMALE) young_female = UserProfile(birthdate=get_epoch_from_datetime(date_young), gender=UserProfile.GENDER_FEMALE) news_item0 = NewsItem(sticky=False, sender=users.User('*****@*****.**'), app_ids=['rogerthat'], timestamp=now(), rogered=False, target_audience_enabled=False) self.assertTrue(news_item0.match_target_audience(profile_old_male)) self.assertTrue(news_item0.match_target_audience(young_female)) self.assertTrue(news_item0.match_target_audience(empty_profile)) news_item1 = NewsItem(sticky=False, sender=users.User('*****@*****.**'), app_ids=['rogerthat'], timestamp=now(), rogered=False, target_audience_enabled=True, target_audience_min_age=0, target_audience_max_age=100, target_audience_gender=UserProfile.GENDER_FEMALE) self.assertFalse(news_item1.match_target_audience(empty_profile)) self.assertFalse(news_item1.match_target_audience(profile_old_male)) self.assertTrue(news_item1.match_target_audience(profile_old_female)) self.assertFalse(news_item1.match_target_audience(profile_ancient_female)) self.assertTrue(news_item1.match_target_audience(young_female)) news_item2 = NewsItem(sticky=False, sender=users.User('*****@*****.**'), app_ids=['rogerthat'], timestamp=now(), rogered=False, target_audience_enabled=True, target_audience_min_age=18, target_audience_max_age=99, target_audience_gender=UserProfile.GENDER_MALE_OR_FEMALE) self.assertFalse(news_item2.match_target_audience(empty_profile)) self.assertTrue(news_item2.match_target_audience(profile_old_male)) self.assertTrue(news_item2.match_target_audience(profile_old_female)) self.assertFalse(news_item2.match_target_audience(profile_ancient_female)) self.assertFalse(news_item2.match_target_audience(young_female))
def _send_event_notification(sln_settings, service_user, service_identity, user_details, event, event_guest): from solutions.common.bizz.messaging import send_inbox_forwarders_message status = translate(sln_settings.main_language, SOLUTION_COMMON, event_guest.status_str) status_message = translate(sln_settings.main_language, SOLUTION_COMMON, u'events_status_notification', status=status, event=event.title) create_chat = True if event_guest.chat_key: create_chat = SolutionInboxMessage.get(event_guest.chat_key) is None if create_chat: event_key = unicode(event.key()) message = create_solution_inbox_message( service_user, service_identity, SolutionInboxMessage.CATEGORY_AGENDA, event_key, False, user_details, now(), status_message, True) event_guest.chat_key = message.solution_inbox_message_key event_guest.put() app_user = user_details[0].toAppUser() else: message, _ = add_solution_inbox_message(service_user, event_guest.chat_key, False, user_details, now(), status_message) app_user = None send_inbox_forwarders_message( service_user, service_identity, app_user, status_message, { 'if_name': user_details[0].name, 'if_email': user_details[0].email }, message_key=message.solution_inbox_message_key, reply_enabled=message.reply_enabled, send_reminder=False) # show as last message sln_i_settings = get_solution_settings_or_identity_settings( sln_settings, service_identity) message_to = SolutionInboxMessageTO.fromModel(message, sln_settings, sln_i_settings, True) send_message(service_user, u'solutions.common.messaging.update', service_identity=service_identity, message=serialize_complex_value(message_to, SolutionInboxMessageTO, False))
def trans(): key_name = "log_analysis_instance" parent = parent_key(users.User(u"*****@*****.**")) la = LogAnalysis.get_by_key_name(key_name, parent) if not la: la = LogAnalysis(key_name=key_name, parent=parent, analyzed_until=now() - 10 * 60) start = la.analyzed_until end = now() la.analyzed_until = end db.put_async(la) deferred.defer(_analyze, start, end, _transactional=True)
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.put() if install_id: if DEBUG: description_url = "http://localhost:8000/datastore/edit/%s" % ce.parent_key() else: description_url = "https://appengine.google.com/datastore/edit?app_id=s~mobicagecloudhr&namespace=&key=%s" % ce.parent_key() installation = Installation.get_by_key_name(install_id) if install_id else None InstallationLog(parent=installation, timestamp=now(), description="ClientError occurred", description_url=description_url).put()
def login(email, password, remember): user = users.get_current_user() if user: return LOGIN_SUCCESS if not email or not password: return LOGIN_FAIL app_user_email = create_app_user_by_email(email).email() user = users.User(app_user_email) if not is_clean_app_user_email(user): return LOGIN_FAIL profile = get_service_or_user_profile(user) if not profile: deactivated_profile = get_deactivated_user_profile(user) if deactivated_profile: ActivationLog(timestamp=now(), email=user.email(), mobile=None, description="Login web with deactivated user").put() return LOGIN_ACCOUNT_DEACTIVATED else: return LOGIN_FAIL if not profile.passwordHash: return LOGIN_FAIL_NO_PASSWORD if not rate_login(user): return LOGIN_TO_MANY_ATTEMPTS if profile.passwordHash != sha256_hex(password): return LOGIN_FAIL response = GenericRESTRequestHandler.getCurrentResponse() try: secret, _ = create_session(user) except ServiceExpiredException: return LOGIN_FAIL_SERVICE_EXPIRED server_settings = get_server_settings() if remember: set_cookie(response, server_settings.cookieSessionName, secret, expires=now() + SESSION_TIMEOUT) else: set_cookie(response, server_settings.cookieSessionName, secret) clear_rate_login(user) request = GenericRESTRequestHandler.getCurrentRequest() update_user_profile_language_from_headers(profile, request.headers) return LOGIN_SUCCESS
def _getBaseFormMessage(self, msg): pos_btn = Button() pos_btn.action = None pos_btn.caption = u"Submit" pos_btn.id = u"positive" pos_btn.index = 0 pos_btn.ui_flags = 0 neg_btn = Button() neg_btn.action = None neg_btn.caption = u"Cancel" neg_btn.id = u"negative" neg_btn.index = 1 neg_btn.ui_flags = 0 buttons = Buttons() buttons.add(pos_btn) buttons.add(neg_btn) ms = MemberStatus() ms.acked_timestamp = 0 ms.button_index = -1 ms.custom_reply = None ms.dismissed = False ms.form_result = None ms.index = 0 ms.received_timestamp = now() ms.status = MemberStatus.STATUS_RECEIVED ms.ack_device = None memberStatuses = MemberStatuses() memberStatuses.add(ms) m = FormMessage() m.sender = users.User(email=u"*****@*****.**") m.members = [users.User(email=u"*****@*****.**")] m.flags = 31 m.alert_flags = 2 m.branding = None m.message = msg m.buttons = buttons m.memberStatusses = memberStatuses m.creationTimestamp = now() m.generation = 1 m.tag = u"tag 123" m.timestamp = now() m.attachments = Attachments() return m
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()
def cancel_charge(customer_id, order_number, charge_id): """Cancels a charge so adjustments can be made to the order. Rolls back the next charge date of the subscription order. Args: customer_id: order_number: charge_id: Returns: None """ to_put = list() now_ = now() charge, order, customer = db.get([Charge.create_key(charge_id, order_number, customer_id), Order.create_key(customer_id, order_number), Customer.create_key(customer_id)]) charge.date_cancelled = now_ charge.status = Charge.STATUS_CANCELLED to_put.append(charge) order_items = list(OrderItem.list_by_order(order.key())) if order.is_subscription_order: months = 0 for item in order_items: product = item.product if product.is_subscription and product.price > 0: months += item.count if not product.is_subscription and product.extra_subscription_months > 0: months += product.extra_subscription_months if months > 0: next_charge_datetime = datetime.datetime.utcfromtimestamp(now()) - relativedelta(months=months) order.next_charge_date = get_epoch_from_datetime(next_charge_datetime) else: order.next_charge_date = Order.default_next_charge_date() else: extra_months = 0 for item in order_items: product = item.product if not product.is_subscription and product.extra_subscription_months > 0: extra_months += product.extra_subscription_months if extra_months > 0: sub_order = Order.get_by_order_number(customer_id, customer.subscription_order_number) next_charge_datetime = datetime.datetime.utcfromtimestamp(sub_order.next_charge_date) - relativedelta( months=extra_months) sub_order.next_charge_date = get_epoch_from_datetime(next_charge_datetime) to_put.append(sub_order) db.put(to_put)
def rate(key, window, limit): """ Rate limit implementation based on memcache. @param key Key which represents the functionality which needs rate limiting. @type str @param window Time in minutes to limit the number of executions @type int @param limit Maximum number of calls to be executed in window minutes """ cache_key = "rate_" + key items = memcache.get(cache_key) # @UndefinedVariable items = items if items else list() mepoch = now() / 60 items = [x for x in items if x[0] >= mepoch - window] # filter elapsed executions count = sum(x[1] for x in items) if count < limit: if len(items) > 0 and items[-1][0] == mepoch: items[-1] = (mepoch, items[-1][1] + 1) else: items.append((mepoch, 1)) memcache.set(cache_key, items, time=(window + 1) * 60) # @UndefinedVariable return True else: return False
def finalize(self): new_outstanding_kicks = {} for rpc_item, registration_id in self.outstandingKicks: if not rpc_item.get_result(): new_outstanding_kicks[registration_id] = OutStandingFirebaseKick( key_name=registration_id, timestamp=now()) if new_outstanding_kicks: rpc_items.append(db.put_async(new_outstanding_kicks.values()), None) del self.outstandingKicks[:] tasks = [] for tuple_ in self.items: if len(tuple_) == 4: rpc_item, start_time, registration_id, payload = tuple_ is_gcm = False else: rpc_item, start_time, registration_id, payload, is_gcm = tuple_ try: check_time = time.time() response = rpc_item.get_result() response_time = time.time() logging.info('Call to FCM. Elapsed: %sms, checked after %sms', int((response_time - start_time) * 1000), int((check_time - start_time) * 1000)) if response.status_code != 200: raise Exception(response.content) except: logging.warn('Failed to reach FCM , deferring ...', exc_info=True) tasks.append(create_task(retry_firebase_request, payload, is_gcm=is_gcm)) if tasks: schedule_tasks(tasks) del self.items[:]
def _populate(self, message): message.alert_flags = Message.ALERT_FLAG_VIBRATE message.childMessages = list() message.branding = None message.buttons = Buttons() message.creationTimestamp = now() message.dismiss_button_ui_flags = 0 message.flags = 31 message.generation = 1 message.originalFlags = message.flags message.senderMobile = None message.tag = None message.timeout = 0 message.timestamp = message.creationTimestamp message.memberStatusses = MemberStatuses() for i in xrange(len(message.members)): ms = MemberStatus() ms.status = 0 ms.received_timestamp = 0 ms.acked_timestamp = 0 ms.index = i ms.dismissed = False ms.button_index = -1 ms.custom_reply = None ms.form_result = None ms.ack_device = None message.memberStatusses.add(ms) message.attachments = Attachments()
def trans(): job = _get_job(job_key, phase) job.phase = next_phase if next_phase == MigrateServiceJob.PHASE_DONE: job.end_timestamp = now() job.put() return job
def _generate_vouchers(service_user, app_id, sln_qr_export_key): voucher_ancestor_key = SolutionCityVoucher.create_parent_key(app_id) now_ = now() voucher_to_put = list() for _ in xrange(100): voucher = SolutionCityVoucher(parent=voucher_ancestor_key) voucher.created = now_ voucher_to_put.append(voucher) db.put(voucher_to_put) voucher_ids = [] for voucher in voucher_to_put: voucher_key = voucher.key() voucher_ids.append(voucher_key.id()) def trans(): sln_qr_export = db.get(sln_qr_export_key) sln_qr_export.voucher_ids = voucher_ids sln_qr_export.put() deferred.defer(_generate_vouchers_qr_codes, service_user, app_id, sln_qr_export.key(), voucher_ids, _transactional=True) db.run_in_transaction(trans)
def _create_news_item(cls, num, sticky=False, news_type=NewsItem.TYPE_NORMAL, role_ids=None, tags=None, feed_names=None, use_media=False, app_ids=None, locations=None): def _get_file_contents(path): f = open(path, "rb") try: return f.read() finally: f.close() current_dir = os.path.dirname(__file__) image_path = os.path.join(current_dir, 'news_image.jpg') image = _get_file_contents(image_path) base_64_image = 'data:image/png,%s' % base64.b64encode(image) media = BaseMediaTO(type='image', content=base_64_image) if use_media else None title = u'test news title %d' % num message = u'test news message %d' % num broadcast_type = cls._get_broadcast_type(num) action_button = NewsActionButtonTO(id_=u'test_%d' % num, action=u'smi://nonexisting_%d' % num, caption=u'This button does nothing %d' % num) return news.publish(sticky=sticky, sticky_until=now() + DAY if sticky else 0, title=title, message=message, image=None if media else base_64_image , news_type=news_type, broadcast_type=broadcast_type, action_buttons=[action_button], qr_code_content=None, qr_code_caption=None, news_id=None, service_identity=None, app_ids=app_ids if app_ids else [App.APP_ID_ROGERTHAT], scheduled_at=0, flags=NewsItem.DEFAULT_FLAGS, role_ids=role_ids, tags=tags, feed_names=feed_names, media=media, locations=locations)
def _create_news_for_app_broadcasts(app_broadcast_key): app_broadcast = AppBroadcastStatistics.get(app_broadcast_key) to_put = [] for tag, message in zip(app_broadcast.tags, app_broadcast.messages): flow_stats = FlowStatistics.get( FlowStatistics.create_key(tag, app_broadcast.service_identity_user)) if not flow_stats: # should only happen on dev server logging.warn( 'Not creating news item for app broadcast with tag %s and message %s.', tag, message) return first_property_length = len(flow_stats.step_0_sent) timestamp = get_epoch_from_datetime( flow_stats.last_entry_datetime_date - relativedelta(days=first_property_length)) news_item = _create_news_item(message, flow_stats.service_identity, flow_stats.service_user, u'Nieuws') flow_stats_to = FlowStatisticsTO.from_model( flow_stats, FlowStatisticsTO.VIEW_STEPS, 99999, FlowStatisticsTO.GROUP_BY_YEAR) news_item.reach = 0 for step in flow_stats_to.steps: for year_stats in step.read_count: news_item.reach += year_stats.count news_item.timestamp = timestamp + DAY / 2 news_item.update_timestamp = now() to_put.append(news_item) if to_put: db.put(to_put)
def compile_source(source, name): user = users.get_current_user() m = new.module(str(name)) try: exec source in m.__dict__ except Exception: logging.warn("Compilation failed for [%s]" % name, exc_info=True) to = CodeTO() to.id = None to.timestamp = -1 to.author = None to.name = None to.source = None to.functions = [] to.version = -1 to.compile_error = unicode(traceback.format_exc()) return to functions = inspect.getmembers(m, lambda x: inspect.isfunction(x) and x.__module__ == m.__name__) code = Code().all().filter("name =", name).get() if not code: code = Code() code.author = user code.timestamp = now() code.name = name code.source = source code.functions = [unicode(f[0]) for f in functions] code.version = code.version + 1 if code.version else 1 code.put() to = CodeTO.fromDBCode(code) to.compile_error = None return to
def add_city_wide_lottery_info(city_app_id, winnings, date, x_winners): service_user = users.get_current_user() sln_settings = get_solution_settings(service_user) try: now_ = now() end_timestamp = date.toEpoch() if end_timestamp <= (now_ + 24 * 3600): raise BusinessException("end-date-24h-in-future") ll_info = SolutionCityWideLottery.load_pending(city_app_id) if ll_info: logging.warn("end_timestamp: %s", end_timestamp) logging.warn("ll_info.end_timestamp: %s", ll_info.end_timestamp) if end_timestamp <= ll_info.end_timestamp: raise BusinessException("lottery-time-bigger-first-upcoming") ll_info = SolutionCityWideLottery(parent=SolutionCityWideLottery.create_parent_key(city_app_id)) ll_info.timestamp = now_ ll_info.end_timestamp = end_timestamp ll_info.schedule_loot_time = ll_info.end_timestamp - 24 * 3600 ll_info.winnings = winnings ll_info.x_winners = x_winners ll_info.winners = [] ll_info.winner_info = [] ll_info.skip_winners = [] ll_info.deleted = False ll_info.pending = True ll_info.put() send_message(service_user, u"solutions.common.loyalty.lottery.update") return RETURNSTATUS_TO_SUCCESS except BusinessException, e: return ReturnStatusTO.create(False, common_translate(sln_settings.main_language, SOLUTION_COMMON, e.message))
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
def run(pos, skipped_users): if skipped_users is None: skipped_users = set() count = 0 while pos < len(to_schedule): user_email, stats = to_schedule[pos] pos += 1 max_timestamp = now() - LEAP_TIME if dry_run and not user_email.split(':', 1)[0].endswith('@mobicage.com'): skipped_users.add(user_email) continue elif (not any((s[3] < max_timestamp for s in stats[1]))): logging.info('Not sending out email reminder for %s because all the unread messages (%s) are sent ' 'after %s (LEAP TIME: %ss)', user_email, len(stats[1]), time.ctime(max_timestamp), LEAP_TIME) skipped_users.add(user_email) continue deferred.defer(send_email, user_email, stats, dry_run, _transactional=True) logging.info("Send email to %s with stats %s" % (user_email, stats)) count += 1 if count == 4: # 4, because max 5 defers in 1 transaction break if pos < len(to_schedule): deferred.defer(schedule_send_email, from_, to, pos, dry_run, skipped_users, _transactional=True) else: deferred.defer(send_overview_email, from_, to, dry_run, skipped_users, _transactional=True)
def analyze_status(): LOG_LIMIT = 30 # to prevent 'InvalidArgumentError: Too many request ids specified.' # Asynchronously fetch troubled services services_in_trouble = get_monitored_services_in_trouble_qry().run() # Asynchronously fetch rogerthat backend status rbe_rpc = db.get_async(RogerthatBackendErrors.get_key()) # Fetch queue statusses default, controller, worker, fast, broadcast = taskqueue.QueueStatistics.fetch( ["default", "highload-controller-queue", "highload-worker-queue", 'fast', 'broadcast-queue']) rbe = rbe_rpc.get_result() logs = logservice.fetch(request_ids=rbe.requestIds[:LOG_LIMIT]) if rbe else None total_error_count = len(rbe.requestIds) if rbe else 0 skipped_error_count = max(0, total_error_count - LOG_LIMIT) services = list(services_in_trouble) five_min_ago = (now() - 300) * 1000000 client_errors = ClientError.all().order("-timestamp").fetch(20) result = dict(queues=dict(default=default, controller=controller, worker=worker, fast=fast, broadcast=broadcast), rogerthatBackendStatus=logs, errorCount=total_error_count, skippedCount=skipped_error_count, services=services, five_min_ago=five_min_ago, client_errors=client_errors) return result
def edit_city_wide_lottery_info(key, winnings, date, x_winners): ll_info_edit = SolutionCityWideLottery.get(key) if not ll_info_edit: return RETURNSTATUS_TO_SUCCESS service_user = users.get_current_user() sln_settings = get_solution_settings(service_user) try: now_ = now() end_timestamp = date.toEpoch() if end_timestamp <= (now_ + 24 * 3600): raise BusinessException("end-date-24h-in-future") ll_info = SolutionCityWideLottery.load_pending(ll_info_edit.app_id) if ll_info_edit.key() != ll_info.key() and end_timestamp <= ll_info.end_timestamp: raise BusinessException("lottery-time-bigger-first-upcoming") ll_info_edit.end_timestamp = end_timestamp ll_info_edit.schedule_loot_time = ll_info_edit.end_timestamp - 24 * 3600 ll_info_edit.winnings = winnings ll_info_edit.x_winners = x_winners ll_info_edit.put() send_message(service_user, u"solutions.common.loyalty.lottery.update") return RETURNSTATUS_TO_SUCCESS except BusinessException, e: return ReturnStatusTO.create(False, common_translate(sln_settings.main_language, SOLUTION_COMMON, e.message))
def get(self): email = self.request.get("email", None) data = self.request.get("data", None) if not email or not data: return self.return_error() try: data, _ = self.parse_data(email, data) except: logging.exception("Could not decipher url!") return self.return_error() now_ = now() timestamp = data["t"] if not (now_ < timestamp < now_ + 5 * 24 * 3600): return self.return_error("The %s link has expired." % data["a"]) user = users.User(email) profile = get_service_or_user_profile(user) if profile and profile.lastUsedMgmtTimestamp + 5 * 24 * 3600 > timestamp: return self.return_error("You cannot use the %s link more than once." % data["a"]) path = os.path.join(_BASE_DIR, 'setpassword.html') self.response.out.write(template.render(path, { 'name': data['n'], 'hide_header': True, 'data': self.request.get("data"), 'email': email, 'action': data['a'] }))
def trans_create(): rogerthat_profile = get_service_or_user_profile(users.User(email)) if rogerthat_profile and isinstance(rogerthat_profile, ServiceProfile): from rogerthat.bizz.service import AppFailedToCreateUserProfileWithExistingServiceException raise AppFailedToCreateUserProfileWithExistingServiceException(email) user_profile = get_user_profile(app_user, cached=False) is_new_profile = False if not user_profile: deactivated_user_profile = get_deactivated_user_profile(app_user) if deactivated_user_profile: deferred.defer(reactivate_user_profile, deactivated_user_profile, app_user, _transactional=True) ActivationLog(timestamp=now(), email=app_user.email(), mobile=None, description="Reactivate user account by registering a paper loyalty card").put() else: is_new_profile = True avatar, image = _create_new_avatar(app_user, add_trial_overlay=False) user_profile = UserProfile(parent=parent_key(app_user), key_name=app_user.email()) user_profile.name = name user_profile.language = language user_profile.avatarId = avatar.key().id() user_profile.app_id = app_id _calculateAndSetAvatarHash(user_profile, image) pp = ProfilePointer(key=db.Key.from_path(ProfilePointer.kind(), user_code)) pp.user = app_user pp.short_url_id = short_url_id if is_new_profile: put_and_invalidate_cache(user_profile, pp, ProfilePointer.create(app_user)) else: pp.put()
def solution_event_guest_status(service_user, email, method, params, tag, service_identity, user_details): sln_settings = get_solution_settings(service_user) app_user = create_app_user_by_email(email, user_details[0].app_id) jsondata = json.loads(params) event_id = long(jsondata['eventId']) status = int(jsondata['status']) event = get_event_by_id(service_user, sln_settings.solution, event_id) r = SendApiCallCallbackResultTO() if event: eg_key = EventGuest.createKey(app_user, event.key()) eg = EventGuest.get(eg_key) if not eg: eg = EventGuest(key=eg_key) eg.guest = SolutionUser.fromTO(user_details[0]) eg.status = status eg.timestamp = now() eg.put() r.result = u"Succesfully update event status" if sln_settings.event_notifications_enabled and ( eg.status == EventGuest.STATUS_GOING or eg.chat_key): _send_event_notification(sln_settings, service_user, service_identity, user_details, event=event, event_guest=eg) else: r.result = u"Event not found. Update status failed." r.error = None return r
def translation_export(service_user): sync_service_translations(service_user) editable_translation_set = get_editable_translation_set(service_user) editable_translation_set.latest_export_timestamp = now() editable_translation_set.put() all_translations = get_all_translations(editable_translation_set) logging.info("exporting %s" % all_translations) translation_set_to = TranslationSetTO() translation_set_to.translations = list() translation_set_to.email = service_user.email() translation_set_to.export_id = editable_translation_set.latest_export_timestamp for type_, translation_dict in sorted(all_translations.iteritems()): if translation_dict: for key, values in sorted(translation_dict.iteritems()): translation_to = TranslationTO() translation_to.type = type_ translation_to.key = key translation_to.values = list() translation_set_to.translations.append(translation_to) if values: for language, value in sorted(values.iteritems()): translation_value_to = TranslationValueTO() translation_value_to.language = language translation_value_to.value = value translation_to.values.append(translation_value_to) return translation_set_to
def get(self): service_user = users.get_current_user() azzert(get_service_profile(service_user)) # must exist message_flow_designs = get_service_message_flow_designs(service_user) result = [] for wiring in message_flow_designs: if wiring.deleted or not wiring.definition: continue wiringDefinition = json.loads(wiring.definition) for k in wiringDefinition.iterkeys(): if k == u"modules": for m in wiringDefinition[k]: if m["value"]: m["config"]["formMessageDef"] = m["value"] result.append({"name": wiring.name, "language": wiring.language, "working": json.dumps(wiringDefinition)}) if not result: wiring_id = "Sample message flow" language = "MessageFlow" result.append({"name": wiring_id, "language": language, "working": SAMPLE_MF}) message_flow_design = MessageFlowDesign(parent=parent_key(service_user), key_name=wiring_id) message_flow_design.definition = SAMPLE_MF message_flow_design.name = wiring_id message_flow_design.language = language message_flow_design.design_timestamp = now() message_flow_design.status = MessageFlowDesign.STATUS_VALID message_flow_design.js_flow_definitions = JsFlowDefinitions() message_flow_design.put() self.response.out.write(json.dumps(result))
def post(self): user = users.get_current_user() app_id = self.request.get("app_id") if not app_id: app_id = self.request.POST.get("app_id_hidden", None) if not app_id: self.redirect('/admin/osa/launcher/apps') return uploaded_file = self.request.POST.get('package') # type: FieldStorage if not isinstance(uploaded_file, FieldStorage): self.redirect('/admin/osa/launcher/apps') return filename = '%s/oca/launcher/apps/%s.apk' % (ROGERTHAT_ATTACHMENTS_BUCKET, app_id) upload_to_gcs(uploaded_file.value, uploaded_file.type, filename) version_code = long(self.request.get("version_code")) app = OSALauncherApp.get_by_app_id(app_id) if not app: app = OSALauncherApp(key=OSALauncherApp.create_key(app_id)) app.user = user app.timestamp = now() app.app_id = app_id app.version_code = version_code app.package = None app.put() self.redirect('/admin/osa/launcher/apps')
def trans(): sln_cwl.pending = False sln_cwl.winners = winners sln_cwl.winners_info = json.dumps( serialize_complex_value(winners_info, ExtendedUserDetailsTO, True)) sln_cwl.put() deferred.defer(_redeem_city_wide_lottery_visits, service_user, sln_cwl_lottery_key, now(), _transactional=True) to_emails = sln_settings.inbox_mail_forwarders if to_emails: solution_server_settings = get_solution_server_settings() subject = 'Winnaars gemeentelijke tombola' body = """Beste, Volgende mensen hebben gewonnen met de tombola: %s Met vriendelijke groeten, Het Onze Stad App Team """ % winner_text send_mail(solution_server_settings.shop_export_email, to_emails, subject, body)
def _reschedule_post_to_social_media(ssb): countdown = ssb.timestamp - now() if countdown >= 0: logging.debug('Rescheduling of post to social media, was scheduled at: %d', countdown) deferred.defer(post_to_social_media_scheduled, ssb.key_str, _countdown=countdown, _queue=SCHEDULED_QUEUE, _transactional=db.is_in_transaction())
def trans_create(avatar, image, share_sid_key): azzert(not get_service_profile(service_user, cached=False)) azzert(not get_default_service_identity_not_cached(service_user)) profile = ServiceProfile(parent=parent_key(service_user), key_name=service_user.email()) profile.avatarId = avatar.key().id() _calculateAndSetAvatarHash(profile, image) service_identity_user = create_service_identity_user(service_user, ServiceIdentity.DEFAULT) service_identity = ServiceIdentity(key=ServiceIdentity.keyFromUser(service_identity_user)) service_identity.inheritanceFlags = 0 service_identity.name = name service_identity.description = "%s (%s)" % (name, service_user.email()) service_identity.shareSIDKey = share_sid_key service_identity.shareEnabled = False service_identity.creationTimestamp = now() service_identity.appIds = supported_app_ids update_result = update_func(profile, service_identity) if update_func else None put_and_invalidate_cache(profile, service_identity, ProfilePointer.create(service_user), ProfileHashIndex.create(service_user)) deferred.defer(create_default_qr_templates, service_user, _transactional=True) return profile, service_identity, update_result
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()
def test_flow_stats_tomorrow(self): stats = FlowStatistics(key=FlowStatistics.create_key(self.tag, self.service_identity_user)) _now = now() stats.set_today(datetime.datetime.utcfromtimestamp(_now - 86400).date()) breadcrumbs = list() current_step_id = 'step_1' status = FlowStatistics.STATUS_SENT btn_id = None stats.add(breadcrumbs, current_step_id, status, btn_id) self.assertListEqual([1], stats.step_0_sent) stats.set_today(datetime.datetime.utcfromtimestamp(_now).date()) self.assertListEqual([1, 0], stats.step_0_sent) stats.add(breadcrumbs, current_step_id, status, btn_id) self.assertListEqual([1, 1], stats.step_0_sent) stats.add(breadcrumbs, current_step_id, status, btn_id) self.assertListEqual([1, 2], stats.step_0_sent) statsTO = FlowStatisticsTO.from_model(stats, FlowStatisticsTO.VIEW_STEPS, days=2) stepTO = statsTO.get_step(current_step_id) self.assertEqual(1, stepTO.sent_count[0].count) # yesterday self.assertEqual(2, stepTO.sent_count[1].count) # today self.assertLess(datetime.date(year=stepTO.sent_count[0].year, month=stepTO.sent_count[0].month, day=stepTO.sent_count[0].day), datetime.date(year=stepTO.sent_count[1].year, month=stepTO.sent_count[1].month, day=stepTO.sent_count[1].day))
def trans(): to_put = list() service_profile = get_service_profile(service_user) service_profile.expiredAt = now() service_profile.enabled = False to_put.append(service_profile) service_identity_keys = get_service_identities_query(service_user, True) search_configs = db.get( [SearchConfig.create_key(create_service_identity_user(users.User(key.parent().name()), key.name())) for key in service_identity_keys]) svc_index = search.Index(name=SERVICE_INDEX) loc_index = search.Index(name=SERVICE_LOCATION_INDEX) for search_config in search_configs: if search_config: search_config.enabled = False to_put.append(search_config) on_trans_committed(_cleanup_search_index, search_config.service_identity_user.email(), svc_index, loc_index) for objects_to_put in chunks(to_put, 200): put_and_invalidate_cache(*objects_to_put) deferred.defer(cleanup_sessions, service_user, _transactional=True) deferred.defer(cleanup_friend_connections, service_user, _transactional=True)
def process(service_api_callback): try: svc_profile = get_service_profile(service_api_callback.service_user) if not svc_profile.enabled: service_api_callback.timestamp = 0 - service_api_callback.timestamp service_api_callback.put() return retry_call_back = True service_api_callback.retryCount += 1 service_api_callback.timestamp = now() + (SERVICE_API_CALLBACK_RETRY_UNIT * 2 ** service_api_callback.retryCount) if service_api_callback.retryCount > 2: logging.warn('Service api failure for %s with retrycount %s', service_api_callback.key(), service_api_callback.retryCount) monitoring.log_service_api_failure(service_api_callback, monitoring.SERVICE_API_CALLBACK) if service_api_callback.retryCount > 7: send_callback_delivery_warning(svc_profile, "callback timeout delivery") service_api_callback.put() if not retry_call_back: return success, message = submit_service_api_callback(svc_profile, service_api_callback) request = json.loads(service_api_callback.call) log_service_activity(svc_profile.user, request["id"], ServiceLog.TYPE_CALLBACK, ServiceLog.STATUS_WAITING_FOR_RESPONSE if success else ServiceLog.STATUS_ERROR, request["method"], service_api_callback.call, message) except Exception, e: logging.error("Could not process service api callback retention:\n%s" % e, exc_info=True)
def trans(): sln_loyalty_settings = db.get(sls_key) sln_loyalty_settings.modification_time = now() sln_loyalty_settings.put() sln_settings = get_solution_settings(sln_loyalty_settings.service_user) if not sln_settings.updates_pending: deferred.defer(_set_content_branding, sln_settings, _transactional=True)
def _send_order_confirmation(service_user, lang, message_flow_run_id, member, steps, end_id, end_message_flow_id, parent_message_key, tag, result_key, flush_id, flush_message_flow_id, service_identity, user_details, order_details): if now() - steps[-1].acknowledged_timestamp < 10: alert_flags = Message.ALERT_FLAG_SILENT else: alert_flags = Message.ALERT_FLAG_VIBRATE users.set_user(service_user) try: messaging.send( parent_key=parent_message_key, parent_message_key=parent_message_key, message=u'%s\n\n%s:\n%s' % (translate(lang, SOLUTION_COMMON, u'order_complete_will_notify'), translate(lang, SOLUTION_COMMON, u'usage-detail'), order_details), answers=list(), flags=Message.FLAG_ALLOW_DISMISS | Message.FLAG_AUTO_LOCK, members=[ MemberTO.from_user(user_details[0].toAppUser(), alert_flags) ], branding=get_solution_main_branding(service_user).branding_key, tag=None, service_identity=service_identity, alert_flags=alert_flags, step_id=u'order_complete') finally: users.clear_user()
def set_service_disabled(customer_or_id, disabled_reason_int): """ Disables the customer his service, disconnects all users and sets the service invisible. Args: customer_or_id (int, long, Customer): customer or id disabled_reason_int (int, long): reason why the service has been disabled Raises: NoSubscriptionException BusinessException """ if isinstance(customer_or_id, Customer): customer = customer_or_id else: customer = Customer.get_by_id(customer_or_id) if not customer.service_email: raise NoSubscriptionException(customer) if disabled_reason_int not in Customer.DISABLED_REASONS: raise BusinessException('Invalid disable service reason') service_user = users.User(customer.service_email) sln_settings = get_solution_settings(service_user) customer.default_app_id = None customer.app_ids = [] customer.service_disabled_at = now() customer.disabled_reason_int = disabled_reason_int customer.subscription_cancel_pending_date = 0 sln_settings.search_enabled = False sln_settings.service_disabled = True db.put([customer, sln_settings]) rogerthat_set_service_disabled(service_user)
def _create_branding(hash_, zip_content, description, service_user, branding_type, meta_properties, pokes): if pokes and not service_user: raise BrandingValidationException('Cannot create branding with one or more poke tags without a service user') filename = get_branding_cloudstorage_path(hash_, service_user) with cloudstorage.open(filename, 'w') as f: f.write(zip_content) blobstore_filename = '/gs' + filename blobstore_key = blobstore.create_gs_key(blobstore_filename) b = Branding(key_name=hash_) b.blob_key = blobstore_key.decode('utf-8') b.description = description b.timestamp = now() b.user = service_user b.pokes = list() b.type = branding_type color_scheme = meta_properties.get('color-scheme') or Branding.DEFAULT_COLOR_SCHEME b.menu_item_color = meta_properties.get('menu-item-color') or Branding.DEFAULT_MENU_ITEM_COLORS[color_scheme] b.content_type = meta_properties.get('content-type') or Branding.CONTENT_TYPE_HTML b.orientation = meta_properties.get('orientation') or Branding.DEFAULT_ORIENTATION puts = [b] for poke_hash, unicode_tag in pokes: ptm = PokeTagMap(key_name=poke_hash, parent=parent_key(service_user)) ptm.tag = unicode_tag b.pokes.append(ptm.key()) puts.append(ptm) db.put(puts) return b
def trans(mobile): debug_request = StartDebuggingRequest(key=StartDebuggingRequest.create_key(app_user, mobile_id), timestamp=now()) db.put_async(debug_request) start_log_forwarding(app_user, target_jid, mobile=mobile) deferred.defer(stop_debugging, app_user, mobile_id, debug_request=debug_request, _countdown=30 * 60, _transactional=True, _queue=SCHEDULED_QUEUE)
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_)
def news_statistics(request, log_entry, slog_entry, counters): """counters = {caches_to_clear: {app_id_1, app_id_2...} }, 'news_to_update': { '465489498': { 'rogerthat': { 'stats_reached': 35, 'stats_gender': [0, 3, 5], # other, male, female 'stats_time': [42, 32, 12, 14, 2, 0], # per hour 'stats_age': [0, 0, 0, 1, 0, 5, 5], # 21 long, first elem = unknown age, 2nd = 0-5, 3rd: 5-10, ... } } } } }""" user_email = slog_entry['e'] if 'e' in slog_entry else None # Should only be None for NEWS_SPONSORING_TIMED_OUT params = slog_entry['a'] action = params['action'] _now = now() if action in (NEWS_REACHED, NEWS_ROGERED, NEWS_NEW_FOLLOWER, NEWS_ACTION): app_id = params['app_id'] user_profile = get_user_profile(users.User(user_email)) if not user_profile: return age_index = NewsItemStatistics.get_age_index(user_profile.age) gender_index = NewsItemStatistics.get_gender_index(user_profile.gender) for news_id in params['news_ids']: if app_id not in counters['news_to_update'][news_id]: counters['news_to_update'][news_id][app_id] = {} news_updates = counters['news_to_update'][news_id][app_id] if action == NEWS_REACHED: news_updates['reached'] = news_updates.get('reached', 0) + 1 elif action == NEWS_ROGERED: users_that_rogered = news_updates.get('rogered', set()) users_that_rogered.add(users.User(user_email)) news_updates['rogered'] = users_that_rogered elif action == NEWS_NEW_FOLLOWER: news_updates['follow_count'] = news_updates.get('follow_count', 0) + 1 elif action == NEWS_ACTION: news_updates['action_count'] = news_updates.get('action_count', 0) + 1 stats_key = 'stats_%s' % action if stats_key not in news_updates: news_updates[stats_key] = defaultdict(lambda: dict) news_updates[stats_key]['gender'] = news_updates[stats_key].get('gender', NewsItemStatistics.default_gender_stats()) news_updates[stats_key]['gender'][gender_index] += 1 news_updates[stats_key]['time'] = news_updates[stats_key].get('time', {}) news_updates[stats_key]['time'][_now] = news_updates[stats_key]['time'].get(_now, 0) + 1 news_updates[stats_key]['age'] = news_updates[stats_key].get('age', NewsItemStatistics.default_age_stats()) news_updates[stats_key]['age'][age_index] += 1 elif action == NEWS_CREATED: # add a new dict if news_id not yet in news_to_update (defaultdict) counters['news_to_update'][params['news_id']] elif action == NEWS_SPONSORING_TIMED_OUT: counters['news_to_update'][params['news_id']]['sticky_timed_out'] = True elif action == NEWS_UPDATED: counters['caches_to_clear']['app_ids'].update(params['app_ids'])
def send_new_job_notification(app_user, job_id): job_criteria = JobMatchingCriteria.create_key(app_user).get() if job_criteria.notifications and \ job_criteria.notifications.how_often in (JobMatchingCriteriaNotifications.AT_MOST_ONCE_A_DAY, JobMatchingCriteriaNotifications.AT_MOST_ONCE_A_WEEK,): @ndb.transactional() def trans(): job_notifications_key = JobMatchingNotifications.create_key(app_user) job_notifications = job_notifications_key.get() if not job_notifications: job_notifications = JobMatchingNotifications(key=job_notifications_key, schedule_time=0) if job_id not in job_notifications.job_ids: job_notifications.job_ids.append(job_id) job_notifications.put() trans() elif job_criteria.notifications and \ job_criteria.notifications.how_often == JobMatchingCriteriaNotifications.AS_IT_HAPPENS: user_profile = get_user_profile(app_user) job_offer = JobOffer.get_by_id(job_id) timestamp = now() interested = False job_offer_to = JobOfferTO.fromJobOffer(job_offer, timestamp, interested, user_profile.language) _send_new_job_notification(app_user, 1, job_offer_to)
def post(self): try: user = users.get_current_user() tb = TempBlob(parent=parent_key(user)) tb.timeout = now() + 24 * 60 * 60 image = self.request.get("newAvatar") image_type = imghdr.what(None, image) try: img = images.Image(image) img.horizontal_flip() img.horizontal_flip() orig_width = img.width orig_height = img.height orig_image = image size = min(100, 100 * 4000 / max(orig_width, orig_height)) # max 4000 wide/high while len(image) > (1024 * 1024 - 100 * 1024): size -= size / 10 img = images.Image(orig_image) img.resize(orig_width * size / 100, orig_height * size / 100) image = img.execute_transforms(images.JPEG if image_type == 'jpeg' else images.PNG) tb.blob = db.Blob(image) except images.NotImageError: logging.info("Sending failure to the logged on users (%s) channel" % user.email()) self.response.out.write(broadcast_via_iframe_result(u"rogerthat.profile.avatar_upload_failed", error=u"The uploaded file is not an image!")) return except IOError, e: logging.info("Sending failure to the logged on users (%s) channel" % user.email()) self.response.out.write(broadcast_via_iframe_result(u"rogerthat.profile.avatar_upload_failed", error=e.message)) return except BadRequestError, e: logging.info("Sending failure to the logged on users (%s) channel" % user.email()) self.response.out.write(broadcast_via_iframe_result(u"rogerthat.profile.avatar_upload_failed", error=e.message)) return
def post(self): install_id = self.request.get("install_id", None) step = self.request.get("step", None) azzert(step is not None, "step is a required argument") msg = LogRegistrationStepHandler.STEPS.get(step, "Unknown step: %s" % step) installation = Installation.get_by_key_name(install_id) if install_id else None InstallationLog(parent=installation, timestamp=now(), description=msg).put()
def send_message_for_pharmacy_order(service_user, order_key, order_status, message): from solutions.common.bizz.messaging import send_inbox_forwarders_message azzert(order_status in SolutionPharmacyOrder.ORDER_STATUSES) def txn(): m = SolutionPharmacyOrder.get(order_key) azzert(service_user == m.service_user) if m.status == SolutionPharmacyOrder.STATUS_RECEIVED and m.status != order_status: m.status = order_status m.put() return m order = db.run_in_transaction(txn) sm_data = [] sm_data.append({u"type": u"solutions.common.pharmacy_orders.update"}) sln_settings = get_solution_settings(service_user) if message: if order.solution_inbox_message_key: sim_parent, _ = add_solution_inbox_message( service_user, order.solution_inbox_message_key, True, None, now(), message, mark_as_unread=False, mark_as_read=True) send_inbox_forwarders_message(service_user, sim_parent.service_identity, None, message, { 'if_name': sim_parent.sender.name, 'if_email': sim_parent.sender.email }, message_key=sim_parent.solution_inbox_message_key, reply_enabled=sim_parent.reply_enabled) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, order.service_identity) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(sim_parent, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) else: sln_main_branding = get_solution_main_branding(service_user) branding = sln_main_branding.branding_key if sln_main_branding else None member = MemberTO() member.alert_flags = Message.ALERT_FLAG_VIBRATE member.member = order.sender.email member.app_id = order.sender.app_id messaging.send(parent_key=None, parent_message_key=None, message=message, answers=[], flags=Message.FLAG_ALLOW_DISMISS, members=[member], branding=branding, tag=None, service_identity=order.service_identity) elif order.solution_inbox_message_key: sim_parent = SolutionInboxMessage.get(order.solution_inbox_message_key) if not sim_parent.read: sim_parent.read = True sim_parent.put() deferred.defer(update_user_data_admins, service_user, order.service_identity) sln_i_settings = get_solution_settings_or_identity_settings(sln_settings, order.service_identity) sm_data.append({u"type": u"solutions.common.messaging.update", u"message": serialize_complex_value(SolutionInboxMessageTO.fromModel(sim_parent, sln_settings, sln_i_settings, True), SolutionInboxMessageTO, False)}) send_message(service_user, sm_data, service_identity=order.service_identity)
def get(self): yesterday = (now() - (24 * 60 * 60)) qry = db.GqlQuery( "SELECT __key__ FROM Event WHERE last_start_date < :epoch") qry.bind(epoch=yesterday) cleanup_size = delete_all(qry) logging.info("Cleanup %s timedout CleanupSolutionEvents" % cleanup_size)
def get_by_service_user(cls, service_user, service_identity): service_identity_user = create_service_identity_user_wo_default( service_user, service_identity) return cls.all().ancestor( parent_key_unsafe(service_identity_user, SOLUTION_COMMON)).filter( "processed =", False).filter("timestamp >", now() - (60 * 60 * 24)).order("timestamp")
def generate_news_from_broadcasts(): # Run this whenever you please (only once, though) _now = now() run_job(_get_solution_scheduled_broadcast_keys, [_now], _create_news_for_scheduled_broadcast, [_now], worker_queue=MIGRATION_QUEUE) run_job(_get_app_broadcast_statistics, [], _create_news_for_app_broadcasts, [], worker_queue=MIGRATION_QUEUE)
def _reschedule_broadcast_message(ssb): service_user = ssb.service_user sln_settings = get_solution_settings(service_user) countdown = (ssb.broadcast_epoch + timezone_offset(sln_settings.timezone)) - now() if countdown >= 0: logging.debug('Rescheduling of broadcast message, was scheduled at: %d', countdown) deferred.defer(_send_scheduled_broadcast, service_user, ssb.key_str, _countdown=countdown, _queue=SCHEDULED_QUEUE, _transactional=db.is_in_transaction())
def _worker(branding_settings): if not branding_settings.show_identity_name: branding_settings.modification_time = now() branding_settings.put() service_user = branding_settings.service_user with users.set_user(service_user): get_and_store_main_branding(service_user) system.publish_changes()
def trans(): ancestor_key = SolutionCityVoucher.create_parent_key( jsondata["app_id"]) sln_city_voucher = SolutionCityVoucher.get_by_id( jsondata["voucher_id"], ancestor_key) if not sln_city_voucher: raise Exception(u"sln_city_voucher was None") if sln_city_voucher.activated: raise Exception(u"sln_city_voucher was already activated") value = jsondata["value"] if not isinstance(value, int) and isinstance(value, long): raise Exception(u"Value is not of expected type (int, long)") if value <= 0: raise Exception(u"Value needs to be bigger then 0") history = SolutionCityVoucherTransaction(parent=sln_city_voucher) history.created = now() history.action = SolutionCityVoucherTransaction.ACTION_ACTIVATED history.value = jsondata["value"] history.service_user = service_user history.service_identity = service_identity history.put() sln_city_voucher.activated = True sln_city_voucher.activation_date = now() sln_city_voucher.value = jsondata["value"] sln_city_voucher.internal_account = jsondata["internal_account"] sln_city_voucher.cost_center = jsondata["cost_center"] sln_city_voucher.username = jsondata["username"] sln_city_voucher.redeemed_value = 0 sln_city_voucher.expiration_date = get_expiration_date_from_today( jsondata['app_id']) app_user_details = jsondata.get('app_user_details') if app_user_details: sln_city_voucher.owner = users.User( email=app_user_details['email']) sln_city_voucher.owner_name = app_user_details['name'] sln_city_voucher.put() deferred.defer(re_index_voucher, sln_city_voucher.key(), _transactional=True)
def trans(): sln_qr_export = SolutionCityVoucherQRCodeExport(parent=ancestor_key) sln_qr_export.created = now() sln_qr_export.ready = False sln_qr_export.put() deferred.defer(_generate_vouchers, service_user, app_id, sln_qr_export.key(), _transactional=True)
def test_recurrent_billing(self): self.set_datastore_hr_probability(1) products_to_order = [(u'MSUP', 12), (Product.PRODUCT_BEACON, 1)] subscription_order, customer = self._create_customer_and_subscription_order(products_to_order) self._create_service(customer) # Turn back next_charge_date more than 12 months subscription_order.next_charge_date -= 367 * 86400 subscription_order.put() # execute recurrent billing code _create_charge(subscription_order.key(), now(), Product.get_products_dict()) # check if a ShopTask was created task = ShopTask.all().get() self.assertEqual(task.type, ShopTask.TYPE_SUPPORT_NEEDED) task.delete() # Check if an expiredsubscription was created expired_subscription = ExpiredSubscription.get_by_customer_id(customer.id) self.assertIsNotNone(expired_subscription) # first set the expired subscription as 'customer will link his credit card' set_expired_subscription_status(customer.id, ExpiredSubscription.STATUS_WILL_LINK_CREDIT_CARD) task = ShopTask.all().get() self.assertIsNotNone(task) self.assertEqual(task.type, ShopTask.TYPE_CHECK_CREDIT_CARD) task.delete() # extend the customer's (expired) subscription charge = set_expired_subscription_status(customer.id, ExpiredSubscription.STATUS_EXTEND_SUBSCRIPTION) subscription_order, \ expired_subscription = db.get((Order.create_key(customer.id, customer.subscription_order_number), ExpiredSubscription.create_key(customer.id))) self.assertEqual(expired_subscription, None) product_subscription = Product.get_by_code(u'MSUP') charge_total = product_subscription.price * 12 # subscription extensions should always be 12 months long self.assertIsNotNone(charge) self.assertEqual(Charge.TYPE_SUBSCRIPTION_EXTENSION, charge.type) self.assertEqual(charge_total, charge.amount) one_year_from_now_plus_one_day = datetime.datetime.utcfromtimestamp(now()) + relativedelta.relativedelta( months=12, days=1) one_year_from_now_minus_one_day = datetime.datetime.utcfromtimestamp(now()) + relativedelta.relativedelta( months=12, days=-1) self.assertLess(subscription_order.next_charge_date, int(one_year_from_now_plus_one_day.strftime('%s'))) self.assertGreater(subscription_order.next_charge_date, int(one_year_from_now_minus_one_day.strftime('%s')))
def get_first_event_date(self): if len(self.start_dates) > 1: first_start_date = self.start_dates[-1] now_ = now() for start_date in self.start_dates: if start_date > now_: first_start_date = start_date break return first_start_date else: return self.start_dates[0]
def _get_default_branding_settings(service_user): return SolutionBrandingSettings( key=SolutionBrandingSettings.create_key(service_user), color_scheme='light', background_color=SolutionBrandingSettings. DEFAULT_LIGHT_BACKGROUND_COLOR, text_color=SolutionBrandingSettings.DEFAULT_LIGHT_TEXT_COLOR, menu_item_color=OUR_CITY_APP_COLOUR, show_identity_name=True, show_avatar=True, modification_time=now())
def _create_news_for_scheduled_broadcast(broadcast_key, _now): broadcast = SolutionScheduledBroadcast.get(broadcast_key) if broadcast.broadcast_epoch < _now and not broadcast.target_audience_enabled: news_item = _create_news_item(broadcast.message, broadcast.service_identity, broadcast.service_user, broadcast.broadcast_type, json.loads(broadcast.json_urls)) news_item.timestamp = broadcast.timestamp news_item.update_timestamp = now() news_item.put()