Add color to items without picture

This commit is contained in:
Gabriel Augendre 2022-04-28 18:34:13 +02:00
parent 406f2b8d78
commit de3d7e4058
4 changed files with 29 additions and 0 deletions

View file

@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
import hashlib
from django.db import models from django.db import models
from django.db.models import Avg, Count, F, Sum from django.db.models import Avg, Count, F, Sum
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
@ -102,6 +104,12 @@ class Product(Model):
def natural_key(self): def natural_key(self):
return (self.name,) return (self.name,)
@property
def color_hue(self):
return int(
hashlib.sha256(bytes(self.name, encoding="utf-8")).hexdigest()[:2], base=16
)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super().save() super().save()
if not self.image: if not self.image:

View file

@ -0,0 +1,10 @@
.product-img-placeholder {
aspect-ratio: 1/1;
font-size: 6em;
display: flex;
flex-direction: column;
justify-content: space-around;
}
.product-img-placeholder span {
text-align: center;
}

View file

@ -1,6 +1,10 @@
{% extends "common/base.html" %} {% extends "common/base.html" %}
{% load static %}
{% load i18n %} {% load i18n %}
{% load crispy_forms_tags purchase %} {% load crispy_forms_tags purchase %}
{% block extrahead %}
<link rel="stylesheet" href="{% static "purchase/css/basket_form.css" %}">
{% endblock %}
{% block content %} {% block content %}
{% if object %} {% if object %}
<h1>{{ object }} <span class="badge bg-secondary">{{ object.price|currency }}</span></h1> <h1>{{ object }} <span class="badge bg-secondary">{{ object.price|currency }}</span></h1>

View file

@ -3,6 +3,13 @@
<div class="card h-100 {% if field.value %}bg-success text-white{% endif %}"> <div class="card h-100 {% if field.value %}bg-success text-white{% endif %}">
{% if product.image %} {% if product.image %}
<img src="{{ product.image.url }}" class="card-img"> <img src="{{ product.image.url }}" class="card-img">
{% else %}
<div class="card-img product-img-placeholder"
style="background-color: hsl({{ product.color_hue }}, 60%, 80%)">
<span>
{{ product.name|slice:"1" }}
</span>
</div>
{% endif %} {% endif %}
<div class="card-body"> <div class="card-body">
<h4 class="card-title">{{ product.name }}</h4> <h4 class="card-title">{{ product.name }}</h4>