Mercurial > hg > ltpdarepo
changeset 32:d0d44e440598
Implement validation for duplicate database IDs.
author | Daniele Nicolodi <daniele@grinta.net> |
---|---|
date | Mon, 27 Jun 2011 19:19:23 +0200 |
parents | 28b5b21ce8dd |
children | 6b7774cff458 |
files | src/ltpdarepo/database.py src/ltpdarepo/tests/manage-databases.txt |
diffstat | 2 files changed, 37 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ltpdarepo/database.py Mon Jun 27 19:04:27 2011 +0200 +++ b/src/ltpdarepo/database.py Mon Jun 27 19:19:23 2011 +0200 @@ -1,9 +1,13 @@ +import re + +from flask import g +from wtforms import validators +from wtforms.fields import TextField +from wtforms.validators import ValidationError + from MySQLdb.cursors import DictCursor -from flask import g + from ltpdarepo.form import Form -from wtforms.fields import TextField -from wtforms import validators -from wtforms.validators import ValidationError class IDatabase(Form): @@ -12,10 +16,14 @@ description = TextField("Description") def validate_id(form, field): - import re expr = r'^[0-9a-zA-Z\-\._]+$' if not re.match(expr, field.data): - raise ValidationError(u"Invalid identifier.") + raise ValidationError(u"Invalid ID.") + curs = g.db.cursor() + curs.execute("SHOW DATABASES") + dbs = [r[0] for r in curs.fetchall()] + if field.data in dbs: + raise ValidationError(u"MySQL already contains a datbase with this ID.") class Database(object): @@ -35,13 +43,12 @@ if db is None: return None obj = Database() - obj.update(db) + for key, value in db.iteritems(): + setattr(obj, key, value) return obj def update(self, vals=None, **kwargs): kwargs.update(vals or {}) - for key, value in kwargs.iteritems(): - setattr(self, key, value) def create(self): curs = g.db.cursor() @@ -214,7 +221,6 @@ PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1""") - finally: # switch back to default database curs.execute("USE `%s`" % current)
--- a/src/ltpdarepo/tests/manage-databases.txt Mon Jun 27 19:04:27 2011 +0200 +++ b/src/ltpdarepo/tests/manage-databases.txt Mon Jun 27 19:19:23 2011 +0200 @@ -56,8 +56,29 @@ >>> browser.contents '...<p class="field"><span class="label">Description:</span> Test Database One</p>...' +Cannot create database with duplicate id:: + + >>> browser.open('/manage/databases/') + >>> browser.getLink('Create new database').click() + >>> browser.getControl(name='id').value = 'database1' + >>> browser.getControl(name='submit').click() + >>> browser.url + 'http://localhost/manage/databases/create' + >>> browser.contents + '...<li>MySQL already contains a datbase with this ID.</li>...' + +or with the id of a system database:: + + >>> browser.getControl(name='id').value = 'mysql' + >>> browser.getControl(name='submit').click() + >>> browser.url + 'http://localhost/manage/databases/create' + >>> browser.contents + '...<li>MySQL already contains a datbase with this ID.</li>...' + Drop operation can be cancelled:: + >>> browser.open('/manage/databases/database1') >>> browser.getLink('Drop').click() >>> browser.url 'http://localhost/manage/databases/database1/drop'