def listt(request, app, cls, subset=None): try: _model = apps.get_model(app, cls) except LookupError: return page_not_found(request) subsetp = None if subset: subset_func = getattr(_model.objects.all(), subset) can_view = subset_func._metadata['%s:can_view' % subset] else: tid = request.GET.get('tid') subsetp = request.GET.get('tab%s' % tid) if tid and subsetp: subset_func = getattr(_model.objects.get_queryset(), subsetp) can_view = subset_func._metadata['%s:can_view' % subsetp] if not permissions.check_group_or_permission(request, can_view): return httprr( request, '/admin/login/?next=%s' % request.get_full_path()) else: permission = '%s.list_%s' % (app, cls) if not request.user.has_perm(permission): return httprr( request, '/admin/login/?next=%s' % request.get_full_path()) qs = _model.objects.all(request.user) list_subsets = subset and [subset] or None if subset: title = getattr(getattr(qs, subset), '_metadata')['%s:title' % subset] else: title = u'%s' % get_metadata(_model, 'verbose_name_plural') paginator = Paginator(request, qs, title, list_subsets=list_subsets, is_list_view=True) response = paginator.get_response() if response: return response if _model in loader.class_actions: for group in loader.class_actions[_model]: for view_name in loader.class_actions[_model][group]: _action = loader.class_actions[_model][group][view_name] action_title = _action['title'] action_message = _action['message'] action_can_execute = _action['can_execute'] action_input = _action['input'] action_css = _action['css'] action_condition = _action['condition'] initial = _action['initial'] choices = _action['choices'] if subsetp: if subsetp not in loader.subset_actions[ _model] or view_name not in loader.subset_actions[ _model][subsetp]: continue else: if subset not in loader.subset_actions[ _model] or view_name not in loader.subset_actions[ _model][subset]: continue if permissions.check_group_or_permission( request, action_can_execute): func = hasattr(qs, view_name) and getattr( qs, view_name) or None if func: char = '?' in request.get_full_path() and '&' or '?' url = '%s%s%s' % (request.get_full_path(), char, '%s=' % view_name) has_input = func.func_code.co_argcount > 1 if not has_input: action_css = action_css.replace('popup', '') paginator.add_subset_action(action_title, url, action_css, None, action_condition) if view_name in request.GET: ids = paginator.get_selected_ids() if ids: qs = paginator.get_queryset( paginate=False).filter(id__in=ids) else: qs = paginator.get_queryset(paginate=False) func = getattr(qs, view_name) redirect_url = None if has_input: form = factory.get_class_action_form( request, _model, _action, func) paginator = '' if form.is_valid(): params = [] for param in func.func_code.co_varnames[ 1:func.func_code.co_argcount]: if param in form.cleaned_data: params.append( form.cleaned_data[param]) try: f_return = func(*params) redirect_url = '..' except ValidationError, e: form.add_error(None, unicode(e.message)) if not redirect_url: return render(request, 'default.html', locals()) else: f_return = func() redirect_url = '.' if redirect_url: request.GET._mutable = True del request.GET['ids'] del request.GET[view_name] request.GET._mutable = False return httprr(request, redirect_url, action_message) else: url = '/%s/%s/' % (app, view_name) if view_name in request.GET: return httprr(request, url) else: action_css = action_css.replace('popup', '') paginator.add_action(action_title, url, action_css, None)
def listt(request, app, cls, subset=None): try: _model = apps.get_model(app, cls) except LookupError as e: return page_not_found(request, e, 'error404.html') title = get_metadata(_model, 'verbose_name_plural') subsetp = None list_display = None list_filter = None search_fields = None if subset: subset_func = getattr(_model.objects.get_queryset(), subset) can_view = subset_func._metadata['{}:can_view'.format(subset)] list_display = subset_func._metadata['{}:list_display'.format(subset)] list_filter = subset_func._metadata['{}:list_filter'.format(subset)] search_fields = subset_func._metadata['{}:search_fields'.format( subset)] title = '{} - {}'.format( title, subset_func._metadata['{}:title'.format(subset)]) else: tid = request.GET.get('tid') subsetp = request.GET.get('tab{}'.format(tid)) if tid and subsetp: subset_func = getattr(_model.objects.get_queryset(), subsetp) subset_title = subset_func._metadata['{}:title'.format(subsetp)] can_view = subset_func._metadata['{}:can_view'.format(subsetp)] title = '{} - {}'.format( title, subset_func._metadata['{}:title'.format(subsetp)]) if not permissions.check_group_or_permission(request, can_view): return httprr( request, '/admin/login/?next={}'.format(request.get_full_path())) else: permission = '{}.list_{}'.format(app, cls) if not request.user.has_perm(permission): return httprr( request, '/admin/login/?next={}'.format(request.get_full_path())) qs = _model.objects.all(request.user) list_subsets = subset and [subset] or None paginator = Paginator(request, qs, title, list_subsets=list_subsets, is_list_view=True, list_display=list_display, list_filter=list_filter, search_fields=search_fields) paginator.process_request() if _model in loader.class_actions: for group in loader.class_actions[_model]: for view_name in loader.class_actions[_model][group]: _action = loader.class_actions[_model][group][view_name] action_title = _action['title'] action_message = _action['message'] action_can_execute = _action['can_execute'] action_input = _action['input'] action_css = _action['css'] action_condition = _action['condition'] initial = _action['initial'] choices = _action['choices'] if subsetp: if subsetp not in loader.subset_actions[ _model] or view_name not in loader.subset_actions[ _model][subsetp]: continue else: if subset not in loader.subset_actions[ _model] or view_name not in loader.subset_actions[ _model][subset]: continue if permissions.check_group_or_permission( request, action_can_execute): func = hasattr(qs, view_name) and getattr( qs, view_name) or None if func: char = '?' in request.get_full_path() and '&' or '?' url = '{}{}{}'.format(request.get_full_path(), char, '{}='.format(view_name)) has_input = func.__code__.co_argcount > 1 if not has_input: action_css = action_css.replace('popup', '') paginator.add_subset_action(action_title, url, action_css, None, action_condition) if view_name in request.GET: ids = paginator.get_selected_ids() if ids: qs = paginator.get_queryset( paginate=False).filter(id__in=ids) else: qs = paginator.get_queryset(paginate=False) func = getattr(qs, view_name) redirect_url = None if has_input: form = factory.get_class_action_form( request, _model, _action, func) paginator = '' if form.is_valid(): params = [] for param in func.__code__.co_varnames[ 1:func.__code__.co_argcount]: if param in form.cleaned_data: params.append( form.cleaned_data[param]) try: f_return = func(*params) redirect_url = '..' except ValidationError as e: form.add_error(None, str(e.message)) if not redirect_url: return render(request, 'default.html', locals()) else: f_return = func() redirect_url = '.' if redirect_url: request.GET._mutable = True del request.GET['ids'] del request.GET[view_name] request.GET._mutable = False return httprr(request, redirect_url, action_message) else: url = '/{}/{}/'.format(app, view_name) if view_name in request.GET: return httprr(request, url) else: action_css = action_css.replace('popup', '') paginator.add_action(action_title, url, action_css, None) paginator.add_actions() return render(request, 'default.html', locals())
def __init__(self, request, obj, current_tab=None, parent=None, fieldsets=None, complete=True): super(ModelPanel, self).__init__(request=request) self.obj = obj self.request = request self.title = unicode(obj) self.tabs = [] self.current_tab = current_tab self.message = None self.complete = complete self.drop_down = None fieldsets = fieldsets or get_metadata(type(obj), 'view_fieldsets', []) if not fieldsets: fields = [] for field in get_metadata(type(obj), 'fields')[1:]: fields.append(field.name) for field in get_metadata(type(obj), 'local_many_to_many'): fields.append(field.name) fieldsets = ((u'Dados Gerais', dict(fields=fields)), ) if self.complete: self.drop_down = ModelDropDown(self.request, type(self.obj)) self.drop_down.add_actions(self.obj, fieldset_title='') else: self.drop_down = GroupDropDown(self.request) self.fieldsets_with_tab_name = [] self.fieldsets_without_tab_name = [] model = type(self.obj) obj.as_pdf = self.as_pdf for fieldset in fieldsets: title, info = fieldset tab_name = None drop_down = ModelDropDown(self.request, model) fieldset_actions = info.get('actions', []) if fieldset_actions: drop_down.add_actions(self.obj, fieldset_title=title) if '::' in title: tab_name, title = title.split('::') url = '/view/%s/%s/%s/%s/' % (get_metadata( model, 'app_label'), model.__name__.lower(), self.obj.pk, slugify(tab_name)) tab = (tab_name, url) if not self.tabs and not self.current_tab: self.current_tab = slugify(tab_name) if tab not in self.tabs: self.tabs.append(tab) if not tab_name or slugify( tab_name) == self.current_tab or self.as_pdf: fieldset_dict = dict(title=title or u'Dados Gerais', tab_name=tab_name, fields=[], paginators=[], drop_down=drop_down, image=None) relations = fieldset[1].get('relations', []) if tab_name or self.as_pdf: self.fieldsets_with_tab_name.append(fieldset_dict) else: self.fieldsets_without_tab_name.append(fieldset_dict) if 'can_view' in fieldset[1]: can_view = fieldset[1]['can_view'] if not permissions.check_group_or_permission( self.request, can_view): continue if 'condition' in fieldset[1]: condition = fieldset[1]['condition'] self.obj.request = self.request if not check_condition(condition, self.obj): continue if 'image' in fieldset[1]: fieldset_dict['image'] = fieldset[1]['image'] if 'fields' in fieldset[1]: for name_or_tuple in fieldset[1]['fields']: if not type(name_or_tuple) == tuple: name_or_tuple = (name_or_tuple, ) attr_names = [] for attr_name in name_or_tuple: if attr_name != parent: attr = getattr(model, attr_name) field = None if hasattr(attr, 'field_name'): field = getattr(model, '_meta').get_field( attr.field_name) elif hasattr(attr, 'field'): field = attr.field if not field or not hasattr( field, 'display') or field.display: if is_one_to_one( model, attr_name ) and attr.field.display == 'detail': relations.append(attr_name) elif is_one_to_many( model, attr_name ) and field.display == 'detail': relations.append(attr_name) elif is_many_to_one( model, attr_name ) and field.display == 'detail': relations.append(attr_name) elif is_many_to_many( model, attr_name) and ( not hasattr(field, 'display') or field.display == 'detail'): relations.append(attr_name) elif is_one_to_many_reverse( model, attr_name): relations.append(attr_name) else: verbose_name, lookup, sortable, to = get_fiendly_name( model, attr_name, as_tuple=True) if to and not should_filter_or_display( self.request, model, to): continue attr_names.append( dict(verbose_name=verbose_name, name=attr_name)) if attr_names: fieldset_dict['fields'].append(attr_names) if self.complete: for relation_name in relations: if relation_name in [ field.name for field in get_metadata(model, 'get_fields') ]: relation_field = find_field_by_name( model, relation_name) relation = getattr(self.obj, relation_name) if not relation and hasattr( relation_field, 'rel') and relation_field.rel.to: relation = relation_field.rel.to() if hasattr(relation.__class__, 'pk'): if relation.pk: fieldset_title = relation_field.verbose_name panel_fieldsets = get_metadata( type(relation), 'view_fieldsets', []) panel_fieldsets = ( (fieldset_title, panel_fieldsets[0][1]), ) panel = ModelPanel( request, relation, fieldsets=panel_fieldsets, complete=False) if is_one_to_one(model, relation_name): app_label = get_metadata( model, 'app_label') model_name = model.__name__.lower() related_model_name = type( relation).__name__.lower() add_url = '/add/%s/%s/%s/%s' % ( app_label, model_name, self.obj.pk, relation_name) delete_url = None if relation.pk: add_url = '%s/%s/' % (add_url, relation.pk) app_label = get_metadata( type(relation), 'app_label') delete_url = '/delete/%s/%s/%s/' % ( app_label, related_model_name, relation.pk) if permissions.has_add_permission( self.request, model ) or permissions.has_edit_permission( self.request, model): if delete_url: panel.drop_down.add_action( 'Excluir %s' % relation_field. verbose_name, delete_url, 'popup', 'fa-close', None) panel.drop_down.add_action( 'Atualizar %s' % relation_field.verbose_name, add_url, 'popup', 'fa-edit') fieldset_dict['paginators'].append(panel) else: fieldset_title = len( relations ) > 1 and title or relation_field.verbose_name if is_one_to_many( model, relation_name) or is_many_to_many( model, relation_name): to = model.__name__.lower() else: to = relation_name related_paginator = Paginator( self.request, relation.all(), title=fieldset_title, to=to, list_subsets=[]) add_url = '/add/%s/%s/%s/%s/' % (get_metadata( model, 'app_label'), model.__name__.lower( ), self.obj.pk, relation_name) if permissions.has_add_permission( self.request, model ) or permissions.has_relate_permission( self.request, model): related_paginator.add_action( 'Adicionar %s' % unicode( get_metadata( relation.model, 'verbose_name')), add_url, 'popup', 'fa-plus') fieldset_dict['paginators'].append( related_paginator) else: is_object_set = False for related_object in list_related_objects(model): if relation_name == related_object.get_accessor_name( ): is_object_set = True break relation = getattr(self.obj, relation_name) if hasattr(relation, 'all'): qs = relation.all() elif hasattr(relation, '__call__'): qs = relation() else: qs = relation to = is_object_set and related_object.field.name or None fieldset_title = len( relations) > 1 and get_metadata( qs.model, 'verbose_name_plural') or title if hasattr(relation, '_metadata'): fieldset_title = relation._metadata[ '%s:verbose_name' % relation_name] exclude = [ is_object_set and related_object.field.name or '' ] related_paginator = Paginator( self.request, qs, fieldset_title, exclude=exclude, list_subsets=[], to=to, readonly=not is_object_set) if is_object_set and ( permissions.has_add_permission( self.request, qs.model) or permissions.has_relate_permission( self.request, qs.model)): instance = qs.model() setattr(instance, related_object.field.name, self.obj) if permissions.can_add(self.request, instance): add_inline = get_metadata( qs.model, 'add_inline') if add_inline: form_name = get_metadata( qs.model, 'add_form') if form_name: fromlist = get_metadata( qs.model, 'app_label') forms_module = __import__( '%s.forms' % fromlist, fromlist=fromlist) Form = getattr( forms_module, form_name) else: class Form(ModelForm): class Meta: model = qs.model fields = get_metadata( qs.model, 'form_fields', '__all__') exclude = get_metadata( qs.model, 'exclude_fields', ()) submit_label = 'Adicionar' title = u'Adicionar %s' % get_metadata( qs.model, 'verbose_name') form = Form(self.request, instance=instance, inline=True) if related_object.field.name in form.fields: del (form.fields[ related_object.field.name]) related_paginator.form = form if form.is_valid(): try: form.save() self.message = u'Ação realizada com sucesso' except ValidationError, e: form.add_error( None, unicode(e.message)) else: add_url = '/add/%s/%s/%s/%s/' % ( get_metadata(model, 'app_label'), model.__name__.lower(), self.obj.pk, relation_name.replace('_set', '')) add_label = u'Adicionar %s' % get_metadata( qs.model, 'verbose_name') add_label = get_metadata( qs.model, 'add_label', add_label) related_paginator.add_action( add_label, add_url, 'popup', 'fa-plus') fieldset_dict['paginators'].append( related_paginator) if 'extra' in fieldset[1]: fieldset_dict['extra'] = [] for info in fieldset[1]['extra']: fieldset_dict['extra'].append(info)