def prepare_read(context, using): pk = context.get('pk') queryset = context.get('queryset') if queryset: # TODO Check queryset model permission model = queryset.model else: model = get_model(context) queryset = model._default_manager count = queryset start = int(context.get('start', '0')) limit = int(context.get('limit', '1')) + start if pk: queryset = queryset.using(using).filter(pk=pk) else: queryset = queryset.using(using).all() if not 'all' in context: queryset = queryset[start:limit] if 'total' in context: count = count.using(using).all().count() else: count = None fields = ['pk', '__str__'] + context.get('fields', _read_fields(model)) disp_fields = _choice_fields(model) display_fn = _display_fn(model) rows = [{f: field_text(getattr(row, f), row, f, disp_fields.get(f, f), display_fn=display_fn.get(f, str)) for f in fields} for row in queryset] return {'items': rows, 'total': count}
def duplicate(self, request, obj): """ Return model object copy. """ from keops.db import models self._prepare_form() obj = copy.copy(obj) r = {'pk': None} for field in self.model_fields: if not field.primary_key and not isinstance(field, (models.ManyToManyField, models.OneToManyField)): r[field.name] = field_text(getattr(obj, field.name)) return HttpJsonResponse(r)
def new_item(self, request): from keops.db import models r = {'pk': None} for field in self.model_fields: if field.default != models.NOT_PROVIDED: if callable(field.default): v = field.default() else: v = field.default else: v = None r[field.name] = field_text(v) return HttpJsonResponse(r)
def grid(request): # TODO use grid method for specific model admin using = get_db(request) model = get_model(request.GET) pk = request.GET.get('pk') query = request.GET.get('query') field = request.GET.get('field') # Check related field disp_fields = {} if field: obj = model.objects.using(using).get(pk=pk) queryset = getattr(obj, field) field = getattr(model, field) model = field.related.model fields = field.list_fields else: queryset = model.objects.using(using) if hasattr(model, 'Extra'): fields = model.Extra.field_groups.get('list_fields') disp_fields = _choice_fields(model) else: fields = None if isinstance(fields, tuple): fields = list(fields) fields = fields or [f.name for f in model._meta.concrete_fields if not f.primary_key] start = int(request.GET.get('start', '0')) limit = int(request.GET.get('limit', '50')) + start # settings count = request.GET.get('total', False) if query: queryset = search_text(queryset, query) if count: count = queryset.all().count() else: count = None queryset = queryset.all()[start:limit] # TODO Check content type permissions permissions get_val = lambda x: isinstance(x, datetime.date) and field_text(x) or ('' if x is None else (callable(x) and x()) or x) fields = ['pk'] + fields display_fn = _display_fn(model) rows = [{f: display_fn.get(f, str)(get_val(getattr(row, disp_fields.get(f, f)))) for f in fields} for row in queryset] data = {'items': rows, 'total': count} return HttpJsonResponse(data)
def lookup(self, request, sel_fields=None, display_fn=str): """ Read lookup data list. """ from keops.views import db context = request.GET field = context.get('field') form_field = self.get_formfield(field).field queryset = form_field.queryset start = int(context.get('start', '0')) limit = int(context.get('limit', '25')) + start # settings query = context.get('query', '') fields = form_field.target_attr.custom_attrs.fields if query: queryset = db.search_text(queryset, query) data = { 'data': [field_text(obj, sel_fields=sel_fields.get(field), display_fn=display_fn) for obj in queryset[start:limit]], 'total': queryset.count() } return HttpJsonResponse(data)
def _get_initial_data(form): def eval_default(val): if isinstance(val, str): context = {'value': None} exec(val, globals(), context) return context['value'] items = form._params['items'] initial = {} for param in items: v = None if 'default' in param: v = eval_default(param['default']) elif 'value' in param: v = param['value'] if isinstance(v, (list, tuple)): v = [field_text(x) for x in v] if not v is None: initial[param['name']] = v return initial