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

Contents of /trunk/site/news.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 158 - (show annotations) (download) (as text)
Mon Oct 17 06:06:11 2005 UTC (9 years ago) by hadfield
File MIME type: text/x-python
File size: 4851 byte(s)
implemented the main page (i.e. news, script listing). Added a date attribute to news items.
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, date, 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, %s)",
28 (uid, subject, body, date), 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 columns = list(columns)
55 author = False
56 if "news_author" in columns:
57 columns.remove("news_author")
58 author = True
59
60 if columns is None:
61 return []
62 else:
63 columns = ", ".join(columns)
64
65 where_clause = ""
66 if constraint is not None:
67 where_clause = " AND ".join(["news_%s = %%s" % key
68 for key in constraint])
69 if where_clause != "":
70 where_clause = "WHERE " + where_clause
71
72 else:
73 constraint = {}
74
75 if author:
76 if where_clause == "":
77 where_clause = "WHERE user_id = news_author_id"
78 else:
79 where_clause += " AND user_id = news_author_id"
80
81 results = SQLdb(config.db).query(
82 "SELECT %s, user_alias as news_author " % columns +
83 "FROM %(prefix)snews, %(prefix)suser %(where)s " %
84 {'prefix': config.db['prefix'], 'where': where_clause},
85 constraint.values(), fetch="all")
86 else:
87 results = SQLdb(config.db).query(
88 "SELECT %(columns)s FROM %(prefix)snews %(where)s" %
89 {'columns': columns, 'prefix': config.db['prefix'],
90 'where': where_clause}, constraint.values(), fetch="all")
91
92 return results
93
94 def get_latest(count):
95 """Returns the most recent news entries, up to 'count'."""
96
97 from user import User
98
99 results = SQLdb(config.db).query(
100 "SELECT news_author_id, news_date, news_subject, news_body " +
101 "FROM glsr_news ORDER BY news_date DESC")
102
103 results = results[:count]
104
105 for result in results:
106 result["news_author"] = User(result["news_author_id"]).get_alias()
107
108 return results
109
110 def get_headlines():
111 """Returns all of the headers in chronological order."""
112
113 results = SQLdb(config.db).query(
114 "SELECT news_subject " +
115 "FROM glsr_news ORDER BY news_date DESC")
116
117 headings = [result["news_subject"] for result in results]
118 return headings
119
120 class News:
121 """Defines the get and set attributes for a news item."""
122
123 def __init__(self, news_id):
124
125 self._db = SQLdb(config.db)
126 self._news_id = news_id
127
128 def _get_attr(self, attr):
129 """Return the value of the specified attribute."""
130
131 result = self._db.query(
132 "SELECT %(attr)s FROM %(prefix)snews " %
133 {'attr': attr, 'prefix': config.db['prefix']} +
134 "WHERE news_id = %s", str(self._news_id), fetch='one')
135
136 if result != None:
137 return result[attr]
138
139 return None
140
141 def get_subject(self):
142 return self._get_attr("news_subject")
143
144 def get_body(self):
145 return self._get_attr("news_body")
146
147 def get_date(self):
148 return self._get_attr("news_date")
149
150 def update(self, uid, subject, date, body):
151
152 self._db.query(
153 "UPDATE %(prefix)snews " % {'prefix': config.db['prefix']} +
154 "SET news_author_id = %s, news_subject = %s, news_body = %s, " +
155 "news_date = %s WHERE news_id = %s",
156 (uid, subject, body, date, self._news_id), fetch='none')
157
158 def delete(self):
159
160 self._db.query("DELETE FROM %snews " % config.db['prefix'] +
161 "WHERE news_id = %s", self._news_id, fetch = "none")

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20