예제 #1
0
파일: relate.py 프로젝트: xujianhai/xadmin
    def __init__(self, admin_view, lookup, value):
        self.admin_view = admin_view
        self.org_model = admin_view.model
        self.opts = admin_view.opts
        self.lookup = lookup
        self.value = value

        parts = lookup.split(LOOKUP_SEP)
        field = self.opts.get_field(parts[0])

        if not is_related_field2(field):
            raise Exception(u"Relate Lookup field must a related field")

        if hasattr(field, "rel"):
            self.to_model = field.rel.to
            self.rel_name = field.rel.get_related_field().name
            self.is_m2m = isinstance(field.rel, models.ManyToManyRel)
        else:
            self.to_model = field.model
            self.rel_name = self.to_model._meta.pk.name
            self.is_m2m = False

        to_qs = self.to_model._default_manager.get_queryset()
        self.to_objs = to_qs.filter(**{self.rel_name: value}).all()

        self.field = field
예제 #2
0
파일: relate.py 프로젝트: ezc/django-xadmin
    def __init__(self, admin_view, lookup, value):
        self.admin_view = admin_view
        self.org_model = admin_view.model
        self.opts = admin_view.opts
        self.lookup = lookup
        self.value = value

        parts = lookup.split(LOOKUP_SEP)
        field = self.opts.get_field(parts[0])

        if not is_related_field2(field):
            raise Exception(u'Relate Lookup field must a related field')

        if hasattr(field, 'rel'):
            self.to_model = field.rel.to
            self.rel_name = field.rel.get_related_field().name
            self.is_m2m = isinstance(field.rel, models.ManyToManyRel)
        else:
            self.to_model = field.model
            self.rel_name = self.to_model._meta.pk.name
            self.is_m2m = False

        to_qs = self.to_model._default_manager.get_queryset()
        self.to_objs = to_qs.filter(**{self.rel_name: value}).all()

        self.field = field
예제 #3
0
 def test(cls, field, request, params, model, admin_view, field_path):
     if not is_related_field2(field):
         return False
     related_modeladmin = admin_view.admin_site._registry.get(
         get_model_from_relation(field))
     return related_modeladmin and getattr(related_modeladmin,
                                           'relfield_style',
                                           None) in ('fk-ajax', 'fk-select')
예제 #4
0
    def get_form_helper(self):
        helper = super(RevisionView, self).get_form_helper()
        diff_fields = {}
        version_data = self.version.field_dict

        for f in self.opts.fields:
            fvalue = f.value_from_object(self.org_obj)
            vvalue = version_data.get(f.name, None)

            if fvalue is None and vvalue == '':
                vvalue = None
            if is_related_field2(f):
                vvalue = version_data.get(f.name + '_' + f.rel.get_related_field().name, None)

            if fvalue != vvalue:
                diff_fields[f.name] = self.detail.get_field_result(f.name).val
        for k, v in diff_fields.items():
            helper[k].wrap(DiffField, orgdata=v)
        return helper
예제 #5
0
    def get_form_helper(self):
        helper = super(RevisionView, self).get_form_helper()
        diff_fields = {}
        version_data = self.version.field_dict
        
        for f in self.opts.fields:
            fvalue = f.value_from_object(self.org_obj)
            vvalue = version_data.get(f.name, None)

            if fvalue is None and vvalue == '':
                vvalue = None
            if is_related_field2(f):
                vvalue = version_data.get(f.name + '_' + f.rel.get_related_field().name, None)

            if fvalue != vvalue:
                diff_fields[f.name] = self.detail.get_field_result(f.name).val
        for k, v in diff_fields.items():
            helper[k].wrap(DiffField, orgdata=v)
        return helper
예제 #6
0
파일: relate.py 프로젝트: meishild/xadmin
    def __init__(self, admin_view, lookup, value):
        self.admin_view = admin_view
        self.org_model = admin_view.model
        self.opts = admin_view.opts
        self.lookup = lookup
        self.value = value

        parts = lookup.split(LOOKUP_SEP)
        field = self.opts.get_field(parts[0])

        if not is_related_field2(field):
            raise Exception(u'Relate Lookup field must a related field')

        self.to_model = field.related_model
        self.rel_name = field.remote_field.name
        self.is_m2m = bool(field.many_to_many)

        to_qs = self.to_model._default_manager.get_queryset()
        self.to_objs = to_qs.filter(**{self.rel_name: value}).all()

        self.field = field
예제 #7
0
    def __init__(self, admin_view, lookup, value):
        self.admin_view = admin_view
        self.org_model = admin_view.model
        self.opts = admin_view.opts
        self.lookup = lookup
        self.value = value

        parts = lookup.split(LOOKUP_SEP)
        field = self.opts.get_field(parts[0])

        if not is_related_field2(field):
            raise Exception(u'Relate Lookup field must a related field')

        self.to_model = field.related_model
        self.rel_name = field.remote_field.name
        self.is_m2m = bool(field.many_to_many)

        to_qs = self.to_model._default_manager.get_queryset()
        self.to_objs = to_qs.filter(**{self.rel_name: value}).all()

        self.field = field
예제 #8
0
 def test(cls, field, request, params, model, admin_view, field_path):
     return is_related_field2(field)
예제 #9
0
    def post(self, request, object_id, *args, **kwargs):
        object_id = unquote(object_id)
        self.obj = self.get_object(object_id)

        if not self.has_change_permission(self.obj):
            raise PermissionDenied

        params = self.request.POST
        if 'version_a' not in params or 'version_b' not in params:
            self.message_user(_("Must select two versions."), 'error')
            return self.get_response()

        version_a_id = params['version_a']
        version_b_id = params['version_b']

        if version_a_id == version_b_id:
            self.message_user(_("Please select two different versions."),
                              'error')
            return self.get_response()

        version_a = get_object_or_404(Version, pk=version_a_id)
        version_b = get_object_or_404(Version, pk=version_b_id)

        diffs = []

        obj_a, detail_a = self.get_version_object(version_a)
        obj_b, detail_b = self.get_version_object(version_b)

        for f in (self.opts.fields + self.opts.many_to_many):
            if is_related_field2(f):
                label = f.opts.verbose_name
            else:
                label = f.verbose_name

            value_a = f.value_from_object(obj_a)
            value_b = f.value_from_object(obj_b)
            is_diff = value_a != value_b

            if type(value_a) in (list, tuple) and type(value_b) in (list, tuple) \
                    and len(value_a) == len(value_b) and is_diff:
                is_diff = False
                for i in xrange(len(value_a)):
                    if value_a[i] != value_a[i]:
                        is_diff = True
                        break
            if type(value_a) is QuerySet and type(value_b) is QuerySet:
                is_diff = list(value_a) != list(value_b)

            diffs.append((label, detail_a.get_field_result(f.name).val,
                          detail_b.get_field_result(f.name).val, is_diff))

        context = super(RevisionListView, self).get_context()
        context.update({
            'object':
            self.obj,
            'opts':
            self.opts,
            'version_a':
            version_a,
            'version_b':
            version_b,
            'revision_a_url':
            self.model_admin_url('revision', quote(version_a.object_id),
                                 version_a.id),
            'revision_b_url':
            self.model_admin_url('revision', quote(version_b.object_id),
                                 version_b.id),
            'diffs':
            diffs
        })

        return TemplateResponse(
            self.request,
            self.revision_diff_template
            or self.get_template_list('views/revision_diff.html'),
            context,
            current_app=self.admin_site.name)
예제 #10
0
    def post(self, request, object_id, *args, **kwargs):
        object_id = unquote(object_id)
        self.obj = self.get_object(object_id)

        if not self.has_change_permission(self.obj):
            raise PermissionDenied

        params = self.request.POST
        if 'version_a' not in params or 'version_b' not in params:
            self.message_user(_("Must select two versions."), 'error')
            return self.get_response()

        version_a_id = params['version_a']
        version_b_id = params['version_b']

        if version_a_id == version_b_id:
            self.message_user(
                _("Please select two different versions."), 'error')
            return self.get_response()

        version_a = get_object_or_404(Version, pk=version_a_id)
        version_b = get_object_or_404(Version, pk=version_b_id)

        diffs = []

        obj_a, detail_a = self.get_version_object(version_a)
        obj_b, detail_b = self.get_version_object(version_b)

        for f in (self.opts.fields + self.opts.many_to_many):
            if is_related_field2(f):
                label = f.opts.verbose_name
            else:
                label = f.verbose_name

            value_a = f.value_from_object(obj_a)
            value_b = f.value_from_object(obj_b)
            is_diff = value_a != value_b

            if type(value_a) in (list, tuple) and type(value_b) in (list, tuple) \
                    and len(value_a) == len(value_b) and is_diff:
                is_diff = False
                for i in xrange(len(value_a)):
                    if value_a[i] != value_a[i]:
                        is_diff = True
                        break
            if type(value_a) is QuerySet and type(value_b) is QuerySet:
                is_diff = list(value_a) != list(value_b)

            diffs.append((label, detail_a.get_field_result(
                f.name).val, detail_b.get_field_result(f.name).val, is_diff))

        context = super(RevisionListView, self).get_context()
        context.update({
            'object': self.obj,
            'opts': self.opts,
            'version_a': version_a,
            'version_b': version_b,
            'revision_a_url': self.model_admin_url('revision', quote(version_a.object_id), version_a.id),
            'revision_b_url': self.model_admin_url('revision', quote(version_b.object_id), version_b.id),
            'diffs': diffs
        })

        return TemplateResponse(
            self.request, self.revision_diff_template or self.get_template_list('views/revision_diff.html'),
            context)
예제 #11
0
 def test(cls, field, request, params, model, admin_view, field_path):
     return is_related_field2(field)
예제 #12
0
 def test(cls, field, request, params, model, admin_view, field_path):
     if not is_related_field2(field):
         return False
     related_modeladmin = admin_view.admin_site._registry.get(
         get_model_from_relation(field))
     return related_modeladmin and getattr(related_modeladmin, 'relfield_style', None) in ('fk-ajax', 'fk-select')