|
@@ -8,29 +8,31 @@ from django.utils.http import urlquote_plus
|
|
|
from django.forms import ModelForm
|
|
|
from django.template import loader
|
|
|
from django.http import HttpResponse
|
|
|
-from rapid.registry import registry, Action, MenuEntry
|
|
|
from rapid.wrappers import InstanceData, ModelData, FieldData
|
|
|
-from rapid import permissions
|
|
|
from rapid.widgets import RapidSelector, RapidRelationReadOnly
|
|
|
from rapid.filters import FilterSet
|
|
|
import math
|
|
|
|
|
|
+
|
|
|
def _build_uri(request, params):
|
|
|
- param_string = "&".join(["%s=%s"%(urlquote_plus(k), urlquote_plus(params[k])) for k in params.keys()])
|
|
|
+ param_string = "&".join(["%s=%s" % (urlquote_plus(k), urlquote_plus(params[k])) for k in params.keys()])
|
|
|
base = request.build_absolute_uri().split("?")[0]
|
|
|
return base + "?" + param_string
|
|
|
|
|
|
+
|
|
|
def _copy_dict(dc):
|
|
|
d = {}
|
|
|
for k in dc.keys():
|
|
|
d[k] = dc[k]
|
|
|
return d
|
|
|
|
|
|
+
|
|
|
def _replace_param(request, param_name, param_value):
|
|
|
get = _copy_dict(request.GET)
|
|
|
get[param_name] = param_value
|
|
|
return _build_uri(request, get)
|
|
|
|
|
|
+
|
|
|
def is_ajax_request(request):
|
|
|
# X-Requested-With: XMLHttpRequest
|
|
|
w = request.META.get('HTTP_X_REQUESTED_WITH')
|
|
@@ -38,6 +40,7 @@ def is_ajax_request(request):
|
|
|
return True
|
|
|
return False
|
|
|
|
|
|
+
|
|
|
class ListView(generic.list.ListView):
|
|
|
template_name = 'rapid/bare/list.html'
|
|
|
action_name = ''
|
|
@@ -47,7 +50,8 @@ class ListView(generic.list.ListView):
|
|
|
fields = None
|
|
|
|
|
|
class Pagination:
|
|
|
- def __init__(self, request, page, number_of_edge_pages, number_of_middle_pages, registers_per_page, total_pages):
|
|
|
+ def __init__(self, request, page, number_of_edge_pages, number_of_middle_pages,
|
|
|
+ registers_per_page, total_pages):
|
|
|
self.request = request
|
|
|
self.number_of_edge_pages = number_of_edge_pages
|
|
|
self.number_of_middle_pages = number_of_middle_pages
|
|
@@ -63,7 +67,7 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def start_numbers(self):
|
|
|
"""
|
|
|
- Números das páginas que serão listadas no começo da paginação.
|
|
|
+ Page numbers listed at the beginning of the pagination area
|
|
|
"""
|
|
|
e = self.number_of_edge_pages
|
|
|
for i in range(1, min(e+1, self.page)):
|
|
@@ -71,13 +75,13 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def start(self):
|
|
|
"""
|
|
|
- Números e URL páginas que serão listadas no começo da paginação.
|
|
|
+ Page numbers and URLs listed at the beginning of the pagination area
|
|
|
"""
|
|
|
return self._page_and_uri(self.start_numbers())
|
|
|
|
|
|
def before_numbers(self):
|
|
|
"""
|
|
|
- Números das páginas que serão listadas na paginação antes da atual.
|
|
|
+ Page numbers listed at the pagination area before the current page
|
|
|
"""
|
|
|
m = self.number_of_middle_pages
|
|
|
for i in range(max(self.page - m, m), self.page):
|
|
@@ -85,13 +89,13 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def before(self):
|
|
|
"""
|
|
|
- Números e URL das páginas que serão listadas na paginação antes da atual.
|
|
|
+ Page numbers and URLs listed at the pagination area before the current page
|
|
|
"""
|
|
|
return self._page_and_uri(self.before_numbers())
|
|
|
|
|
|
def after_numbers(self):
|
|
|
"""
|
|
|
- Números das páginas que serão listadas na paginação depois da atual.
|
|
|
+ Page numbers listed at the pagination area after the current page
|
|
|
"""
|
|
|
m = self.number_of_middle_pages
|
|
|
e = self.number_of_edge_pages
|
|
@@ -100,13 +104,13 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def after(self):
|
|
|
"""
|
|
|
- Números e URL das páginas que serão listadas na paginação depois da atual.
|
|
|
+ Page numbers and URLs listed at the pagination area after the current page
|
|
|
"""
|
|
|
return self._page_and_uri(self.after_numbers())
|
|
|
|
|
|
def end_numbers(self):
|
|
|
"""
|
|
|
- Números das páginas que serão listadas no final da paginação.
|
|
|
+ Page numbers listed at the end of the pagination area
|
|
|
"""
|
|
|
e = self.number_of_edge_pages
|
|
|
for i in range(max(self.page + 1, self.total_pages - e + 1), self.total_pages + 1):
|
|
@@ -114,19 +118,19 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def end(self):
|
|
|
"""
|
|
|
- Números e URLs das páginas que serão listadas no final da paginação.
|
|
|
+ Page numbers and URLs listed at the end of the pagination area
|
|
|
"""
|
|
|
return self._page_and_uri(self.end_numbers())
|
|
|
|
|
|
def separate_end(self):
|
|
|
"""
|
|
|
- Indica se deve haver um separador entre o começo da paginação e as páginas antes da atual.
|
|
|
+ True if there's a separator between the next pages and the end pages.
|
|
|
"""
|
|
|
return self.page < self.total_pages - self.number_of_edge_pages - self.number_of_middle_pages
|
|
|
|
|
|
def separate_start(self):
|
|
|
"""
|
|
|
- Indica se deve haver um separador entre as páginas depois da atual e o fim da paginação.
|
|
|
+ True if there's a separator between the start pages and the before pages.
|
|
|
"""
|
|
|
return self.page > self.number_of_edge_pages + self.number_of_middle_pages
|
|
|
|
|
@@ -193,34 +197,30 @@ class ListView(generic.list.ListView):
|
|
|
mdata = ModelData(self.model, request)
|
|
|
if not mdata.has_permission(request, self.action_name):
|
|
|
raise PermissionDenied
|
|
|
- #De forma similar ao get de ListView, recupera os objetos e verifica
|
|
|
- #se pode mostrar uma lista vazia.
|
|
|
object_list = self.get_queryset()
|
|
|
- allow_empty = self.get_allow_empty()
|
|
|
|
|
|
- # Agora, popula o contexto com os request processors e os dados específicos
|
|
|
- # da listagem nos SA.
|
|
|
+ # Populates the context
|
|
|
context = RequestContext(request).flatten()
|
|
|
context.update(kwargs)
|
|
|
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,
|
|
|
- total_pages)
|
|
|
+ self.number_of_middle_pages, self.registers_per_page,
|
|
|
+ total_pages)
|
|
|
context['pages'] = p
|
|
|
context['model'] = ModelData(self.model, request, fields=self.fields)
|
|
|
context['view'] = self.View(request, self.action_name, self.model, object_list, self.fields, p)
|
|
|
- default_ordering = self.model._meta.ordering if hasattr(self.model._meta, 'ordering') else ''
|
|
|
- ordering = request.GET.get('order', default_ordering)
|
|
|
offset = (page - 1) * self.registers_per_page
|
|
|
self.object_list = object_list[offset: offset + self.registers_per_page]
|
|
|
context[self.get_context_object_name(object_list)] = object_list
|
|
|
context['object_list'] = object_list
|
|
|
context = self.get_context_data(**context)
|
|
|
+ # noinspection PyAttributeOutsideInit
|
|
|
self.object_list = object_list
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
+
|
|
|
class ReadView(generic.detail.DetailView):
|
|
|
template_name = 'rapid/bare/detail.html'
|
|
|
action_name = ''
|
|
@@ -229,6 +229,7 @@ class ReadView(generic.detail.DetailView):
|
|
|
def get_object(self, request=None):
|
|
|
return get_object_or_404(self.model, pk=self.kwargs['pk'])
|
|
|
|
|
|
+ # noinspection PyMethodOverriding
|
|
|
def get(self, request, pk, **kwargs):
|
|
|
context = RequestContext(request).flatten()
|
|
|
context.update(kwargs)
|
|
@@ -243,6 +244,7 @@ class ReadView(generic.detail.DetailView):
|
|
|
excludes = request.GET.get('exclude')
|
|
|
if excludes:
|
|
|
cd.excludes = excludes.split(",")
|
|
|
+ # noinspection PyAttributeOutsideInit
|
|
|
self.object = cd
|
|
|
context['object_data'] = cd
|
|
|
|
|
@@ -250,6 +252,7 @@ class ReadView(generic.detail.DetailView):
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
+
|
|
|
def _get_form(request, model):
|
|
|
default_relations_bare = request.GET.get('default')
|
|
|
widgets = []
|
|
@@ -266,9 +269,9 @@ 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().has_permission(request, 'select')]
|
|
|
- #ModelForm.Meta tem atributos com esses mesmos nomes,
|
|
|
- #então eu tenho que renomear.
|
|
|
+ widgets += [(f.bare_name(), RapidSelector(f)) for f in ask_relations if
|
|
|
+ f.related_model().has_permission(request, 'select')]
|
|
|
+ # ModelForm.Meta has attributes with the same names, thus I'll rename them
|
|
|
form_model = model
|
|
|
form_widgets = dict(widgets)
|
|
|
|
|
@@ -287,7 +290,6 @@ def _get_form(request, model):
|
|
|
return CForm
|
|
|
|
|
|
|
|
|
-
|
|
|
class CreateView(generic.edit.CreateView):
|
|
|
template_name = 'rapid/bare/create.html'
|
|
|
action_name = ''
|
|
@@ -308,9 +310,6 @@ class CreateView(generic.edit.CreateView):
|
|
|
raise PermissionDenied
|
|
|
context['model_data'] = cd
|
|
|
|
|
|
- parent_model = self.model
|
|
|
- parent_fields = self.fields
|
|
|
-
|
|
|
if request.POST:
|
|
|
context['form'] = self.request_form(request)(request.POST, request.FILEs)
|
|
|
else:
|
|
@@ -330,6 +329,7 @@ class CreateView(generic.edit.CreateView):
|
|
|
return self.get(request, **kwargs)
|
|
|
raise PermissionDenied()
|
|
|
|
|
|
+
|
|
|
class UpdateView(generic.edit.UpdateView):
|
|
|
template_name = 'rapid/bare/update.html'
|
|
|
action_name = ''
|
|
@@ -342,6 +342,7 @@ class UpdateView(generic.edit.UpdateView):
|
|
|
def request_form(self, request):
|
|
|
return _get_form(request, self.model)
|
|
|
|
|
|
+ # noinspection PyMethodOverriding
|
|
|
def get(self, request, pk, **kwargs):
|
|
|
context = RequestContext(request).flatten()
|
|
|
context.update(kwargs)
|
|
@@ -353,6 +354,7 @@ class UpdateView(generic.edit.UpdateView):
|
|
|
cd = InstanceData(obj, request=request)
|
|
|
if not cd.has_permission(request, self.action_name):
|
|
|
raise PermissionDenied
|
|
|
+ # noinspection PyAttributeOutsideInit
|
|
|
self.object = cd
|
|
|
context['object_data'] = cd
|
|
|
|
|
@@ -365,6 +367,7 @@ class UpdateView(generic.edit.UpdateView):
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
+ # noinspection PyMethodOverriding,PyAttributeOutsideInit
|
|
|
def post(self, request, pk, **kwargs):
|
|
|
obj = self.get_object()
|
|
|
m = InstanceData(obj, request=request)
|
|
@@ -408,8 +411,7 @@ class DeleteView(generic.edit.DeleteView):
|
|
|
|
|
|
class SelectView(ListView):
|
|
|
"""
|
|
|
- Apresenta uma lista selecionavel de objetos, para
|
|
|
- o preenchimento de relações.
|
|
|
+ A selectable list of objects.
|
|
|
"""
|
|
|
template_name = 'rapid/bare/select.html'
|
|
|
|
|
@@ -421,7 +423,7 @@ def bare_or_main(view):
|
|
|
resp = view(request, *args, **kwargs)
|
|
|
bare = is_ajax_request(request)
|
|
|
if bare:
|
|
|
- if resp.status_code >= 300 and resp.status_code <= 399:
|
|
|
+ if 300 <= resp.status_code <= 399:
|
|
|
# I'll interpret redirects as successful POST,
|
|
|
# thus, the response'll get replaced by something with
|
|
|
# a header that says "success!"
|
|
@@ -438,9 +440,9 @@ def bare_or_main(view):
|
|
|
return resp
|
|
|
return vw
|
|
|
|
|
|
+
|
|
|
def update_form_class(form):
|
|
|
class F(UpdateView):
|
|
|
def request_form(self, request):
|
|
|
return form
|
|
|
return F
|
|
|
-
|