Projet

Général

Profil

0003-misc-start-a-new-theme-panel-8777.patch

Frédéric Péters, 28 décembre 2015 14:39

Télécharger (14,1 ko)

Voir les différences:

Subject: [PATCH 3/4] misc: start a new 'theme' panel (#8777)

 MANIFEST.in                                       |  1 +
 hobo/environment/forms.py                         |  2 +-
 hobo/environment/migrations/0010_variable_auto.py | 20 +++++++
 hobo/environment/models.py                        |  1 +
 hobo/environment/views.py                         | 18 ++++++-
 hobo/settings.py                                  |  3 ++
 hobo/static/css/style.css                         | 16 ++++++
 hobo/templates/hobo/home.html                     |  1 +
 hobo/theme/__init__.py                            |  0
 hobo/theme/templates/hobo/theme_home.html         | 29 ++++++++++
 hobo/theme/urls.py                                | 24 +++++++++
 hobo/theme/utils.py                               | 64 +++++++++++++++++++++++
 hobo/theme/views.py                               | 45 ++++++++++++++++
 hobo/urls.py                                      |  3 ++
 14 files changed, 224 insertions(+), 3 deletions(-)
 create mode 100644 hobo/environment/migrations/0010_variable_auto.py
 create mode 100644 hobo/theme/__init__.py
 create mode 100644 hobo/theme/templates/hobo/theme_home.html
 create mode 100644 hobo/theme/urls.py
 create mode 100644 hobo/theme/utils.py
 create mode 100644 hobo/theme/views.py
MANIFEST.in
1 1
recursive-include hobo/static *.css *.png *.js
2 2
recursive-include hobo/templates *.html *.txt
3 3
recursive-include hobo/profile/templates *.html *.txt
4
recursive-include hobo/theme/templates *.html *.txt
4 5
recursive-include hobo/environment/templates *.html *.txt
5 6
recursive-include hobo/locale *.po *.mo
6 7
recursive-include hobo/environment/locale *.po *.mo
hobo/environment/forms.py
113 113
class VariableForm(forms.ModelForm):
114 114
    class Meta:
115 115
        model = Variable
116
        exclude = ('service_type', 'service_pk')
116
        exclude = ('service_type', 'service_pk', 'auto')
117 117

  
118 118
    def __init__(self, service=None, **kwargs):
119 119
        self.service = service
hobo/environment/migrations/0010_variable_auto.py
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

  
4
from django.db import models, migrations
5

  
6

  
7
class Migration(migrations.Migration):
8

  
9
    dependencies = [
10
        ('environment', '0009_mandayejs'),
11
    ]
12

  
13
    operations = [
14
        migrations.AddField(
15
            model_name='variable',
16
            name='auto',
17
            field=models.BooleanField(default=False),
18
            preserve_default=True,
19
        ),
20
    ]
hobo/environment/models.py
23 23
    value = models.TextField(verbose_name=_('value'),
24 24
            blank=True,
25 25
            help_text=_('start with [ or { for a JSON document'))
26
    auto = models.BooleanField(default=False)
26 27
    service_type = models.ForeignKey(ContentType, null=True)
27 28
    service_pk = models.PositiveIntegerField(null=True)
28 29
    service = generic.GenericForeignKey('service_type', 'service_pk')
hobo/environment/views.py
3 3

  
4 4
from django.conf import settings
5 5
from django.core.urlresolvers import reverse_lazy
6
from django.http import HttpResponse, Http404
6
from django.http import HttpResponse, HttpResponseRedirect, Http404
7 7
from django.shortcuts import get_object_or_404
8 8
from django.views.generic.base import TemplateView
9 9
from django.views.generic.edit import CreateView, UpdateView, DeleteView
......
35 35

  
36 36
    def get_context_data(self, **kwargs):
37 37
        context = super(VariablesView, self).get_context_data(**kwargs)
38
        context['variables'] = Variable.objects.filter(service_pk__isnull=True)
38
        context['variables'] = Variable.objects.filter(auto=False,
39
                service_pk__isnull=True)
39 40
        return context
40 41

  
41 42

  
......
54 55
                    break
55 56
        return kwargs
56 57

  
58
    def form_valid(self, form):
59
        try:
60
            self.object = Variable.objects.get(
61
                    name=form.instance.name,
62
                    service_pk__isnull=True)
63
        except Variable.DoesNotExist:
64
            self.object = form.save()
65
        else:
66
            self.object.auto = False
67
            self.object.value = form.instance.value
68
            self.object.save()
69
        return HttpResponseRedirect(self.get_success_url())
70

  
57 71
    def get_success_url(self):
58 72
        if self.object.service is None:
59 73
            return reverse_lazy('environment-variables')
hobo/settings.py
41 41
    'gadjo',
42 42
    'hobo.environment',
43 43
    'hobo.profile',
44
    'hobo.theme',
44 45
    'hobo.deploy',
45 46
)
46 47

  
......
166 167
    'is_superuser': 'true',
167 168
}
168 169

  
170
THEMES_DIRECTORY = '/usr/share/publik/themes/'
171

  
169 172
MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}'
170 173

  
171 174
local_settings_file = os.environ.get('HOBO_SETTINGS_FILE',
hobo/static/css/style.css
102 102
h4.untoggled + div {
103 103
	display: none;
104 104
}
105

  
106
div.objects-list > div > label {
107
	display: block;
108
	width: 100%;
109
	border-left: 20px solid white;
110
	min-height: 20px;
111
	padding-left: 1ex;
112
}
113

  
114
div.objects-list > div > label > input[type=radio] {
115
	margin-right: 1.5ex;
116
}
117

  
118
div.buttons {
119
	margin: 1em 0;
120
}
hobo/templates/hobo/home.html
10 10

  
11 11
<ul class="apps">
12 12
  <li class="icon-users"><a href="{% url 'profile-home' %}">{% trans 'User Profile' %}</a></li>
13
  <li class="icon-theme"><a href="{% url 'theme-home' %}">{% trans 'Theme' %}</a></li>
13 14
  <li class="icon-portal"><a href="{% url 'environment-home' %}">{% trans 'Sites' %}</a></li>
14 15
  <li class="icon-settings"><a href="{% url 'environment-variables' %}">{% trans 'Variables' %}</a></li>
15 16
  {% for passerelle in passerelles %}
hobo/theme/templates/hobo/theme_home.html
1
{% extends "hobo/base.html" %}
2
{% load i18n %}
3

  
4
{% block breadcrumb %}
5
{{ block.super }}
6
<a href="{% url 'theme-home' %}">{% trans 'Theme' %}</a>
7
{% endblock %}
8

  
9
{% block appbar %}
10
  <h2>{% trans 'Theme' %}</h2>
11
{% endblock %}
12

  
13
{% block content %}
14

  
15
<form action="{% url 'theme-select' %}" method="post">
16
{% csrf_token %}
17
<div class="objects-list" >
18
 {% for theme in themes %}
19
 <div data-id="{{theme.id}}">
20
   <label style="border-color: {{theme.color}}"><input type="radio" name="theme" value="{{theme.id}}" {% if theme.id == selected_theme %}checked="checked"{% endif %}/>{{theme.label}}</label>
21
 </div>
22
 {% endfor %}
23
</div>
24
<div class="buttons">
25
<button>{% trans "Submit" %}</button>
26
</div>
27
</form>
28

  
29
{% endblock %}
hobo/theme/urls.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.conf.urls import patterns, url
18

  
19
from . import views
20

  
21
urlpatterns = patterns('',
22
    url(r'^$', views.home, name='theme-home'),
23
    url(r'^select$', views.select, name='theme-select'),
24
)
hobo/theme/utils.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import json
18
import os
19

  
20
from django.conf import settings
21

  
22
from hobo.environment.models import Variable
23

  
24

  
25
def get_themes():
26
    themes = []
27
    for dirname in os.listdir(settings.THEMES_DIRECTORY):
28
        filename = os.path.join(settings.THEMES_DIRECTORY, dirname,
29
                'themes.json')
30
        if not os.path.exists(filename):
31
            continue
32
        themes.extend(json.load(open(filename)))
33
    themes.sort(lambda x, y: cmp(x.get('label'), y.get('label')))
34
    return themes
35

  
36
def get_selected_theme():
37
    try:
38
        selected_theme = Variable.objects.get(name='theme').value
39
    except Variable.DoesNotExist:
40
        selected_theme = None
41
    return selected_theme
42

  
43
def set_theme(theme_id):
44
    selected_theme, created = Variable.objects.get_or_create(name='theme')
45
    if selected_theme.value != theme_id:
46
        theme = get_theme(theme_id)
47
        for variable in theme.get('variables', {}).keys():
48
            theme_variable, created = Variable.objects.get_or_create(
49
                    name=variable)
50
            theme_variable.auto = True
51
            theme_variable.value = theme['variables'][variable]
52
            theme_variable.save()
53

  
54
        selected_theme.value = theme_id
55
        selected_theme.auto = True
56
        selected_theme.save()
57

  
58
def get_theme(theme_id):
59
    themes = get_themes()
60
    for theme in themes:
61
        if theme.get('id') != theme_id:
62
            continue
63
        return theme
64
    return None
hobo/theme/views.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.core.urlresolvers import reverse
18
from django.shortcuts import redirect
19
from django.views.generic import RedirectView, TemplateView
20

  
21
from .utils import (get_themes, get_selected_theme, set_theme)
22

  
23

  
24
class HomeView(TemplateView):
25
    template_name = 'hobo/theme_home.html'
26

  
27
    def get_context_data(self, **kwargs):
28
        context = super(HomeView, self).get_context_data(**kwargs)
29

  
30
        context['themes'] = get_themes()
31
        context['selected_theme'] = get_selected_theme()
32

  
33
        return context
34

  
35
home = HomeView.as_view()
36

  
37

  
38
class SelectView(RedirectView):
39
    permanent = False
40

  
41
    def get_redirect_url(self):
42
        set_theme(self.request.POST['theme'])
43
        return reverse('theme-home')
44

  
45
select = SelectView.as_view()
hobo/urls.py
8 8
from .urls_utils import decorated_includes
9 9
from .environment.urls import urlpatterns as environment_urls
10 10
from .profile.urls import urlpatterns as profile_urls
11
from .theme.urls import urlpatterns as theme_urls
11 12

  
12 13
urlpatterns = patterns('',
13 14
    url(r'^$', 'hobo.views.home', name='home'),
......
15 16
                                       include(environment_urls))),
16 17
    url(r'^profile/', decorated_includes(admin_required,
17 18
                                             include(profile_urls))),
19
    url(r'^theme/', decorated_includes(admin_required,
20
                                             include(theme_urls))),
18 21
    url(r'^menu.json$', 'hobo.views.menu_json', name='menu_json'),
19 22
    url(r'^hobos.json$', 'hobo.views.hobo'),
20 23
    url(r'^admin/', include(admin.site.urls)),
21
-