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

Contents of /trunk/site/category.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 154 - (show annotations) (download) (as text)
Tue Sep 27 05:45:43 2005 UTC (9 years, 1 month ago) by hadfield
File MIME type: text/x-python
File size: 3871 byte(s)
implemented the category admin
1 # Copyright 2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 #
4
5 """The DB access module for the category table.
6
7 Implements all functions necessary for creating, modifying, and managing
8 categories.
9 """
10
11 __revision__ = "$Id$"
12 __authors__ = ["Scott Hadfield <hadfield@gentoo.org>",
13 "Ian Leitch <port001@gentoo.org>"]
14 __modulename__ = "category"
15
16 from glsr.setup import config
17 from glsr.core.db.mysql import SQLdb
18
19 def create(name, descr, parent_id = 0):
20 """Add a new category to the database."""
21
22 sql_handle = SQLdb(config.db)
23
24 sql_handle.query(
25 "INSERT INTO %(prefix)scategory " % {"prefix": config.db["prefix"]} +
26 "(category_name, category_descr) VALUES (%s, %s)",
27 (name, descr), fetch = "none")
28
29 results = sql_handle.query("SELECT LAST_INSERT_ID() as id", fetch = "one")
30 assert(results.has_key("id"))
31
32 return results["id"]
33
34 def list_all(columns = None, constraint = None):
35 """Return all categories who comply to the constraints.
36
37 This is copied from the user.list_all method.
38 """
39
40 # FIXME: Sanity checks on column names and contrainst keys.
41 # Without this we could have mysql injection attacks.
42
43 if columns is None:
44 return []
45 else:
46 columns = ", ".join(columns)
47
48 where_clause = ""
49 if constraint is not None:
50 where_clause = " AND ".join(["category_%s = %%s" % key
51 for key in constraint])
52 if where_clause != "":
53 where_clause = "WHERE " + where_clause
54 else:
55 constraint = {}
56
57 results = SQLdb(config.db).query(
58 "SELECT %(columns)s FROM %(prefix)scategory %(where)s" %
59 {'columns': columns, 'prefix': config.db['prefix'],
60 'where': where_clause}, constraint.values(), fetch="all")
61
62 return results
63
64 def exists(category_id):
65
66 result = SQLdb(config.db).query(
67 "SELECT category_id FROM %scategory " % config.db["prefix"] +
68 "WHERE category_id = %s", category_id, fetch = "one")
69
70 if result is None:
71 return False
72 return True
73
74 class Category:
75 """Defines the get and set attributes for a Category."""
76
77 def __init__(self, category_id):
78
79 self._db = SQLdb(config.db)
80 self._category_id = category_id
81
82 def _get_attr(self, attr):
83 """Return the value of the specified attribute, or DB field."""
84
85 result = self._db.query(
86 "SELECT %(attr)s FROM %(prefix)scategory " %
87 {'attr': attr, 'prefix': config.db['prefix']} +
88 "WHERE category_id = %s", str(self._category_id), fetch='one')
89
90 if result != None:
91 return result[attr]
92
93 return None
94
95
96 def update(self, name, descr):
97 """Modify the details of this category."""
98
99 self._db.query("UPDATE %scategory " % config.db["prefix"] +
100 "SET category_name = %s, category_descr = %s "+
101 "WHERE category_id = %s",
102 (name, descr, self._category_id), fetch = "none")
103
104 def delete(self):
105 """Delete this category and any sub categories.
106
107 This will also orphan any scripts that belong to this category,
108 and in essence make them category-less. It will set their category_id
109 to 0.
110 """
111
112 self._db.query("DELETE FROM %scategory " % config.db["prefix"] +
113 "WHERE category_id = %s", self._category_id,
114 fetch = "none")
115
116 def children(self):
117 """Return the id's for all of this categories children."""
118 pass
119
120 def name(self):
121 """Return the name of this category."""
122 return self._get_attr("category_name")
123
124 def parent_id(self):
125 """Return the parent id for this category."""
126 pass

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20