Projet

Général

Profil

« Précédent | Suivant » 

Révision f4843b93

Ajouté par Serghei Mihai (congés, retour 15/05) il y a presque 9 ans

statics and theme management (#7070)

Templates and statics upload to organization dir

Voir les différences:

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()

Formats disponibles : Unified diff