source: weekplanplugin/trunk/weekplan/model.py

Last change on this file was 14780, checked in by lucid, 8 years ago

WeekPlanPlugin: PostgreSQL compatibility: quote "end" table name
Avoid SQL injection
Bump version to 1.2
(fix #12423)

File size: 3.1 KB
Line 
1# -*- coding: utf-8 -*-
2
3from trac.db import Table, Column, Index
4from trac.util.datefmt import from_utimestamp, to_utimestamp
5from trac.wiki.formatter import format_to_html
6
7SCHEMA = [
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'),
14        Index(['plan', 'start', 'end']),
15    ],
16]
17
18
19class 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),
33            'start': self.start.date().isoformat(),
34            'end': self.end.date().isoformat(),
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
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)))
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
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))
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):
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]
76
77    @classmethod
78    def select_by_plans_and_time(cls, env, plans, start, end):
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]
Note: See TracBrowser for help on using the repository browser.