【问题标题】:How to put hours of work into table?如何将工作时间放入表中?
【发布时间】:2015-06-11 10:12:29
【问题描述】:

我有模特:

class LoginLogout(models.Model):
    user = models.ForeignKey(User)
    t_login = models.DateTimeField(default=datetime.now)
    t_logout = models.DateTimeField(blank=True, null=True)

我想生成这样的表格:

2015-01-02

+-------+- USER1 -+- USER2 -+- USER3 -+
| 00:30 |         |         |         |
| 01:00 | ####### |         |         |
| 01:30 | ####### |         |         |
| 02:00 | ####### | ####### |         |
| 02:30 | ####### | ####### |         |
| 03:00 |         | ####### |         |
| 03:30 |         |         |         |
| 04:00 |         |         |         |
| 04:30 |         |         |         |
| 05:00 |         |         | ####### |
| 05:30 |         |         | ####### |
| 06:00 |         |         |         |
| 06:30 |         |         |         |
| [...] |         |         |         |
+-------+---------+---------+---------+

我可以生成小时数(感谢 qaphla - Create a select list in half-hour increments in python

[((str(i / 60 % 24 + 1) if (i / 60 % 24 + 1) > 9 else "0" + str(i / 60 % 24 + 1)) + ":" + (str(i % 60) if i % 60 > 9 else "0" + str(i % 60)) ) for i in xrange(0, 1360, 30)]

在模板中:

<table class="table table-bordered">
    <tbody>
        <tr>
            <th>Hour</th>
            {% for user in users %}
                <th>
                    {{ user }}
                </th>
            {% endfor %}
        </tr>
        {% for hour in hours %}
            <tr>
                <td>{{ hour }}</td>
                {% for user in users %}
                    <td>

                    </td>
                {% endfor %}
            </tr>
        {% endfor %}
    </tbody>
</table>

我不知道如何将工作时间放在表中。怎么做?

【问题讨论】:

    标签: django django-templates django-template-filters


    【解决方案1】:

    首先,关于你的这张桌子:

    2015-01-02
    
    +-------+- USER1 -+- USER2 -+- USER3 -+
    | 00:30 |         |         |         |
    | 01:00 | ####### |         |         |
    | 01:30 | ####### |         |         |
    | 02:00 | ####### | ####### |         |
    | 02:30 | ####### | ####### |         |
    | 03:00 |         | ####### |         |
    | 03:30 |         |         |         |
    | 04:00 |         |         |         |
    | 04:30 |         |         |         |
    | 05:00 |         |         | ####### |
    | 05:30 |         |         | ####### |
    | 06:00 |         |         |         |
    | 06:30 |         |         |         |
    | [...] |         |         |         |
    +-------+---------+---------+---------+
    

    我不认为您打算每天制作一张桌子,而是建议您使用这张桌子:

    它将每天在一个表中跟踪用户。模型定义是这样的:

    def time_slots(*args):
        class TimeSlot(models.Model):
            class Meta:
                abstract = True
    
        for slot in args[0]:
            models.BooleanField(
            default=False
        ).contribute_to_class(TimeSlot, slot)
        return TimeSlot
    slots = ['%s:%s%s' % (h, m, ap) for ap in ('am', 'pm') for h in ([12] + [x for x in range(1,12)]) for m in ('00', '30')]
    class UserTableSlot(time_slots(slots)):
        user = models.ForeignKey(User)
        date = models.DateField()
    

    好吧,如果你打算手动输入值到表中,那真的很麻烦。而使用celery 将是一个更好的选择。

    例如,如果你使用celery的periodic task,那么每天12点,你就可以得到前一天所有的用户登录信息,输入表格如下:

    from celery.schedules import crontab
    @periodic_task(run_every=crontab(minute=0, hour=0))
    def update_user_time_slots():
            for user in Users.objects.all():
                 for item in LoginLogout.objects.filter(user=user, t_login__gte=datetime.datetime.now()-timedelta(days=1)):
                      slots = ['%s:%s%s' % (h, m, ap) for ap in ('am', 'pm') for h in ([12] + [x for x in range(1,12)]) for m in ('00', '30')]
                      for s in slots:
                          if time.strptime(s, "%I:%M") > time.strptime((item.t_login.strftime('%H:%M')),'%H:%M'):
                               t_slot, _is_created = UserTimeSlot.objects.get_or_create(user=user, date=datetime.datetime.now().date())
                               if time.strptime(s, "%I:%M") < time.strptime((item.t_logout.strftime('%H:%M')),'%H:%M'):
                                     break
                                setattr(t_slot, s, True)
    

    【讨论】:

      猜你喜欢
      • 2017-11-16
      • 1970-01-01
      • 2022-01-05
      • 2012-08-18
      • 2021-07-25
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多