Projet

Général

Profil

« Précédent | Suivant » 

Révision f4843b93

Ajouté par Serghei Mihai il y a presque 9 ans

statics and theme management (#7070)

Templates and statics upload to organization dir

Voir les différences:

debian/debian_config.py
23 23

  
24 24
SECRET_KEY = file('/etc/%s/secret' % PROJECT_NAME).read()
25 25

  
26
ORGANIZATIONS_DIR = os.path.join(VAR_DIR, 'organizations')
27

  
26 28
execfile(os.path.join(ETC_DIR, 'settings.py'))
debian/nginx-example.conf
9 9
        access_log  /var/log/nginx/u-auth.example.org-access.log combined;
10 10
        error_log  /var/log/nginx/u-auth.example.org-error.log;
11 11

  
12
        location ~ ^/static/(.+)$ {
12
        location ~ ^(.*)/static/(.+)$ {
13 13
            root /;
14
            try_files /var/lib/u-auth/static/$1
15
                      /var/lib/u-auth/collectstatic/$1
14
            try_files /var/lib/u-auth/organizations/$1/static/$2
15
                      /var/lib/u-auth/static/$2
16
                      /var/lib/u-auth/collectstatic/$2
16 17
                      =404;
17 18
        }
18 19

  
debian/u-auth.dirs
1 1
/etc/u-auth
2 2
/usr/lib/u-auth
3
/var/lib/u-auth/organizations
3 4
/var/lib/u-auth/collectstatic
4 5
/var/lib/u-auth/static
5 6
/var/lib/u-auth/templates
uauth/context_processors.py
1
import os
2

  
3
from django.conf import settings
4
from django.template.loader import get_template, TemplateDoesNotExist
5

  
6
def theme_base(request):
7
    if request.session.get('organization'):
8
        try:
9
            base = get_template('base.html', [os.path.join(settings.ORGANIZATIONS_DIR,
10
                                request.session['organization'], 'templates')])
11
        except TemplateDoesNotExist:
12
            base = get_template('uauth/base.html')
13

  
14
    return {'theme_base': base}
uauth/organization/forms.py
31 31

  
32 32
class UsersImportForm(forms.Form):
33 33
    users_file = forms.FileField(_('Users file'))
34

  
35

  
36
class TemplateForm(forms.Form):
37
    template_file = forms.FileField(_('Template file'))
38

  
39
class StaticForm(forms.Form):
40
    static_file = forms.FileField(_('Static file'))
uauth/organization/templates/organization/manage.html
4 4
{% block content %}
5 5
<ul class="apps">
6 6
  <li class="users"><a href="{% url 'manage-users' organization.slug %}">{% trans 'Users' %}</a></li>
7
  <li class="theme"><a href="{% url 'manage-theme' organization.slug %}">{% trans 'Theme' %}</a></li>
7 8
</ul>
8 9
{% endblock %}
uauth/organization/templates/organization/theme.html
1
{% extends "organization/base.html" %}
2
{% load i18n %}
3

  
4
{% block page-title %}
5
{% trans 'Theme management' %}
6
{% endblock %}
7

  
8
{% block appbar %}
9
<h2>{% trans "Theme" %}</h2>
10
<a href="{% url "static-upload" organization.slug %}" rel="popup">{% trans "Upload static" %}</a>
11
<a href="{% url "template-upload" organization.slug %}" rel="popup">{% trans "Upload template" %}</a>
12

  
13
<h3>{% trans "Templates" %}</h3>
14
<form action='{% url "template-delete" organization.slug %}'>
15
<table>
16
  <thead>
17
    <tr><td>{% trans "Filename" %}</td><td></td></tr>
18
  </thead>
19
  <tbody>
20
  {% for template in templates %}
21
  <tr><td>{{ template }}</td><td> <button name="template" value="{{ template }}" class="icon-delete">{% trans "Remove" %}</button></td></tr>
22
  {% empty %}
23
  <tr><td colspan=2>{% trans "No templates uploaded yet" %}</td></tr>
24
  {% endfor %}
25
  </tbody>
26
</table>
27
</form>
28

  
29
<h3>{% trans "Statics" %}</h3>
30
<form action='{% url "static-delete" organization.slug %}'>
31
  <table>
32
    <thead>
33
      <tr><td>{% trans "Filename" %}</td><td></td></tr>
34
    </thead>
35
    <tbody>
36
  {% for static in statics %}
37
  <tr><td>{{ static }}</td><td><button name="static" value="{{ static }}" class="icon-delete">{% trans "Remove" %}</button></td></tr>
38
  {% empty %}
39
  <tr><td>{% trans "No statics uploaded yet" %}</td></tr>
40
  {% endfor %}
41
    </tbody>
42
  </table>
43
  </form>
44
{% endblock %}
45

  
uauth/organization/templates/organization/upload.html
1
{% extends "organization/base.html" %}
2
{% load i18n %}
3

  
4
{% block more-user-links %}
5
{{ block.super }}
6
  <a href="{% url "manage-theme" organization.slug %}">{% trans 'Theme' %}</a>
7
{% endblock %}
8

  
9
{% block appbar %}
10
<h2>{% trans "Theme" %}</h2>
11
<a href="{% url "static-upload" organization.slug %}" rel="popup">{% trans "Upload static" %}</a>
12
<a href="{% url "template-upload" organization.slug %}" rel="popup">{% trans "Upload template" %}</a>
13
{% endblock %}
14

  
15
{% block content %}
16
<form method="post" enctype="multipart/form-data">
17
  {% csrf_token %}
18
  {{ form.as_p }}
19
  <p><button name="upload">{% trans "Upload" %}</button>
20
</form>
21
{% endblock %}
uauth/organization/urls.py
9 9
    url(r'^users/import$', import_users, name='import-users'),
10 10
    url(r'^users/(?P<pk>[\w]+)/$', view_user, name='view-user'),
11 11
    url(r'^users/(?P<pk>[\w]+)/edit$', edit_user, name='edit-user'),
12
    url(r'^theme/?$', theme, name='manage-theme'),
13
    url(r'^theme/template/upload$', template_upload, name='template-upload'),
14
    url(r'^theme/template/delete$', template_delete, name='template-delete'),
15
    url(r'^theme/static/upload$', static_upload, name='static-upload'),
16
    url(r'^theme/static/delete$', static_delete, name='static-delete'),
12 17
)
uauth/organization/views.py
1
import os
1 2
import csv
2 3
import datetime
3 4

  
5
from django.conf import settings
4 6
from django.utils.translation import ugettext as _
5 7
from django.core.urlresolvers import reverse_lazy
6 8
from django.shortcuts import render, redirect
......
15 17

  
16 18
from .utils import create_user, create_or_update_users
17 19
from .models import LocalAccount, Organization
18
from .forms import LocalAccountCreateForm, LocalAccountForm, UsersImportForm
20
from .forms import *
19 21
from .tables import AccountTable
20 22

  
21 23

  
......
153 155
            return self.render_to_response(context)
154 156

  
155 157
import_users = ImportUsersView.as_view()
158

  
159

  
160
class ThemeView(OrganizationMixin, TemplateView):
161
    template_name = 'organization/theme.html'
162

  
163
    def get_success_url(self):
164
        return reverse_lazy('manage-theme', kwargs={'organization_slug': self.kwargs['organization_slug']})
165

  
166
    def get_context_data(self, **kwargs):
167
        ctx = super(ThemeView, self).get_context_data(**kwargs)
168
        organization = ctx['organization']
169
        templates_dir = os.path.join(settings.ORGANIZATIONS_DIR,
170
                                organization.slug, 'templates')
171
        statics_dir = os.path.join(settings.ORGANIZATIONS_DIR,
172
                                organization.slug, 'static')
173
        ctx['templates'] = []
174
        ctx['statics'] = []
175
        if os.path.exists(templates_dir):
176
            ctx['templates'] = os.listdir(templates_dir)
177
        if os.path.exists(statics_dir):
178
            ctx['statics'] = os.listdir(statics_dir)
179
        ctx['templates_dir'] = templates_dir
180
        ctx['statics_dir'] = statics_dir
181
        return ctx
182

  
183
theme = ThemeView.as_view()
184

  
185
class UploadMixin(object):
186
    template_name = "organization/upload.html"
187

  
188
    def get_context_data(self, **kwargs):
189
        ctx = super(UploadMixin, self).get_context_data(**kwargs)
190
        ctx['form'] = self.form_class()
191
        return ctx
192

  
193
    def post(self, request, *args, **kwargs):
194
        form = self.form_class(request.POST, request.FILES)
195
        context = self.get_context_data(**kwargs)
196
        context['form'] = form
197
        organization = context['organization']
198
        destination_dir = os.path.join(settings.ORGANIZATIONS_DIR,
199
                                       organization.slug, self.upload_dir)
200
        if form.is_valid():
201
            data = form.cleaned_data[self.filename_param]
202
            if not os.path.exists(destination_dir):
203
                os.makedirs(destination_dir)
204
            try:
205
                with open(os.path.join(destination_dir, data.name), 'w') as template:
206
                    template.write(data.read())
207
                messages.info(request, _('File "%s" successfully uploaded') % data.name)
208
            except OSError:
209
                messages.error(request, _('An error occured while uploading file "%s"') % data.name)
210
            return redirect(self.get_success_url())
211
        else:
212
            return self.render_to_response(context)
213

  
214

  
215
class TemplateUpload(UploadMixin, ThemeView):
216
    form_class = TemplateForm
217
    filename_param = 'template_file'
218
    upload_dir = 'templates'
219

  
220
template_upload = TemplateUpload.as_view()
221

  
222

  
223
class TemplateDelete(ThemeView):
224

  
225
    def get(self, request, *args, **kwargs):
226
        ctx = self.get_context_data(**kwargs)
227
        template = request.GET.get('template')
228
        if os.path.exists(os.path.join(ctx['templates_dir'], template)):
229
            try:
230
                os.remove(os.path.join(ctx['templates_dir'], template))
231
                messages.info(request, _('Template %s successfully removed') % template)
232
            except IOError:
233
                messages.error(request, _('An error occured while removing file %s') % template)
234
        else:
235
            messages.error(request, _('Unknown template %s') % template)
236
        return redirect(self.get_success_url())
237

  
238

  
239
template_delete = TemplateDelete.as_view()
240

  
241

  
242
class StaticUpload(UploadMixin, ThemeView):
243
    form_class = StaticForm
244
    filename_param = 'static_file'
245
    upload_dir = 'static'
246

  
247
static_upload = StaticUpload.as_view()
248

  
249

  
250
class StaticDelete(ThemeView):
251

  
252
    def get(self, request, *args, **kwargs):
253
        ctx = self.get_context_data(**kwargs)
254
        static = request.GET.get('static')
255
        if os.path.exists(os.path.join(ctx['statics_dir'], static)):
256
            try:
257
                os.remove(os.path.join(ctx['statics_dir'], static))
258
                messages.info(request, _('Static file %s successfully removed') % static)
259
            except IOError:
260
                messages.error(request, _('An error occured while removing file %s') % static)
261
        else:
262
            messages.error(request, _('Unknown static %s') % static)
263
        return redirect(self.get_success_url())
264

  
265
static_delete = StaticDelete.as_view()
uauth/settings.py
54 54
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
55 55
)
56 56

  
57
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + ('django.core.context_processors.request',)
57
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + \
58
                              ('django.core.context_processors.request',
59
                               'uauth.context_processors.theme_base',)
58 60

  
59 61
ROOT_URLCONF = 'uauth.urls'
60 62

  
......
97 99
    'dn': 'ou=radius,dc=entrouvert,dc=org',
98 100
}
99 101

  
102
ORGANIZATIONS_DIR = os.path.join(BASE_DIR, 'organizations')
103

  
100 104
AUTHENTICATION_BACKENDS = global_settings.AUTHENTICATION_BACKENDS + (
101 105
    'mellon.backends.SAMLBackend',
102 106
    'uauth.backends.LocalAccountPasswordBackend',
uauth/static/css/style.css
74 74
}
75 75

  
76 76
/* icons */
77
li.users a {background-image: url(icons/icon-personnes.png);}
77
li.users a {background-image: url(icons/icon-personnes.png);}
78
li.theme a {background-image: url(icons/icon-ressources.png);}
uauth/templates/uauth/organization.html
1
{% extends "uauth/base.html" %}
1
{% extends theme_base %}
2 2
{% load i18n %}
3 3

  
4 4
{% block more-user-links %}
uauth/views.py
77 77
        context = super(OrganizationPageView, self).get_context_data(**kwargs)
78 78
        idps = get_idp_list()
79 79
        organization = Organization.objects.get(slug=self.kwargs['organization_slug'])
80
        self.request.session['organization'] = organization.slug
80 81
        self.request.session[organization.slug] = self.request.GET.urlencode()
81 82
        relay = signing.dumps({'organization': organization.slug})
82 83
        context.update({'idps': idps,

Formats disponibles : Unified diff