def get_solution_loyalty_settings_or_identity_settings(sln_l_settings, service_identity): if is_default_service_identity(service_identity): return sln_l_settings else: return SolutionLoyaltyIdentitySettings.get_by_user( sln_l_settings.service_user, service_identity)
def get(self): service_user = users.get_current_user() session_ = users.get_current_session() service_identity = session_.service_identity slide_id = self.request.get('i') if not slide_id: self.redirect("/ourcityapp") return slide_id = long(slide_id) if is_default_service_identity(service_identity): service_identity_user = service_user else: service_identity_user = create_service_identity_user(service_user, service_identity) def trans(): slide = SolutionLoyaltySlide.get_by_id(slide_id, parent=parent_key_unsafe(service_identity_user, SOLUTION_COMMON)) return slide slide = db.run_in_transaction(trans) if not slide: self.redirect("/ourcityapp") return server_settings = get_server_settings() jinja_template = JINJA_ENVIRONMENT.get_template('loyalty_preview.html') self.response.out.write(jinja_template.render({'slide_id': slide_id, 'full_url': slide.slide_url(), 'overlay_url': '%s/common/loyalty/slide/overlay' % (server_settings.baseUrl)}))
def is_in_holiday(service_user, service_identity, date): if is_default_service_identity(service_identity): sln_i_settings = get_solution_settings(service_user) else: sln_i_settings = get_solution_identity_settings( service_user, service_identity) return sln_i_settings.is_in_holiday_for_date(date)
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
def rest_get_news_promoted_count(app_ids): service_user = users.get_current_user() session_ = users.get_current_session() service_identity = session_.service_identity if is_default_service_identity(service_identity): service_identity_user = create_service_identity_user(service_user) else: service_identity_user = create_service_identity_user( service_user, service_identity) return get_sponsored_news_count(service_identity_user, app_ids)
def _set_content_branding(sln_settings): users.set_user(sln_settings.service_user) try: sln_main_branding = get_solution_main_branding(sln_settings.service_user) populate_identity(sln_settings, sln_main_branding.branding_key) identities = [None] if sln_settings.identities: identities.extend(sln_settings.identities) for service_identity in identities: if is_default_service_identity(service_identity): service_identity_user = create_service_identity_user(sln_settings.service_user) else: service_identity_user = create_service_identity_user(sln_settings.service_user, service_identity) deferred.defer(_update_tablets, service_identity_user, None, _queue=HIGH_LOAD_CONTROLLER_QUEUE) finally: users.clear_user()
def get(self, page): service_user = users.get_current_user() session_ = users.get_current_session() service_identity = session_.service_identity _, extension = os.path.splitext(page.lower()) if extension in (".jpg", ".jpeg"): self.response.headers['Content-Type'] = "image/jpeg" elif extension == ".png": self.response.headers['Content-Type'] = "image/png" elif extension == ".css": self.response.headers['Content-Type'] = "text/css" main_branding = get_solution_main_branding(service_user) with closing(ZipFile(StringIO(main_branding.blob))) as zip_file: try: content = zip_file.read(page) except KeyError: if page == 'avatar.jpg': self.abort(404) else: raise if page == 'branding.html': doc = html.fromstring(content) sln_i_settings = None for elem in doc.xpath("//nuntiuz_identity_name"): if not sln_i_settings: if is_default_service_identity(service_identity): sln_i_settings = get_solution_settings(service_user) else: sln_i_settings = get_solution_identity_settings( service_user, service_identity) parent = elem.getparent() elem.drop_tree() parent.text = sln_i_settings.name content = etree.tostring(doc) # @UndefinedVariable self.response.out.write(content)
def save_settings(service_user, service_identity, name, description=None, opening_hours=None, address=None, phone_number=None, facebook_page=None, facebook_name=None, facebook_action=None, currency=None, search_enabled=True, search_keywords=None, timezone=None, events_visible=None, email_address=None, inbox_email_reminders=None, iban=None, bic=None, search_enabled_check=False): address_geocoded = True sln_settings = get_solution_settings(service_user) sln_i_settings = get_solution_settings_or_identity_settings( sln_settings, service_identity) sln_i_settings.name = name sln_i_settings.description = description sln_i_settings.opening_hours = opening_hours sln_i_settings.phone_number = phone_number if email_address and email_address != service_user.email(): sln_i_settings.qualified_identifier = email_address else: sln_i_settings.qualified_identifier = None if facebook_page is not None: if sln_settings.facebook_page != facebook_page: send_message(service_user, u"solutions.common.settings.facebookPageChanged", facebook_page=facebook_page) sln_settings.facebook_page = facebook_page sln_settings.facebook_name = facebook_name if facebook_page else "" sln_settings.facebook_action = facebook_action if currency is not None: sln_settings.currency = currency sln_settings.search_enabled = search_enabled sln_settings.search_enabled_check = search_enabled_check sln_i_settings.search_keywords = search_keywords if address and (sln_i_settings.address != address or not sln_i_settings.location): sln_i_settings.address = address try: lat, lon = _get_location(address) sln_i_settings.location = db.GeoPt(lat, lon) except: address_geocoded = False logging.warning("Failed to resolve address: %s" % sln_i_settings.address, exc_info=1) if timezone is not None: if sln_settings.timezone != timezone: send_message(service_user, u"solutions.common.settings.timezoneChanged") sln_settings.timezone = timezone if events_visible is not None: sln_settings.events_visible = events_visible if inbox_email_reminders is not None: sln_i_settings.inbox_email_reminders_enabled = inbox_email_reminders sln_settings.iban = iban sln_settings.bic = bic sln_settings.updates_pending = True if not is_default_service_identity(service_identity): sln_i_settings.put() sln_settings.put() broadcast_updates_pending(sln_settings) return address_geocoded
def get_broadcast_statistics_excel(service_user, service_identity): """ Generates an excel file containing the broadcast statistics. Format: Date | Sent to | Received | Read | Acknowledged | Message Args: service_user(users.User): Service user Returns: Excel file """ sln_settings = get_solution_settings(service_user) lang = sln_settings.main_language def transl(key): return translate(lang, SOLUTION_COMMON, key) if is_default_service_identity(service_identity): service_identity_user = create_service_identity_user(service_user, ServiceIdentity.DEFAULT) else: service_identity_user = create_service_identity_user(service_user, service_identity) bc_stats = BroadcastStatistic.get_all_by_service_identity_user(service_identity_user).order('timestamp') column_timestamp = 0 column_sent = 1 column_received = 2 column_read = 3 # column_acknowledged = 4 column_message = 4 bold_style = xlwt.XFStyle() bold_style.font.bold = True book = xlwt.Workbook(encoding="utf-8") messages_sheet = book.add_sheet(transl('broadcast_statistics')[0:31]) messages_sheet.write(0, column_timestamp, transl('Date'), bold_style) messages_sheet.write(0, column_sent, transl('sent_to'), bold_style) messages_sheet.write(0, column_received, transl('received'), bold_style) messages_sheet.write(0, column_read, transl('Read'), bold_style) # messages_sheet.write(0, column_acknowledged, transl('acknowledged'), bold_style) messages_sheet.write(0, column_message, transl('inbox-message'), bold_style) messages_sheet.col(column_timestamp).width = 6000 messages_sheet.col(column_sent).width = 5000 messages_sheet.col(column_received).width = 5000 messages_sheet.col(column_read).width = 5000 # messages_sheet.col(column_acknowledged).width = 5000 messages_sheet.col(column_message).width = 20000 for i, statistic in enumerate(bc_stats): i += 1 if statistic.timestamp: datetime_string = format_datetime(datetime.datetime.utcfromtimestamp(statistic.timestamp), locale=lang, tzinfo=sln_settings.tz_info) else: datetime_string = '' messages_sheet.write(i, column_timestamp, datetime_string) messages_sheet.write(i, column_message, statistic.message or '') messages_sheet.write(i, column_sent, statistic.sent or 0) messages_sheet.write(i, column_received, statistic.received or 0) messages_sheet.write(i, column_read, statistic.read or 0) # messages_sheet.write(i, column_acknowledged, statistic.acknowledged or 0) excel_file = StringIO() book.save(excel_file) return excel_file.getvalue()
def rest_put_news_item( title, message, broadcast_type, image, sponsored=False, action_button=None, order_items=None, type=MISSING, qr_code_caption=MISSING, app_ids=MISSING, # @ReservedAssignment scheduled_at=MISSING, news_id=None, broadcast_on_facebook=False, broadcast_on_twitter=False, facebook_access_token=None, target_audience=None, role_ids=None): """ Args: title (unicode) message (unicode) broadcast_type (unicode) sponsored (bool) image (unicode) action_button (NewsButtonTO) order_items (list of OrderItemTO) type (int) qr_code_caption (unicode) app_ids (list of unicode) scheduled_at (long) news_id (long): id of the news to update. When not specified a new item is created broadcast_on_facebook (bool) broadcast_on_twitter (bool) facebook_access_token (unicode): user or page access token target_audience (NewsTargetAudienceTO) role_ids (list of long) """ service_user = users.get_current_user() session_ = users.get_current_session() service_identity = session_.service_identity if is_default_service_identity(service_identity): service_identity_user = create_service_identity_user(service_user) else: service_identity_user = create_service_identity_user( service_user, service_identity) try: host = get_current_http_host() return put_news_item(service_identity_user, title, message, broadcast_type, sponsored, image, action_button, order_items, type, qr_code_caption, app_ids, scheduled_at, news_id, broadcast_on_facebook, broadcast_on_twitter, facebook_access_token, target_audience=target_audience, role_ids=role_ids, host=host, accept_missing=True) except BusinessException as e: sln_settings = get_solution_settings(service_user) try: message = common_translate(sln_settings.main_language, SOLUTION_COMMON, e.message) except ValueError: message = e.message return ReturnStatusTO.create(False, message)
def get_solution_settings_or_identity_settings(sln_settings, service_identity): if is_default_service_identity(service_identity): return sln_settings else: return get_solution_identity_settings(sln_settings.service_user, service_identity)
def _7000_migrate_solution(job_key): phase = MigrateServiceJob.PHASE_7000_MIGRATE_SOLUTION next_phase = MigrateServiceJob.PHASE_8000_FINISH_MODELS # Validate that the job still exists job = _get_job(job_key, phase) # Do the work _log_progress(job) if job.solution: logging.info('0/ Migrate the solution models without ancestor, but with service_user as key name.') def trans0(): new_models = list() old_models = list() for model_class in (SolutionMainBranding, SolutionLogo, SolutionAvatar): old_model = db.get(model_class.create_key(job.from_service_user)) if old_model: kwargs = copy_model_properties(old_model) new_model = model_class(key=model_class.create_key(job.to_service_user), **kwargs) new_models.append(new_model) old_models.append(old_model) if new_models: put_and_invalidate_cache(*new_models) if old_models: db.delete(old_models) xg_on = db.create_transaction_options(xg=True) db.run_in_transaction_options(xg_on, trans0) old_sln_settings = get_solution_settings(job.from_service_user) identities = [None] if old_sln_settings.identities: identities.extend(old_sln_settings.identities) logging.info('1/ Migrate RestaurantReservations') for service_identity in identities: from_si_user = create_service_identity_user_wo_default(job.from_service_user, service_identity) to_si_user = create_service_identity_user_wo_default(job.to_service_user, service_identity) while True: reservations = RestaurantReservation.all().filter('service_user', from_si_user).fetch(300) if not reservations: break for r in reservations: r.service_user = to_si_user _put_and_invalidate_cache_and_allocate_ids(*reservations) logging.info('2/ Migrate EventReminders') for service_identity in identities: from_si_user = create_service_identity_user(job.from_service_user) to_si_user = create_service_identity_user(job.to_service_user) while True: reminders = EventReminder.all().filter('service_identity_user', from_si_user).fetch(300) if not reminders: break for r in reminders: r.service_identity_user = to_si_user _put_and_invalidate_cache_and_allocate_ids(*reminders) logging.info('3/ Migrate RestaurantProfile.reserve_flow_part2') restaurant_profile = db.get(RestaurantProfile.create_key(job.from_service_user)) if restaurant_profile and restaurant_profile.reserve_flow_part2: old_part2_key = db.Key(restaurant_profile.reserve_flow_part2) new_part2_key = db.Key.from_path(old_part2_key.kind(), old_part2_key.id_or_name(), parent=parent_key(job.to_service_user)) logging.debug('New key: %r', new_part2_key) restaurant_profile.reserve_flow_part2 = str(new_part2_key) _put_and_invalidate_cache_and_allocate_ids(restaurant_profile) logging.info('4/ Delete all SolutionQRs. They will be recreated when provisioning.') for service_identity in identities: service_identity_user = create_service_identity_user_wo_default(job.from_service_user, service_identity) db.delete(SolutionQR.all(keys_only=True).ancestor(parent_key_unsafe(service_identity_user, job.solution))) logging.info('5/ Delete Loyalty QR. It will be recreated when provisioning.') if SolutionModule.LOYALTY in old_sln_settings.modules: for service_identity in identities: if is_default_service_identity(service_identity): loyalty_i_settings = db.get(SolutionLoyaltySettings.create_key(job.from_service_user)) else: loyalty_i_settings = db.get( SolutionLoyaltyIdentitySettings.create_key(job.from_service_user, service_identity)) if loyalty_i_settings: loyalty_i_settings.image_uri = None loyalty_i_settings.content_uri = None _put_and_invalidate_cache_and_allocate_ids(loyalty_i_settings) logging.info('6/ Change ancestor keys of solution models.') for solution in ['common', job.solution]: for service_identity in identities: if is_default_service_identity(service_identity): _migrate_ancestor_models(job, parent_key(job.from_service_user, solution)) else: service_identity_user = create_service_identity_user_wo_default( job.from_service_user, service_identity) _migrate_ancestor_models(job, parent_key_unsafe(service_identity_user, solution)) sln_settings = get_solution_settings(job.to_service_user) sln_main_branding = get_solution_main_branding(job.to_service_user) users.set_user(job.to_service_user) try: populate_identity(sln_settings, sln_main_branding.branding_key) finally: users.clear_user() # Set the next phase _set_job_in_next_phase(job_key, phase, next_phase)