summaryrefslogtreecommitdiff
path: root/chimere/rss/views.py
blob: 04d69dc668347a02e0175f13c4ff8c51e9293a30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010 Pierre Clarenc <pierre.crc_AT_gmailDOTcom>,
#                    Samuel Renard <renard.samuel_AT_gmailDOTcom>,
#                    Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

"""
Views of the project
"""

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

from chimere import settings
from chimere.main.views import get_base_response
from chimere.main.actions import actions
from chimere.main.models import SubCategory,Area
from chimere.main.forms import AreaForm
from chimere.main.widgets import AreaWidget

def rss(request, area_name=''):
    '''
    Redirect to RSS subscription page
    '''
    response_dct = get_base_response()
    response_dct.update({'actions':actions, 'action_selected':('rss',),
                         'category_rss_feed':'',})
    # If the form has been submited
    if request.method == "POST":
        # User has defined the kind of POI he is interested in : POI in a area
        # (GET method is used for the link with RSS icon in the browser)
        if 'rss_category' in request.POST:
            #User wants to follow all the new POI
            if request.POST['rss_category'] == 'global':
                feeds_link = '/' + settings.EXTRA_URL + 'rss/global/'
                return HttpResponseRedirect(feeds_link)
            # User wants to follow all the new POI by category or subcategory
            elif request.POST['rss_category'] == 'poi':
                response_dct['category_rss_feed'] = 'category'
                response_dct['sub_categories'] = SubCategory.getAvailable(['M',
                                                                           'B'])
                return render_to_response('rss.html', response_dct)
            # User wants to follow all the new POI situated in a defined area
            elif request.POST['rss_category'] == 'area':
                # An unbound form
                form = AreaForm()
                area_widget = AreaWidget().render('area', None)
                response_dct.update({'map_layer':settings.MAP_LAYER,
                                     'extra_head':form.media,
                                     'form':form,
                                     'category_rss_feed':'area',
                                     'area_id':Area.getAvailable(),
                                     'area_widget':area_widget
                })
                return render_to_response('rss.html', response_dct)
            # Error when submitting the form
            else:
                error = _("Incorrect choice in the list")
                response_dct.update({'error_message':error,
                    'category_rss_feed':'category',
                    'sub_categories':SubCategory.getAvailable(['M', 'B'])})
                return render_to_response('rss.html', response_dct)

        # User has specified the category or subcategory he wants to follow =>
        # we redirect him towards the related rss feed
        if 'subcategory' in request.POST and request.POST['subcategory'] != '':
            idCat =  request.POST['subcategory']
            if idCat.find("cat_") != -1 :
                list_Cat = idCat.split('_')
                feeds_link = '/' + settings.EXTRA_URL + 'rss/category/'
                feeds_link += list_Cat[1]
                return HttpResponseRedirect(feeds_link)

            else:
                feeds_link = '/' + settings.EXTRA_URL + 'rss/subcategory/' + \
                                                                           idCat
                return HttpResponseRedirect(feeds_link)

        # User has specified the ID of the area he wants to follow
        if 'id_area' in request.POST and request.POST['id_area'] != '':
            feeds_link = '/' + settings.EXTRA_URL + 'rss/areaid/' \
                         + request.POST['id_area']
            return HttpResponseRedirect(feeds_link)

        # User has specified the area  he wants to follow => we redirect him
        # towards the related rss feed (using upper left and lower right
        # coordinates)
        elif 'upper_left_lat' in request.POST and \
             request.POST['upper_left_lat'] != '' and \
             'upper_left_lon' in request.POST and \
             request.POST['upper_left_lon'] != '' and \
             'lower_right_lon' in request.POST and \
             request.POST['lower_right_lon'] != '' and \
             'lower_right_lat' in request.POST and \
             request.POST['lower_right_lat'] != '' :
            feeds_link = '/' + settings.EXTRA_URL + 'rss/area/' + \
request.POST['upper_left_lat'] + '_' + request.POST['upper_left_lon'] + '_' + \
request.POST['lower_right_lat'] + '_' + request.POST['lower_right_lon']
            return HttpResponseRedirect(feeds_link)


    # GET method is used for linking with the RSS icon in the browser when user
    # wants to choose a category to follow
    elif request.method == "GET" and 'rss_category' in request.GET:
        if request.GET['rss_category'] == 'global':
            feeds_link = '/' + settings.EXTRA_URL + 'rss/global/'
            return HttpResponseRedirect(feeds_link)
        if request.GET['rss_category'] == 'poi':
            response_dct['category_rss_feed'] = 'category'
            response_dct['sub_categories'] = SubCategory.getAvailable(['M','B'])
            return render_to_response('rss.html', response_dct)
        if request.GET['rss_category'] == 'area':
            # An unbound form
            form = AreaForm()
            response_dct.update({'map_layer':settings.MAP_LAYER,
                            'extra_head':form.media,
                            'form':form,
                            'category_rss_feed':'area',
                            'area_id':Area.getAvailable(),
                            'area_widget':AreaWidget().render('area', None)})
            return render_to_response('rss.html', response_dct)

    # User access to the RSS tab
    else:
        return render_to_response('rss.html', response_dct)