/[glsr]/trunk/site/news.py
Gentoo

Contents of /trunk/site/news.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 149 - (show annotations) (download) (as text)
Fri Sep 23 06:34:51 2005 UTC (9 years, 2 months ago) by hadfield
File MIME type: text/x-python
File size: 2937 byte(s)
more adminpanel implementation. implemented the news db access module.
1 # Copyright 2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 #
4
5 """The news DB access module.
6
7 Implements all functions necessary for creating/deleting/editing news.
8 """
9
10 __revision__ = "$Id$"
11 __authors__ = "Scott Hadfield <hadfield@gentoo.org>"
12 __modulename__ = "news"
13
14 from glsr.setup import config
15 from glsr.core.db.mysql import SQLdb
16
17 def create(uid, subject, body):
18 """Add a new news article to the database."""
19
20 sql_handle = SQLdb(config.db)
21
22 # TODO: Currently the date is auto-set. It might be desireable to have it
23 # be set by the person who created it.
24 sql_handle.query(
25 "INSERT INTO %(prefix)snews " % {'prefix': config.db['prefix']} +
26 "(news_author_id, news_subject, news_body, news_date)"
27 " VALUES (%s, %s, %s, NOW())",
28 (uid, subject, body), fetch='none')
29
30 results = sql_handle.query("SELECT LAST_INSERT_ID() as id", fetch = "one")
31 assert(results.has_key("id"))
32
33 return results["id"]
34
35 def exists(news_id):
36
37 result = SQLdb(config.db).query(
38 "SELECT news_id FROM %snews " % config.db["prefix"] +
39 "WHERE news_id = %s", news_id, fetch = "one")
40
41 if result is None:
42 return False
43 return True
44
45 def list_all(columns = None, constraint = None):
46 """Return selected columns from all rows in the news table.
47
48 This is copied from the user module.
49 """
50
51 # FIXME: Sanity checks on column names and contrainst keys.
52 # Without this we could have mysql injection attacks.
53
54 if columns is None:
55 return []
56 else:
57 columns = ", ".join(columns)
58
59 where_clause = ""
60 if constraint is not None:
61 where_clause = " AND ".join(["news_%s = %%s" % key
62 for key in constraint])
63 if where_clause != "":
64 where_clause = "WHERE " + where_clause
65 else:
66 constraint = {}
67
68 results = SQLdb(config.db).query(
69 "SELECT %(columns)s FROM %(prefix)snews %(where)s" %
70 {'columns': columns, 'prefix': config.db['prefix'],
71 'where': where_clause}, constraint.values(), fetch="all")
72
73 return results
74
75
76 class News:
77 """Defines the get and set attributes for a news item."""
78
79 def __init__(self, news_id):
80
81 self._db = SQLdb(config.db)
82 self._news_id = news_id
83
84 def update(self, uid, subject, body):
85
86 # FIXME: The updater of an article should be able to set the date.
87 self._db.query(
88 "UPDATE %(prefix)snews " % {'prefix': config.db['prefix']} +
89 "SET news_author_id = %s, news_subject = %s, news_body = %s, " +
90 "news_date = NOW() WHERE news_id = %s",
91 (uid, subject, body, self._news_id), fetch='none')
92
93 def delete(self):
94
95 self._db.query("DELETE FROM %snews " % config.db['prefix'] +
96 "WHERE news_id = %s", self._news_id, fetch = "none")

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20