]> git.parisson.com Git - teleforma.git/commitdiff
add autonomous user XLS class
authorGuillaume Pellerin <yomguy@parisson.com>
Mon, 11 May 2015 22:22:21 +0000 (00:22 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Mon, 11 May 2015 22:22:21 +0000 (00:22 +0200)
teleforma/admin.py
teleforma/views/crfpa.py

index 25b5916e382153168f931057702a6a51d1f982ea..1ad03d4608b998067fdf925bdd1b30aa42fe6b5f 100644 (file)
@@ -1,11 +1,14 @@
 # -*- coding: utf-8 -*-
 from teleforma.models import *
+from teleforma.views import *
 from teleforma.exam.models import *
 from django.contrib import admin
 from django.contrib.auth.models import User
 from django.contrib.auth.admin import UserAdmin
 from django.contrib.admin import SimpleListFilter
 from django.utils.translation import ugettext_lazy as _
+from django.http import HttpResponse
+from django.core import serializers
 
 
 class PeriodListFilter(SimpleListFilter):
@@ -49,7 +52,9 @@ class StudentInline(admin.StackedInline):
     model = Student
     extra = 1
 
+
 class StudentAdmin(admin.ModelAdmin):
+
     model = Student
     exclude = ['options']
     filter_horizontal = ['trainings']
@@ -59,6 +64,7 @@ class StudentAdmin(admin.ModelAdmin):
                     'procedure', 'written_speciality', 'oral_speciality',
                     'oral_1', 'oral_2']
     list_display = ['student_name', 'total_payments', 'total_fees', 'balance']
+    actions = ['export_xls']
 
     def student_name(self, instance):
         return instance.user.last_name + ' ' + instance.user.first_name
@@ -71,6 +77,20 @@ class StudentAdmin(admin.ModelAdmin):
     #     qs = qs.annotate(models.Count('warehouse__amount'))
     #     return qs
 
+    def export_json(self, request, queryset):
+        response = HttpResponse(content_type="application/json")
+        serializers.serialize("json", queryset, stream=response)
+        return response
+
+    def export_xls(self, request, queryset):
+        view = UsersExportView()
+        view.users = queryset
+        return view.get(request)
+
+    export_xls.short_description = "Export to XLS"
+
+
+
 class ProfessorProfileInline(admin.StackedInline):
     model = Professor
     filter_horizontal = ['courses']
index 1c0e9f6d1ae5d4ea02267bc70c6dce01be17da0b..2353762d73fbb964295ec248d4e443d12f0cd7a7 100644 (file)
@@ -203,10 +203,15 @@ class UserLoginView(View):
         return super(UserLoginView, self).dispatch(*args, **kwargs)
 
 
-class UsersExportView(UsersView):
+class UserBook(object):
 
     first_row = 2
 
+    def __init__(self, users):
+        self.book = Workbook()
+        self.users = users
+        self.sheet = self.book.add_sheet('Etudiants')
+
     def export_user(self, counter, user):
         student = Student.objects.filter(user=user)
         if student:
@@ -243,13 +248,7 @@ class UsersExportView(UsersView):
         else:
             return counter
 
-    @method_decorator(permission_required('is_staff'))
-    def get(self, *args, **kwargs):
-        super(UsersExportView, self).get(*args, **kwargs)
-        self.users = self.users
-        self.book = Workbook()
-        self.sheet = self.book.add_sheet('Etudiants')
-
+    def write(self):
         row = self.sheet.row(0)
         cols = [{'name':'NOM', 'width':5000},
                 {'name':'PRENOM', 'width':5000},
@@ -276,9 +275,19 @@ class UsersExportView(UsersView):
         counter = 0
         for user in self.users:
             counter = self.export_user(counter, user)
+
+
+
+class UsersExportView(UsersView):
+
+    @method_decorator(permission_required('is_staff'))
+    def get(self, *args, **kwargs):
+        super(UsersExportView, self).get(*args, **kwargs)
+        book = UserBook(self.users)
+        book.write()
         response = HttpResponse(mimetype="application/vnd.ms-excel")
         response['Content-Disposition'] = 'attachment; filename=users.xls'
-        self.book.save(response)
+        book.book.save(response)
         return response