简体   繁体   English

在 web 应用程序中,在哪里存储颜色信息?

[英]Where to store color information in a web application?

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.我一直在为调度系统开发 Django 应用程序(使用 AJAX dhtmlScheduler 库),我需要对事件类型进行颜色编码。 As part of each event, the client expects me to return a #00F162 string indicating the color of the event.作为每个事件的一部分,客户希望我返回一个#00F162字符串,指示事件的颜色。 This is then parsed by the client and displayed by the Javascript.然后由客户端解析并由 Javascript 显示。

The styling guide is described below: http://docs.dhtmlx.com/doku.php?id=dhtmlxscheduler:custom_styling样式指南描述如下: 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.第一个选项是在数据库中存储一个十六进制值,可能在event_type数据库中。 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.将条目存储在event_type数据库中似乎很奇怪,因为我觉得我将外观与 model 混淆了,而 colors 不会改变。 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.在这种特殊情况下,我会将十六进制颜色代码存储在 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.附加说明,“常量”的原因是使使用此 model 的代码简洁明了。

# 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:此外,这是一种在 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"

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.如果 event_type 的颜色需要可编辑,我会将其作为 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.如果颜色由你决定,那么我可能会 slugify the __unicode__ of event_type 并使用 CSS 来定位 class。如果需要添加新的 event_type,这确实会给你带来少量维护,但它给你关注点分离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM