Projet

Général

Profil

0001-theme-check-we-get-a-post-value-on-theme-selection-4.patch

Nicolas Roche, 27 mars 2020 11:36

Télécharger (3,44 ko)

Voir les différences:

Subject: [PATCH] theme: check we get a post value on theme selection (#41079)

 hobo/theme/views.py | 10 ++++++----
 tests/test_theme.py | 10 +++++++++-
 2 files changed, 15 insertions(+), 5 deletions(-)
hobo/theme/views.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import colorsys
18 18
import random
19 19

  
20 20
from django.contrib import messages
21 21
from django.core.urlresolvers import reverse
22
from django.http import HttpResponseRedirect
22 23
from django.shortcuts import redirect
23 24
from django.utils.translation import ugettext as _
24 25
from django.views.generic import RedirectView, TemplateView
25 26

  
26 27
from hobo.environment.forms import VariablesFormMixin
27 28
from .forms import ThemeOptionsForm
28 29
from .utils import (get_themes, get_selected_theme, set_theme)
29 30

  
......
51 52
        return context
52 53

  
53 54
home = HomeView.as_view()
54 55

  
55 56

  
56 57
class SelectView(RedirectView):
57 58
    permanent = False
58 59

  
59
    def get_redirect_url(self):
60
        set_theme(self.request.POST['theme'])
61
        messages.info(self.request, _('The theme has been changed, it will soon be visible on the sites.'))
62
        return reverse('theme-home')
60
    def get(self, request):
61
        if request.method == 'POST':
62
            set_theme(self.request.POST['theme'])
63
            messages.info(self.request, _('The theme has been changed, it will soon be visible on the sites.'))
64
        return HttpResponseRedirect(reverse('theme-home'))
63 65

  
64 66
select = SelectView.as_view()
65 67

  
66 68

  
67 69
class OptionsView(VariablesFormMixin, TemplateView):
68 70
    template_name = 'hobo/theme_options.html'
69 71
    variables = ['global_title']
70 72
    form_class = ThemeOptionsForm
tests/test_theme.py
32 32
    assert Variable.objects.filter(name='foo')[0].value == ''
33 33

  
34 34
    # easter egg, sometimes it gets sorted by colour
35 35
    mocked_random.return_value = 0.09
36 36
    resp = app.get('/theme').follow()
37 37
    assert [x['value'] for x in resp.html.findAll('input', {'type': 'radio'})] == ['publik', 'alfortville']
38 38

  
39 39

  
40
def test_thme_view_empty(app, admin_user, settings):
40
def test_theme_view_empty(app, admin_user, settings):
41 41
    del settings.THEMES_DIRECTORY
42 42
    app = login(app)
43 43
    resp = app.get('/theme').follow()
44 44
    assert 'Theme' in resp.text
45 45

  
46 46

  
47 47
def test_theme_option_view(app, admin_user):
48 48
    app = login(app)
......
52 52
    resp.form['global_title'] = 'foo'
53 53
    resp = resp.form.submit()
54 54
    assert Variable.objects.all()[0].name == 'global_title'
55 55
    assert Variable.objects.all()[0].value == 'foo'
56 56

  
57 57
    assert resp.location == '.'
58 58
    resp = resp.follow()
59 59
    assert resp.html.find('li').text == 'foo'
60

  
61

  
62
def test_theme_view_without_theme_selected(app, admin_user, fake_themes):
63
    app = login(app)
64
    resp = app.get('/theme/select')
65
    assert resp.location == '/theme/'
66
    resp = resp.follow()
67
    assert resp.html.find('input', {'type': 'radio', 'checked': 'checked'})
60
-