上下文处理器仅在服务器重启时更新

时间:2015-07-13 07:35:12

标签: django

我有一个由数据库驱动的上下文处理器。

def on_air(request):              
    on_air = OnAir()              
    return {                      
        'on_air': {               
            'now': on_air.now(),  
            'next': on_air.next(),
        },                        
    }   

编辑:这是OnAir类

from collections import defaultdict

from datetime import datetime, time

from .models import ShowPage


def schedule():
    schedule = defaultdict(list)
    shows = ShowPage.objects.order_by(
        'weekday_start',
        'saturday_start',
        'sunday_start').all()

    for show in shows:
        for day in ('weekday', 'saturday', 'sunday',):
            if getattr(show, '{day}_start'.format(day=day)):
                schedule[day].append({
                    'show': show,
                    'start': getattr(show, '{day}_start'.format(day=day)),
                    'end': getattr(show, '{day}_end'.format(day=day)),
                })

    return schedule


class OnAir:

    def __init__(self, schedule=schedule()):
        self.schedule = schedule
        self.isoweekday = datetime.now().isoweekday()
        self.current_time = datetime.now().time()

    def now(self):
        shows = [item for item in self.schedule[self._today()]
                 if item['start'] <= self.current_time
                 and item['end'] > self.current_time]

        val = None
        if shows:
            val = shows[0]
        else:
            if self.current_time > time(12, 0):
                # If no shows are found and it is after midday,
                # then it means that the last show wraps over into the new day,
                # eg. from 11pm to 1am the next morning
                val = self._last_show_of_the_day(self._today())
            else:
                # It is the crack of dawn and yesterday's last show
                # started at say 11pm and carried through to say 3am today
                val = self._last_show_of_the_day(self._yesterday())

        return val['show'] if val else None

    def next(self):
        shows = [item for item in self.schedule[self._today()]
                 if item['start'] > self.current_time]

        val = None
        if shows:
            val = shows[0]
        else:
            val = self._first_show_of_the_day(self._tomorrow())

        return val['show'] if val else None

    def _isoweekday_to_key(self, isoweekday):
        if isoweekday == 0:
            isoweekday = 7

        if isoweekday == 8:
            isoweekday = 1

        val = 'weekday'

        if isoweekday == 6:
            val = 'saturday'

        if isoweekday == 7:
            val = 'sunday'

        return val

    def _today(self):
        return self._isoweekday_to_key(self.isoweekday)

    def _yesterday(self):
        return self._isoweekday_to_key(self.isoweekday - 1)

    def _tomorrow(self):
        return self._isoweekday_to_key(self.isoweekday + 1)

    def _first_show_of_the_day(self, today):
        today = self.schedule[today]
        return today[0] if today else None

    def _last_show_of_the_day(self, today):
        today = self.schedule[today]
        return today[-1] if today else None

如果我更新数据库并刷新页面,则没有任何变化。我必须重新启动开发服务器才能显示更改。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

问题在于__init__类的OnAir方法。

class OnAir:
    def __init__(self, schedule=schedule()):
加载模块时会对{p> schedule()进行一次评估,而不是每次都按照您的预期创建OnAir实例。

您可以通过将默认值更改为None,然后在__init__方法中创建计划来解决此问题。

class OnAir:
    def __init__(self, schedule=None):
        if schedule is None:
            schedule = schedule()
        ...