2015-04-06 20 views
0

我有模式:如何將幾個小時的工作放到表格中?

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> 

我不知道如何將數小時的工作放到表格中。怎麼做?

回答

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) 
相關問題