|
@@ -8,29 +8,31 @@ from django.utils.http import urlquote_plus
|
|
from django.forms import ModelForm
|
|
from django.forms import ModelForm
|
|
from django.template import loader
|
|
from django.template import loader
|
|
from django.http import HttpResponse
|
|
from django.http import HttpResponse
|
|
-from rapid.registry import registry, Action, MenuEntry
|
|
|
|
from rapid.wrappers import InstanceData, ModelData, FieldData
|
|
from rapid.wrappers import InstanceData, ModelData, FieldData
|
|
-from rapid import permissions
|
|
|
|
from rapid.widgets import RapidSelector, RapidRelationReadOnly
|
|
from rapid.widgets import RapidSelector, RapidRelationReadOnly
|
|
from rapid.filters import FilterSet
|
|
from rapid.filters import FilterSet
|
|
import math
|
|
import math
|
|
|
|
|
|
|
|
+
|
|
def _build_uri(request, params):
|
|
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]
|
|
base = request.build_absolute_uri().split("?")[0]
|
|
return base + "?" + param_string
|
|
return base + "?" + param_string
|
|
|
|
|
|
|
|
+
|
|
def _copy_dict(dc):
|
|
def _copy_dict(dc):
|
|
d = {}
|
|
d = {}
|
|
for k in dc.keys():
|
|
for k in dc.keys():
|
|
d[k] = dc[k]
|
|
d[k] = dc[k]
|
|
return d
|
|
return d
|
|
|
|
|
|
|
|
+
|
|
def _replace_param(request, param_name, param_value):
|
|
def _replace_param(request, param_name, param_value):
|
|
get = _copy_dict(request.GET)
|
|
get = _copy_dict(request.GET)
|
|
get[param_name] = param_value
|
|
get[param_name] = param_value
|
|
return _build_uri(request, get)
|
|
return _build_uri(request, get)
|
|
|
|
|
|
|
|
+
|
|
def is_ajax_request(request):
|
|
def is_ajax_request(request):
|
|
# X-Requested-With: XMLHttpRequest
|
|
# X-Requested-With: XMLHttpRequest
|
|
w = request.META.get('HTTP_X_REQUESTED_WITH')
|
|
w = request.META.get('HTTP_X_REQUESTED_WITH')
|
|
@@ -38,6 +40,7 @@ def is_ajax_request(request):
|
|
return True
|
|
return True
|
|
return False
|
|
return False
|
|
|
|
|
|
|
|
+
|
|
class ListView(generic.list.ListView):
|
|
class ListView(generic.list.ListView):
|
|
template_name = 'rapid/bare/list.html'
|
|
template_name = 'rapid/bare/list.html'
|
|
action_name = ''
|
|
action_name = ''
|
|
@@ -47,7 +50,8 @@ class ListView(generic.list.ListView):
|
|
fields = None
|
|
fields = None
|
|
|
|
|
|
class Pagination:
|
|
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.request = request
|
|
self.number_of_edge_pages = number_of_edge_pages
|
|
self.number_of_edge_pages = number_of_edge_pages
|
|
self.number_of_middle_pages = number_of_middle_pages
|
|
self.number_of_middle_pages = number_of_middle_pages
|
|
@@ -63,7 +67,7 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def start_numbers(self):
|
|
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
|
|
e = self.number_of_edge_pages
|
|
for i in range(1, min(e+1, self.page)):
|
|
for i in range(1, min(e+1, self.page)):
|
|
@@ -71,13 +75,13 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def start(self):
|
|
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())
|
|
return self._page_and_uri(self.start_numbers())
|
|
|
|
|
|
def before_numbers(self):
|
|
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
|
|
m = self.number_of_middle_pages
|
|
for i in range(max(self.page - m, m), self.page):
|
|
for i in range(max(self.page - m, m), self.page):
|
|
@@ -85,13 +89,13 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def before(self):
|
|
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())
|
|
return self._page_and_uri(self.before_numbers())
|
|
|
|
|
|
def after_numbers(self):
|
|
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
|
|
m = self.number_of_middle_pages
|
|
e = self.number_of_edge_pages
|
|
e = self.number_of_edge_pages
|
|
@@ -100,13 +104,13 @@ class ListView(generic.list.ListView):
|
|
|
|
|
|
def after(self):
|
|
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())
|
|
return self._page_and_uri(self.after_numbers())
|
|
|
|
|
|
def end_numbers(self):
|
|
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
|
|
e = self.number_of_edge_pages
|
|
for i in range(max(self.page + 1, self.total_pages - e + 1), self.total_pages + 1):
|
|
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):
|
|
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())
|
|
return self._page_and_uri(self.end_numbers())
|
|
|
|
|
|
def separate_end(self):
|
|
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
|
|
return self.page < self.total_pages - self.number_of_edge_pages - self.number_of_middle_pages
|
|
|
|
|
|
def separate_start(self):
|
|
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
|
|
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)
|
|
mdata = ModelData(self.model, request)
|
|
if not mdata.has_permission(request, self.action_name):
|
|
if not mdata.has_permission(request, self.action_name):
|
|
raise PermissionDenied
|
|
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()
|
|
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 = RequestContext(request).flatten()
|
|
context.update(kwargs)
|
|
context.update(kwargs)
|
|
total_pages = int(math.ceil(object_list.count() / self.registers_per_page))
|
|
total_pages = int(math.ceil(object_list.count() / self.registers_per_page))
|
|
page = int(request.GET.get('page', 1))
|
|
page = int(request.GET.get('page', 1))
|
|
p = self.Pagination(request, page, self.number_of_edge_pages,
|
|
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['pages'] = p
|
|
context['model'] = ModelData(self.model, request, fields=self.fields)
|
|
context['model'] = ModelData(self.model, request, fields=self.fields)
|
|
context['view'] = self.View(request, self.action_name, self.model, object_list, self.fields, p)
|
|
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
|
|
offset = (page - 1) * self.registers_per_page
|
|
self.object_list = object_list[offset: offset + 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[self.get_context_object_name(object_list)] = object_list
|
|
context['object_list'] = object_list
|
|
context['object_list'] = object_list
|
|
context = self.get_context_data(**context)
|
|
context = self.get_context_data(**context)
|
|
|
|
+ # noinspection PyAttributeOutsideInit
|
|
self.object_list = object_list
|
|
self.object_list = object_list
|
|
|
|
|
|
return self.render_to_response(context)
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
+
|
|
class ReadView(generic.detail.DetailView):
|
|
class ReadView(generic.detail.DetailView):
|
|
template_name = 'rapid/bare/detail.html'
|
|
template_name = 'rapid/bare/detail.html'
|
|
action_name = ''
|
|
action_name = ''
|
|
@@ -229,6 +229,7 @@ class ReadView(generic.detail.DetailView):
|
|
def get_object(self, request=None):
|
|
def get_object(self, request=None):
|
|
return get_object_or_404(self.model, pk=self.kwargs['pk'])
|
|
return get_object_or_404(self.model, pk=self.kwargs['pk'])
|
|
|
|
|
|
|
|
+ # noinspection PyMethodOverriding
|
|
def get(self, request, pk, **kwargs):
|
|
def get(self, request, pk, **kwargs):
|
|
context = RequestContext(request).flatten()
|
|
context = RequestContext(request).flatten()
|
|
context.update(kwargs)
|
|
context.update(kwargs)
|
|
@@ -243,6 +244,7 @@ class ReadView(generic.detail.DetailView):
|
|
excludes = request.GET.get('exclude')
|
|
excludes = request.GET.get('exclude')
|
|
if excludes:
|
|
if excludes:
|
|
cd.excludes = excludes.split(",")
|
|
cd.excludes = excludes.split(",")
|
|
|
|
+ # noinspection PyAttributeOutsideInit
|
|
self.object = cd
|
|
self.object = cd
|
|
context['object_data'] = cd
|
|
context['object_data'] = cd
|
|
|
|
|
|
@@ -250,6 +252,7 @@ class ReadView(generic.detail.DetailView):
|
|
|
|
|
|
return self.render_to_response(context)
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
+
|
|
def _get_form(request, model):
|
|
def _get_form(request, model):
|
|
default_relations_bare = request.GET.get('default')
|
|
default_relations_bare = request.GET.get('default')
|
|
widgets = []
|
|
widgets = []
|
|
@@ -266,9 +269,9 @@ def _get_form(request, model):
|
|
for f in ModelData(model).local_fields():
|
|
for f in ModelData(model).local_fields():
|
|
if f.is_relation() and unicode(f.bare_name()) not in default_relations_fields:
|
|
if f.is_relation() and unicode(f.bare_name()) not in default_relations_fields:
|
|
ask_relations.append(f)
|
|
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_model = model
|
|
form_widgets = dict(widgets)
|
|
form_widgets = dict(widgets)
|
|
|
|
|
|
@@ -287,7 +290,6 @@ def _get_form(request, model):
|
|
return CForm
|
|
return CForm
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
class CreateView(generic.edit.CreateView):
|
|
class CreateView(generic.edit.CreateView):
|
|
template_name = 'rapid/bare/create.html'
|
|
template_name = 'rapid/bare/create.html'
|
|
action_name = ''
|
|
action_name = ''
|
|
@@ -308,9 +310,6 @@ class CreateView(generic.edit.CreateView):
|
|
raise PermissionDenied
|
|
raise PermissionDenied
|
|
context['model_data'] = cd
|
|
context['model_data'] = cd
|
|
|
|
|
|
- parent_model = self.model
|
|
|
|
- parent_fields = self.fields
|
|
|
|
-
|
|
|
|
if request.POST:
|
|
if request.POST:
|
|
context['form'] = self.request_form(request)(request.POST, request.FILEs)
|
|
context['form'] = self.request_form(request)(request.POST, request.FILEs)
|
|
else:
|
|
else:
|
|
@@ -330,6 +329,7 @@ class CreateView(generic.edit.CreateView):
|
|
return self.get(request, **kwargs)
|
|
return self.get(request, **kwargs)
|
|
raise PermissionDenied()
|
|
raise PermissionDenied()
|
|
|
|
|
|
|
|
+
|
|
class UpdateView(generic.edit.UpdateView):
|
|
class UpdateView(generic.edit.UpdateView):
|
|
template_name = 'rapid/bare/update.html'
|
|
template_name = 'rapid/bare/update.html'
|
|
action_name = ''
|
|
action_name = ''
|
|
@@ -342,6 +342,7 @@ class UpdateView(generic.edit.UpdateView):
|
|
def request_form(self, request):
|
|
def request_form(self, request):
|
|
return _get_form(request, self.model)
|
|
return _get_form(request, self.model)
|
|
|
|
|
|
|
|
+ # noinspection PyMethodOverriding
|
|
def get(self, request, pk, **kwargs):
|
|
def get(self, request, pk, **kwargs):
|
|
context = RequestContext(request).flatten()
|
|
context = RequestContext(request).flatten()
|
|
context.update(kwargs)
|
|
context.update(kwargs)
|
|
@@ -353,6 +354,7 @@ class UpdateView(generic.edit.UpdateView):
|
|
cd = InstanceData(obj, request=request)
|
|
cd = InstanceData(obj, request=request)
|
|
if not cd.has_permission(request, self.action_name):
|
|
if not cd.has_permission(request, self.action_name):
|
|
raise PermissionDenied
|
|
raise PermissionDenied
|
|
|
|
+ # noinspection PyAttributeOutsideInit
|
|
self.object = cd
|
|
self.object = cd
|
|
context['object_data'] = cd
|
|
context['object_data'] = cd
|
|
|
|
|
|
@@ -365,6 +367,7 @@ class UpdateView(generic.edit.UpdateView):
|
|
|
|
|
|
return self.render_to_response(context)
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
+ # noinspection PyMethodOverriding,PyAttributeOutsideInit
|
|
def post(self, request, pk, **kwargs):
|
|
def post(self, request, pk, **kwargs):
|
|
obj = self.get_object()
|
|
obj = self.get_object()
|
|
m = InstanceData(obj, request=request)
|
|
m = InstanceData(obj, request=request)
|
|
@@ -408,8 +411,7 @@ class DeleteView(generic.edit.DeleteView):
|
|
|
|
|
|
class SelectView(ListView):
|
|
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'
|
|
template_name = 'rapid/bare/select.html'
|
|
|
|
|
|
@@ -421,7 +423,7 @@ def bare_or_main(view):
|
|
resp = view(request, *args, **kwargs)
|
|
resp = view(request, *args, **kwargs)
|
|
bare = is_ajax_request(request)
|
|
bare = is_ajax_request(request)
|
|
if bare:
|
|
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,
|
|
# I'll interpret redirects as successful POST,
|
|
# thus, the response'll get replaced by something with
|
|
# thus, the response'll get replaced by something with
|
|
# a header that says "success!"
|
|
# a header that says "success!"
|
|
@@ -438,9 +440,9 @@ def bare_or_main(view):
|
|
return resp
|
|
return resp
|
|
return vw
|
|
return vw
|
|
|
|
|
|
|
|
+
|
|
def update_form_class(form):
|
|
def update_form_class(form):
|
|
class F(UpdateView):
|
|
class F(UpdateView):
|
|
def request_form(self, request):
|
|
def request_form(self, request):
|
|
return form
|
|
return form
|
|
return F
|
|
return F
|
|
-
|
|
|