[code] orari dopo le 24

che mufhd0 ce la manni bona
se si rompono cose, QUESTO è il commit da revertare

madonna la monnezza che ho fatto
This commit is contained in:
boyska 2018-07-06 17:00:32 +02:00
parent f3f33bae33
commit eb3754ce0b
2 changed files with 132 additions and 81 deletions

View file

@ -100,6 +100,25 @@ def get_global_data():
return data return data
def _get_time_shift(timestring):
''' Il problema che abbiamo è che vogliamo dire che le 2 di notte del sabato sono in realtà "parte" del
venerdì. Per farlo accettiamo orari che superano le 24, ad esempio 25.30 vuol dire 1.30.
Questa funzione ritorna una timedelta in base alla stringa passata
'''
timeparts = re.findall(r'\d+', timestring)
if not timeparts or len(timeparts) > 2:
raise ValueError("Malformed time %s" % timestring)
timeparts += [0,0] # "padding" per essere sicuro ci siano anche [1] e [2]
duration = datetime.timedelta(hours=int(timeparts[0]),
minutes=int(timeparts[1]),
seconds=int(timeparts[2]))
if duration.total_seconds() > 3600 * 31 or duration.total_seconds() < 0:
raise ValueError("Sforamento eccessivo: %d" % duration.hours)
return duration
@memoize @memoize
def get_talk_data(talkname): def get_talk_data(talkname):
fname = os.path.join(pelican.settings['TALKS_PATH'], talkname, 'meta.yaml') fname = os.path.join(pelican.settings['TALKS_PATH'], talkname, 'meta.yaml')
@ -155,15 +174,18 @@ def get_talk_data(talkname):
else: else:
data['day'] = get_global_data()['startdate'] + \ data['day'] = get_global_data()['startdate'] + \
datetime.timedelta(days=data['day']) datetime.timedelta(days=data['day'])
timeparts = re.findall(r'\d+', str(data['time'])) try:
if 4 > len(timeparts) > 0: shift = _get_time_shift(str(data['time']))
timeparts = [int(p) for p in timeparts] except ValueError:
data['time'] = datetime.datetime.combine(
data['day'], datetime.time(*timeparts))
data['time'] = data['time'].replace(
tzinfo=dateutil.tz.gettz('Europe/Rome'))
else:
logging.error("Talk <%s> has malformed `time`", talkname) logging.error("Talk <%s> has malformed `time`", talkname)
data['delta'] = shift
data['time'] = datetime.datetime.combine(
data['day'], datetime.time(0,0,0))
data['time'] += shift
data['time'] = data['time'].replace(tzinfo=dateutil.tz.gettz('Europe/Rome'))
data['id'] = talkname data['id'] = talkname
resdir = os.path.join(pelican.settings['TALKS_PATH'], talkname, resdir = os.path.join(pelican.settings['TALKS_PATH'], talkname,
pelican.settings['TALKS_ATTACHMENT_PATH']) pelican.settings['TALKS_ATTACHMENT_PATH'])
@ -282,6 +304,27 @@ class TalkDirective(Directive):
] ]
def _delta_to_position(delta):
gridstep = pelican.settings['TALKS_GRID_STEP']
sec = delta.total_seconds() // gridstep * gridstep
return int('%2d%02d' % (sec // 3600, (sec % 3600) // 60))
def _delta_inc_position(delta, i):
gridstep = pelican.settings['TALKS_GRID_STEP']
delta = delta + datetime.timedelta(minutes=i*gridstep)
sec = delta.total_seconds() // gridstep * gridstep
return int('%2d%02d' % (sec // 3600, (sec % 3600) // 60))
def _approx_timestr(timestr):
gridstep = pelican.settings['TALKS_GRID_STEP']
t = str(timestr)
minutes = int(t[-2:])
hours = t[:-2]
minutes = minutes // gridstep * gridstep
return int('%s%02d' % (hours, minutes))
class TalkGridDirective(Directive): class TalkGridDirective(Directive):
'''A complete grid''' '''A complete grid'''
final_argument_whitespace = True final_argument_whitespace = True
@ -291,6 +334,7 @@ class TalkGridDirective(Directive):
} }
def run(self): def run(self):
try:
lang = self.options.get('lang', 'C') lang = self.options.get('lang', 'C')
tmpl = jinja_env().get_template('grid.html') tmpl = jinja_env().get_template('grid.html')
output = [] output = []
@ -318,21 +362,21 @@ class TalkGridDirective(Directive):
# Remove it unless that day only has special rooms # Remove it unless that day only has special rooms
if '*' in rooms and len(rooms) > 1: if '*' in rooms and len(rooms) > 1:
del rooms[rooms.index('*')] del rooms[rooms.index('*')]
mintime = min({talk['time'].hour * 60 + mintimedelta = min({talk['delta'] for talk in talks})
talk['time'].minute maxtimedelta = max({talk['delta'] + datetime.timedelta(minutes=talk['duration']) for talk in talks})
for talk in talks}) // gridstep * gridstep mintime = _delta_to_position(mintimedelta)
maxtime = max({talk['time'].hour * 60 + maxtime = _delta_to_position(maxtimedelta)
talk['time'].minute +
talk['duration']
for talk in talks})
times = {} times = {}
for t in range(mintime, maxtime, gridstep): t = mintimedelta
times[t] = [None] * len(rooms) while t <= maxtimedelta:
for talk in sorted(talks, key=lambda x: x['time']): times[_delta_to_position(t)] = [None] * len(rooms)
talktime = talk['time'].hour * 60 + talk['time'].minute t += datetime.timedelta(minutes=gridstep)
position = talktime // gridstep * gridstep # round print(list(times.keys()))
assert position in times for talk in sorted(talks, key=lambda x: x['delta']):
talktime = _delta_to_position(talk['delta'])
position = _approx_timestr(talktime)
assert position in times, 'pos=%d,time=%d' % (position, talktime)
if talk['room'] == '*': if talk['room'] == '*':
roomnums = range(len(rooms)) roomnums = range(len(rooms))
elif type(talk['room']) is list: elif type(talk['room']) is list:
@ -350,8 +394,10 @@ class TalkGridDirective(Directive):
times[position][roomnum] = copy(talk) times[position][roomnum] = copy(talk)
times[position][roomnum]['skip'] = False times[position][roomnum]['skip'] = False
for i in range(1, talk['duration'] // gridstep): for i in range(1, talk['duration'] // gridstep):
times[position + i*gridstep][roomnum] = copy(talk) p = _approx_timestr(_delta_inc_position(talk['delta'], i))
times[position + i*gridstep][roomnum]['skip'] = True times[p][roomnum] = copy(talk)
times[p][roomnum]['skip'] = True
render = tmpl.render(times=times, render = tmpl.render(times=times,
rooms=rooms, rooms=rooms,
@ -364,6 +410,11 @@ class TalkGridDirective(Directive):
locale=lang), locale=lang),
format='html')) format='html'))
output.append(nodes.raw('', render, format='html')) output.append(nodes.raw('', render, format='html'))
except:
logging.exception("Error on talk grid")
import traceback
traceback.print_exc()
return []
return output return output

View file

@ -8,10 +8,10 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for time in range (mintime, maxtime, timestep) %} {% for time in times %}
<tr> <tr>
<td>{{time//60}}:{{ "%02d" % (time % 60)}}</td> <td>{{time}}</td>
{% for talk in times[time / timestep * timestep] %} {% for talk in times[time] %}
{% if not loop.first and talk.room == '*' %} {% if not loop.first and talk.room == '*' %}
{# skip: covered by colspan #} {# skip: covered by colspan #}
{% elif talk == None %} {% elif talk == None %}