def get_many_to_many_form(request, obj, related_field_name, related_pk): _model = type(obj) initial = hasattr(obj, 'initial') and obj.initial() or {} choices = hasattr(obj, 'choices') and obj.choices() or {} related_field_model = find_field_by_name( _model, related_field_name).remote_field.model class Form(forms.ModelForm): related_objects = forms.MultipleModelChoiceField( related_field_model.objects.all(), label=get_metadata(related_field_model, 'verbose_name')) class Meta: model = _model fields = () title = 'Adicionar {}'.format( get_metadata(related_field_model, 'verbose_name')) icon = get_metadata(related_field_model, 'icon', None) # is_inner = True def save(self, *args, **kwargs): for related_object in self.cleaned_data['related_objects']: getattr(self.instance, related_field_name).add(related_object) form = Form(request, instance=obj, initial=initial) form.name = '{}Form'.format(_model.__name__) for field_name in choices: if field_name in form.fields: form.fields[field_name].queryset = choices[field_name] return form
def get_one_to_one_form(request, obj, related_field_name, related_pk, **kwargs): _model = type(obj) related_field = find_field_by_name(_model, related_field_name) related_object = related_pk and related_field.rel.to.objects.get( pk=related_pk) initial = hasattr(related_field.rel.to, 'initial') and related_field.rel.to.initial() or {} choices = hasattr(related_field.rel.to, 'choices') and related_field.rel.to.choices() or {} class Form(forms.ModelForm): class Meta: model = related_field.rel.to fields = get_metadata(related_field.rel.to, 'form_fields', '__all__') exclude = get_metadata(related_field.rel.to, 'exclude_fields', ()) submit_label = 'Atualizar %s' % related_field.verbose_name title = u'Atualizar %s' % related_field.verbose_name icon = get_metadata(related_field.rel.to, 'icon', None) def save(self, *args, **kwargs): super(Form, self).save(*args, **kwargs) setattr(obj, related_field_name, self.instance) obj.save() form = Form(request, instance=related_object, initial=initial, **kwargs) form.name = u'%sForm' % related_field.rel.to.__name__ for field_name in choices: if field_name in form.fields: form.fields[field_name].queryset = choices[field_name] return form
def get_one_to_many_form(request, obj, related_field_name, **kwargs): _model = type(obj) related_field = find_field_by_name(_model, related_field_name) class Form(forms.ModelForm): class Meta: model = related_field.remote_field.model fields = get_metadata(related_field.remote_field.model, 'form_fields', '__all__') exclude = get_metadata(related_field.remote_field.model, 'exclude_fields', ()) submit_label = 'Adicionar {}'.format( get_metadata(related_field.remote_field.model, 'verbose_name')) title = 'Adicionar {}'.format( get_metadata(related_field.remote_field.model, 'verbose_name')) icon = get_metadata(related_field.remote_field.model, 'icon', None) is_inner = True class Media: js = get_js(related_field.remote_field.model) def save(self, *args, **kwargs): super(Form, self).save(*args, **kwargs) getattr(obj, related_field_name).add(self.instance) form = Form(request, **kwargs) return form
def get_one_to_one_form(request, obj, related_field_name, related_pk, **kwargs): _model = type(obj) related_field = find_field_by_name(_model, related_field_name) related_object = related_pk and related_field.remote_field.model.objects.get( pk=related_pk) or related_field.remote_field.model() related_object._user = request.user initial = hasattr( related_field.remote_field.model, 'initial') and related_field.remote_field.model.initial() or {} choices = hasattr( related_field.remote_field.model, 'choices') and related_field.remote_field.model.choices() or {} class Form(forms.ModelForm): class Meta: model = related_field.remote_field.model fields = get_metadata(related_field.remote_field.model, 'form_fields', '__all__') exclude = get_metadata(related_field.remote_field.model, 'exclude_fields', ()) submit_label = 'Atualizar {}'.format(related_field.verbose_name) title = 'Atualizar {}'.format(related_field.verbose_name) icon = get_metadata(related_field.remote_field.model, 'icon', None) is_inner = True class Media: js = get_js(related_field.remote_field.model) def save(self, *args, **kwargs): super(Form, self).save(*args, **kwargs) setattr(obj, related_field_name, self.instance) _model.objects.filter(pk=obj.pk).update( **{related_field_name: self.instance}) form = Form(request, instance=related_object, initial=initial, **kwargs) form.name = '{}Form'.format(related_field.remote_field.model.__name__) for field_name in choices: if field_name in form.fields: form.fields[field_name].queryset = choices[field_name] return form
def get_one_to_many_form(request, obj, related_field_name, **kwargs): _model = type(obj) rel = find_field_by_name(_model, related_field_name).rel class Form(forms.ModelForm): class Meta: model = rel.to fields = get_metadata(rel.to, 'form_fields', '__all__') exclude = get_metadata(rel.to, 'exclude_fields', ()) submit_label = u'Adicionar %s' % get_metadata( rel.to, 'verbose_name') title = u'Adicionar %s' % get_metadata(rel.to, 'verbose_name') icon = get_metadata(rel.to, 'icon', None) def save(self, *args, **kwargs): super(Form, self).save(*args, **kwargs) getattr(obj, related_field_name).add(self.instance) form = Form(request, **kwargs) return form
def obj_icons(request, obj, to=None, edit=True, delete=True, css='ajax'): l = [] model = obj.__class__ cls = model.__name__.lower() app = get_metadata(model, 'app_label') css = to and u'popup' or css parent = to and hasattr(obj, to) and getattr(obj, to) or None if hasattr(obj, 'get_absolute_url'): view_url = obj.get_absolute_url() else: view_url = u'/view/%s/%s/%s/' % (app, cls, obj.pk) url_id = slugify(view_url) url_title = u'Visualizar' if parent: from djangoplus.cache import loader can_spy = True if type(obj) in loader.actions: for category in loader.actions[type(obj)]: for action_name in loader.actions[type(obj)][category]: action = loader.actions[type(obj)][category][action_name] if not action['inline']: can_spy = False break fieldsets = get_metadata(obj, 'fieldsets') if fieldsets: for fieldset in fieldsets: if '::' in fieldset[0]: can_spy = False break l.append(u'<a id="%s" class="%s" href="%s?parent=%s" title="%s"><i class="fa fa-search fa-lg"></i></a>' % (url_id, 'ajax', view_url, to, url_title)) if can_spy: l.append(u'<a id="%s" class="%s" href="%s?parent=%s" title="%s"><i class="fa fa-eye fa-lg"></i></a>' % (url_id, 'popup', view_url, to, url_title)) else: l.append(u'<a id="%s" class="%s" href="%s" title="%s"><i class="fa fa-search fa-lg"></i></a>' % (url_id, to and 'popup' or 'ajax', view_url, url_title)) if edit or delete: if parent and is_many_to_many(parent.__class__, to): if edit and (not hasattr(obj, 'can_edit') or obj.can_edit()): delete_url = u'/delete/%s/%s/%s/%s/%s/' % (app, parent.__class__.__name__.lower(), parent.pk, to, obj.pk) url_id = slugify(delete_url) url_title = u'Remover' l.append(u' <a id="%s" class="%s" href="%s" title="%s"><i class="fa fa-times fa-lg"></i></a>' % (url_id, 'ajax', delete_url, url_title)) else: if edit and permissions.has_edit_permission(request, type(obj)) and (not hasattr(obj, 'can_edit') or obj.can_edit()): if parent: _app = get_metadata(parent.__class__, 'app_label') _cls = parent.__class__.__name__.lower() _related_field = find_field_by_name(obj.__class__, to).remote_field.get_accessor_name() edit_url = u'/add/%s/%s/%s/%s/%s/' % (_app, _cls, parent.pk, _related_field, obj.pk) else: edit_url = u'/add/%s/%s/%s/' % (app, cls, obj.pk) url_id = slugify(edit_url) url_title = u'Editar' l.append(u' <a id="%s" class="%s" href="%s" title="%s"><i class="fa fa-edit fa-lg"></i></a>' % (url_id, css, edit_url, url_title)) if delete and permissions.has_delete_permission(request, type(obj)) and (not hasattr(obj, 'can_delete') or obj.can_delete()): delete_url = u'/delete/%s/%s/%s/' % (app, cls, obj.pk) url_id = slugify(delete_url) url_title = u'Excluir' l.append(u' <a id="%s" class="%s" href="%s" title="%s"><i class="fa fa-trash-o fa-lg"></i></a>' % (url_id, 'popup', delete_url, url_title)) tree_index_field = hasattr(obj, 'get_tree_index_field') and obj.get_tree_index_field() or None if tree_index_field: add_url = u'/add/%s/%s/%s/%s/' % (app, cls, obj.pk, cls) url_id = slugify(view_url) url_title = u'Adicionar' l.append(u' <a id="%s" class="%s" href="%s" title="%s"><i class="fa fa-plus fa-lg"></i></a>' % ( url_id, 'popup', add_url, url_title)) return mark_safe(''.join(l))
def __init__(self, request, obj, current_tab=None, parent=None, fieldsets=None, complete=True, readonly=False, printable=True): super(ModelPanel, self).__init__(obj.pk, request) self.obj = obj self.title = obj.pk and str(obj) or get_metadata( type(obj), 'verbose_name') self.id = self.title self.tabs = [] self.current_tab = current_tab self.message = None self.complete = complete self.readonly = readonly self.printable = printable self.drop_down = None fieldsets = fieldsets or get_metadata(type(obj), 'view_fieldsets', []) if not fieldsets: fieldsets = get_fieldsets(type(obj)) if self.complete: self.drop_down = ModelDropDown(self.request, type(self.obj)) self.drop_down.add_actions(self.obj, fieldset='') if self.printable: print_url = '?pdf={}&pk='.format(self.id) self.drop_down.add_action('Imprimir', url=print_url, css='ajax', icon='fa-print', category='Imprimir') 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', []) fieldset_image = info.get('image') if fieldset_actions: drop_down.add_actions(self.obj, fieldset=title) if 'condition' in fieldset[1]: condition = fieldset[1]['condition'] self.obj.request = self.request if not check_condition(self.request.user, condition, self.obj): continue if '::' in title: tab_name, title = title.split('::') url = '/view/{}/{}/{}/{}/'.format( 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 'Dados Gerais', tab_name=tab_name, fields=[], paginators=[], drop_down=drop_down, image=None) relations = list(fieldset[1].get('relations', [])) inlines = list(fieldset[1].get('inlines', [])) 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 '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 and attr_name != fieldset_image: 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: 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: from djangoplus.utils.relations import Relation for relation_name in relations + inlines: component = Relation(self.obj, relation_name).get_component( self.request, self.as_pdf) fieldset_dict['paginators'].append(component) else: for relation_name in relations + inlines: if relation_name in [ field.name for field in get_metadata(model, 'get_fields') ]: relation_field = find_field_by_name( model, relation_name) if is_one_to_one(model, relation_name) or is_many_to_one( model, relation_name): fieldset_dict['fields'].append([ dict(verbose_name=relation_field. verbose_name, name=relation_name) ]) if 'extra' in fieldset[1]: fieldset_dict['extra'] = [] for info in fieldset[1]['extra']: fieldset_dict['extra'].append(info)
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)