| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- encoding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | ## |
|---|
| 5 | ## views.py |
|---|
| 6 | ## Created on Mon Jul 31 22:38:29 2006 |
|---|
| 7 | ## by Antti Kaihola <antti.kaihola@linux-aktivaattori.org> |
|---|
| 8 | ## |
|---|
| 9 | ## Copyright (C) 2006 Antti Kaihola |
|---|
| 10 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 11 | ## it under the terms of the GNU General Public License as published by |
|---|
| 12 | ## the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | ## (at your option) any later version. |
|---|
| 14 | ## |
|---|
| 15 | ## This program is distributed in the hope that it will be useful, |
|---|
| 16 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | ## GNU General Public License for more details. |
|---|
| 19 | ## |
|---|
| 20 | ## You should have received a copy of the GNU General Public License |
|---|
| 21 | ## along with this program; if not, write to the Free Software |
|---|
| 22 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | ## |
|---|
| 24 | |
|---|
| 25 | from ambidjangolib.multilangflatpages.models import MultiLangFlatPageTranslation |
|---|
| 26 | from ambidjangolib import languages |
|---|
| 27 | from django.template import loader, RequestContext |
|---|
| 28 | from django.http import HttpResponse, Http404 |
|---|
| 29 | from django.conf import settings |
|---|
| 30 | |
|---|
| 31 | DEFAULT_TEMPLATE = 'multilangflatpage/default.html' |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | def multilangflatpage(request, url, langcode=None): |
|---|
| 35 | """ |
|---|
| 36 | Multi-Language Flat Page view. |
|---|
| 37 | |
|---|
| 38 | Models: `multilangflatpages.MultiLangFlatPageTranslation` |
|---|
| 39 | Templates: Uses the template defined by the ``template_name`` field, |
|---|
| 40 | or `base.html` if template_name is not defined. |
|---|
| 41 | Context: |
|---|
| 42 | flatpage |
|---|
| 43 | `flatpages.flatpages` object |
|---|
| 44 | """ |
|---|
| 45 | if not url.startswith('/'): |
|---|
| 46 | url = "/" + url |
|---|
| 47 | |
|---|
| 48 | try: |
|---|
| 49 | if langcode is None: |
|---|
| 50 | f = MultiLangFlatPageTranslation.in_current_language.get( |
|---|
| 51 | flatpage__url__exact=url) |
|---|
| 52 | else: |
|---|
| 53 | f = MultiLangFlatPageTranslation.objects.get( |
|---|
| 54 | flatpage__url__exact=url, |
|---|
| 55 | language__code__exact=langcode) |
|---|
| 56 | except MultiLangFlatPageTranslation.DoesNotExist: |
|---|
| 57 | raise Http404 |
|---|
| 58 | |
|---|
| 59 | if f.flatpage.template_name: |
|---|
| 60 | t = loader.select_template((f.flatpage.template_name, DEFAULT_TEMPLATE)) |
|---|
| 61 | else: |
|---|
| 62 | t = loader.get_template(DEFAULT_TEMPLATE) |
|---|
| 63 | c = RequestContext(request, {'multilangflatpage': f}) |
|---|
| 64 | |
|---|
| 65 | return HttpResponse(t.render(c)) |
|---|