20140222 (Saturday, 22 February 2014)

Worked on a reported bug in Renamed “Lino Faggio” to “Lino Voga”: It seems that courses.Course.update_events does not always work correctly. To make this reliable, we need a serious suite of unit tests.

demo_date atelier.utils.date_offset().

Worked with Manuel on Lino Sunto:

Managing images of a product

How to manage a series of uploadable images per Product? Our idea after some brainstorming was to use a virtual HtmlBox:

class Product(dd.Model):
    ...
    @dd.virtualfield(dd.HtmlBox())
    def product_image(self, ar):
        url = self.get_image_url()
        img = E.a(E.img(src=url, width="100%"), href=url, target="_blank")
        sar = ar.spawn(ImagesByProduct, master_instance=self)

        toolbar = ar.renderer.quick_manage_toolbar(sar, self)
        return E.div(E.p(img), toolbar)

    def get_image_url(self):
        try:
            im = ProductImage.objects.get(product=self, default_image=True)
        except ProductImage.DoesNotExist:
            return "placeholder.jpg"
        return im.file.url

Which assumes the following database model and tables:

class ProductImage(dd.Uploadable):
    product = models.ForeignKey(Product)
    default_image = models.BooleanField(default=False)


class ProductImages(dd.Table):
    model = ProductImage

    insert_layout = """
    product
    default_image
    file
    """

    detail_layout = """
    product
    default_image
    file
    """


class ImagesByProduct(ProductImages):
    master_key = "product"

Remarks:

  • See lino.utils.xmlgen.html
  • ar is the calling request, i.e. a web request asking “give me the detail information about this product instance”
  • self is the Product instance
  • quick_manage_toolbar. is a new method we added together.