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