Ejemplo n.º 1
0
def get_buyer_for_user(user):
    """
    Returns buyer associated with user using relation defined in settings as PLANS_USER_BUYER_RELATION.
    :param user:
    :return: plans.models.Buyer
    """
    try:
        return operator.attrgetter(USER_BUYER_RELATION)(user)
    except AttributeError:
        raise FieldDoesNotExist(
            f'User model should have defined a ForeignKey named {USER_BUYER_RELATION} '
            f'to the model set in {USER_BUYER_RELATION_SETTING}')
Ejemplo n.º 2
0
    def test_get_filter_args_bad_range_key(self):
        start_time = datetime.datetime.utcnow()
        fake_request = self.mox.CreateMockAnything()
        fake_request.GET = {'somebadfield_max': str(start_time)}
        fake_model = self.make_fake_model()
        fake_model._meta.get_field_by_name('somebadfield')\
                  .AndRaise(FieldDoesNotExist())
        self.mox.ReplayAll()

        self.assertRaises(dbapi.BadRequestException, dbapi._get_filter_args,
                          fake_model, fake_request)

        self.mox.VerifyAll()
Ejemplo n.º 3
0
        def get_expandable_field(self, name):
            field = self.regular_fields.get(name)

            if field is None:
                raise FieldDoesNotExist("The field {}.{} doesn't exist".format(
                    self._model._meta.verbose_name, name))

            if not self.is_expandable(field):
                raise FieldNotAllowed(
                    'The field {}.{} is not expandable'.format(
                        self._model._meta.verbose_name, name))

            return field
Ejemplo n.º 4
0
        def get_field(self, name):
            if name.startswith('customfield-'):
                field = self.custom_fields.get(name)
            else:
                field = self.regular_fields.get(name)

                if field and not self.is_updatable(field):
                    raise FieldNotAllowed(
                        'The field {}.{} is not editable'.format(
                            self._model._meta.verbose_name, name))

            if field is None:
                raise FieldDoesNotExist("The field {}.{} doesn't exist".format(
                    self._model._meta.verbose_name, name))

            return field
Ejemplo n.º 5
0
    def __init__(self, model, field_name):
        """ Constructor.

        @param model: Class inheriting django.db.models.Model.
        @param field_name: String representing a 'chain' of fields; eg: 'book__author__name'.
        @throws FieldDoesNotExist
        """
        self._model = model
        self.__fields = fields = []
        subfield_names = field_name.split('__')

        for subfield_name in subfield_names[:-1]:
            field = model._meta.get_field(subfield_name)
            remote_field = getattr(field, 'remote_field', None)

            if remote_field is None:
                raise FieldDoesNotExist('"{}" is not a ForeignKey/ManyToManyField,'
                                        ' so it can have a sub-field'.format(subfield_name)
                                       )

            model = remote_field.model
            fields.append(field)

        fields.append(model._meta.get_field(subfield_names[-1]))
Ejemplo n.º 6
0
 def get_related_field(self):
     field = self.to._meta.get_field(self.field_name)
     if not field.concrete:
         raise FieldDoesNotExist("No related field named '%s'" %
                                 self.field_name)
     return field