示例#1
0
class AbstractContent(models.Model):
    # Generic Foreign Key Fields
    content_type = models.ForeignKey(
        ContentType,
        null=True,
        blank=True,
        related_name="%(app_label)s_%(class)s_cntype",
        on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField(_('object ID'),
                                            null=True,
                                            blank=True)
    content_object = GenericForeignKey('content_type', 'object_id')
    primary = models.BooleanField(_('Is primary'), default=False)

    class Meta:
        abstract = True

    objects = AbstractContentManager()

    def __str__(self):
        # noinspection PyBroadException
        try:
            return "%s - %s " % (self.content_object.get_name, self.pk)
        except:
            return None

    def get_content_object(self):
        return self.content_object
示例#2
0
class Doc(AbstractContent, AbstractFile):
    filetype = models.IntegerField(_("Doc type"),
                                   choices=DOC_TYPE,
                                   default=DOC_FILE)
    doc = models.FileField(_("File"),
                           upload_to='doc/' + get_date_directory(),
                           max_length=1024,
                           blank=True)

    class Meta:
        ordering = [
            'position',
        ]
        verbose_name = _("Doc")
        verbose_name_plural = _("Docs")

    objects = AbstractContentManager()

    def save(self, *args, **kwargs):
        # noinspection PyBroadException
        try:
            docs = Doc.objects.for_object(self.content_object)
            if self.pk:
                docs = docs.exclude(pk=self.pk)
            if DOC_MAX_PER_OBJECT > 1:
                if self.primary:
                    docs = docs.filter(primary=True)
                    docs.update(primary=False)
            else:
                docs.delete()
        except:
            pass
        fullpath = os.path.join(settings.MEDIA_ROOT, self.doc.field.upload_to,
                                self.doc.path)
        self.size = os.path.getsize(fullpath)
        super(Doc, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        # noinspection PyBroadException
        try:
            remove_file(self.doc.path)
        except:
            pass
        super(Doc, self).delete(*args, **kwargs)

    def get_absolute_url(self):
        return reverse(os.path.join(settings.MEDIA_URL, self.doc.url))

    def get_file_link(self):
        return os.path.join(settings.MEDIA_URL, self.doc.url)

    def get_del_url(self):
        return reverse("doc_del", args=[self.id])

    def get_edit_url(self):
        return reverse("doc_edit", args=[self.id])
示例#3
0
class Pic(AbstractContent, AbstractFile, AbstractImg):
    source = models.URLField(verbose_name=_("Source"),
                             max_length=256,
                             blank=True)

    objects = AbstractContentManager()

    class Meta:
        ordering = [
            'created_date',
        ]
        verbose_name = _("Pic")
        verbose_name_plural = _("Pics")

    def __str__(self):
        return _('Pic for %(type)s: %(obj)s') % {
            'type': self.content_type,
            'obj': self.content_object
        }

    def get_file_link(self):
        return os.path.join(settings.MEDIA_URL, self.img.url)

    def save(self, *args, **kwargs):
        pics = Pic.objects.for_object(self.content_object)
        if self.pk:
            pics = pics.exclude(pk=self.pk)
        if IMG_MAX_PER_OBJECT > 1:
            if self.primary:
                pics = pics.filter(primary=True)
                pics.update(primary=False)
        else:
            pics.delete()
        # noinspection PyBroadException
        try:
            remove_thumbnails(self.img.path)
        except:
            pass
        fullpath = get_path_from_url(self.img.url)
        self.size = os.path.getsize(fullpath)
        super(Pic, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        # noinspection PyBroadException
        try:
            remove_thumbnails(self.img.path)
            remove_file(self.img.path)
        except:
            pass
        super(Pic, self).delete(*args, **kwargs)

    def create_thumbnail(self, size, quality=None):
        try:
            orig = self.img.storage.open(self.img.name, 'rb').read()
            image = Image.open(StringIO(orig))
        except IOError as ioerr:
            return  # What should we do here?  Render a "sorry, didn't work" img?
        quality = quality or IMG_THUMB_QUALITY
        (w, h) = image.size
        if w != size or h != size:
            if w > h:
                diff = (w - h) / 2
                image = image.crop((diff, 0, w - diff, h))
            else:
                diff = (h - w) / 2
                image = image.crop((0, diff, w, h - diff))
            if image.mode != "RGB":
                image = image.convert("RGB")
            image = image.resize((size, size), IMG_RESIZE_METHOD)
            thumb = StringIO()
            image.save(thumb, IMG_THUMB_FORMAT, quality=quality)
            thumb_file = ContentFile(thumb.getvalue())
        else:
            thumb_file = ContentFile(orig)
        thumb = self.img.storage.save(self.img.name(size), thumb_file)

    def get_del_url(self):
        return "pic_del", (), {'object_id': self.pk}

    def get_edit_url(self):
        return reverse("pic_edit", args=[self.pk])

    def get_view_url(self):
        return reverse("pic_view", args=[self.pk])

    def get_editor_url(self):
        return reverse("pic_editor", args=[self.pk])

    def slide_thumbnail(self):
        if self.img:
            path = self.img.url
            tmb = make_thumbnail(path, width=60, height=60, aspect=1)
        else:
            tmb = '/static/img/icon-no.gif"'
            path = '/static/img/icon-no.gif"'
        return '<a target="_blank" href="%s"><img src="%s" /></a>' % (path,
                                                                      tmb)

    slide_thumbnail.allow_tags = True
示例#4
0
class FlatNnmcomment(AbstractNnmcomment):
    pass

    objects = AbstractContentManager()