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

Contents of /trunk/site/language.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (show annotations) (download) (as text)
Mon Sep 26 07:36:16 2005 UTC (8 years, 11 months ago) by hadfield
File MIME type: text/x-python
File size: 3438 byte(s)
implemented the languages 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 language table.
6
7 Implements all functions necessary for creating, modifying, and managing
8 languages.
9 """
10
11 __revision__ = "$Id$"
12 __authors__ = ["Scott Hadfield <hadfield@gentoo.org>",
13 "Ian Leitch <port001@gentoo.org>"]
14 __modulename__ = "language"
15
16 from glsr.setup import config
17 from glsr.core.db.mysql import SQLdb
18
19 def create(name, descr):
20 """Add a new language to the database."""
21
22 sql_handle = SQLdb(config.db)
23
24 sql_handle.query(
25 "INSERT INTO %(prefix)slanguage " % {'prefix': config.db['prefix']} +
26 "(language_name, language_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
35 def list_all(columns = None, constraint = None):
36 """Return all languages.
37
38 This is copied from the user.list_all method.
39 """
40
41 # FIXME: Sanity checks on column names and contrainst keys.
42 # Without this we could have mysql injection attacks.
43
44 if columns is None:
45 return []
46 else:
47 columns = ", ".join(columns)
48
49 where_clause = ""
50 if constraint is not None:
51 where_clause = " AND ".join(["language_%s = %%s" % key
52 for key in constraint])
53 if where_clause != "":
54 where_clause = "WHERE " + where_clause
55 else:
56 constraint = {}
57
58 results = SQLdb(config.db).query(
59 "SELECT %(columns)s FROM %(prefix)slanguage %(where)s" %
60 {'columns': columns, 'prefix': config.db['prefix'],
61 'where': where_clause}, constraint.values(), fetch="all")
62
63 return results
64
65 def exists(language_id):
66
67 result = SQLdb(config.db).query(
68 "SELECT language_id FROM %slanguage " % config.db["prefix"] +
69 "WHERE language_id = %s", language_id, fetch = "one")
70
71 if result is None:
72 return False
73 return True
74
75 class Language:
76 """Defines the get and set attributes for a Category."""
77
78 def __init__(self, language_id):
79
80 self._db = SQLdb(config.db)
81 self._language_id = language_id
82
83 def _get_attr(self, attr):
84 """Return the value of the specified attribute, or DB field."""
85
86 result = self._db.query(
87 "SELECT %(attr)s FROM %(prefix)slanguage " %
88 {'attr': attr, 'prefix': config.db['prefix']} +
89 "WHERE language_id = %s", str(self._language_id), fetch='one')
90
91 if result != None:
92 return result[attr]
93
94 return None
95
96 def update(self, name, descr):
97 """Modify the details of this language."""
98
99 self._db.query("UPDATE %slanguage " % config.db["prefix"] +
100 "SET language_name = %s, language_descr = %s "+
101 "WHERE language_id = %s",
102 (name, descr, self._language_id), fetch = "none")
103
104 def delete(self):
105 """Delete self from database."""
106
107 self._db.query("DELETE FROM %slanguage " % config.db["prefix"] +
108 "WHERE language_id = %s", self._language_id,
109 fetch = "none")
110
111 def name(self):
112 """Return the name of this language."""
113 return self._get_attr("language_name")

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20