def test_active(self): """Active announcement shows.""" AnnouncementFactory( show_after=datetime.now() - timedelta(days=2), show_until=datetime.now() + timedelta(days=2), ) eq_(1, Announcement.get_site_wide().count())
def create_for_locale(request): """An ajax view to create a new announcement for the current locale.""" user = request.user locale = Locale.objects.get(locale=request.LANGUAGE_CODE) if not user_can_announce(user, locale): return HttpResponseForbidden() form = AnnouncementForm(request.POST) if form.is_valid(): a = Announcement(creator=user, locale=locale, **form.cleaned_data) a.save() return HttpResponse(json.dumps({"id": a.id}), content_type="application/json") else: return HttpResponse(json.dumps(form.errors), status=400, content_type="application/json")
def render_readouts(request, readouts, template, locale=None, extra_data=None, product=None): """Render a readouts, possibly with overview page. Use the given template, pass the template the given readouts, limit the considered data to the given locale, and pass along anything in the `extra_data` dict to the template in addition to the standard data. """ current_locale = locale or request.LANGUAGE_CODE on_default_locale = request.LANGUAGE_CODE == settings.WIKI_DEFAULT_LANGUAGE default_kwargs = { 'locale': settings.WIKI_DEFAULT_LANGUAGE, } locale_kwargs = { 'locale': request.LANGUAGE_CODE, } ready_kwargs = {} if product is not None: default_kwargs['product'] = product.slug locale_kwargs['product'] = product.slug ready_kwargs['product'] = product.slug data = {'readouts': SortedDict((slug, class_(request, locale=locale, product=product)) for slug, class_ in readouts.iteritems() if class_.should_show_to(request)), 'default_locale': settings.WIKI_DEFAULT_LANGUAGE, 'default_locale_name': LOCALES[settings.WIKI_DEFAULT_LANGUAGE].native, 'current_locale': current_locale, 'current_locale_name': LOCALES[current_locale].native, 'request_locale_name': LOCALES[request.LANGUAGE_CODE].native, 'is_watching_default_approved': ApproveRevisionInLocaleEvent.is_notifying( request.user, **default_kwargs), 'is_watching_other_approved': None if on_default_locale else ApproveRevisionInLocaleEvent.is_notifying( request.user, **locale_kwargs), 'is_watching_default_locale': ReviewableRevisionInLocaleEvent.is_notifying( request.user, **default_kwargs), 'is_watching_other_locale': None if on_default_locale else ReviewableRevisionInLocaleEvent.is_notifying( request.user, **locale_kwargs), 'is_watching_default_ready': ReadyRevisionEvent.is_notifying(request.user, **ready_kwargs), 'on_default_locale': on_default_locale, 'announce_form': AnnouncementForm(), 'announcements': Announcement.get_for_locale_name(current_locale), 'product': product, 'products': Product.objects.filter(visible=True), } if extra_data: data.update(extra_data) return render(request, 'dashboards/' + template, data)
def create_for_locale(request): """An ajax view to create a new announcement for the current locale.""" user = request.user locale = Locale.objects.get(locale=request.LANGUAGE_CODE) if not user_can_announce(user, locale): return HttpResponseForbidden() form = AnnouncementForm(request.POST) if form.is_valid(): a = Announcement(creator=user, locale=locale, **form.cleaned_data) a.save() return HttpResponse(json.dumps({'id': a.id}), content_type="application/json") else: return HttpResponse(json.dumps(form.errors), status=400, content_type="application/json")
def test_get_for_group_id(self): """If no groups are passed, nothing is returned.""" # Site-wide announcement announcement().save() # Announcement in a group. a = announcement(group=self.group, save=True) group_ann = Announcement.get_for_group_id(self.group.id) eq_(1, len(group_ann)) eq_(a, group_ann[0])
def test_get_for_group_id(self): """If no groups are passed, nothing is returned.""" # Site-wide announcement AnnouncementFactory() # Announcement in a group. a = AnnouncementFactory(group=self.group) group_ann = Announcement.get_for_group_id(self.group.id) eq_(1, len(group_ann)) eq_(a, group_ann[0])
def test_always_visible(self): """Always visible announcements are shown.""" # This one doesn't show announcement(show_after=datetime.now() + timedelta(days=2)).save() announcement(show_after=datetime.now() - timedelta(days=2), content='stardate 43125').save() site_wide = Announcement.get_site_wide() eq_(1, site_wide.count()) eq_('stardate 43125', site_wide[0].content)
def test_get_for_locale_name(self): """Announcements for a specific locale are shown.""" # Site-wide announcement AnnouncementFactory() # Announcement in a locale a = AnnouncementFactory(locale=self.locale) locale_ann = Announcement.get_for_locale_name(self.locale.locale) eq_(1, locale_ann.count()) eq_(a, locale_ann[0])
def test_get_for_locale_name(self): """Announcements for a specific locale are shown.""" # Site-wide announcement announcement(save=True) # Announcement in a locale a = announcement(locale=self.locale, save=True) locale_ann = Announcement.get_for_locale_name(self.locale.locale) eq_(1, locale_ann.count()) eq_(a, locale_ann[0])
def test_always_visible(self): """Always visible announcements are shown.""" # This one doesn't show AnnouncementFactory(show_after=datetime.now() + timedelta(days=2)) AnnouncementFactory(show_after=datetime.now() - timedelta(days=2), content='stardate 43125') site_wide = Announcement.get_site_wide() eq_(1, site_wide.count()) eq_('stardate 43125', site_wide[0].content)
def render_readouts(request, readouts, template, locale=None, extra_data=None, product=None): """Render a readouts, possibly with overview page. Use the given template, pass the template the given readouts, limit the considered data to the given locale, and pass along anything in the `extra_data` dict to the template in addition to the standard data. """ current_locale = locale or request.LANGUAGE_CODE on_default_locale = request.LANGUAGE_CODE == settings.WIKI_DEFAULT_LANGUAGE default_kwargs = { 'locale': settings.WIKI_DEFAULT_LANGUAGE, } locale_kwargs = { 'locale': request.LANGUAGE_CODE, } ready_kwargs = {} if product is not None: default_kwargs['product'] = product.slug locale_kwargs['product'] = product.slug ready_kwargs['product'] = product.slug data = { 'readouts': OrderedDict((slug, class_(request, locale=locale, product=product)) for slug, class_ in readouts.items() if class_.should_show_to(request)), 'default_locale': settings.WIKI_DEFAULT_LANGUAGE, 'default_locale_name': LOCALES[settings.WIKI_DEFAULT_LANGUAGE].native, 'current_locale': current_locale, 'current_locale_name': LOCALES[current_locale].native, 'request_locale_name': LOCALES[request.LANGUAGE_CODE].native, 'is_watching_default_approved': ApproveRevisionInLocaleEvent.is_notifying(request.user, **default_kwargs), 'is_watching_other_approved': ( None if on_default_locale else ApproveRevisionInLocaleEvent.is_notifying(request.user, **locale_kwargs)), 'is_watching_default_locale': ( ReviewableRevisionInLocaleEvent.is_notifying(request.user, **default_kwargs)), 'is_watching_other_locale': ( None if on_default_locale else ReviewableRevisionInLocaleEvent.is_notifying(request.user, **locale_kwargs)), 'is_watching_default_ready': ReadyRevisionEvent.is_notifying(request.user, **ready_kwargs), 'on_default_locale': on_default_locale, 'announce_form': AnnouncementForm(), 'announcements': Announcement.get_for_locale_name(current_locale), 'product': product, 'products': Product.objects.filter(visible=True), } if extra_data: data.update(extra_data) return render(request, 'dashboards/' + template, data)
def announcement(visible_dates=True, **kwargs): # Pass in visible_dates=False to hide the announcement. if visible_dates: defaults = {'show_after': datetime.now() - timedelta(days=2)} else: defaults = { 'show_after': datetime.now() - timedelta(days=4), 'show_until': datetime.now() - timedelta(days=2) } defaults['content'] = ("*crackles* Captain's log, stardate 43124.5 " "We are doomed.") if 'creator' not in kwargs: defaults['creator'] = user(save=True) defaults.update(kwargs) return Announcement(**defaults)
def get_announcements(): return Announcement.get_site_wide()
def test_group_excluded(self): """Announcements in a group are not shown.""" AnnouncementFactory(group=self.group) eq_(0, Announcement.get_site_wide().count())
def test_group_excluded(self): """Announcements in a group are not shown.""" announcement(group=self.group).save() eq_(0, Announcement.get_site_wide().count())
def test_active(self): """Active announcement shows.""" announcement(show_after=datetime.now() - timedelta(days=2), show_until=datetime.now() + timedelta(days=2)).save() eq_(1, Announcement.get_site_wide().count())