def test_get_absolute_url_honors_script_prefix(self): pf = FlatPage(title="Tea!", url="/tea/") set_script_prefix("/beverages/") try: self.assertEqual(pf.get_absolute_url(), "/beverages/tea/") finally: clear_script_prefix()
def test_get_absolute_url_honors_script_prefix(self): pf = FlatPage(title="Tea!", url='/tea/') set_script_prefix('/beverages/') try: self.assertEqual(pf.get_absolute_url(), '/beverages/tea/') finally: clear_script_prefix()
def handle(self, *args, **options): # commands need explicit activation of translations translation.activate(settings.LANGUAGE_CODE) # this causes url handling to force absolute urls url = "https://%s/" % Site.objects.get_current().domain set_script_prefix(url) try: if args: now = datetime.datetime.strptime(args[0], '%d.%m.%Y') else: # yesterday now = datetime.date.today() - datetime.timedelta(1) subject = 'Kiberpipa, weekly report: %d. %d. %d' % (now.day, now.month, now.year) days_range = 7 events = Event.objects.all() # 1. events that are newer or equal may pass # 2. events that are older or equal may pass events = events.filter(start_date__gte=(now - datetime.timedelta(days=days_range))).filter(start_date__lte=now) all_visitors = 0 for e in events: all_visitors += e.visitors # is public and no visitors no_visitors = events.filter(public__exact=True).filter(visitors__exact=0) # is videoed and no attached video no_video = events.filter(require_video__exact=True).filter(video__isnull=True) # is pictured and no flicker id no_pictures = events.filter(require_photo__exact=True).filter(flickr_set_id__exact=None) if events.count() == 0: print "no events to send" return unfinished_events = (no_visitors, no_video, no_pictures) html = get_template('mail/events_report.html').render(Context({ 'days_range': days_range, 'all_visitors': all_visitors, 'events': events, 'unfinished_events': unfinished_events })) text = get_template('mail/events_report.txt').render(Context({ 'days_range': days_range, 'all_visitors': all_visitors, 'events': events, 'unfinished_events': unfinished_events })) email = EmailMultiAlternatives(subject, text, settings.DEFAULT_FROM_EMAIL, ['*****@*****.**']) email.attach_alternative(html, 'text/html') email.send() print "events email sent" finally: # set_script_prefix is global for current thread clear_script_prefix()
def test_build_with_force_script_name(): NEW_STATIC_ROOT = os.path.join(settings.BASE_DIR, 'test_collectstatic', 'urlwriter', 'build_with_force_script_name') rmtree(path=NEW_STATIC_ROOT, ignore_errors=True) with override_settings(FORCE_SCRIPT_NAME='test.php', BASE_DIR=NEW_STATIC_ROOT): reader = URLReader(urls=( reverse('content_a'), reverse('content_b'), )) read_results = tuple(reader()) writer = URLWriter(data=read_results) storage = writer.storage output = writer() files_saved = [] for built in output: files_saved.append(built.storage_result) sorted_files_saved = sorted(files_saved) assert sorted_files_saved == [ 'content/a/b/index.html', 'content/a/index.html' ] try: assert storage.open( sorted_files_saved[-2]).readlines() == [b'content_b'] assert storage.open( sorted_files_saved[-1]).readlines() == [b'content_a'] finally: # failure to do this will bleed the value of FORCE_SCRIPT_NAME # into other tests ... clear_script_prefix()
def app_reverse(viewname, urlconf=None, args=None, kwargs=None, *vargs, **vkwargs): """ Reverse URLs from application contents Works almost like Django's own reverse() method except that it resolves URLs from application contents. The second argument, ``urlconf``, has to correspond to the URLconf parameter passed in the ``APPLICATIONS`` list to ``Page.create_content_type``:: app_reverse('mymodel-detail', 'myapp.urls', args=...) or app_reverse('mymodel-detail', 'myapp.urls', kwargs=...) The second argument may also be a request object if you want to reverse an URL belonging to the current application content. """ # First parameter might be a request instead of an urlconf path, so # we'll try to be helpful and extract the current urlconf from it extra_context = getattr(urlconf, '_feincms_extra_context', {}) appconfig = extra_context.get('app_config', {}) urlconf = appconfig.get('urlconf_path', urlconf) appcontent_class = ApplicationWidget._feincms_content_models[0] cache_key = appcontent_class.app_reverse_cache_key(urlconf) url_prefix = cache.get(cache_key) if url_prefix is None: clear_script_prefix() content = appcontent_class.closest_match(urlconf) if content is not None: if urlconf in appcontent_class.ALL_APPS_CONFIG: # We have an overridden URLconf app_config = appcontent_class.ALL_APPS_CONFIG[urlconf] urlconf = app_config['config'].get('urls', urlconf) prefix = content.parent.get_absolute_url() prefix += '/' if prefix[-1] != '/' else '' url_prefix = (urlconf, prefix) cache.set(cache_key, url_prefix, timeout=APP_REVERSE_CACHE_TIMEOUT) if url_prefix: # vargs and vkwargs are used to send through additional parameters # which are uninteresting to us (such as current_app) prefix = get_script_prefix() try: set_script_prefix(url_prefix[1]) return reverse( viewname, url_prefix[0], args=args, kwargs=kwargs, *vargs, **vkwargs) finally: set_script_prefix(prefix) raise NoReverseMatch("Unable to find ApplicationContent for %r" % urlconf)
def handle(self, *args, **options): self.verbosity = int(options.get('verbosity')) if args: interested_datetime = datetime.datetime.strptime(args[0], '%d.%m.%Y') else: # yesterday interested_datetime = datetime.date.today() - datetime.timedelta(1) subject = 'Kiberpipa, dnevno porocilo: %d. %d. %d' % (interested_datetime.day, interested_datetime.month, interested_datetime.year) diaries = Diary.objects.filter(pub_date__year=interested_datetime.year, pub_date__month=interested_datetime.month, pub_date__day=interested_datetime.day) try: scratchpad = Scratchpad.objects.all()[0].content except Scratchpad.DoesNotExist: pass lends = Lend.objects.filter(returned=False) # warnings for events: # today and tomorrow events = Event.objects.get_date_events( datetime.datetime(interested_datetime.year, interested_datetime.month, interested_datetime.day, 0, 0) + relativedelta(days=1), datetime.datetime(interested_datetime.year, interested_datetime.month, interested_datetime.day, 0, 0) + relativedelta(days=3), ) # no technician no_tech = events.filter(require_technician__exact=True).filter(technician__isnull=True) # no officers on duty no_responsible = events.filter(require_officers_on_duty__exact=True).filter(officers_on_duty__isnull=True) if diaries or no_tech or no_responsible: pass else: if self.verbosity >= 1: print "nothing to send" return # this causes url handling to force absolute urls url = "https://%s/" % Site.objects.get_current().domain set_script_prefix(url) try: text = get_template('mail/diary_report.txt').render(Context(locals())) html = get_template('mail/diary_report.html').render(Context(locals())) email = EmailMultiAlternatives(subject, text, settings.DEFAULT_FROM_EMAIL, ['*****@*****.**']) email.attach_alternative(html, 'text/html') email.send() if self.verbosity >= 1: print "email sent" finally: # set_script_prefix is global for current thread clear_script_prefix()
def app_reverse(viewname, urlconf=None, args=None, kwargs=None, *vargs, **vkwargs): """ Reverse URLs from application contents Works almost like Django's own reverse() method except that it resolves URLs from application contents. The second argument, ``urlconf``, has to correspond to the URLconf parameter passed in the ``APPLICATIONS`` list to ``Page.create_content_type``:: app_reverse('mymodel-detail', 'myapp.urls', args=...) or app_reverse('mymodel-detail', 'myapp.urls', kwargs=...) The second argument may also be a request object if you want to reverse an URL belonging to the current application content. """ # First parameter might be a request instead of an urlconf path, so # we'll try to be helpful and extract the current urlconf from it extra_context = getattr(urlconf, '_feincms_extra_context', {}) appconfig = extra_context.get('app_config', {}) urlconf = appconfig.get('urlconf_path', urlconf) appcontent_class = ApplicationWidget._feincms_content_models[0] cache_key = appcontent_class.app_reverse_cache_key(urlconf) url_prefix = cache.get(cache_key) if url_prefix is None: clear_script_prefix() content = appcontent_class.closest_match(urlconf) if content is not None: if urlconf in appcontent_class.ALL_APPS_CONFIG: # We have an overridden URLconf app_config = appcontent_class.ALL_APPS_CONFIG[urlconf] urlconf = app_config['config'].get('urls', urlconf) prefix = content.parent.get_absolute_url() prefix += '/' if prefix[-1] != '/' else '' url_prefix = (urlconf, prefix) cache.set(cache_key, url_prefix, timeout=APP_REVERSE_CACHE_TIMEOUT) if url_prefix: # vargs and vkwargs are used to send through additional parameters # which are uninteresting to us (such as current_app) prefix = get_script_prefix() try: set_script_prefix(url_prefix[1]) return reverse(viewname, url_prefix[0], args=args, kwargs=kwargs, *vargs, **vkwargs) finally: set_script_prefix(prefix) raise NoReverseMatch("Unable to find ApplicationContent for %r" % urlconf)