def change_view(self, request, object_id, extra_context=None): """The ``change`` admin view for the :class:`Page <pages.models.Page>`.""" language = get_language_from_request(request) extra_context = { 'language': language, # don't see where it's used #'lang': current_lang, 'page_languages': settings.PAGE_LANGUAGES, } try: int(object_id) except ValueError: raise Http404('The "%s" part of the location is invalid.' % str(object_id)) try: obj = self.model.objects.get(pk=object_id) except self.model.DoesNotExist: # Don't raise Http404 just yet, because we haven't checked # permissions yet. We don't want an unauthenticated user to be able # to determine whether a given object exists. obj = None else: template = get_template_from_request(request, obj) extra_context['placeholders'] = get_placeholders(template) extra_context['traduction_languages'] = [l for l in settings.PAGE_LANGUAGES if Content.objects.get_content(obj, l[0], "title") and l[0] != language] extra_context['page'] = obj return super(PageAdmin, self).change_view(request, object_id, extra_context=extra_context)
def list_pages(self, request, template_name=None, extra_context=None): """List root pages""" if not self.admin_site.has_permission(request): return self.admin_site.login(request) language = get_language_from_request(request) query = request.POST.get('q', '').strip() if query: page_ids = list(set([c.page.pk for c in Content.objects.filter(body__icontains=query)])) pages = Page.objects.filter(pk__in=page_ids) else: pages = Page.objects.root() if settings.PAGE_HIDE_SITES: pages = pages.filter(sites=settings.SITE_ID) context = { 'can_publish': request.user.has_perm('pages.can_publish'), 'language': language, 'name': _("page"), 'pages': pages, 'opts': self.model._meta, 'q': query } context.update(extra_context or {}) change_list = self.changelist_view(request, context) return change_list
def list_pages(self, request, template_name=None, extra_context=None): """List root pages""" if not self.admin_site.has_permission(request): return self.admin_site.login(request) language = get_language_from_request(request) query = request.POST.get('q', '').strip() if query: page_ids = list( set([ c.page.pk for c in Content.objects.filter(body__icontains=query) ])) pages = Page.objects.filter(pk__in=page_ids) else: pages = Page.objects.root() if settings.PAGE_HIDE_SITES: pages = pages.filter(sites=settings.SITE_ID) context = { 'can_publish': request.user.has_perm('pages.can_publish'), 'language': language, 'name': _("page"), 'pages': pages, 'opts': self.model._meta, 'q': query } context.update(extra_context or {}) change_list = self.changelist_view(request, context) return change_list
def change_view(self, request, object_id, extra_context=None): """The ``change`` admin view for the :class:`Page <pages.models.Page>`.""" language = get_language_from_request(request) extra_context = { 'language': language, # don't see where it's used #'lang': current_lang, 'page_languages': settings.PAGE_LANGUAGES, } try: int(object_id) except ValueError: raise Http404('The "%s" part of the location is invalid.' % str(object_id)) try: obj = self.model.objects.get(pk=object_id) except self.model.DoesNotExist: # Don't raise Http404 just yet, because we haven't checked # permissions yet. We don't want an unauthenticated user to be able # to determine whether a given object exists. obj = None else: template = get_template_from_request(request, obj) extra_context['placeholders'] = get_placeholders(template) extra_context['traduction_languages'] = [ l for l in settings.PAGE_LANGUAGES if Content.objects.get_content(obj, l[0], "title") and l[0] != language ] extra_context['page'] = obj return super(PageAdmin, self).change_view(request, object_id, extra_context=extra_context)
def test_get_language_from_request(self): """ Test that get_language_from_request return the default language even if a unaccepted language is used. """ class Req(): LANGUAGE_CODE = 'en-us' GET = {} request = Req() self.assertEqual(get_language_from_request(request), 'en-us') request.LANGUAGE_CODE = 'dont' self.assertEqual(get_language_from_request(request), 'en-us') request.LANGUAGE_CODE = 'fr-ch' self.assertEqual(get_language_from_request(request), 'fr-ch')
def add_view(self, request, form_url='', extra_context=None): """The ``add`` admin view for the :class:`Page <pages.models.Page>`.""" extra_context = { 'language': get_language_from_request(request), 'page_languages': settings.PAGE_LANGUAGES, } return super(PageAdmin, self).add_view(request, form_url, extra_context)
def test_get_language_from_request(self): """ Test that get_language_from_request return the default language even if a unaccepted language is used. """ class Req(): LANGUAGE_CODE = 'en-us' GET = {} request = Req() self.assertEqual( get_language_from_request(request), 'en-us') request.LANGUAGE_CODE = 'dont' self.assertEqual( get_language_from_request(request), 'en-us') request.LANGUAGE_CODE = 'fr-ch' self.assertEqual( get_language_from_request(request), 'fr-ch')
def list_pages_ajax(request, invalid_move=False): """Render pages table for ajax function.""" language = get_language_from_request(request) pages = Page.objects.root() context = { 'invalid_move': invalid_move, 'language': language, 'pages': pages, } return render_to_response("admin/basic_cms/page/change_list_table.html", context, context_instance=RequestContext(request))
def list_pages_ajax(request, invalid_move=False): """Render pages table for ajax function.""" language = get_language_from_request(request) pages = Page.objects.root() context = { 'invalid_move': invalid_move, 'language': language, 'pages': pages, } return render_to_response("admin/basic_cms/page/change_list_table.html", context, context_instance=RequestContext(request) )
def get_form(self, request, obj=None, **kwargs): """Get a :class:`Page <pages.admin.forms.PageForm>` for the :class:`Page <pages.models.Page>` and modify its fields depending on the request.""" #form = super(PageAdmin, self).get_form(request, obj, **kwargs) template = get_template_from_request(request, obj) form = make_form(self.model, get_placeholders(template)) language = get_language_from_request(request) form.base_fields['language'].initial = language if obj: initial_slug = obj.slug(language=language, fallback=False) initial_title = obj.title(language=language, fallback=False) form.base_fields['slug'].initial = initial_slug form.base_fields['title'].initial = initial_title form.base_fields['slug'].label = _('Slug') template = get_template_from_request(request, obj) page_templates = settings.get_page_templates() if len(page_templates) > 0: template_choices = list(page_templates) template_choices.insert( 0, (settings.PAGE_DEFAULT_TEMPLATE, _('Default template'))) form.base_fields['template'].choices = template_choices form.base_fields['template'].initial = force_text(template) for placeholder in get_placeholders(template): name = placeholder.name if obj: initial = placeholder.get_content(obj, language, name) else: initial = None form.base_fields[name] = placeholder.get_field(obj, language, initial=initial) for placeholder in self.metadata_fields: name = placeholder['name'] initial = None if obj: try: initial = Content.objects.get(page=obj, language=language, type=name).body except Content.DoesNotExist: pass form.base_fields[name] = placeholder['field'] form.base_fields[name].initial = initial return form
def get_form(self, request, obj=None, **kwargs): """Get a :class:`Page <pages.admin.forms.PageForm>` for the :class:`Page <pages.models.Page>` and modify its fields depending on the request.""" #form = super(PageAdmin, self).get_form(request, obj, **kwargs) template = get_template_from_request(request, obj) form = make_form(self.model, get_placeholders(template)) language = get_language_from_request(request) form.base_fields['language'].initial = language if obj: initial_slug = obj.slug(language=language, fallback=False) initial_title = obj.title(language=language, fallback=False) form.base_fields['slug'].initial = initial_slug form.base_fields['title'].initial = initial_title form.base_fields['slug'].label = _('Slug') template = get_template_from_request(request, obj) page_templates = settings.get_page_templates() if len(page_templates) > 0: template_choices = list(page_templates) template_choices.insert(0, (settings.PAGE_DEFAULT_TEMPLATE, _('Default template'))) form.base_fields['template'].choices = template_choices form.base_fields['template'].initial = force_text(template) for placeholder in get_placeholders(template): name = placeholder.name if obj: initial = placeholder.get_content(obj, language, name) else: initial = None form.base_fields[name] = placeholder.get_field(obj, language, initial=initial) for placeholder in self.metadata_fields: name = placeholder['name'] initial = None if obj: try: initial = Content.objects.get(page=obj, language=language, type=name).body except Content.DoesNotExist: pass form.base_fields[name] = placeholder['field'] form.base_fields[name].initial = initial return form
def choose_language(self, lang, request): """Deal with the multiple corner case of choosing the language.""" # Can be an empty string or None if not lang: lang = get_language_from_request(request) # Raise a 404 if the language is not in not in the list if lang not in [key for (key, value) in settings.PAGE_LANGUAGES]: raise Http404 # We're going to serve CMS pages in language lang; # make django gettext use that language too if lang and translation.check_for_language(lang): translation.activate(lang) return lang
def get_form(self, request, obj=None, **kwargs): form = super(PageAdminWithDefaultContent, self ).get_form(request, obj, **kwargs) language = get_language_from_request(request) if global_settings.LANGUAGE_CODE == language: # this is the "official" language return form if Content.objects.filter(page=obj, language=language).count(): return form # this is a new page, try to find some default content template = get_template_from_request(request, obj) for placeholder in get_placeholders(template): name = placeholder.name form.base_fields[name] = placeholder.get_field(obj, language, initial=Content.objects.get_content(obj, global_settings.LANGUAGE_CODE, name)) return form
def has_delete_permission(self, request, obj=None): """Return ``True`` if the current user has permission on the page.""" lang = get_language_from_request(request) return PagePermission(request.user).check('change', page=obj, lang=lang)
def has_add_permission(self, request): """Return ``True`` if the current user has permission to add a new page.""" lang = get_language_from_request(request) return PagePermission(request.user).check('add', lang=lang)