# HG changeset patch # User Daniele Nicolodi # Date 1309195163 -7200 # Node ID d0d44e4405982e337a383e23164299e7279c03cc # Parent 28b5b21ce8ddcb5af7dc8910bbb948f85a74b844 Implement validation for duplicate database IDs. diff -r 28b5b21ce8dd -r d0d44e440598 src/ltpdarepo/database.py --- 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) diff -r 28b5b21ce8dd -r d0d44e440598 src/ltpdarepo/tests/manage-databases.txt --- 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 '...

Description: Test Database One

...' +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 + '...
  • MySQL already contains a datbase with this ID.
  • ...' + +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 + '...
  • MySQL already contains a datbase with this ID.
  • ...' + Drop operation can be cancelled:: + >>> browser.open('/manage/databases/database1') >>> browser.getLink('Drop').click() >>> browser.url 'http://localhost/manage/databases/database1/drop'