Where to store color information in a web application?

Question:

I’ve been working on a Django app for a scheduling system (using AJAX dhtmlScheduler library) and I need to color code the types of events. As part of each event, the client expects me to return a #00F162 string indicating the color of the event. This is then parsed by the client and displayed by the Javascript.

The styling guide is described below:
http://docs.dhtmlx.com/doku.php?id=dhtmlxscheduler:custom_styling

The first option is to store a hex value in the DB, perhaps in the event_type database. The second option, is to place the logic for this in my application and calculate it based upon the shift selected.

Storing the entry in event_type database seems strange, as I feel like I am mixing up the appearance with the model and the colors will not change. The second option means I am hard-coding the values in the app.

Which would be the best approach?

Asked By: james_dean

||

Answers:

There are a couple of ways to do this.

If the color for an event_type needs to be editable, I would store it in the database as a varchar.

If the color is up to you, then I would probably slugify the __unicode__ of event_type and use CSS to target that class. This does present you with a small amount of maintenance, should a new event_type need to be added, but it gives you the separation of concerns.

Answered By: Brandon Taylor

In this particular case I would store the hex colour code in a field within a model.

In essence:

class Event(models.Model):
    ALERT   = "alert"
    WARNING = "warning"
    ERROR   = "error"
    EVENT_TYPES = (
        (ALERT,   "Alert"),
        (WARNING, "Warning"),
        (ERROR,   "Error"),
    )

    YELLOW = "FF6A00"
    ORANGE = "FFE800"
    RED    = "FF0000"
    COLOURS = (
        (YELLOW, "Yellow"),
        (ORANGE, "Orange"),
        (RED,    "Red"),
    )

    event_type = models.CharField(max_length=16, choices=EVENT_TYPES, default=ALERT)
    event_colour = models.CharField(max_length=6, choices=COLOURS, default=YELLOW)

Additional note, the reason for the “constants” is to make code that uses this model clean and simple.

# example 1
error_events = Event.objects.filter(event_type=Event.ERROR)

# example 2
if my_event.event_type == Event.Error:
    # this is an error event
    pass

Also, here’s one way you could do it without a colour field on the model:

class Event(models.Model):
    ALERT   = "alert"
    WARNING = "warning"
    ERROR   = "error"
    EVENT_TYPES = (
        (ALERT,   "Alert"),
        (WARNING, "Warning"),
        (ERROR,   "Error"),
    )

    # map events to colours
    COLOUR = {
        ALERT:   "FF6A00",
        WARNING: "FFE800",
        ERROR:   "FF0000",
    }

    event_type = models.CharField(max_length=16, choices=EVENT_TYPES, default=ALERT)

    @property
    def colour(self):
        """
        Return the hexadecimal colour of this event
        """
        self.COLOUR[event_type]

# now this would return True
my_error_event.colour == "FF0000"
Answered By: Matt Deacalion

I came across the following python package which may help out with a colour field in Django:

https://pypi.org/project/django-colorfield/

It looks straightforward enough although I haven’t fully tested it as of yet.

Answered By: kiro
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.