如何把工作时间放到桌子上?

时间:2015-04-06 22:55:10

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

我有模特:

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>

我不知道如何把工作时间放到桌子上。怎么做?

1 个答案:

答案 0 :(得分:0)

首先,关于你的这张表:

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 |         |         |         |
| [...] |         |         |         |
+-------+---------+---------+---------+

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

enter image description here

每天都会在一张桌子中跟踪用户。模型定义如下:

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将是一个更好的选择。

例如,如果您使用芹菜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)