| 239 | | def before_update(self, mapper, connection, instance): |
|---|
| 240 | | """ |
|---|
| 241 | | Inspect the column changes that triggered this update, and |
|---|
| 242 | | raise an exception if a key has been changed, issue warnings if |
|---|
| 243 | | content columns have been changed, and perform updates if metadata |
|---|
| 244 | | columns have been changed. |
|---|
| 245 | | |
|---|
| 246 | | """ |
|---|
| 247 | | # `original_data` is a mapping of the column names and their values |
|---|
| 248 | | # before any modifications of `instance` were made. |
|---|
| 249 | | original_data = instance._state['original'].data |
|---|
| 250 | | |
|---|
| 251 | | def is_key_column(column): |
|---|
| 252 | | """ |
|---|
| 253 | | Determine if `column` is, or points to, a primary key of |
|---|
| 254 | | one of the shared tables involved in the revision model. |
|---|
| 255 | | """ |
|---|
| 256 | | key_tables = [revision_table, content_table, node_table] |
|---|
| 257 | | return ( |
|---|
| 258 | | column.primary_key or ( |
|---|
| 259 | | column.foreign_key and |
|---|
| 260 | | column.foreign_key.column.table in key_tables |
|---|
| 261 | | ) |
|---|
| 262 | | ) |
|---|
| 263 | | |
|---|
| 264 | | def is_content_column(column): |
|---|
| 265 | | """Determine if `column` is from a revisioned content table.""" |
|---|
| 266 | | node_table = get_column_table(mapper.c.node_revision_id) |
|---|
| 267 | | generic_table = get_column_table(mapper.c.generic_revision_id) |
|---|
| 268 | | localized_table = get_column_table(mapper.c.localized_revision_id) |
|---|
| 269 | | content_tables = [node_table, generic_table, localized_table] |
|---|
| 270 | | return get_column_table(column) in content_tables |
|---|
| 271 | | |
|---|
| 272 | | # For each column in `instance`, determine if it was modified, |
|---|
| 273 | | # and raise an exception or issue warnings if the column is a key |
|---|
| 274 | | # or metadata, respectively. |
|---|
| 275 | | for column_name, original_value in original_data.iteritems(): |
|---|
| 276 | | new_value = getattr(instance, column_name, None) |
|---|
| 277 | | # Check if the column was modified... |
|---|
| 278 | | if new_value != original_value: |
|---|
| 279 | | column = mapper.c[column_name] |
|---|
| 280 | | if is_key_column(column): |
|---|
| 281 | | raise KeyModifiedError( |
|---|
| 282 | | "Cannot modify key column %s!" % column.name |
|---|
| 283 | | ) |
|---|
| 284 | | elif is_content_column(column): |
|---|
| 285 | | import warnings |
|---|
| 286 | | warnings.warn( |
|---|
| 287 | | "Content column %s was modified and will be " |
|---|
| 288 | | "updated, are you sure you didn't mean to make " |
|---|
| 289 | | "a new revision?" % column.name, |
|---|
| 290 | | ContentModifiedWarning |
|---|
| 291 | | ) |
|---|
| 292 | | |
|---|
| 293 | | return EXT_PASS |
|---|
| 294 | | |
|---|