1 | # -*- encoding: utf-8 -*- |
---|
2 | from django.conf import settings |
---|
3 | from django_datatables_view.base_datatable_view import BaseDatatableView |
---|
4 | from django.contrib.auth.models import ( |
---|
5 | User |
---|
6 | ) |
---|
7 | from utils.views import ( |
---|
8 | LoginRequeridoPerAuth |
---|
9 | ) |
---|
10 | |
---|
11 | class ListUsersAjaxView(LoginRequeridoPerAuth, BaseDatatableView): |
---|
12 | """! |
---|
13 | Prepara la data para mostrar en el datatable |
---|
14 | |
---|
15 | @author Ing. Leonel P. Hernandez M. (lhernandez at cenditel.gob.ve) |
---|
16 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
17 | @date 09-01-2017 |
---|
18 | @version 1.0.0 |
---|
19 | """ |
---|
20 | # The model we're going to show |
---|
21 | model = User |
---|
22 | # define the columns that will be returned |
---|
23 | columns = ['pk', 'first_name', 'last_name', 'username', 'email', |
---|
24 | 'date_joined', 'last_joined', 'is_active', 'groups'] |
---|
25 | # define column names that will be used in sorting |
---|
26 | # order is important and should be same as order of columns |
---|
27 | # displayed by datatables. For non sortable columns use empty |
---|
28 | # value like '' |
---|
29 | order_columns = ['pk', 'username', 'first_name', 'last_name', 'email', |
---|
30 | 'date_joined', 'last_login', 'is_active', 'groups', |
---|
31 | 'acciones'] |
---|
32 | # set max limit of records returned, this is used to protect our site if someone tries to attack our site |
---|
33 | # and make it return huge amount of data |
---|
34 | max_display_length = 500 |
---|
35 | group_required = [u"Administradores"] |
---|
36 | |
---|
37 | def __init__(self): |
---|
38 | super(ListUsersAjaxView, self).__init__() |
---|
39 | |
---|
40 | def get_initial_queryset(self): |
---|
41 | """! |
---|
42 | Consulta el modelo User |
---|
43 | |
---|
44 | @return: Objeto de la consulta |
---|
45 | """ |
---|
46 | # return queryset used as base for futher sorting/filtering |
---|
47 | # these are simply objects displayed in datatable |
---|
48 | # You should not filter data returned here by any filter values entered by user. This is because |
---|
49 | # we need some base queryset to count total number of records. |
---|
50 | return self.model.objects.all() |
---|
51 | |
---|
52 | def prepare_results(self, qs): |
---|
53 | """! |
---|
54 | Prepara la data para mostrar en el datatable |
---|
55 | @return: Objeto json con los datos de los usuarios |
---|
56 | """ |
---|
57 | # prepare list with output column data |
---|
58 | json_data = [] |
---|
59 | for item in qs: |
---|
60 | user = "<a data-toggle='modal' data-target='#myModal' \ |
---|
61 | class='btn btn-block btn-info btn-xs fa fa-edit' \ |
---|
62 | onclick='modal_user(%s)'>%s</a>\ |
---|
63 | " % (str(item.pk), str(item.username)) |
---|
64 | if item.last_login: |
---|
65 | last_login = item.last_login.strftime("%Y-%m-%d %H:%M:%S") |
---|
66 | else: |
---|
67 | last_login = "No ha ingresado" |
---|
68 | if item.is_active: |
---|
69 | activo = "Activo" |
---|
70 | activar = "<input type='checkbox' id='user-" + str(item.pk) + "' value='" + str(item.pk) + "' name='inactivar' onclick='$(\"#forma_activar\").submit();'/>\ |
---|
71 | <label for='user-" + str(item.pk) + "'>\ |
---|
72 | <img src='" + settings.MEDIA_URL +\ |
---|
73 | "imagenes/inactivar.png' id='inactivo'\ |
---|
74 | title='Inactivar Usuario' \ |
---|
75 | />\ |
---|
76 | </label>" |
---|
77 | |
---|
78 | else: |
---|
79 | activo = "Inactivo" |
---|
80 | activar = "<input type='checkbox' id='user-" + str(item.pk) + "' value='" + str(item.pk) + "' name='activar' onclick='$(\"#forma_activar\").submit();'/>\ |
---|
81 | <label for='user-" + str(item.pk) + "'>\ |
---|
82 | <img src='" + settings.MEDIA_URL + \ |
---|
83 | "imagenes/activar.png' id='activo' \ |
---|
84 | title='Activar Usuario'/>\ |
---|
85 | </label>" |
---|
86 | try: |
---|
87 | grupos = item.groups.all() |
---|
88 | grupo = [] |
---|
89 | if len(grupos) > 1: |
---|
90 | for g in grupos: |
---|
91 | grupo += str(g), |
---|
92 | else: |
---|
93 | grupo = str(item.groups.get()) |
---|
94 | except: |
---|
95 | grupo = "No pertenece a un grupo" |
---|
96 | json_data.append([ |
---|
97 | user, |
---|
98 | "{0} {1}".format(str(item.first_name), str(item.last_name)), |
---|
99 | item.email, |
---|
100 | item.date_joined.strftime("%Y-%m-%d %H:%M:%S"), |
---|
101 | last_login, |
---|
102 | activo, |
---|
103 | grupo, |
---|
104 | activar |
---|
105 | ]) |
---|
106 | grupo = "" |
---|
107 | return json_data |
---|