changeset 150:b101828faa0e

split views.py in sub-modules
author souliane <souliane@mailoo.org>
date Tue, 17 Oct 2017 22:16:20 +0200
parents b1c16cd53b62
children 41aaaaf7b7fd
files sat_website/urls.py sat_website/views.py sat_website/views/__init__.py sat_website/views/category.py sat_website/views/whereami.py
diffstat 4 files changed, 166 insertions(+), 135 deletions(-) [+]
line wrap: on
line diff
--- a/sat_website/urls.py	Tue Oct 17 04:11:21 2017 +0200
+++ b/sat_website/urls.py	Tue Oct 17 22:16:20 2017 +0200
@@ -21,11 +21,12 @@
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 """
 from django.conf.urls import include, url
-from sat_website.views import generic_cat, whereami
+from sat_website.views.whereami import WhereAmIView
+from sat_website.views.category import CategoryView
 
 urlpatterns = [
-    url(r'^(?P<category>\w*)(.html)?$', generic_cat),
     url(r'^i18n/', include('django.conf.urls.i18n')),
-    url(r'^whereami/?$', whereami)
+    url(r'^whereami/?$', WhereAmIView.as_view()),
+    url(r'^(?P<category>\w*)(.html)?$', CategoryView.as_view()),
 ]
 
--- a/sat_website/views.py	Tue Oct 17 04:11:21 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,132 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-
-"""
-SàT website: Salut à Toi's presentation website
-Copyright (C) 2012  Jérôme Poisson (goffi@goffi.org)
-
-This file is part of SàT website.
-
-SàT website is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-Foobar is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU Affero General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
-"""
-from django.http import Http404, HttpResponse
-from django.shortcuts import render_to_response
-from django.template.context_processors import csrf
-from django.utils.translation import ugettext_lazy as _
-from collections import OrderedDict
-import media, social_contract, utils, forms
-
-
-CATEGORIES = OrderedDict([('frontends', (_(u"Presentation"),
-                                OrderedDict([("features", _(u"Features")),
-                                             ("frontends", _(u"Frontends")),
-                                             ("media", _(u"Screenshots & Videos")),
-                                             ("news", _(u"News")),
-                                             ]))),
-                          ('principles', (_(u"Technical area"),
-                                OrderedDict([("principles", _(u"Principles")),
-                                             ("specifications", _(u"Specifications")),
-                                             ("downloads", _(u"Downloads")),
-                                             ("developers", _(u"Developers corner")),
-                                             ]))),
-                          ('community', (_(u"Community"),
-                                OrderedDict([("community", _(u"Get in touch")),
-                                             ("association", _(u"Association")),
-                                             ("links", _(u"Links")),
-                                             ("press", _(u"Press")),
-                                             ("faq", _(u"FAQ")),
-                                             ]))),
-                          ])
-
-CATEGORIES_RIGHT = OrderedDict([("membership", _(u"Membership")),
-                                ("social_contract", _(u"Social contract")),
-                                ])
-
-CATEGORIES_ALIASES = {"adhesion": "membership",
-                      "adhesion_form": "membership_form",
-                      "screenshots": "media",
-                      }
-
-CATEGORIES_HIDDEN = ('membership_form', 'thank_you')
-
-PROJECTS_INFOS = {'sat': _(u'contains the backend, Primitivus and Jp'),
-                  'sat_media': _(u'Images and other media needed to launch SàT'),
-                  'urwid_satext': _(u'console display library needed by Primitivus'),
-                  'sat_pubsub': _(u'PubSub server component needed for SàT experimental blogging features'),
-                  'libervia': _(u'Libervia frontend (web server and client)'),
-                  }
-
-
-def overview(request):
-    return render_to_response('sat_website/overview.html')
-
-
-def generic_cat(request, category):
-    if category in CATEGORIES_ALIASES:
-        category = CATEGORIES_ALIASES[category]
-
-    context = {
-               "available_languages": ['fr', 'en'],
-               "categories": CATEGORIES,
-               "categories_right": CATEGORIES_RIGHT,
-               "category": category,
-               "libervia_demo_url": utils.get_libervia_demo_url(),
-               "subscription_amounts": utils.get_asso_subscr_amounts(),
-               }
-
-    context.update(csrf(request))
-    context.update(utils.get_asso_finance_status())
-
-    if not category or category == "overview":
-        context.update(utils.get_asso_finance_status())
-        return render_to_response('sat_website/overview.html', context)
-    elif category == "media":
-        context["media"] = media.media
-        context["media_tech"] = media.media_tech
-    elif category == "social_contract":
-        context["SOCIAL_CONTRACT"] = social_contract.get_social_contract()
-    elif category == "downloads":
-        context["projects_infos"] = utils.get_projects_infos(PROJECTS_INFOS)
-    elif category in ("association", "membership"):
-        context.update(utils.get_asso_urls())
-    elif category == "membership_form":
-        if request.method == 'POST':
-            form = forms.RegistrationForm(request.POST)
-            form.process_submitted_data()
-            if form.is_valid():
-                category = 'thank_you'
-        else:
-            form = forms.RegistrationForm(initial={'subscription_amount': 10,
-                                                   'mailing': True})
-        context['form'] = form
-        context.update(utils.get_asso_urls())
-
-    def all_keys(cats):
-        subcats = [value[1].keys() for value in cats.values() if isinstance(value, tuple)]
-        return sum(subcats, cats.keys())
-
-    if category in all_keys(CATEGORIES) or category in all_keys(CATEGORIES_RIGHT) or category in CATEGORIES_HIDDEN:
-        return render_to_response('sat_website/%s.html' % (category,), context)
-    else:
-        raise Http404
-
-
-def whereami(request):
-    """Return the client IP address"""
-    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
-    if x_forwarded_for:
-        ip = x_forwarded_for.split(',')[0]
-    else:
-        ip = request.META.get('REMOTE_ADDR')
-    return HttpResponse(ip)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sat_website/views/category.py	Tue Oct 17 22:16:20 2017 +0200
@@ -0,0 +1,127 @@
+# -*- coding: utf-8 -*-
+"""
+SàT website: Salut à Toi's presentation website
+Copyright (C) 2012  Jérôme Poisson (goffi@goffi.org)
+
+This file is part of SàT website.
+
+SàT website is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Foobar is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with SàT website.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from collections import OrderedDict
+
+from django.http import Http404
+from django.views.generic.base import View
+from django.shortcuts import render_to_response
+from django.template.context_processors import csrf
+from django.utils.translation import ugettext_lazy as _
+
+from sat_website import utils, media, social_contract, forms
+
+
+CATEGORIES = OrderedDict([('frontends', (_(u"Presentation"),
+                                OrderedDict([("features", _(u"Features")),
+                                             ("frontends", _(u"Frontends")),
+                                             ("media", _(u"Screenshots & Videos")),
+                                             ("news", _(u"News")),
+                                             ]))),
+                          ('principles', (_(u"Technical area"),
+                                OrderedDict([("principles", _(u"Principles")),
+                                             ("specifications", _(u"Specifications")),
+                                             ("downloads", _(u"Downloads")),
+                                             ("developers", _(u"Developers corner")),
+                                             ]))),
+                          ('community', (_(u"Community"),
+                                OrderedDict([("community", _(u"Get in touch")),
+                                             ("association", _(u"Association")),
+                                             ("links", _(u"Links")),
+                                             ("press", _(u"Press")),
+                                             ("faq", _(u"FAQ")),
+                                             ]))),
+                          ])
+
+CATEGORIES_RIGHT = OrderedDict([("membership", _(u"Membership")),
+                                ("social_contract", _(u"Social contract")),
+                                ])
+
+CATEGORIES_ALIASES = {"adhesion": "membership",
+                      "adhesion_form": "membership_form",
+                      "screenshots": "media",
+                      }
+
+CATEGORIES_HIDDEN = ('membership_form', 'thank_you')
+
+PROJECTS_INFOS = {'sat': _(u'contains the backend, Primitivus and Jp'),
+                  'sat_media': _(u'Images and other media needed to launch SàT'),
+                  'urwid_satext': _(u'console display library needed by Primitivus'),
+                  'sat_pubsub': _(u'PubSub server component needed for SàT experimental blogging features'),
+                  'libervia': _(u'Libervia frontend (web server and client)'),
+                  }
+
+
+class CategoryView(View):
+
+    @staticmethod
+    def all_keys(cats):
+        subcats = [value[1].keys() for value in cats.values() if isinstance(value, tuple)]
+        return sum(subcats, cats.keys())
+
+    def get(self, request, category):
+        if category in CATEGORIES_ALIASES:
+            category = CATEGORIES_ALIASES[category]
+    
+        context = {
+                   "available_languages": ['fr', 'en'],
+                   "categories": CATEGORIES,
+                   "categories_right": CATEGORIES_RIGHT,
+                   "category": category,
+                   "libervia_demo_url": utils.get_libervia_demo_url(),
+                   "subscription_amounts": utils.get_asso_subscr_amounts(),
+                   }
+    
+        context.update(csrf(request))
+        context.update(utils.get_asso_finance_status())
+    
+        if not category or category == "overview":
+            context.update(utils.get_asso_finance_status())
+            return render_to_response('sat_website/overview.html', context)
+        elif category == "media":
+            context["media"] = media.media
+            context["media_tech"] = media.media_tech
+        elif category == "social_contract":
+            context["SOCIAL_CONTRACT"] = social_contract.get_social_contract()
+        elif category == "downloads":
+            context["projects_infos"] = utils.get_projects_infos(PROJECTS_INFOS)
+        elif category in ("association", "membership"):
+            context.update(utils.get_asso_urls())
+        elif category == "membership_form":
+            if request.method == 'POST':
+                form = forms.RegistrationForm(request.POST)
+                form.process_submitted_data()
+                if form.is_valid():
+                    category = 'thank_you'
+            else:
+                form = forms.RegistrationForm(initial={'subscription_amount': 10,
+                                                       'mailing': True})
+            context['form'] = form
+            context.update(utils.get_asso_urls())
+        
+        if (
+            category in self.all_keys(CATEGORIES) or
+            category in self.all_keys(CATEGORIES_RIGHT) or
+            category in CATEGORIES_HIDDEN
+        ):
+            return render_to_response('sat_website/%s.html' % (category,), context)
+        else:
+            raise Http404
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sat_website/views/whereami.py	Tue Oct 17 22:16:20 2017 +0200
@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+"""
+SàT website: Salut à Toi's presentation website
+Copyright (C) 2012  Jérôme Poisson (goffi@goffi.org)
+
+This file is part of SàT website.
+
+SàT website is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Foobar is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with SàT website.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from django.views import View
+from django.http.response import HttpResponse
+
+
+class WhereAmIView(View):
+
+    def get(self, request):
+        """Return the client IP address"""
+        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
+        if x_forwarded_for:
+            ip = x_forwarded_for.split(',')[0]
+        else:
+            ip = request.META.get('REMOTE_ADDR')
+        return HttpResponse(ip)