def topnav(context): node = context.get('instance') request = context.get('request') language = get_active_language(request) nav = [] ## Find top/secondlevel published nodes in one query XXX for toplevel in queries.toplevel_visible_children(language=language): ## make sure /foo/bar does not match in /football by adding the / item = dict(active=False, node=toplevel) content = toplevel.content(language=language) item['url'] = toplevel.get_absolute_url(language=language) item['title'] = content.title if content else "" if toplevel == node or \ (node and node.path.startswith(toplevel.path + '/')): item['active'] = True sub = [] for secondlevel in queries.get_visible_children(toplevel, language=language): slitem = dict(node=secondlevel) content = secondlevel.content(language=language) slitem['url'] = secondlevel.get_absolute_url(language=language) slitem['title'] = content.title if content else "" sub.append(slitem) item['sub'] = sub nav.append(item) return dict(toplevel=nav)
def languages(request): if len(settings.LANGUAGES) <= 1: return None current_language = get_active_language() current_label = dict(settings.LANGUAGES).get(current_language, current_language) res = [] ld = getattr(settings, 'LANGUAGE_DOMAINS', {}) for lang, label in settings.CONTENT_LANGUAGES: if lang == current_language: is_current = True else: is_current = False url = Node.root().get_absolute_url(language=lang) has_translation = False domain = ld.get(lang) if domain: protocol = "https" if request.is_secure() else "http" url = "%s://%s%s" % (protocol, domain, url) res.append(dict(id=lang, label=label, url=url, has_translation=has_translation, is_current=is_current)) return dict(languages=dict(current=dict(id=current_language, label=current_label), languages=res))
def contentlisting_context(handler, request, node): language = get_active_language() q = node_proxy_factory(Node, language).objects.children(node).filter(contentbase__language=language).order_by("position") if not access.has_access(request.user, node): q = q.public() return dict(contents=q)
def show_translate(self): if self.status == 'special': ## special page return False ## do not show when creating or updating if self.status in ("update", "create"): return False active_language = get_active_language(self.request) ## there's an instance, it has (primary) content but not in the ## active language if self.instance and self.instance.primary_content() and not self.instance.content(language=active_language): return True return False
def blog_context(handler, request, node): """ provide context both in Page as in Blog context """ ## XXX This context provider will not work for non-valve (derived) blogs ## due to the hardcoded ValveEntry.classname dependency ctx = {} ctx['body_class'] = "wheelcms_valve" try: p = max(1, int(request.GET.get('page', 1))) except (ValueError, TypeError): p = 1 kw = {} if not handler.hasaccess(): kw['contentbase__state'] = "published" language = get_active_language() kw['contentbase__language'] = language category_slug = request.GET.get('category', '') if category_slug: ## XXX Untested category = handler.instance.child(category_slug, language=language) kw['contentbase__categories'] = category try: ownerid = int(request.GET.get('ownerid', '')) kw['contentbase__owner__id'] = ownerid except ValueError: pass ## this will actually ignore the blog publication state! XXX ctx['paginator'] = paginator = SectionedPaginator( node_proxy_factory(Node, language) .objects.offspring(node) .filter(contentbase__meta_type=ValveEntry.classname, **kw) .order_by("-contentbase__created"), 4) b, m, e = paginator.sections(p, windowsize=6) ctx['begin'] = b ctx['middle'] = m ctx['end'] = e try: ctx['page'] = paginator.page(p) except InvalidPage: ctx['page'] = paginator.page(1) return ctx
def resolve(cls, nodepath): """ resolve a node path to an actual node in correct language context. """ ## Do a bit of path normalization: Except for root, start with /, ## remove trailing / ## Old 'coerce' used to # locale.activate_content_language(None) ## Is that necessary for anything? if nodepath in ("/", ""): nodepath = "" else: nodepath = "/{0}".format(nodepath.strip('/')) language = get_active_language() return Node.get(nodepath, language=language)
def translations(self): """ Return data for the translations/languages menu. This means "switch to" options for translated languages and "translate to" for untranslated languages. If there's no second language (ignoring 'Any'), don't return anything; this will hide the translation menu entirely. """ if not self.instance or self.status == Toolbar.SPECIAL: return None if len(settings.CONTENT_LANGUAGES) == 1: return None active = None translated = [] untranslated = [] active_language = get_active_language() for (lang, langtitle) in translate.languages(): option = dict(id=lang, language=langtitle) content = self.instance.content(language=lang) ## In view mode toch edit tonen om vertaling te maken! base_url = "switch_admin_language?path=" + self.instance.tree_path + "&switchto=" + lang if lang == active_language: active = option else: if self.status == Toolbar.UPDATE: option["action_url"] = base_url + "&rest=edit" elif self.status == Toolbar.VIEW: option["action_url"] = base_url + "" elif self.status == Toolbar.LIST: option["action_url"] = base_url + "&rest=list" elif self.status == Toolbar.CREATE: option["action_url"] = ( base_url + "&rest=" + urllib2.quote("create?type=" + self.request.GET.get("type")) ) if content and self.status != Toolbar.CREATE: translated.append(option) else: untranslated.append(option) return dict(active=active, translated=translated, untranslated=untranslated)
def navigation_items(request, node): language = get_active_language() nav = [] ## remove visible_children? Unnecessary with permission checks ## Find top/secondlevel published nodes in one query XXX for toplevel in queries.toplevel_visible_children(language=language): ## make sure /foo/bar does not match in /football by adding the / item = dict(active=False, node=toplevel) content = toplevel.content(language=language) if content: spoke = content.spoke() perm = spoke.permissions.get('view') if not auth.has_access(request, spoke, spoke, perm): continue item['url'] = toplevel.get_absolute_url(language=language) item['title'] = content.title if content else "" if toplevel == node or \ (node and node.path.startswith(toplevel.path + '/')): item['active'] = True sub = [] for secondlevel in queries.get_visible_children(toplevel, language=language): slitem = dict(node=secondlevel) content = secondlevel.content(language=language) spoke = content.spoke() perm = spoke.permissions.get('view') if not auth.has_access(request, spoke, spoke, perm): continue slitem['url'] = secondlevel.get_absolute_url(language=language) slitem['title'] = content.title if content else "" sub.append(slitem) item['sub'] = sub nav.append(item) return dict(toplevel=nav)
def show_translate(self): """ The translate button is shown on pages that are not available in the current (admin) language. E.g. a page is written in english but the admin language is set to Spanish, you will get a 'translate' button in stead of an 'edit' button (with the exact same action, though!) """ if self.status == Toolbar.SPECIAL: ## special page return False ## do not show when creating or updating if self.status in (Toolbar.UPDATE, Toolbar.CREATE): return False active_language = get_active_language() ## there's an instance, it has (primary) content but not in the ## active language if self.instance and self.instance.primary_content() and not self.instance.content(language=active_language): return True return False
def get(self, request): page = int(request.REQUEST.get('page', 1)) sqs = SearchQuerySet() language = utils.get_active_language() sqs = sqs.filter(language__in=(language, "any")) if not self.hasaccess(): sqs = sqs.filter(state__in=("visible", "published")) form = self.context['form'] = SearchForm(self.request.REQUEST, searchqueryset=sqs) if page < 1: page = 1 if form.is_valid(): self.context['query'] = form.cleaned_data['q'] self.context['paginator'] = paginator = \ SectionedPaginator(form.search(), self.results_per_page) try: self.context['page'] = paginator.page(page) except InvalidPage: return self.notfound() self.context['GET_string'] = urllib.urlencode([(k, v) for (k, v) in self.request.REQUEST.iteritems() if k != 'page']) b, m, e = paginator.sections(page, windowsize=4) self.context['begin'] = b self.context['middle'] = m self.context['end'] = e ## move to wheecms_axle folder? return self.template("search/search.html")
def translations(self): if not self.instance or self.status == "special": return None active = None translated = [] untranslated = [] active_language = get_active_language(self.request) for (lang, langtitle) in translate.languages(): option = dict(id=lang, language=langtitle) content = self.instance.content(language=lang) ## In view mode toch edit tonen om vertaling te maken! base_url = "switch_admin_language?path=" + self.instance.tree_path + "&language=" + lang if lang == active_language: active = option else: if self.status == "update": option['action_url'] = base_url + '&rest=edit' elif self.status == "view": option['action_url'] = base_url + '' elif self.status == "list": option['action_url'] = base_url + '&rest=list' elif self.status == "create": option['action_url'] = base_url + '&rest=' + urllib2.quote('create?type=' + self.request.GET.get('type')) if content and self.status != 'create': translated.append(option) else: untranslated.append(option) return dict(active=active, translated=translated, untranslated=untranslated)
def get_language(): return get_active_language()
def active_language(self): return get_active_language()
def get(self, request, nodepath=None, handlerpath="", action="", **kw): """ instance - the path to a piece of content path - remaining, specifies operation to be invoked. To be deprecated in favor of +actins """ self.toolbar = get_toolbar() # locale.activate_content_language(None) self.instance = self.resolve(nodepath) ## an action may end in slash: remove it if action: action = action.rstrip('/') language = get_active_language() if self.toolbar: self.toolbar.instance = self.instance self.toolbar.status = Toolbar.VIEW if self.instance is None: return self.notfound() ## make sure the instance is part of the context self.context['instance'] = self.instance ## pre_handler is to be deprecated. It also depends on self.instance self.pre_handler() spoke = self.spoke(language=language) ## retrieve content type info if spoke: ## update the context with addtional data from the spoke self.context.update(spoke.context(self, self.request, self.instance)) perm = spoke.permissions.get('view') else: perm = Spoke.permissions.get('view') ## Set default current tab self.context['tab_action'] = 'attributes' try: if handlerpath: handler = gethandler(self, handlerpath) if handler: return handler() return self.notfound() if action: action_handler = action_registry.get(action, self.instance.path, spoke) if action_handler is None: return self.notfound() ## Should the (decorator for) the action handler do permission checks? required_permission = getattr(action_handler, 'permission', perm) if required_permission is None: # default to spoke permission required_permission = perm if not auth.has_access(self.request, spoke, spoke, required_permission): return self.forbidden() ## if there's an action, assume it's actually an update action. if self.toolbar: self.toolbar.status = Toolbar.UPDATE ## Update context if it's a tab action if tabaction(action_handler): self.context['tab_action'] = tabaction(action_handler) return action_handler(self, request, action) if not handlerpath: ## special case: post to content means edit/update if self.is_post: return self.edit() return self.view() return self.notfound() finally: # XXX Does this work as expected? self.post_handler()
def handle_comment_post(handler, request, action): """ Action to create a comment on any Spoke """ form = CommentForm(request.POST) if not form.is_valid(): ## since we're redirecting, use sessions to temporarily store ## the comment data request.session['comment_post'] = request.POST return handler.redirect(handler.instance.get_absolute_url(), error=ugettext("Please fix your errors"), hash="commentform") name = request.POST.get('name') body = request.POST.get('body') captcha = request.POST.get('body') ## if someone tries really really hard... id = "%s-%s" % (timezone.now().strftime("%Y%m%d%H%M%S"), random.randint(0,1000)) title = _("Comment by %(owner)s on %(date)s") % \ dict(owner=name, date=timezone.now()) n = handler.instance.add(id) lang = get_active_language() c = Comment(title=title, name=name, body=body, node=n, state="pending", language=lang).save() if 'posted_comments' not in request.session: request.session['posted_comments'] = [] request.session['posted_comments'].append(c.node.path) request.session.modified = True baseconf = BaseConfiguration.config() try: notify = baseconf.comments.get().notify_address.strip() except Configuration.DoesNotExist: notify = "" sender = baseconf.sendermail.strip() if notify and sender: content = handler.instance.content() domain = Site.objects.get_current().domain content_url = "http://%s%s" % (domain, content.get_absolute_url()) comment_url = "http://%s%s" % (domain, n.get_absolute_url()) send_mail('New comment on "%s"' % content.title, """A new comment has been posted on "%(title)s" %(content_url)s Name: %(name)s Content: %(body)s View/edit/delete comment: %(comment_url)sedit#collapseadvanced""" % dict(title=content.title, content_url=content_url, comment_url=comment_url, name=name, body=body), sender, [notify], fail_silently=True) ## log details (ip, etc) in description, or add extra fields ## send notification, if enabled ## optionally 'recognize' the user and show the comment only to him/her ## allow approval/rejection of comment through view? return handler.redirect(handler.instance.get_absolute_url(), info="Your comment is being processed")