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?
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"