File: PgNewsHandler.py

package info (click to toggle)
mbot 0.3-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 212 kB
  • ctags: 119
  • sloc: python: 595; makefile: 102; php: 98; sql: 59
file content (100 lines) | stat: -rw-r--r-- 2,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python

# mbot - a mail handling robot
#
# Author:  Dimitri Fontaine <dim@tapoueh.org>
# Author:  Christophe Truffier <toffe@nah-ko.org>
#
# This code is licensed under the GPL.
# Get yourself a version here : http://www.gnu.org/copyleft/gpl.html

# $Id: PgNewsHandler.py,v 1.4 2003/12/22 15:23:58 nah-ko Exp $

import NewsHandler
import sys, os, email
import ConfigParser

import pg
from pg import INV_WRITE

class PgNewsHandler(NewsHandler.NewsHandler):
    """ Manage adding news in a PostgreSQL data base """

    def dbconn(self):
        """ Connect to the data base """

        self.log.notice("[PgNewsHandler]: dbconn")
        db = pg.connect(dbname=self.db, host=self.host,
                        user=self.db_user, passwd=self.db_pass)

        return db

    def execQuery(self, sql):
        """ Execute the given query """

        self.log.notice("[PgNewsHandler]: execQuery")
        db      = self.dbconn()
        req     = db.query(sql)
        self.id = self.getid(db, self.news_tblsq)
        db.close()

        return self.id

    def getid(self, conn, table=None):
        """ Get the next available news Id """

        self.log.notice("[PgNewsHandler]: getid")

        # result return a tuple which contains (value,?)  where
        # 'value' is the ID.
        # So we get the value with getresult()[0][0]
        id = conn.query("select currval('%s')" % table).getresult()[0][0]

        self.log.debug("[PgNewsHandler]: getid -> id='%d'" % id)
        return id

    def add_img(self, filename, filetype, filedata, TNfiledata, filesize):
        """ Add an image as a Large Object in the database """ 

        self.log.notice("[PgNewsHandler]: add_img")

        news_id	= self.id
        desc	= "[News] " + filename
        
        db      = self.dbconn()
        db.query("begin")

        # We create Large Object
        img_LO   = db.locreate(INV_WRITE)
        TNimg_LO = db.locreate(INV_WRITE)

        img_LO.open(INV_WRITE)
        img_LO.write(filedata)
        img_LO.close()
        
        TNimg_LO.open(INV_WRITE)
        TNimg_LO.write(TNfiledata)
        TNimg_LO.close()
        
        # The SQL query
        sql = """
        INSERT INTO %s (description, img_data, tnimg_data,
                        filename, filesize,filetype)
        VALUES ('%s','%s','%s','%s','%d','%s')
        """ % (self.photo_tbl, desc, img_LO.oid, TNimg_LO.oid,
               filename, filesize, filetype)
        req      = db.query(sql)

        db.query("commit")

        # Now we add the link to the image from the news table
        id      = self.getid(db, self.photo_tblsq)
        self.log.debug("[PgNewsHandler]: add_img => id='%d'" % id)
        myquery	= "UPDATE %s SET id_img='%d' WHERE id='%d'" \
                  % (self.news_tbl, id, news_id)
        
        req     = db.query(myquery)
        db.close()

        return id