Ejemplo n.º 1
0
    def _find_embedded_inlines(self):
        emb_inlines = []
        exclude = self.exclude or []
        for name in self.model._fields_ordered:
            f = self.model._fields.get(name)
            if not (isinstance(f, ListField) and isinstance(
                    getattr(f, 'field', None), EmbeddedDocumentField)
                    ) and not isinstance(f, EmbeddedDocumentField):
                continue
            # Should only reach here if there is an embedded document...
            if f.name in exclude:
                continue
            if hasattr(f, 'field') and f.field is not None:
                embedded_document = f.field.document_type
            elif hasattr(f, 'document_type'):
                embedded_document = f.document_type
            else:
                # For some reason we found an embedded field were either
                # the field attribute or the field's document type is None.
                # This shouldn't happen, but apparently does happen:
                # https://github.com/jschrewe/django-mongoadmin/issues/4
                # The solution for now is to ignore that field entirely.
                continue

            init_document_options(embedded_document)

            embedded_admin_base = EmbeddedStackedDocumentInline
            embedded_admin_name = "%sAdmin" % embedded_document.__name__
            inline_attrs = {
                'model': embedded_document,
                'parent_field_name': f.name,
            }
            # if f is an EmbeddedDocumentField set the maximum allowed form
            # instances to one
            if isinstance(f, EmbeddedDocumentField):
                inline_attrs['max_num'] = 1
            embedded_admin = type(embedded_admin_name, (embedded_admin_base, ),
                                  inline_attrs)
            # check if there is an admin for the embedded document in
            # self.inlines. If there is, use this, else use default.
            for inline_class in self.inlines:
                if inline_class.document == embedded_document:
                    embedded_admin = inline_class
            emb_inlines.append(embedded_admin)

            if f.name not in exclude:
                exclude.append(f.name)

        # sort out the declared inlines. Embedded admins take a different
        # set of arguments for init and are stored seperately. So the
        # embedded stuff has to be removed from self.inlines here
        inlines = [i for i in self.inlines if i not in emb_inlines]

        self.exclude = exclude

        return inlines + emb_inlines
Ejemplo n.º 2
0
    def _find_embedded_inlines(self):
        emb_inlines = []
        exclude = self.exclude or []
        for name in self.model._fields_ordered:
            f = self.model._fields.get(name)
            if not (isinstance(f, ListField) and isinstance(getattr(f, 'field', None), EmbeddedDocumentField)) and not isinstance(f, EmbeddedDocumentField):
                continue
            # Should only reach here if there is an embedded document...
            if f.name in exclude:
                continue
            if hasattr(f, 'field') and f.field is not None:
                embedded_document = f.field.document_type
            elif hasattr(f, 'document_type'):
                embedded_document = f.document_type
            else:
                # For some reason we found an embedded field were either
                # the field attribute or the field's document type is None.
                # This shouldn't happen, but apparently does happen:
                # https://github.com/jschrewe/django-mongoadmin/issues/4
                # The solution for now is to ignore that field entirely.
                continue

            init_document_options(embedded_document)

            embedded_admin_base = EmbeddedStackedDocumentInline
            embedded_admin_name = "%sAdmin" % embedded_document.__name__
            inline_attrs = {
                'model': embedded_document,
                'parent_field_name': f.name,
            }
            # if f is an EmbeddedDocumentField set the maximum allowed form
            # instances to one
            if isinstance(f, EmbeddedDocumentField):
                inline_attrs['max_num'] = 1
            embedded_admin = type(
                embedded_admin_name, (embedded_admin_base,), inline_attrs
            )
            # check if there is an admin for the embedded document in
            # self.inlines. If there is, use this, else use default.
            for inline_class in self.inlines:
                if inline_class.document == embedded_document:
                    embedded_admin = inline_class
            emb_inlines.append(embedded_admin)

            if f.name not in exclude:
                exclude.append(f.name)

        # sort out the declared inlines. Embedded admins take a different
        # set of arguments for init and are stored seperately. So the
        # embedded stuff has to be removed from self.inlines here
        inlines = [i for i in self.inlines if i not in emb_inlines]

        self.exclude = exclude

        return inlines + emb_inlines
Ejemplo n.º 3
0
def label_for_field(name, model, model_admin=None, return_attr=False):
    attr = None
    model._meta = init_document_options(model)
    try:
        field = model._meta.get_field_by_name(name)[0]
        label = field.name.replace('_', ' ')
    except FieldDoesNotExist: 
        if name == "__unicode__":
            label = force_unicode(model._meta.verbose_name)
        elif name == "__str__":
            label = smart_str(model._meta.verbose_name)
        else:
            if isinstance(name, collections.Callable):
                attr = name
            elif model_admin is not None and hasattr(model_admin, name):
                attr = getattr(model_admin, name)
            elif hasattr(model, name):
                attr = getattr(model, name)
            else:
                message = "Unable to lookup '%s' on %s" % (name, model._meta.object_name)
                if model_admin:
                    message += " or %s" % (model_admin.__class__.__name__,)
                raise AttributeError(message)


            if hasattr(attr, "short_description"):
                label = attr.short_description
            elif isinstance(attr, collections.Callable):
                if attr.__name__ == "<lambda>":
                    label = "--"
                else:
                    label = pretty_name(attr.__name__)
            else:
                label = pretty_name(name)
    if return_attr:
        return (label, attr)
    else:
        return label
Ejemplo n.º 4
0
def label_for_field(name, model, model_admin=None, return_attr=False):
    attr = None
    model._meta = init_document_options(model)
    try:
        field = model._meta.get_field_by_name(name)[0]
        label = field.name.replace('_', ' ')
    except FieldDoesNotExist:
        if name == "__unicode__":
            label = force_unicode(model._meta.verbose_name)
        elif name == "__str__":
            label = smart_str(model._meta.verbose_name)
        else:
            if isinstance(name, collections.Callable):
                attr = name
            elif model_admin is not None and hasattr(model_admin, name):
                attr = getattr(model_admin, name)
            elif hasattr(model, name):
                attr = getattr(model, name)
            else:
                message = "Unable to lookup '%s' on %s" % (
                    name, model._meta.object_name)
                if model_admin:
                    message += " or %s" % (model_admin.__class__.__name__, )
                raise AttributeError(message)

            if hasattr(attr, "short_description"):
                label = attr.short_description
            elif isinstance(attr, collections.Callable):
                if attr.__name__ == "<lambda>":
                    label = "--"
                else:
                    label = pretty_name(attr.__name__)
            else:
                label = pretty_name(name)
    if return_attr:
        return (label, attr)
    else:
        return label
Ejemplo n.º 5
0
 def __init__(self, document):
     self.to = init_document_options(document)
Ejemplo n.º 6
0
 def __init__(self, document, related_field=None):
     self.to = init_document_options(document)
     # for django 1.10
     self.model = self.to
     self.related_field = related_field
Ejemplo n.º 7
0
 def __init__(self, document):
     self.to = init_document_options(document)
Ejemplo n.º 8
0
 def __init__(self, document, parent_document=None):
     self.to = self.model = init_document_options(document)
     self.parent_document = parent_document
Ejemplo n.º 9
0
 def __init__(self, document, related_field=None):
     self.to = init_document_options(document)
     # for django 1.10
     self.model = self.to
     self.related_field = related_field