|
@@ -42,8 +42,8 @@ class ListView(generic.list.ListView):
|
|
|
template_name = 'rapid/bare/list.html'
|
|
|
action_name = ''
|
|
|
registers_per_page = 50
|
|
|
- number_of_edge_pages = 3
|
|
|
- number_of_middle_pages = 3
|
|
|
+ number_of_edge_pages = 5
|
|
|
+ number_of_middle_pages = 5
|
|
|
fields = None
|
|
|
|
|
|
class Pagination:
|
|
@@ -66,7 +66,7 @@ class ListView(generic.list.ListView):
|
|
|
Números das páginas que serão listadas no começo da paginação.
|
|
|
"""
|
|
|
e = self.number_of_edge_pages
|
|
|
- for i in range(1, min(e, self.page - 1)):
|
|
|
+ for i in range(1, min(e+1, self.page)):
|
|
|
yield i
|
|
|
|
|
|
def start(self):
|
|
@@ -80,7 +80,7 @@ class ListView(generic.list.ListView):
|
|
|
Números das páginas que serão listadas na paginação antes da atual.
|
|
|
"""
|
|
|
m = self.number_of_middle_pages
|
|
|
- for i in range(max(self.page - m, m), self.page - 1):
|
|
|
+ for i in range(max(self.page - m, m), self.page):
|
|
|
yield i
|
|
|
|
|
|
def before(self):
|
|
@@ -95,7 +95,7 @@ class ListView(generic.list.ListView):
|
|
|
"""
|
|
|
m = self.number_of_middle_pages
|
|
|
e = self.number_of_edge_pages
|
|
|
- for i in range(self.page + 1, min(self.page + m, self.total_pages - e)):
|
|
|
+ for i in range(self.page + 1, min(self.page + m + 1, self.total_pages - e + 1)):
|
|
|
yield i
|
|
|
|
|
|
def after(self):
|
|
@@ -109,7 +109,7 @@ class ListView(generic.list.ListView):
|
|
|
Números das páginas que serão listadas no final da paginação.
|
|
|
"""
|
|
|
e = self.number_of_edge_pages
|
|
|
- for i in range(max(self.page + 1, self.total_pages - e), self.total_pages):
|
|
|
+ for i in range(max(self.page + 1, self.total_pages - e + 1), self.total_pages + 1):
|
|
|
yield i
|
|
|
|
|
|
def end(self):
|
|
@@ -131,7 +131,7 @@ class ListView(generic.list.ListView):
|
|
|
return self.page > self.number_of_edge_pages + self.number_of_middle_pages
|
|
|
|
|
|
def get_page(self, queryset):
|
|
|
- return queryset[self.registers_per_page*self.page:self.registers_per_page*(self.page+1)-1]
|
|
|
+ return queryset[self.registers_per_page*(self.page-1):self.registers_per_page*self.page]
|
|
|
|
|
|
class View:
|
|
|
order_param = 'order'
|
|
@@ -149,6 +149,9 @@ class ListView(generic.list.ListView):
|
|
|
q = q.filter(**self.filters.query_dict())
|
|
|
self.queryset = q
|
|
|
|
|
|
+ total = q.count()
|
|
|
+ pagination.total_pages = int(math.ceil(total / pagination.registers_per_page))
|
|
|
+
|
|
|
order = request.GET.get(self.order_param)
|
|
|
if order:
|
|
|
q = q.order_by(order)
|
|
@@ -199,7 +202,7 @@ class ListView(generic.list.ListView):
|
|
|
# da listagem nos SA.
|
|
|
context = RequestContext(request).flatten()
|
|
|
context.update(kwargs)
|
|
|
- total_pages = int(math.ceil(object_list.count() / self.registers_per_page)) + 1
|
|
|
+ total_pages = int(math.ceil(object_list.count() / self.registers_per_page))
|
|
|
page = int(request.GET.get('page', 1))
|
|
|
p = self.Pagination(request, page, self.number_of_edge_pages,
|
|
|
self.number_of_middle_pages, self.registers_per_page,
|
|
@@ -268,7 +271,7 @@ def _get_form(request, model):
|
|
|
for f in ModelData(model).local_fields():
|
|
|
if f.is_relation() and unicode(f.bare_name()) not in default_relations_fields:
|
|
|
ask_relations.append(f)
|
|
|
- widgets += [(f.bare_name(), RapidSelector(f)) for f in ask_relations if f.related_model().is_controlled()]
|
|
|
+ widgets += [(f.bare_name(), RapidSelector(f)) for f in ask_relations if f.related_model().has_permission(request, 'select')]
|
|
|
#ModelForm.Meta tem atributos com esses mesmos nomes,
|
|
|
#então eu tenho que renomear.
|
|
|
form_model = model
|
|
@@ -314,7 +317,7 @@ class CreateView(generic.edit.CreateView):
|
|
|
parent_fields = self.fields
|
|
|
|
|
|
if request.POST:
|
|
|
- context['form'] = self.request_form(request)(request.POST)
|
|
|
+ context['form'] = self.request_form(request)(request.POST, request.FILEs)
|
|
|
else:
|
|
|
context['form'] = self.request_form(request)()
|
|
|
|
|
@@ -325,7 +328,7 @@ class CreateView(generic.edit.CreateView):
|
|
|
def post(self, request, **kwargs):
|
|
|
m = ModelData(self.model, request=request)
|
|
|
if m.has_permission(request, self.action_name):
|
|
|
- f = self.request_form(request)(request.POST)
|
|
|
+ f = self.request_form(request)(request.POST, request.FILES)
|
|
|
if f.is_valid():
|
|
|
f.save()
|
|
|
return redirect(m.list_url())
|
|
@@ -359,7 +362,7 @@ class UpdateView(generic.edit.UpdateView):
|
|
|
context['object_data'] = cd
|
|
|
|
|
|
if request.POST:
|
|
|
- context['form'] = self.request_form(request)(request.POST, instance=obj)
|
|
|
+ context['form'] = self.request_form(request)(request.POST, request.FILES, instance=obj)
|
|
|
else:
|
|
|
context['form'] = self.request_form(request)(instance=obj)
|
|
|
|
|
@@ -371,7 +374,7 @@ class UpdateView(generic.edit.UpdateView):
|
|
|
obj = self.get_object()
|
|
|
m = InstanceData(obj, request=request)
|
|
|
if m.has_permission(request, self.action_name):
|
|
|
- f = self.request_form(request)(request.POST, instance=obj)
|
|
|
+ f = self.request_form(request)(request.POST, request.FILES, instance=obj)
|
|
|
if f.is_valid():
|
|
|
if f.instance.pk != obj.pk:
|
|
|
raise PermissionDenied
|
|
@@ -440,56 +443,9 @@ def bare_or_main(view):
|
|
|
return resp
|
|
|
return vw
|
|
|
|
|
|
-def _rvw(view_class, action_name):
|
|
|
- view_class.action_name = action_name
|
|
|
-
|
|
|
- def vw(model):
|
|
|
- return bare_or_main(view_class.as_view(model=model))
|
|
|
- return vw
|
|
|
-
|
|
|
-default_actions = [
|
|
|
- (False, Action("list", "", [], _rvw(ListView, 'list'), "listar", "fa-list")),
|
|
|
- (False, Action("view", "(?P<pk>[0-9]+)", ['pk'], _rvw(ReadView, 'view'), "ver", "fa-eye")),
|
|
|
- (True, Action("edit", "(?P<pk>[0-9]+)", ['pk'], _rvw(UpdateView, 'edit'), "editar", "fa-pencil")),
|
|
|
- (True, Action("add", "", [], _rvw(CreateView, 'add'), "adicionar", "fa-plus")),
|
|
|
- (False, Action("select", "", [], _rvw(SelectView, 'select'), "selecionar", "fa-hand-o-up",
|
|
|
- visibility=Action.Visibility.hidden)),
|
|
|
-]
|
|
|
-
|
|
|
-def register_model(model, name=None, read_set=None, write_set=None, can_erase=False):
|
|
|
- if not read_set:
|
|
|
- read_set = permissions.default_read(model)
|
|
|
- if not write_set:
|
|
|
- write_set = permissions.default_write(model)
|
|
|
- ret = []
|
|
|
- for edt, a in default_actions:
|
|
|
- if edt:
|
|
|
- ret.append(registry.register_action(a, MenuEntry(model, write_set, name=name)))
|
|
|
- else:
|
|
|
- ret.append(registry.register_action(a, MenuEntry(model, read_set, name=name)))
|
|
|
- if can_erase:
|
|
|
- a = Action("delete", "(?P<pk>[0-9]+)", ['pk'], _rvw(DeleteView, 'delete'), "apagar", "fa-trash-o")
|
|
|
- ret.append(registry.register_action(a, MenuEntry(model, write_set, name=name)))
|
|
|
- return [u for u in ret if u]
|
|
|
-
|
|
|
def update_form_class(form):
|
|
|
class F(UpdateView):
|
|
|
def request_form(self, request):
|
|
|
return form
|
|
|
return F
|
|
|
|
|
|
-def register_instance_form(model, action_name, menu_name, form, permission_set, icon=None, visibility=None):
|
|
|
- if not icon:
|
|
|
- icon = ''
|
|
|
- a = Action(action_name, "(?P<pk>[0-9]+)", ['pk'], _rvw(update_form_class(form), action_name),
|
|
|
- menu_name, icon, visibility)
|
|
|
- u = registry.register_action(a, MenuEntry(model, permission_set))
|
|
|
- return [u] if u else []
|
|
|
-
|
|
|
-def register_simple_select(model, visible_fields, permission_set, name=None):
|
|
|
- class Vw(SelectView):
|
|
|
- fields = visible_fields
|
|
|
- a = Action("select", "", [], _rvw(Vw, 'select'), "selecionar", "fa-hand-o-up",
|
|
|
- visibility=Action.Visibility.hidden)
|
|
|
- u = registry.register_action(a, MenuEntry(model, permission_set, name=name))
|
|
|
- return [u] if u else []
|