Esempio n. 1
0
class BaseModel(models.Model):

    delete_flag = models.BooleanField(default=False, verbose_name="是否被删除")

    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    last_update_time = models.DateTimeField(auto_now=True,
                                            verbose_name="最后更新时间")

    delete_time = models.DateTimeField(null=True,
                                       blank=True,
                                       verbose_name="删除时间")

    objects = BaseManager()

    class Meta:
        abstract = True

    @classmethod
    def create_test_items(cls, params_set: dict = {}, number: int = 5):
        """
            创建测试用例
        :param number: 创建个数
        :param params_set: 创建项参数集合
        :return:
        """
        if isinstance(params_set, dict) and isinstance(
                number, int) and 0 < number < 20:
            cls.objects.bulk_create(
                [cls(**params_set) for _ in range(0, number)])
        else:
            raise TypeError(
                f"TEST: create test {cls.__name__} item parameter error")
Esempio n. 2
0
class BaseModel(models.Model):
    """ An abstract class that every model should inherit from """
    created_at = models.DateTimeField(
        auto_now_add=True,
        help_text="creation date",
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        null=True,
        help_text="edition date",
    )

    # using BaseManager
    objects = BaseManager()

    class Meta:
        """ set to abstract """
        abstract = True

    # public methods
    def update(self, **kwargs):
        """ proxy method for the QuerySet: update method
        highly recommended when you need to save just one field

        """
        kwargs['updated_at'] = timezone.now()

        for kw in kwargs:
            self.__setattr__(kw, kwargs[kw])

        self.__class__.objects.filter(pk=self.pk).update(**kwargs)

    def get_image_url(self, field_name="picture", width=None, height=None):
        """ returns the url of an image stored in the attribute with the name
        field_name and with the size specified by width and height
        """

        if width and height:
            file_path = "{}{}x{}/{}".format(settings.MEDIA_URL, width, height,
                                            str(self.picture))
        else:
            file_path = "{}{}".format(settings.MEDIA_URL, str(self.picture))

        return file_path
Esempio n. 3
0
class BaseModel(models.Model):
    """ An abstract class that every model should inherit from """
    BOOLEAN_CHOICES = ((False, _(u'No')), (True, _(u'Yes')))

    created_at = models.DateTimeField(
        auto_now_add=True,
        help_text=_("creation date"),
    )
    updated_at = models.DateTimeField(
        auto_now=True, null=True,
        help_text=_("edition date"),
    )

    # using BaseManager
    objects = BaseManager()

    class Meta:
        """ set to abstract """
        abstract = True

    # public methods
    def file_path(self, name):
        """
        Generic method to give to a FileField or ImageField in it's upload_to
        parameter.

        This returns the name of the class, concatenated with the id of the
        object and the name of the file.
        """
        base_path = "{}/{}/{}"

        return base_path.format(self.__class__.__name__, name)

    def update(self, **kwargs):
        """ proxy method for the QuerySet: update method
        highly recommended when you need to save just one field

        """
        kwargs['updated_at'] = timezone.now()

        for kw in kwargs:
            self.__setattr__(kw, kwargs[kw])

        self.__class__.objects.filter(pk=self.pk).update(**kwargs)

    def to_dict(instance, fields=None, exclude=None):
        """
        Returns a dict containing the data in ``instance``

        ``fields`` is an optional list of field names. If provided, only the
        named fields will be included in the returned dict.

        ``exclude`` is an optional list of field names. If provided, the named
        fields will be excluded from the returned dict, even if they are listed
        in the ``fields`` argument.
        """

        opts = instance._meta
        data = {}
        for f in opts.fields + opts.many_to_many:
            if fields and f.name not in fields:
                continue
            if exclude and f.name in exclude:
                continue
            if isinstance(f, models.fields.related.ManyToManyField):
                # If the object doesn't have a primary key yet, just use an
                # emptylist for its m2m fields. Calling f.value_from_object
                # will raise an exception.
                if instance.pk is None:
                    data[f.name] = []
                else:
                    # MultipleChoiceWidget needs a list of pks, not objects.
                    data[f.name] = list(
                        f.value_from_object(instance).values_list('pk',
                                                                  flat=True))
            else:
                data[f.name] = f.value_from_object(instance)
        return data

    def to_json(self, fields=None, exclude=None, **kargs):
        """
        Returns a string containing the data in of the instance in json format

        ``fields`` is an optional list of field names. If provided, only the
        named fields will be included in the returned dict.

        ``exclude`` is an optional list of field names. If provided, the named
        fields will be excluded from the returned dict, even if they are listed
        in the ``fields`` argument.

        kwargs are optional named parameters for the json.dumps method
        """
        # obtain a dict of the instance data
        data = self.to_dict(fields=fields, exclude=exclude)

        # turn the dict to json
        return json.dumps(data, cls=ModelEncoder, **kargs)
Esempio n. 4
0
class BaseModel(AuditMixin, models.Model):
    """ An abstract class that every model should inherit from """
    BOOLEAN_CHOICES = ((False, _('No')), (True, _('Yes')))

    created_at = models.DateTimeField(
        auto_now_add=True,
        help_text=_("creation date"),
        verbose_name=_('created at'),
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        null=True,
        help_text=_("edition date"),
        verbose_name=_('updated at'),
    )

    # field used to store a dictionary with the instance original fields
    original_dict = None

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.original_dict = self.to_dict(
            exclude=settings.LOG_IGNORE_FIELDS,
            include_m2m=False,
        )

    # using BaseManager
    objects = BaseManager()

    class Meta:
        """ set to abstract """
        abstract = True

    # public methods
    def update(self, **kwargs):
        """ proxy method for the QuerySet: update method
        highly recommended when you need to save just one field

        """
        kwargs['updated_at'] = timezone.now()

        for kw in kwargs:
            self.__setattr__(kw, kwargs[kw])

        self.__class__.objects.filter(pk=self.pk).update(**kwargs)

    def to_dict(instance, fields=None, exclude=None, include_m2m=True):
        """
        Returns a dict containing the data in ``instance``

        ``fields`` is an optional list of field names. If provided, only the
        named fields will be included in the returned dict.

        ``exclude`` is an optional list of field names. If provided, the named
        fields will be excluded from the returned dict, even if they are listed
        in the ``fields`` argument.
        """

        opts = instance._meta
        data = {}
        for f in opts.fields + opts.many_to_many:
            if fields and f.name not in fields:
                continue
            if exclude and f.name in exclude:
                continue
            if isinstance(f, models.fields.related.ForeignKey):
                data[f.name + '_id'] = f.value_from_object(instance)
            elif isinstance(f, models.fields.related.ManyToManyField):
                if include_m2m:
                    # If the object doesn't have a primary key yet, just use an
                    # emptylist for its m2m fields. Calling f.value_from_object
                    # will raise an exception.
                    if instance.pk is None:
                        data[f.name] = []
                    else:
                        # MultipleChoiceWidget needs a list of pks, not objects
                        data[f.name + '_ids'] = list(
                            getattr(instance,
                                    f.attname).values_list('pk', flat=True))
            else:
                data[f.name] = f.value_from_object(instance)
        return data

    def to_json(self, fields=None, exclude=None, **kargs):
        """
        Returns a string containing the data in of the instance in json format

        ``fields`` is an optional list of field names. If provided, only the
        named fields will be included in the returned dict.

        ``exclude`` is an optional list of field names. If provided, the named
        fields will be excluded from the returned dict, even if they are listed
        in the ``fields`` argument.

        kwargs are optional named parameters for the json.dumps method
        """
        # obtain a dict of the instance data
        data = self.to_dict(fields=fields, exclude=exclude)

        # turn the dict to json
        return json.dumps(data, cls=ModelEncoder, **kargs)

    def get_full_url(self):
        absolute_url = self.get_absolute_url()
        site = Site.objects.get_current().domain
        return 'http://{site}{path}'.format(site=site, path=absolute_url)
Esempio n. 5
0
class BaseModel(models.Model):
    """ An abstract class that every model should inherit from """
    BOOLEAN_CHOICES = ((False, _('No')), (True, _('Yes')))

    created_at = models.DateTimeField(
        auto_now_add=True,
        help_text=_("creation date"),
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        null=True,
        help_text=_("edition date"),
    )

    # using BaseManager
    objects = BaseManager()

    class Meta:
        """ set to abstract """
        abstract = True

    # public methods
    def update(self, **kwargs):
        """ proxy method for the QuerySet: update method
        highly recommended when you need to save just one field

        """
        kwargs['updated_at'] = timezone.now()

        for kw in kwargs:
            self.__setattr__(kw, kwargs[kw])

        self.__class__.objects.filter(pk=self.pk).update(**kwargs)

    def to_dict(instance, fields=None, exclude=None):
        """
        Returns a dict containing the data in ``instance``

        ``fields`` is an optional list of field names. If provided, only the
        named fields will be included in the returned dict.

        ``exclude`` is an optional list of field names. If provided, the named
        fields will be excluded from the returned dict, even if they are listed
        in the ``fields`` argument.
        """

        opts = instance._meta
        data = {}
        for f in opts.fields + opts.many_to_many:
            if fields and f.name not in fields:
                continue
            if exclude and f.name in exclude:
                continue
            if isinstance(f, models.fields.related.ManyToManyField):
                # If the object doesn't have a primary key yet, just use an
                # emptylist for its m2m fields. Calling f.value_from_object
                # will raise an exception.
                if instance.pk is None:
                    data[f.name] = []
                else:
                    # MultipleChoiceWidget needs a list of pks, not objects.
                    data[f.name] = list(
                        f.value_from_object(instance).values_list('pk',
                                                                  flat=True))
            else:
                data[f.name] = f.value_from_object(instance)
        return data

    def to_json(self, fields=None, exclude=None, **kargs):
        """
        Returns a string containing the data in of the instance in json format

        ``fields`` is an optional list of field names. If provided, only the
        named fields will be included in the returned dict.

        ``exclude`` is an optional list of field names. If provided, the named
        fields will be excluded from the returned dict, even if they are listed
        in the ``fields`` argument.

        kwargs are optional named parameters for the json.dumps method
        """
        # obtain a dict of the instance data
        data = self.to_dict(fields=fields, exclude=exclude)

        # turn the dict to json
        return json.dumps(data, cls=ModelEncoder, **kargs)

    def get_full_url(self):
        absolute_url = self.get_absolute_url()
        site = Site.objects.get_current().domain
        return 'https://{site}{path}'.format(site=site, path=absolute_url)

    def get_elasticsearch_id(self, language_code=None):
        if language_code is None:
            language_code = 'ALL'
            if hasattr(self, 'language_code'):
                language_code = self.language_code
        return (str(type(self).__name__) + '-' + str(self.id) + '-' +
                language_code)

    def get_elasticsearch_doc(self, language_code=None):
        """
        Returns the elasticsearch index corresponding to that document, returns
        None if it doesn't exist
        """
        return SearchIndex.get(
            id=self.get_elasticsearch_id(language_code=language_code),
            ignore=404)

    def get_elasticsearch_kwargs(self):
        """
        Returns the arguments that should be pass when creating the
        elasticearch document
        """
        kwargs = {}
        if hasattr(self, 'name'):
            kwargs['name'] = self.name
        if hasattr(self, 'title'):
            kwargs['title'] = self.title
        if hasattr(self, 'description'):
            kwargs['description'] = remove_tags(self.description)
        if hasattr(self, 'language_code'):
            kwargs['language_code'] = self.language_code
        else:
            kwargs['language_code'] = 'ALL'
        kwargs['url'] = self.get_absolute_url()
        # kwargs['boost'] = boost

        return kwargs

    def index_in_elasticsearch(self, boost=1):
        """
        Indexes a document in elasticearch with the info of this object
        """
        kwargs = self.get_elasticsearch_kwargs()
        doc = SearchIndex(boost=boost, **kwargs)
        try:
            doc.save(obj=self)
        except (TransportError, ConnectionError) as e:
            error_logger.error(
                'Couldn\'t index document {id} in elasticsearch: {err}'.format(
                    id=self.get_elasticsearch_id(), err=str(e)),
                exc_info=False)

    def deindex_in_elasticsearch(self):
        """
        Deletes the elasticsearch document related to this object if it exists
        """
        languages = ('es', 'en', 'ALL')
        try:
            for language in languages:
                SearchIndex().delete(
                    id=self.get_elasticsearch_id(language_code=language),
                    ignore=404)
        except (TransportError, ConnectionError) as e:
            error_logger.error(
                'Couldn\'t delete document {id} in elasticsearch: {err}'.
                format(id=self.get_elasticsearch_id(), err=str(e)),
                exc_info=False)