Add a new hostname.
(user,
data,
db,
config,
messages)
| 110 | config = 'ddserver.config:Config', |
| 111 | messages = 'ddserver.interface.message:MessageManager') |
| 112 | def post_hosts_add(user, |
| 113 | data, |
| 114 | db, |
| 115 | config, |
| 116 | messages): |
| 117 | ''' Add a new hostname. ''' |
| 118 | |
| 119 | # We do net check passed suffix, as mysql will tell us later on |
| 120 | |
| 121 | encrypted_password = pwd.encrypt(data.password) |
| 122 | |
| 123 | with db.cursor() as cur: |
| 124 | cur.execute(''' |
| 125 | SELECT * |
| 126 | FROM `hosts` |
| 127 | WHERE `user_id` = %(user_id)s |
| 128 | ''', {'user_id': user.id}) |
| 129 | |
| 130 | # users can have an individual hostname limit, unlimited hostnames (-1) |
| 131 | # or have no limit set in the db to use the default from the config |
| 132 | if ((user.maxhosts is None and cur.rowcount >= int(config.dns.max_hosts)) or |
| 133 | (user.maxhosts is not None and |
| 134 | (int(cur.rowcount >= user.maxhosts) and int(user.maxhosts) is not -1))): |
| 135 | messages.error('Maximum number of hosts reached') |
| 136 | bottle.redirect('/user/hosts/list') |
| 137 | |
| 138 | cur.execute(''' |
| 139 | INSERT |
| 140 | INTO `hosts` |
| 141 | SET `hostname` = %(hostname)s, |
| 142 | `address` = %(address)s, |
| 143 | `description` = %(description)s, |
| 144 | `password` = %(password)s, |
| 145 | `user_id` = %(user_id)s, |
| 146 | `suffix_id` = %(suffix_id)s |
| 147 | ''', {'hostname': data.hostname, |
| 148 | 'address': data.address, |
| 149 | 'description': data.description, |
| 150 | 'password': encrypted_password, |
| 151 | 'user_id': user.id, |
| 152 | 'suffix_id': data.suffix}) |
| 153 | |
| 154 | messages.success('Ok, done.') |
| 155 | |
| 156 | bottle.redirect('/user/hosts/list') |
| 157 | |
| 158 | |
| 159 |