| [13632] | 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | from trac.db import Table, Column, Index |
|---|
| [14089] | 4 | from trac.util.datefmt import from_utimestamp, to_utimestamp |
|---|
| [13632] | 5 | from trac.wiki.formatter import format_to_html |
|---|
| 6 | |
|---|
| 7 | SCHEMA = [ |
|---|
| 8 | Table('weekplan', key='id')[ |
|---|
| 9 | Column('id', auto_increment=True), |
|---|
| 10 | Column('plan'), |
|---|
| 11 | Column('title'), |
|---|
| 12 | Column('start', type='int64'), |
|---|
| 13 | Column('end', type='int64'), |
|---|
| [14089] | 14 | Index(['plan', 'start', 'end']), |
|---|
| [13632] | 15 | ], |
|---|
| 16 | ] |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | class WeekPlanEvent(object): |
|---|
| 20 | |
|---|
| 21 | def __init__(self, id, plan, title, start, end): |
|---|
| 22 | self.id = id |
|---|
| 23 | self.plan = plan |
|---|
| 24 | self.title = title |
|---|
| 25 | self.start = start |
|---|
| 26 | self.end = end |
|---|
| 27 | |
|---|
| 28 | def serialized(self, env, context): |
|---|
| 29 | return { |
|---|
| 30 | 'id': self.id, |
|---|
| 31 | 'title': self.title, |
|---|
| 32 | 'title_html': format_to_html(env, context, self.title), |
|---|
| [14089] | 33 | 'start': self.start.date().isoformat(), |
|---|
| 34 | 'end': self.end.date().isoformat(), |
|---|
| [13632] | 35 | 'plan': self.plan, # Custom field |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | @classmethod |
|---|
| 39 | def add(cls, env, event): |
|---|
| 40 | with env.db_transaction as db: |
|---|
| 41 | cursor = db.cursor() |
|---|
| 42 | cursor.execute(""" |
|---|
| 43 | INSERT INTO weekplan |
|---|
| [14780] | 44 | (plan, title, start, %s) |
|---|
| 45 | VALUES (%%s, %%s, %%s, %%s) |
|---|
| 46 | """ % db.quote('end'), (event.plan, event.title, to_utimestamp(event.start), to_utimestamp(event.end))) |
|---|
| [13632] | 47 | event.id = db.get_last_id(cursor, 'weekplan') |
|---|
| 48 | |
|---|
| 49 | @classmethod |
|---|
| 50 | def update(cls, env, event): |
|---|
| 51 | with env.db_transaction as db: |
|---|
| 52 | cursor = db.cursor() |
|---|
| 53 | cursor.execute(""" |
|---|
| 54 | UPDATE weekplan |
|---|
| [14780] | 55 | SET plan=%%s, title=%%s, start=%%s, %s=%%s |
|---|
| 56 | WHERE id=%%s |
|---|
| 57 | """ % db.quote('end'), (event.plan, event.title, to_utimestamp(event.start), to_utimestamp(event.end), event.id)) |
|---|
| [13632] | 58 | |
|---|
| 59 | @classmethod |
|---|
| 60 | def delete_by_id(cls, env, event_id): |
|---|
| 61 | with env.db_transaction as db: |
|---|
| 62 | db(""" |
|---|
| 63 | DELETE FROM weekplan |
|---|
| 64 | WHERE id=%s |
|---|
| 65 | """, (event_id,)) |
|---|
| 66 | |
|---|
| 67 | @classmethod |
|---|
| 68 | def select_by_plan(cls, env, plan): |
|---|
| [14780] | 69 | with env.db_query as db: |
|---|
| 70 | rows = db(""" |
|---|
| 71 | SELECT id, plan, title, start, %s |
|---|
| 72 | FROM weekplan |
|---|
| 73 | WHERE plan=%%s |
|---|
| 74 | """ % db.quote('end'), (plan,)) |
|---|
| 75 | return [WeekPlanEvent(id, plan, title, from_utimestamp(start), from_utimestamp(end)) for id, plan, title, start, end in rows] |
|---|
| [13632] | 76 | |
|---|
| 77 | @classmethod |
|---|
| [14089] | 78 | def select_by_plans_and_time(cls, env, plans, start, end): |
|---|
| [14780] | 79 | if not plans: |
|---|
| 80 | return [] |
|---|
| 81 | with env.db_query as db: |
|---|
| 82 | plan_holder = ','.join(['%s'] * len(plans)) |
|---|
| 83 | rows = db(""" |
|---|
| 84 | SELECT id, plan, title, start, %s |
|---|
| 85 | FROM weekplan |
|---|
| 86 | WHERE plan IN (%s) AND start < %%s AND %s > %%s |
|---|
| 87 | """ % (db.quote('end'), plan_holder, db.quote('end')), |
|---|
| 88 | list(plans) + [to_utimestamp(end), to_utimestamp(start)]) |
|---|
| 89 | return [WeekPlanEvent(id, plan, title, from_utimestamp(start), from_utimestamp(end)) for id, plan, title, start, end in rows] |
|---|