showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1 | """ |
| 2 | Extensions to Django's model logic. |
| 3 | """ |
| 4 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 5 | import re |
Michael Liang | 8864e86 | 2014-07-22 08:36:05 -0700 | [diff] [blame] | 6 | import time |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 7 | import django.core.exceptions |
Prashanth Balasubramanian | 75be1d3 | 2014-11-25 18:03:09 -0800 | [diff] [blame] | 8 | from django.db import backend |
| 9 | from django.db import connection |
| 10 | from django.db import connections |
| 11 | from django.db import models as dbmodels |
| 12 | from django.db import transaction |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 13 | from django.db.models.sql import query |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 14 | import django.db.models.sql.where |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 15 | from django.utils import datastructures |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 16 | from autotest_lib.frontend.afe import rdb_model_extensions |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 17 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 18 | |
| 19 | class ValidationError(django.core.exceptions.ValidationError): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 20 | """\ |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 21 | Data validation error in adding or updating an object. The associated |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 22 | value is a dictionary mapping field names to error strings. |
| 23 | """ |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 24 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 25 | def _quote_name(name): |
| 26 | """Shorthand for connection.ops.quote_name().""" |
| 27 | return connection.ops.quote_name(name) |
| 28 | |
| 29 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 30 | class LeasedHostManager(dbmodels.Manager): |
| 31 | """Query manager for unleased, unlocked hosts. |
| 32 | """ |
| 33 | def get_query_set(self): |
| 34 | return (super(LeasedHostManager, self).get_query_set().filter( |
| 35 | leased=0, locked=0)) |
| 36 | |
| 37 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 38 | class ExtendedManager(dbmodels.Manager): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | """\ |
| 40 | Extended manager supporting subquery filtering. |
| 41 | """ |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 42 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 43 | class CustomQuery(query.Query): |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 44 | def __init__(self, *args, **kwargs): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 45 | super(ExtendedManager.CustomQuery, self).__init__(*args, **kwargs) |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 46 | self._custom_joins = [] |
| 47 | |
| 48 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 49 | def clone(self, klass=None, **kwargs): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 50 | obj = super(ExtendedManager.CustomQuery, self).clone(klass) |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 51 | obj._custom_joins = list(self._custom_joins) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 52 | return obj |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 53 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 54 | |
| 55 | def combine(self, rhs, connector): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 56 | super(ExtendedManager.CustomQuery, self).combine(rhs, connector) |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 57 | if hasattr(rhs, '_custom_joins'): |
| 58 | self._custom_joins.extend(rhs._custom_joins) |
| 59 | |
| 60 | |
| 61 | def add_custom_join(self, table, condition, join_type, |
| 62 | condition_values=(), alias=None): |
| 63 | if alias is None: |
| 64 | alias = table |
| 65 | join_dict = dict(table=table, |
| 66 | condition=condition, |
| 67 | condition_values=condition_values, |
| 68 | join_type=join_type, |
| 69 | alias=alias) |
| 70 | self._custom_joins.append(join_dict) |
| 71 | |
| 72 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 73 | @classmethod |
| 74 | def convert_query(self, query_set): |
| 75 | """ |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 76 | Convert the query set's "query" attribute to a CustomQuery. |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 77 | """ |
| 78 | # Make a copy of the query set |
| 79 | query_set = query_set.all() |
| 80 | query_set.query = query_set.query.clone( |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 81 | klass=ExtendedManager.CustomQuery, |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 82 | _custom_joins=[]) |
| 83 | return query_set |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 84 | |
| 85 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 86 | class _WhereClause(object): |
| 87 | """Object allowing us to inject arbitrary SQL into Django queries. |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 88 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 89 | By using this instead of extra(where=...), we can still freely combine |
| 90 | queries with & and |. |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 91 | """ |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 92 | def __init__(self, clause, values=()): |
| 93 | self._clause = clause |
| 94 | self._values = values |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 95 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 96 | |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 97 | def as_sql(self, qn=None, connection=None): |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 98 | return self._clause, self._values |
| 99 | |
| 100 | |
| 101 | def relabel_aliases(self, change_map): |
| 102 | return |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 103 | |
| 104 | |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 105 | def add_join(self, query_set, join_table, join_key, join_condition='', |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 106 | join_condition_values=(), join_from_key=None, alias=None, |
| 107 | suffix='', exclude=False, force_left_join=False): |
| 108 | """Add a join to query_set. |
| 109 | |
| 110 | Join looks like this: |
| 111 | (INNER|LEFT) JOIN <join_table> AS <alias> |
| 112 | ON (<this table>.<join_from_key> = <join_table>.<join_key> |
| 113 | and <join_condition>) |
| 114 | |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 115 | @param join_table table to join to |
| 116 | @param join_key field referencing back to this model to use for the join |
| 117 | @param join_condition extra condition for the ON clause of the join |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 118 | @param join_condition_values values to substitute into join_condition |
| 119 | @param join_from_key column on this model to join from. |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 120 | @param alias alias to use for for join |
| 121 | @param suffix suffix to add to join_table for the join alias, if no |
| 122 | alias is provided |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 123 | @param exclude if true, exclude rows that match this join (will use a |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 124 | LEFT OUTER JOIN and an appropriate WHERE condition) |
showard | c478040 | 2009-08-31 18:31:34 +0000 | [diff] [blame] | 125 | @param force_left_join - if true, a LEFT OUTER JOIN will be used |
| 126 | instead of an INNER JOIN regardless of other options |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 127 | """ |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 128 | join_from_table = query_set.model._meta.db_table |
| 129 | if join_from_key is None: |
| 130 | join_from_key = self.model._meta.pk.name |
| 131 | if alias is None: |
| 132 | alias = join_table + suffix |
| 133 | full_join_key = _quote_name(alias) + '.' + _quote_name(join_key) |
| 134 | full_join_condition = '%s = %s.%s' % (full_join_key, |
| 135 | _quote_name(join_from_table), |
| 136 | _quote_name(join_from_key)) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 137 | if join_condition: |
| 138 | full_join_condition += ' AND (' + join_condition + ')' |
| 139 | if exclude or force_left_join: |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 140 | join_type = query_set.query.LOUTER |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 141 | else: |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 142 | join_type = query_set.query.INNER |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 143 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 144 | query_set = self.CustomQuery.convert_query(query_set) |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 145 | query_set.query.add_custom_join(join_table, |
| 146 | full_join_condition, |
| 147 | join_type, |
| 148 | condition_values=join_condition_values, |
| 149 | alias=alias) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 150 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 151 | if exclude: |
| 152 | query_set = query_set.extra(where=[full_join_key + ' IS NULL']) |
| 153 | |
| 154 | return query_set |
| 155 | |
| 156 | |
| 157 | def _info_for_many_to_one_join(self, field, join_to_query, alias): |
| 158 | """ |
| 159 | @param field: the ForeignKey field on the related model |
| 160 | @param join_to_query: the query over the related model that we're |
| 161 | joining to |
| 162 | @param alias: alias of joined table |
| 163 | """ |
| 164 | info = {} |
| 165 | rhs_table = join_to_query.model._meta.db_table |
| 166 | info['rhs_table'] = rhs_table |
| 167 | info['rhs_column'] = field.column |
| 168 | info['lhs_column'] = field.rel.get_related_field().column |
| 169 | rhs_where = join_to_query.query.where |
| 170 | rhs_where.relabel_aliases({rhs_table: alias}) |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 171 | compiler = join_to_query.query.get_compiler(using=join_to_query.db) |
| 172 | initial_clause, values = compiler.as_sql() |
| 173 | all_clauses = (initial_clause,) |
| 174 | if hasattr(join_to_query.query, 'extra_where'): |
| 175 | all_clauses += join_to_query.query.extra_where |
| 176 | info['where_clause'] = ( |
| 177 | ' AND '.join('(%s)' % clause for clause in all_clauses)) |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 178 | info['values'] = values |
| 179 | return info |
| 180 | |
| 181 | |
| 182 | def _info_for_many_to_many_join(self, m2m_field, join_to_query, alias, |
| 183 | m2m_is_on_this_model): |
| 184 | """ |
| 185 | @param m2m_field: a Django field representing the M2M relationship. |
| 186 | It uses a pivot table with the following structure: |
| 187 | this model table <---> M2M pivot table <---> joined model table |
| 188 | @param join_to_query: the query over the related model that we're |
| 189 | joining to. |
| 190 | @param alias: alias of joined table |
| 191 | """ |
| 192 | if m2m_is_on_this_model: |
| 193 | # referenced field on this model |
| 194 | lhs_id_field = self.model._meta.pk |
| 195 | # foreign key on the pivot table referencing lhs_id_field |
| 196 | m2m_lhs_column = m2m_field.m2m_column_name() |
| 197 | # foreign key on the pivot table referencing rhd_id_field |
| 198 | m2m_rhs_column = m2m_field.m2m_reverse_name() |
| 199 | # referenced field on related model |
| 200 | rhs_id_field = m2m_field.rel.get_related_field() |
| 201 | else: |
| 202 | lhs_id_field = m2m_field.rel.get_related_field() |
| 203 | m2m_lhs_column = m2m_field.m2m_reverse_name() |
| 204 | m2m_rhs_column = m2m_field.m2m_column_name() |
| 205 | rhs_id_field = join_to_query.model._meta.pk |
| 206 | |
| 207 | info = {} |
| 208 | info['rhs_table'] = m2m_field.m2m_db_table() |
| 209 | info['rhs_column'] = m2m_lhs_column |
| 210 | info['lhs_column'] = lhs_id_field.column |
| 211 | |
| 212 | # select the ID of related models relevant to this join. we can only do |
| 213 | # a single join, so we need to gather this information up front and |
| 214 | # include it in the join condition. |
| 215 | rhs_ids = join_to_query.values_list(rhs_id_field.attname, flat=True) |
| 216 | assert len(rhs_ids) == 1, ('Many-to-many custom field joins can only ' |
| 217 | 'match a single related object.') |
| 218 | rhs_id = rhs_ids[0] |
| 219 | |
| 220 | info['where_clause'] = '%s.%s = %s' % (_quote_name(alias), |
| 221 | _quote_name(m2m_rhs_column), |
| 222 | rhs_id) |
| 223 | info['values'] = () |
| 224 | return info |
| 225 | |
| 226 | |
| 227 | def join_custom_field(self, query_set, join_to_query, alias, |
| 228 | left_join=True): |
| 229 | """Join to a related model to create a custom field in the given query. |
| 230 | |
| 231 | This method is used to construct a custom field on the given query based |
| 232 | on a many-valued relationsip. join_to_query should be a simple query |
| 233 | (no joins) on the related model which returns at most one related row |
| 234 | per instance of this model. |
| 235 | |
| 236 | For many-to-one relationships, the joined table contains the matching |
| 237 | row from the related model it one is related, NULL otherwise. |
| 238 | |
| 239 | For many-to-many relationships, the joined table contains the matching |
| 240 | row if it's related, NULL otherwise. |
| 241 | """ |
| 242 | relationship_type, field = self.determine_relationship( |
| 243 | join_to_query.model) |
| 244 | |
| 245 | if relationship_type == self.MANY_TO_ONE: |
| 246 | info = self._info_for_many_to_one_join(field, join_to_query, alias) |
| 247 | elif relationship_type == self.M2M_ON_RELATED_MODEL: |
| 248 | info = self._info_for_many_to_many_join( |
| 249 | m2m_field=field, join_to_query=join_to_query, alias=alias, |
| 250 | m2m_is_on_this_model=False) |
| 251 | elif relationship_type ==self.M2M_ON_THIS_MODEL: |
| 252 | info = self._info_for_many_to_many_join( |
| 253 | m2m_field=field, join_to_query=join_to_query, alias=alias, |
| 254 | m2m_is_on_this_model=True) |
| 255 | |
| 256 | return self.add_join(query_set, info['rhs_table'], info['rhs_column'], |
| 257 | join_from_key=info['lhs_column'], |
| 258 | join_condition=info['where_clause'], |
| 259 | join_condition_values=info['values'], |
| 260 | alias=alias, |
| 261 | force_left_join=left_join) |
| 262 | |
| 263 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 264 | def key_on_joined_table(self, join_to_query): |
| 265 | """Get a non-null column on the table joined for the given query. |
| 266 | |
| 267 | This analyzes the join that would be produced if join_to_query were |
| 268 | passed to join_custom_field. |
| 269 | """ |
| 270 | relationship_type, field = self.determine_relationship( |
| 271 | join_to_query.model) |
| 272 | if relationship_type == self.MANY_TO_ONE: |
| 273 | return join_to_query.model._meta.pk.column |
| 274 | return field.m2m_column_name() # any column on the M2M table will do |
| 275 | |
| 276 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 277 | def add_where(self, query_set, where, values=()): |
| 278 | query_set = query_set.all() |
| 279 | query_set.query.where.add(self._WhereClause(where, values), |
| 280 | django.db.models.sql.where.AND) |
showard | c478040 | 2009-08-31 18:31:34 +0000 | [diff] [blame] | 281 | return query_set |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 282 | |
| 283 | |
showard | eaccf8f | 2009-04-16 03:11:33 +0000 | [diff] [blame] | 284 | def _get_quoted_field(self, table, field): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 285 | return _quote_name(table) + '.' + _quote_name(field) |
showard | 5ef36e9 | 2008-07-02 16:37:09 +0000 | [diff] [blame] | 286 | |
| 287 | |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 288 | def get_key_on_this_table(self, key_field=None): |
showard | 5ef36e9 | 2008-07-02 16:37:09 +0000 | [diff] [blame] | 289 | if key_field is None: |
| 290 | # default to primary key |
| 291 | key_field = self.model._meta.pk.column |
| 292 | return self._get_quoted_field(self.model._meta.db_table, key_field) |
| 293 | |
| 294 | |
showard | eaccf8f | 2009-04-16 03:11:33 +0000 | [diff] [blame] | 295 | def escape_user_sql(self, sql): |
| 296 | return sql.replace('%', '%%') |
| 297 | |
showard | 5ef36e9 | 2008-07-02 16:37:09 +0000 | [diff] [blame] | 298 | |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 299 | def _custom_select_query(self, query_set, selects): |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 300 | """Execute a custom select query. |
| 301 | |
| 302 | @param query_set: query set as returned by query_objects. |
| 303 | @param selects: Tables/Columns to select, e.g. tko_test_labels_list.id. |
| 304 | |
| 305 | @returns: Result of the query as returned by cursor.fetchall(). |
| 306 | """ |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 307 | compiler = query_set.query.get_compiler(using=query_set.db) |
| 308 | sql, params = compiler.as_sql() |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 309 | from_ = sql[sql.find(' FROM'):] |
| 310 | |
| 311 | if query_set.query.distinct: |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 312 | distinct = 'DISTINCT ' |
| 313 | else: |
| 314 | distinct = '' |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 315 | |
| 316 | sql_query = ('SELECT ' + distinct + ','.join(selects) + from_) |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 317 | # Chose the connection that's responsible for this type of object |
| 318 | cursor = connections[query_set.db].cursor() |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 319 | cursor.execute(sql_query, params) |
| 320 | return cursor.fetchall() |
| 321 | |
| 322 | |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 323 | def _is_relation_to(self, field, model_class): |
| 324 | return field.rel and field.rel.to is model_class |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 325 | |
| 326 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 327 | MANY_TO_ONE = object() |
| 328 | M2M_ON_RELATED_MODEL = object() |
| 329 | M2M_ON_THIS_MODEL = object() |
| 330 | |
| 331 | def determine_relationship(self, related_model): |
| 332 | """ |
| 333 | Determine the relationship between this model and related_model. |
| 334 | |
| 335 | related_model must have some sort of many-valued relationship to this |
| 336 | manager's model. |
| 337 | @returns (relationship_type, field), where relationship_type is one of |
| 338 | MANY_TO_ONE, M2M_ON_RELATED_MODEL, M2M_ON_THIS_MODEL, and field |
| 339 | is the Django field object for the relationship. |
| 340 | """ |
| 341 | # look for a foreign key field on related_model relating to this model |
| 342 | for field in related_model._meta.fields: |
| 343 | if self._is_relation_to(field, self.model): |
| 344 | return self.MANY_TO_ONE, field |
| 345 | |
| 346 | # look for an M2M field on related_model relating to this model |
| 347 | for field in related_model._meta.many_to_many: |
| 348 | if self._is_relation_to(field, self.model): |
| 349 | return self.M2M_ON_RELATED_MODEL, field |
| 350 | |
| 351 | # maybe this model has the many-to-many field |
| 352 | for field in self.model._meta.many_to_many: |
| 353 | if self._is_relation_to(field, related_model): |
| 354 | return self.M2M_ON_THIS_MODEL, field |
| 355 | |
| 356 | raise ValueError('%s has no relation to %s' % |
| 357 | (related_model, self.model)) |
| 358 | |
| 359 | |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 360 | def _get_pivot_iterator(self, base_objects_by_id, related_model): |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 361 | """ |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 362 | Determine the relationship between this model and related_model, and |
| 363 | return a pivot iterator. |
| 364 | @param base_objects_by_id: dict of instances of this model indexed by |
| 365 | their IDs |
| 366 | @returns a pivot iterator, which yields a tuple (base_object, |
| 367 | related_object) for each relationship between a base object and a |
| 368 | related object. all base_object instances come from base_objects_by_id. |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 369 | Note -- this depends on Django model internals. |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 370 | """ |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 371 | relationship_type, field = self.determine_relationship(related_model) |
| 372 | if relationship_type == self.MANY_TO_ONE: |
| 373 | return self._many_to_one_pivot(base_objects_by_id, |
| 374 | related_model, field) |
| 375 | elif relationship_type == self.M2M_ON_RELATED_MODEL: |
| 376 | return self._many_to_many_pivot( |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 377 | base_objects_by_id, related_model, field.m2m_db_table(), |
| 378 | field.m2m_reverse_name(), field.m2m_column_name()) |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 379 | else: |
| 380 | assert relationship_type == self.M2M_ON_THIS_MODEL |
| 381 | return self._many_to_many_pivot( |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 382 | base_objects_by_id, related_model, field.m2m_db_table(), |
| 383 | field.m2m_column_name(), field.m2m_reverse_name()) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 384 | |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 385 | |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 386 | def _many_to_one_pivot(self, base_objects_by_id, related_model, |
| 387 | foreign_key_field): |
| 388 | """ |
| 389 | @returns a pivot iterator - see _get_pivot_iterator() |
| 390 | """ |
| 391 | filter_data = {foreign_key_field.name + '__pk__in': |
| 392 | base_objects_by_id.keys()} |
| 393 | for related_object in related_model.objects.filter(**filter_data): |
showard | a5a72c9 | 2009-08-20 23:35:21 +0000 | [diff] [blame] | 394 | # lookup base object in the dict, rather than grabbing it from the |
| 395 | # related object. we need to return instances from the dict, not |
| 396 | # fresh instances of the same models (and grabbing model instances |
| 397 | # from the related models incurs a DB query each time). |
| 398 | base_object_id = getattr(related_object, foreign_key_field.attname) |
| 399 | base_object = base_objects_by_id[base_object_id] |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 400 | yield base_object, related_object |
| 401 | |
| 402 | |
| 403 | def _query_pivot_table(self, base_objects_by_id, pivot_table, |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 404 | pivot_from_field, pivot_to_field, related_model): |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 405 | """ |
| 406 | @param id_list list of IDs of self.model objects to include |
| 407 | @param pivot_table the name of the pivot table |
| 408 | @param pivot_from_field a field name on pivot_table referencing |
| 409 | self.model |
| 410 | @param pivot_to_field a field name on pivot_table referencing the |
| 411 | related model. |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 412 | @param related_model the related model |
| 413 | |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 414 | @returns pivot list of IDs (base_id, related_id) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 415 | """ |
| 416 | query = """ |
| 417 | SELECT %(from_field)s, %(to_field)s |
| 418 | FROM %(table)s |
| 419 | WHERE %(from_field)s IN (%(id_list)s) |
| 420 | """ % dict(from_field=pivot_from_field, |
| 421 | to_field=pivot_to_field, |
| 422 | table=pivot_table, |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 423 | id_list=','.join(str(id_) for id_ |
| 424 | in base_objects_by_id.iterkeys())) |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 425 | |
| 426 | # Chose the connection that's responsible for this type of object |
| 427 | # The databases for related_model and the current model will always |
| 428 | # be the same, related_model is just easier to obtain here because |
| 429 | # self is only a ExtendedManager, not the object. |
| 430 | cursor = connections[related_model.objects.db].cursor() |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 431 | cursor.execute(query) |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 432 | return cursor.fetchall() |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 433 | |
| 434 | |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 435 | def _many_to_many_pivot(self, base_objects_by_id, related_model, |
| 436 | pivot_table, pivot_from_field, pivot_to_field): |
| 437 | """ |
| 438 | @param pivot_table: see _query_pivot_table |
| 439 | @param pivot_from_field: see _query_pivot_table |
| 440 | @param pivot_to_field: see _query_pivot_table |
| 441 | @returns a pivot iterator - see _get_pivot_iterator() |
| 442 | """ |
| 443 | id_pivot = self._query_pivot_table(base_objects_by_id, pivot_table, |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 444 | pivot_from_field, pivot_to_field, |
| 445 | related_model) |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 446 | |
| 447 | all_related_ids = list(set(related_id for base_id, related_id |
| 448 | in id_pivot)) |
| 449 | related_objects_by_id = related_model.objects.in_bulk(all_related_ids) |
| 450 | |
| 451 | for base_id, related_id in id_pivot: |
| 452 | yield base_objects_by_id[base_id], related_objects_by_id[related_id] |
| 453 | |
| 454 | |
| 455 | def populate_relationships(self, base_objects, related_model, |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 456 | related_list_name): |
| 457 | """ |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 458 | For each instance of this model in base_objects, add a field named |
| 459 | related_list_name listing all the related objects of type related_model. |
| 460 | related_model must be in a many-to-one or many-to-many relationship with |
| 461 | this model. |
| 462 | @param base_objects - list of instances of this model |
| 463 | @param related_model - model class related to this model |
| 464 | @param related_list_name - attribute name in which to store the related |
| 465 | object list. |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 466 | """ |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 467 | if not base_objects: |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 468 | # if we don't bail early, we'll get a SQL error later |
| 469 | return |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 470 | |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 471 | base_objects_by_id = dict((base_object._get_pk_val(), base_object) |
| 472 | for base_object in base_objects) |
| 473 | pivot_iterator = self._get_pivot_iterator(base_objects_by_id, |
| 474 | related_model) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 475 | |
showard | 68693f7 | 2009-05-20 00:31:53 +0000 | [diff] [blame] | 476 | for base_object in base_objects: |
| 477 | setattr(base_object, related_list_name, []) |
| 478 | |
| 479 | for base_object, related_object in pivot_iterator: |
| 480 | getattr(base_object, related_list_name).append(related_object) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 481 | |
| 482 | |
jamesren | e365623 | 2010-03-02 00:00:30 +0000 | [diff] [blame] | 483 | class ModelWithInvalidQuerySet(dbmodels.query.QuerySet): |
| 484 | """ |
| 485 | QuerySet that handles delete() properly for models with an "invalid" bit |
| 486 | """ |
| 487 | def delete(self): |
| 488 | for model in self: |
| 489 | model.delete() |
| 490 | |
| 491 | |
| 492 | class ModelWithInvalidManager(ExtendedManager): |
| 493 | """ |
| 494 | Manager for objects with an "invalid" bit |
| 495 | """ |
| 496 | def get_query_set(self): |
| 497 | return ModelWithInvalidQuerySet(self.model) |
| 498 | |
| 499 | |
| 500 | class ValidObjectsManager(ModelWithInvalidManager): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 501 | """ |
| 502 | Manager returning only objects with invalid=False. |
| 503 | """ |
| 504 | def get_query_set(self): |
| 505 | queryset = super(ValidObjectsManager, self).get_query_set() |
| 506 | return queryset.filter(invalid=False) |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 507 | |
| 508 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 509 | class ModelExtensions(rdb_model_extensions.ModelValidators): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 510 | """\ |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 511 | Mixin with convenience functions for models, built on top of |
| 512 | the model validators in rdb_model_extensions. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 513 | """ |
| 514 | # TODO: at least some of these functions really belong in a custom |
| 515 | # Manager class |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 516 | |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 517 | |
| 518 | SERIALIZATION_LINKS_TO_FOLLOW = set() |
| 519 | """ |
| 520 | To be able to send jobs and hosts to shards, it's necessary to find their |
| 521 | dependencies. |
| 522 | The most generic approach for this would be to traverse all relationships |
| 523 | to other objects recursively. This would list all objects that are related |
| 524 | in any way. |
| 525 | But this approach finds too many objects: If a host should be transferred, |
| 526 | all it's relationships would be traversed. This would find an acl group. |
| 527 | If then the acl group's relationships are traversed, the relationship |
| 528 | would be followed backwards and many other hosts would be found. |
| 529 | |
| 530 | This mapping tells that algorithm which relations to follow explicitly. |
| 531 | """ |
| 532 | |
Jakob Juelich | f865d33 | 2014-09-29 10:47:49 -0700 | [diff] [blame] | 533 | |
| 534 | SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set() |
| 535 | """ |
| 536 | On deserializion, if the object to persist already exists, local fields |
| 537 | will only be updated, if their name is in this set. |
| 538 | """ |
| 539 | |
| 540 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 541 | @classmethod |
| 542 | def convert_human_readable_values(cls, data, to_human_readable=False): |
| 543 | """\ |
| 544 | Performs conversions on user-supplied field data, to make it |
| 545 | easier for users to pass human-readable data. |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 546 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 547 | For all fields that have choice sets, convert their values |
| 548 | from human-readable strings to enum values, if necessary. This |
| 549 | allows users to pass strings instead of the corresponding |
| 550 | integer values. |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 551 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 552 | For all foreign key fields, call smart_get with the supplied |
| 553 | data. This allows the user to pass either an ID value or |
| 554 | the name of the object as a string. |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 555 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 556 | If to_human_readable=True, perform the inverse - i.e. convert |
| 557 | numeric values to human readable values. |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 558 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 559 | This method modifies data in-place. |
| 560 | """ |
| 561 | field_dict = cls.get_field_dict() |
| 562 | for field_name in data: |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 563 | if field_name not in field_dict or data[field_name] is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 564 | continue |
| 565 | field_obj = field_dict[field_name] |
| 566 | # convert enum values |
| 567 | if field_obj.choices: |
| 568 | for choice_data in field_obj.choices: |
| 569 | # choice_data is (value, name) |
| 570 | if to_human_readable: |
| 571 | from_val, to_val = choice_data |
| 572 | else: |
| 573 | to_val, from_val = choice_data |
| 574 | if from_val == data[field_name]: |
| 575 | data[field_name] = to_val |
| 576 | break |
| 577 | # convert foreign key values |
| 578 | elif field_obj.rel: |
showard | a4ea574 | 2009-02-17 20:56:23 +0000 | [diff] [blame] | 579 | dest_obj = field_obj.rel.to.smart_get(data[field_name], |
| 580 | valid_only=False) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 581 | if to_human_readable: |
Paul Pendlebury | 5a8c6ad | 2011-02-01 07:20:17 -0800 | [diff] [blame] | 582 | # parameterized_jobs do not have a name_field |
| 583 | if (field_name != 'parameterized_job' and |
| 584 | dest_obj.name_field is not None): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 585 | data[field_name] = getattr(dest_obj, |
| 586 | dest_obj.name_field) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 587 | else: |
showard | b0a7303 | 2009-03-27 18:35:41 +0000 | [diff] [blame] | 588 | data[field_name] = dest_obj |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 589 | |
| 590 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 591 | |
| 592 | |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 593 | def _validate_unique(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 594 | """\ |
| 595 | Validate that unique fields are unique. Django manipulators do |
| 596 | this too, but they're a huge pain to use manually. Trust me. |
| 597 | """ |
| 598 | errors = {} |
| 599 | cls = type(self) |
| 600 | field_dict = self.get_field_dict() |
| 601 | manager = cls.get_valid_manager() |
| 602 | for field_name, field_obj in field_dict.iteritems(): |
| 603 | if not field_obj.unique: |
| 604 | continue |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 605 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 606 | value = getattr(self, field_name) |
showard | bd18ab7 | 2009-09-18 21:20:27 +0000 | [diff] [blame] | 607 | if value is None and field_obj.auto_created: |
| 608 | # don't bother checking autoincrement fields about to be |
| 609 | # generated |
| 610 | continue |
| 611 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 612 | existing_objs = manager.filter(**{field_name : value}) |
| 613 | num_existing = existing_objs.count() |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 614 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 615 | if num_existing == 0: |
| 616 | continue |
| 617 | if num_existing == 1 and existing_objs[0].id == self.id: |
| 618 | continue |
| 619 | errors[field_name] = ( |
| 620 | 'This value must be unique (%s)' % (value)) |
| 621 | return errors |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 622 | |
| 623 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 624 | def _validate(self): |
| 625 | """ |
| 626 | First coerces all fields on this instance to their proper Python types. |
| 627 | Then runs validation on every field. Returns a dictionary of |
| 628 | field_name -> error_list. |
| 629 | |
| 630 | Based on validate() from django.db.models.Model in Django 0.96, which |
| 631 | was removed in Django 1.0. It should reappear in a later version. See: |
| 632 | http://code.djangoproject.com/ticket/6845 |
| 633 | """ |
| 634 | error_dict = {} |
| 635 | for f in self._meta.fields: |
| 636 | try: |
| 637 | python_value = f.to_python( |
| 638 | getattr(self, f.attname, f.get_default())) |
| 639 | except django.core.exceptions.ValidationError, e: |
jamesren | 1e0a4ce | 2010-04-21 17:45:11 +0000 | [diff] [blame] | 640 | error_dict[f.name] = str(e) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 641 | continue |
| 642 | |
| 643 | if not f.blank and not python_value: |
| 644 | error_dict[f.name] = 'This field is required.' |
| 645 | continue |
| 646 | |
| 647 | setattr(self, f.attname, python_value) |
| 648 | |
| 649 | return error_dict |
| 650 | |
| 651 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 652 | def do_validate(self): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 653 | errors = self._validate() |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 654 | unique_errors = self._validate_unique() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 655 | for field_name, error in unique_errors.iteritems(): |
| 656 | errors.setdefault(field_name, error) |
| 657 | if errors: |
| 658 | raise ValidationError(errors) |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 659 | |
| 660 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 661 | # actually (externally) useful methods follow |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 662 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 663 | @classmethod |
| 664 | def add_object(cls, data={}, **kwargs): |
| 665 | """\ |
| 666 | Returns a new object created with the given data (a dictionary |
| 667 | mapping field names to values). Merges any extra keyword args |
| 668 | into data. |
| 669 | """ |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 670 | data = dict(data) |
| 671 | data.update(kwargs) |
| 672 | data = cls.prepare_data_args(data) |
| 673 | cls.convert_human_readable_values(data) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 674 | data = cls.provide_default_values(data) |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 675 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 676 | obj = cls(**data) |
| 677 | obj.do_validate() |
| 678 | obj.save() |
| 679 | return obj |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 680 | |
| 681 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 682 | def update_object(self, data={}, **kwargs): |
| 683 | """\ |
| 684 | Updates the object with the given data (a dictionary mapping |
| 685 | field names to values). Merges any extra keyword args into |
| 686 | data. |
| 687 | """ |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 688 | data = dict(data) |
| 689 | data.update(kwargs) |
| 690 | data = self.prepare_data_args(data) |
| 691 | self.convert_human_readable_values(data) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 692 | for field_name, value in data.iteritems(): |
showard | b0a7303 | 2009-03-27 18:35:41 +0000 | [diff] [blame] | 693 | setattr(self, field_name, value) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 694 | self.do_validate() |
| 695 | self.save() |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 696 | |
| 697 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 698 | # see query_objects() |
| 699 | _SPECIAL_FILTER_KEYS = ('query_start', 'query_limit', 'sort_by', |
| 700 | 'extra_args', 'extra_where', 'no_distinct') |
| 701 | |
| 702 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 703 | @classmethod |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 704 | def _extract_special_params(cls, filter_data): |
| 705 | """ |
| 706 | @returns a tuple of dicts (special_params, regular_filters), where |
| 707 | special_params contains the parameters we handle specially and |
| 708 | regular_filters is the remaining data to be handled by Django. |
| 709 | """ |
| 710 | regular_filters = dict(filter_data) |
| 711 | special_params = {} |
| 712 | for key in cls._SPECIAL_FILTER_KEYS: |
| 713 | if key in regular_filters: |
| 714 | special_params[key] = regular_filters.pop(key) |
| 715 | return special_params, regular_filters |
| 716 | |
| 717 | |
| 718 | @classmethod |
| 719 | def apply_presentation(cls, query, filter_data): |
| 720 | """ |
| 721 | Apply presentation parameters -- sorting and paging -- to the given |
| 722 | query. |
| 723 | @returns new query with presentation applied |
| 724 | """ |
| 725 | special_params, _ = cls._extract_special_params(filter_data) |
| 726 | sort_by = special_params.get('sort_by', None) |
| 727 | if sort_by: |
| 728 | assert isinstance(sort_by, list) or isinstance(sort_by, tuple) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 729 | query = query.extra(order_by=sort_by) |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 730 | |
| 731 | query_start = special_params.get('query_start', None) |
| 732 | query_limit = special_params.get('query_limit', None) |
| 733 | if query_start is not None: |
| 734 | if query_limit is None: |
| 735 | raise ValueError('Cannot pass query_start without query_limit') |
| 736 | # query_limit is passed as a page size |
showard | 7074b74 | 2009-10-12 20:30:04 +0000 | [diff] [blame] | 737 | query_limit += query_start |
| 738 | return query[query_start:query_limit] |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 739 | |
| 740 | |
| 741 | @classmethod |
| 742 | def query_objects(cls, filter_data, valid_only=True, initial_query=None, |
| 743 | apply_presentation=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 744 | """\ |
| 745 | Returns a QuerySet object for querying the given model_class |
| 746 | with the given filter_data. Optional special arguments in |
| 747 | filter_data include: |
| 748 | -query_start: index of first return to return |
| 749 | -query_limit: maximum number of results to return |
| 750 | -sort_by: list of fields to sort on. prefixing a '-' onto a |
| 751 | field name changes the sort to descending order. |
| 752 | -extra_args: keyword args to pass to query.extra() (see Django |
| 753 | DB layer documentation) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 754 | -extra_where: extra WHERE clause to append |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 755 | -no_distinct: if True, a DISTINCT will not be added to the SELECT |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 756 | """ |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 757 | special_params, regular_filters = cls._extract_special_params( |
| 758 | filter_data) |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 759 | |
showard | 7ac7b7a | 2008-07-21 20:24:29 +0000 | [diff] [blame] | 760 | if initial_query is None: |
| 761 | if valid_only: |
| 762 | initial_query = cls.get_valid_manager() |
| 763 | else: |
| 764 | initial_query = cls.objects |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 765 | |
| 766 | query = initial_query.filter(**regular_filters) |
| 767 | |
| 768 | use_distinct = not special_params.get('no_distinct', False) |
showard | 7ac7b7a | 2008-07-21 20:24:29 +0000 | [diff] [blame] | 769 | if use_distinct: |
| 770 | query = query.distinct() |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 771 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 772 | extra_args = special_params.get('extra_args', {}) |
| 773 | extra_where = special_params.get('extra_where', None) |
| 774 | if extra_where: |
| 775 | # escape %'s |
| 776 | extra_where = cls.objects.escape_user_sql(extra_where) |
| 777 | extra_args.setdefault('where', []).append(extra_where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 778 | if extra_args: |
| 779 | query = query.extra(**extra_args) |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 780 | # TODO: Use readonly connection for these queries. |
| 781 | # This has been disabled, because it's not used anyway, as the |
| 782 | # configured readonly user is the same as the real user anyway. |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 783 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 784 | if apply_presentation: |
| 785 | query = cls.apply_presentation(query, filter_data) |
| 786 | |
| 787 | return query |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 788 | |
| 789 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 790 | @classmethod |
showard | 585c2ab | 2008-07-23 19:29:49 +0000 | [diff] [blame] | 791 | def query_count(cls, filter_data, initial_query=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 792 | """\ |
| 793 | Like query_objects, but retreive only the count of results. |
| 794 | """ |
| 795 | filter_data.pop('query_start', None) |
| 796 | filter_data.pop('query_limit', None) |
showard | 585c2ab | 2008-07-23 19:29:49 +0000 | [diff] [blame] | 797 | query = cls.query_objects(filter_data, initial_query=initial_query) |
| 798 | return query.count() |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 799 | |
| 800 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 801 | @classmethod |
| 802 | def clean_object_dicts(cls, field_dicts): |
| 803 | """\ |
| 804 | Take a list of dicts corresponding to object (as returned by |
| 805 | query.values()) and clean the data to be more suitable for |
| 806 | returning to the user. |
| 807 | """ |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 808 | for field_dict in field_dicts: |
| 809 | cls.clean_foreign_keys(field_dict) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 810 | cls._convert_booleans(field_dict) |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 811 | cls.convert_human_readable_values(field_dict, |
| 812 | to_human_readable=True) |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 813 | |
| 814 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 815 | @classmethod |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 816 | def list_objects(cls, filter_data, initial_query=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 817 | """\ |
| 818 | Like query_objects, but return a list of dictionaries. |
| 819 | """ |
showard | 7ac7b7a | 2008-07-21 20:24:29 +0000 | [diff] [blame] | 820 | query = cls.query_objects(filter_data, initial_query=initial_query) |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 821 | extra_fields = query.query.extra_select.keys() |
| 822 | field_dicts = [model_object.get_object_dict(extra_fields=extra_fields) |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 823 | for model_object in query] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 824 | return field_dicts |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 825 | |
| 826 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 827 | @classmethod |
showard | a4ea574 | 2009-02-17 20:56:23 +0000 | [diff] [blame] | 828 | def smart_get(cls, id_or_name, valid_only=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 829 | """\ |
| 830 | smart_get(integer) -> get object by ID |
| 831 | smart_get(string) -> get object by name_field |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 832 | """ |
showard | a4ea574 | 2009-02-17 20:56:23 +0000 | [diff] [blame] | 833 | if valid_only: |
| 834 | manager = cls.get_valid_manager() |
| 835 | else: |
| 836 | manager = cls.objects |
| 837 | |
| 838 | if isinstance(id_or_name, (int, long)): |
| 839 | return manager.get(pk=id_or_name) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 840 | if isinstance(id_or_name, basestring) and hasattr(cls, 'name_field'): |
showard | a4ea574 | 2009-02-17 20:56:23 +0000 | [diff] [blame] | 841 | return manager.get(**{cls.name_field : id_or_name}) |
| 842 | raise ValueError( |
| 843 | 'Invalid positional argument: %s (%s)' % (id_or_name, |
| 844 | type(id_or_name))) |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 845 | |
| 846 | |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 847 | @classmethod |
| 848 | def smart_get_bulk(cls, id_or_name_list): |
| 849 | invalid_inputs = [] |
| 850 | result_objects = [] |
| 851 | for id_or_name in id_or_name_list: |
| 852 | try: |
| 853 | result_objects.append(cls.smart_get(id_or_name)) |
| 854 | except cls.DoesNotExist: |
| 855 | invalid_inputs.append(id_or_name) |
| 856 | if invalid_inputs: |
mbligh | 7a3ebe3 | 2008-12-01 17:10:33 +0000 | [diff] [blame] | 857 | raise cls.DoesNotExist('The following %ss do not exist: %s' |
| 858 | % (cls.__name__.lower(), |
| 859 | ', '.join(invalid_inputs))) |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 860 | return result_objects |
| 861 | |
| 862 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 863 | def get_object_dict(self, extra_fields=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 864 | """\ |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 865 | Return a dictionary mapping fields to this object's values. @param |
| 866 | extra_fields: list of extra attribute names to include, in addition to |
| 867 | the fields defined on this object. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 868 | """ |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 869 | fields = self.get_field_dict().keys() |
| 870 | if extra_fields: |
| 871 | fields += extra_fields |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 872 | object_dict = dict((field_name, getattr(self, field_name)) |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 873 | for field_name in fields) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 874 | self.clean_object_dicts([object_dict]) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 875 | self._postprocess_object_dict(object_dict) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 876 | return object_dict |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 877 | |
| 878 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 879 | def _postprocess_object_dict(self, object_dict): |
| 880 | """For subclasses to override.""" |
| 881 | pass |
| 882 | |
| 883 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 884 | @classmethod |
| 885 | def get_valid_manager(cls): |
| 886 | return cls.objects |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 887 | |
| 888 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 889 | def _record_attributes(self, attributes): |
| 890 | """ |
| 891 | See on_attribute_changed. |
| 892 | """ |
| 893 | assert not isinstance(attributes, basestring) |
| 894 | self._recorded_attributes = dict((attribute, getattr(self, attribute)) |
| 895 | for attribute in attributes) |
| 896 | |
| 897 | |
| 898 | def _check_for_updated_attributes(self): |
| 899 | """ |
| 900 | See on_attribute_changed. |
| 901 | """ |
| 902 | for attribute, original_value in self._recorded_attributes.iteritems(): |
| 903 | new_value = getattr(self, attribute) |
| 904 | if original_value != new_value: |
| 905 | self.on_attribute_changed(attribute, original_value) |
| 906 | self._record_attributes(self._recorded_attributes.keys()) |
| 907 | |
| 908 | |
| 909 | def on_attribute_changed(self, attribute, old_value): |
| 910 | """ |
| 911 | Called whenever an attribute is updated. To be overridden. |
| 912 | |
| 913 | To use this method, you must: |
| 914 | * call _record_attributes() from __init__() (after making the super |
| 915 | call) with a list of attributes for which you want to be notified upon |
| 916 | change. |
| 917 | * call _check_for_updated_attributes() from save(). |
| 918 | """ |
| 919 | pass |
| 920 | |
| 921 | |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 922 | def serialize(self, include_dependencies=True): |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 923 | """Serializes the object with dependencies. |
| 924 | |
| 925 | The variable SERIALIZATION_LINKS_TO_FOLLOW defines which dependencies |
| 926 | this function will serialize with the object. |
| 927 | |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 928 | @param include_dependencies: Whether or not to follow relations to |
| 929 | objects this object depends on. |
| 930 | This parameter is used when uploading |
| 931 | jobs from a shard to the master, as the |
| 932 | master already has all the dependent |
| 933 | objects. |
| 934 | |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 935 | @returns: Dictionary representation of the object. |
| 936 | """ |
| 937 | serialized = {} |
| 938 | for field in self._meta.concrete_model._meta.local_fields: |
| 939 | if field.rel is None: |
| 940 | serialized[field.name] = field._get_val_from_obj(self) |
| 941 | |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 942 | if include_dependencies: |
| 943 | for link in self.SERIALIZATION_LINKS_TO_FOLLOW: |
| 944 | serialized[link] = self._serialize_relation(link) |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 945 | |
| 946 | return serialized |
| 947 | |
| 948 | |
| 949 | def _serialize_relation(self, link): |
| 950 | """Serializes dependent objects given the name of the relation. |
| 951 | |
| 952 | @param link: Name of the relation to take objects from. |
| 953 | |
| 954 | @returns For To-Many relationships a list of the serialized related |
| 955 | objects, for To-One relationships the serialized related object. |
| 956 | """ |
| 957 | try: |
| 958 | attr = getattr(self, link) |
| 959 | except AttributeError: |
| 960 | # One-To-One relationships that point to None may raise this |
| 961 | return None |
| 962 | |
| 963 | if attr is None: |
| 964 | return None |
| 965 | if hasattr(attr, 'all'): |
| 966 | return [obj.serialize() for obj in attr.all()] |
| 967 | return attr.serialize() |
| 968 | |
| 969 | |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 970 | @classmethod |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 971 | def _split_local_from_foreign_values(cls, data): |
| 972 | """This splits local from foreign values in a serialized object. |
| 973 | |
| 974 | @param data: The serialized object. |
| 975 | |
| 976 | @returns A tuple of two lists, both containing tuples in the form |
| 977 | (link_name, link_value). The first list contains all links |
| 978 | for local fields, the second one contains those for foreign |
| 979 | fields/objects. |
| 980 | """ |
| 981 | links_to_local_values, links_to_related_values = [], [] |
| 982 | for link, value in data.iteritems(): |
| 983 | if link in cls.SERIALIZATION_LINKS_TO_FOLLOW: |
| 984 | # It's a foreign key |
| 985 | links_to_related_values.append((link, value)) |
| 986 | else: |
| 987 | # It's a local attribute |
| 988 | links_to_local_values.append((link, value)) |
| 989 | return links_to_local_values, links_to_related_values |
| 990 | |
| 991 | |
Jakob Juelich | f865d33 | 2014-09-29 10:47:49 -0700 | [diff] [blame] | 992 | @classmethod |
| 993 | def _filter_update_allowed_fields(cls, data): |
| 994 | """Filters data and returns only files that updates are allowed on. |
| 995 | |
| 996 | This is i.e. needed for syncing aborted bits from the master to shards. |
| 997 | |
| 998 | Local links are only allowed to be updated, if they are in |
| 999 | SERIALIZATION_LOCAL_LINKS_TO_UPDATE. |
| 1000 | Overwriting existing values is allowed in order to be able to sync i.e. |
| 1001 | the aborted bit from the master to a shard. |
| 1002 | |
| 1003 | The whitelisting mechanism is in place to prevent overwriting local |
| 1004 | status: If all fields were overwritten, jobs would be completely be |
| 1005 | set back to their original (unstarted) state. |
| 1006 | |
| 1007 | @param data: List with tuples of the form (link_name, link_value), as |
| 1008 | returned by _split_local_from_foreign_values. |
| 1009 | |
| 1010 | @returns List of the same format as data, but only containing data for |
| 1011 | fields that updates are allowed on. |
| 1012 | """ |
| 1013 | return [pair for pair in data |
| 1014 | if pair[0] in cls.SERIALIZATION_LOCAL_LINKS_TO_UPDATE] |
| 1015 | |
| 1016 | |
Prashanth Balasubramanian | af51664 | 2014-12-12 18:16:32 -0800 | [diff] [blame^] | 1017 | @classmethod |
| 1018 | def delete_matching_record(cls, **filter_args): |
| 1019 | """Delete records matching the filter. |
| 1020 | |
| 1021 | @param filter_args: Arguments for the django filter |
| 1022 | used to locate the record to delete. |
| 1023 | """ |
| 1024 | try: |
| 1025 | existing_record = cls.objects.get(**filter_args) |
| 1026 | except cls.DoesNotExist: |
| 1027 | return |
| 1028 | existing_record.delete() |
| 1029 | |
| 1030 | |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1031 | def _deserialize_local(self, data): |
| 1032 | """Set local attributes from a list of tuples. |
| 1033 | |
| 1034 | @param data: List of tuples like returned by |
| 1035 | _split_local_from_foreign_values. |
| 1036 | """ |
Prashanth Balasubramanian | af51664 | 2014-12-12 18:16:32 -0800 | [diff] [blame^] | 1037 | if not data: |
| 1038 | return |
| 1039 | |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1040 | for link, value in data: |
| 1041 | setattr(self, link, value) |
| 1042 | # Overwridden save() methods are prone to errors, so don't execute them. |
| 1043 | # This is because: |
| 1044 | # - the overwritten methods depend on ACL groups that don't yet exist |
| 1045 | # and don't handle errors |
| 1046 | # - the overwritten methods think this object already exists in the db |
| 1047 | # because the id is already set |
| 1048 | super(type(self), self).save() |
| 1049 | |
| 1050 | |
| 1051 | def _deserialize_relations(self, data): |
| 1052 | """Set foreign attributes from a list of tuples. |
| 1053 | |
| 1054 | This deserialized the related objects using their own deserialize() |
| 1055 | function and then sets the relation. |
| 1056 | |
| 1057 | @param data: List of tuples like returned by |
| 1058 | _split_local_from_foreign_values. |
| 1059 | """ |
| 1060 | for link, value in data: |
| 1061 | self._deserialize_relation(link, value) |
| 1062 | # See comment in _deserialize_local |
| 1063 | super(type(self), self).save() |
| 1064 | |
| 1065 | |
| 1066 | @classmethod |
Prashanth Balasubramanian | af51664 | 2014-12-12 18:16:32 -0800 | [diff] [blame^] | 1067 | def get_record(cls, data): |
| 1068 | """Retrieve a record with the data in the given input arg. |
| 1069 | |
| 1070 | @param data: A dictionary containing the information to use in a query |
| 1071 | for data. If child models have different constraints of |
| 1072 | uniqueness they should override this model. |
| 1073 | |
| 1074 | @return: An object with matching data. |
| 1075 | |
| 1076 | @raises DoesNotExist: If a record with the given data doesn't exist. |
| 1077 | """ |
| 1078 | return cls.objects.get(id=data['id']) |
| 1079 | |
| 1080 | |
| 1081 | @classmethod |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1082 | def deserialize(cls, data): |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1083 | """Recursively deserializes and saves an object with it's dependencies. |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1084 | |
| 1085 | This takes the result of the serialize method and creates objects |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1086 | in the database that are just like the original. |
| 1087 | |
| 1088 | If an object of the same type with the same id already exists, it's |
Jakob Juelich | f865d33 | 2014-09-29 10:47:49 -0700 | [diff] [blame] | 1089 | local values will be left untouched, unless they are explicitly |
| 1090 | whitelisted in SERIALIZATION_LOCAL_LINKS_TO_UPDATE. |
| 1091 | |
| 1092 | Deserialize will always recursively propagate to all related objects |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1093 | present in data though. |
| 1094 | I.e. this is necessary to add users to an already existing acl-group. |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1095 | |
| 1096 | @param data: Representation of an object and its dependencies, as |
| 1097 | returned by serialize. |
| 1098 | |
| 1099 | @returns: The object represented by data if it didn't exist before, |
| 1100 | otherwise the object that existed before and has the same type |
| 1101 | and id as the one described by data. |
| 1102 | """ |
| 1103 | if data is None: |
| 1104 | return None |
| 1105 | |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1106 | local, related = cls._split_local_from_foreign_values(data) |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1107 | try: |
Prashanth Balasubramanian | af51664 | 2014-12-12 18:16:32 -0800 | [diff] [blame^] | 1108 | instance = cls.get_record(data) |
Jakob Juelich | f865d33 | 2014-09-29 10:47:49 -0700 | [diff] [blame] | 1109 | local = cls._filter_update_allowed_fields(local) |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1110 | except cls.DoesNotExist: |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1111 | instance = cls() |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1112 | |
Jakob Juelich | f865d33 | 2014-09-29 10:47:49 -0700 | [diff] [blame] | 1113 | instance._deserialize_local(local) |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1114 | instance._deserialize_relations(related) |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1115 | |
| 1116 | return instance |
| 1117 | |
| 1118 | |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1119 | def sanity_check_update_from_shard(self, shard, updated_serialized, |
| 1120 | *args, **kwargs): |
| 1121 | """Check if an update sent from a shard is legitimate. |
| 1122 | |
| 1123 | @raises error.UnallowedRecordsSentToMaster if an update is not |
| 1124 | legitimate. |
| 1125 | """ |
| 1126 | raise NotImplementedError( |
| 1127 | 'sanity_check_update_from_shard must be implemented by subclass %s ' |
| 1128 | 'for type %s' % type(self)) |
| 1129 | |
| 1130 | |
Prashanth Balasubramanian | 75be1d3 | 2014-11-25 18:03:09 -0800 | [diff] [blame] | 1131 | @transaction.commit_on_success |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1132 | def update_from_serialized(self, serialized): |
| 1133 | """Updates local fields of an existing object from a serialized form. |
| 1134 | |
| 1135 | This is different than the normal deserialize() in the way that it |
| 1136 | does update local values, which deserialize doesn't, but doesn't |
| 1137 | recursively propagate to related objects, which deserialize() does. |
| 1138 | |
| 1139 | The use case of this function is to update job records on the master |
| 1140 | after the jobs have been executed on a slave, as the master is not |
| 1141 | interested in updates for users, labels, specialtasks, etc. |
| 1142 | |
| 1143 | @param serialized: Representation of an object and its dependencies, as |
| 1144 | returned by serialize. |
| 1145 | |
| 1146 | @raises ValueError: if serialized contains related objects, i.e. not |
| 1147 | only local fields. |
| 1148 | """ |
| 1149 | local, related = ( |
| 1150 | self._split_local_from_foreign_values(serialized)) |
| 1151 | if related: |
| 1152 | raise ValueError('Serialized must not contain foreign ' |
| 1153 | 'objects: %s' % related) |
| 1154 | |
| 1155 | self._deserialize_local(local) |
| 1156 | |
| 1157 | |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1158 | def custom_deserialize_relation(self, link, data): |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1159 | """Allows overriding the deserialization behaviour by subclasses.""" |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1160 | raise NotImplementedError( |
| 1161 | 'custom_deserialize_relation must be implemented by subclass %s ' |
| 1162 | 'for relation %s' % (type(self), link)) |
| 1163 | |
| 1164 | |
| 1165 | def _deserialize_relation(self, link, data): |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1166 | """Deserializes related objects and sets references on this object. |
| 1167 | |
| 1168 | Relations that point to a list of objects are handled automatically. |
| 1169 | For many-to-one or one-to-one relations custom_deserialize_relation |
| 1170 | must be overridden by the subclass. |
| 1171 | |
| 1172 | Related objects are deserialized using their deserialize() method. |
| 1173 | Thereby they and their dependencies are created if they don't exist |
| 1174 | and saved to the database. |
| 1175 | |
| 1176 | @param link: Name of the relation. |
| 1177 | @param data: Serialized representation of the related object(s). |
| 1178 | This means a list of dictionaries for to-many relations, |
| 1179 | just a dictionary for to-one relations. |
| 1180 | """ |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1181 | field = getattr(self, link) |
| 1182 | |
| 1183 | if field and hasattr(field, 'all'): |
| 1184 | self._deserialize_2m_relation(link, data, field.model) |
| 1185 | else: |
| 1186 | self.custom_deserialize_relation(link, data) |
| 1187 | |
| 1188 | |
| 1189 | def _deserialize_2m_relation(self, link, data, related_class): |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1190 | """Deserialize related objects for one to-many relationship. |
| 1191 | |
| 1192 | @param link: Name of the relation. |
| 1193 | @param data: Serialized representation of the related objects. |
| 1194 | This is a list with of dictionaries. |
| 1195 | """ |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1196 | relation_set = getattr(self, link) |
| 1197 | for serialized in data: |
| 1198 | relation_set.add(related_class.deserialize(serialized)) |
| 1199 | |
| 1200 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1201 | class ModelWithInvalid(ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1202 | """ |
| 1203 | Overrides model methods save() and delete() to support invalidation in |
| 1204 | place of actual deletion. Subclasses must have a boolean "invalid" |
| 1205 | field. |
| 1206 | """ |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1207 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1208 | def save(self, *args, **kwargs): |
showard | ddb9099 | 2009-02-11 23:39:32 +0000 | [diff] [blame] | 1209 | first_time = (self.id is None) |
| 1210 | if first_time: |
| 1211 | # see if this object was previously added and invalidated |
| 1212 | my_name = getattr(self, self.name_field) |
| 1213 | filters = {self.name_field : my_name, 'invalid' : True} |
| 1214 | try: |
| 1215 | old_object = self.__class__.objects.get(**filters) |
showard | afd97de | 2009-10-01 18:45:09 +0000 | [diff] [blame] | 1216 | self.resurrect_object(old_object) |
showard | ddb9099 | 2009-02-11 23:39:32 +0000 | [diff] [blame] | 1217 | except self.DoesNotExist: |
| 1218 | # no existing object |
| 1219 | pass |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1220 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1221 | super(ModelWithInvalid, self).save(*args, **kwargs) |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1222 | |
| 1223 | |
showard | afd97de | 2009-10-01 18:45:09 +0000 | [diff] [blame] | 1224 | def resurrect_object(self, old_object): |
| 1225 | """ |
| 1226 | Called when self is about to be saved for the first time and is actually |
| 1227 | "undeleting" a previously deleted object. Can be overridden by |
| 1228 | subclasses to copy data as desired from the deleted entry (but this |
| 1229 | superclass implementation must normally be called). |
| 1230 | """ |
| 1231 | self.id = old_object.id |
| 1232 | |
| 1233 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1234 | def clean_object(self): |
| 1235 | """ |
| 1236 | This method is called when an object is marked invalid. |
| 1237 | Subclasses should override this to clean up relationships that |
showard | afd97de | 2009-10-01 18:45:09 +0000 | [diff] [blame] | 1238 | should no longer exist if the object were deleted. |
| 1239 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1240 | pass |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1241 | |
| 1242 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1243 | def delete(self): |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 1244 | self.invalid = self.invalid |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1245 | assert not self.invalid |
| 1246 | self.invalid = True |
| 1247 | self.save() |
| 1248 | self.clean_object() |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1249 | |
| 1250 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1251 | @classmethod |
| 1252 | def get_valid_manager(cls): |
| 1253 | return cls.valid_objects |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1254 | |
| 1255 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1256 | class Manipulator(object): |
| 1257 | """ |
| 1258 | Force default manipulators to look only at valid objects - |
| 1259 | otherwise they will match against invalid objects when checking |
| 1260 | uniqueness. |
| 1261 | """ |
| 1262 | @classmethod |
| 1263 | def _prepare(cls, model): |
| 1264 | super(ModelWithInvalid.Manipulator, cls)._prepare(model) |
| 1265 | cls.manager = model.valid_objects |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 1266 | |
| 1267 | |
| 1268 | class ModelWithAttributes(object): |
| 1269 | """ |
| 1270 | Mixin class for models that have an attribute model associated with them. |
| 1271 | The attribute model is assumed to have its value field named "value". |
| 1272 | """ |
| 1273 | |
| 1274 | def _get_attribute_model_and_args(self, attribute): |
| 1275 | """ |
| 1276 | Subclasses should override this to return a tuple (attribute_model, |
| 1277 | keyword_args), where attribute_model is a model class and keyword_args |
| 1278 | is a dict of args to pass to attribute_model.objects.get() to get an |
| 1279 | instance of the given attribute on this object. |
| 1280 | """ |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 1281 | raise NotImplementedError |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 1282 | |
| 1283 | |
| 1284 | def set_attribute(self, attribute, value): |
| 1285 | attribute_model, get_args = self._get_attribute_model_and_args( |
| 1286 | attribute) |
| 1287 | attribute_object, _ = attribute_model.objects.get_or_create(**get_args) |
| 1288 | attribute_object.value = value |
| 1289 | attribute_object.save() |
| 1290 | |
| 1291 | |
| 1292 | def delete_attribute(self, attribute): |
| 1293 | attribute_model, get_args = self._get_attribute_model_and_args( |
| 1294 | attribute) |
| 1295 | try: |
| 1296 | attribute_model.objects.get(**get_args).delete() |
showard | 1624542 | 2009-09-08 16:28:15 +0000 | [diff] [blame] | 1297 | except attribute_model.DoesNotExist: |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 1298 | pass |
| 1299 | |
| 1300 | |
| 1301 | def set_or_delete_attribute(self, attribute, value): |
| 1302 | if value is None: |
| 1303 | self.delete_attribute(attribute) |
| 1304 | else: |
| 1305 | self.set_attribute(attribute, value) |
showard | 26b7ec7 | 2009-12-21 22:43:57 +0000 | [diff] [blame] | 1306 | |
| 1307 | |
| 1308 | class ModelWithHashManager(dbmodels.Manager): |
| 1309 | """Manager for use with the ModelWithHash abstract model class""" |
| 1310 | |
| 1311 | def create(self, **kwargs): |
| 1312 | raise Exception('ModelWithHash manager should use get_or_create() ' |
| 1313 | 'instead of create()') |
| 1314 | |
| 1315 | |
| 1316 | def get_or_create(self, **kwargs): |
| 1317 | kwargs['the_hash'] = self.model._compute_hash(**kwargs) |
| 1318 | return super(ModelWithHashManager, self).get_or_create(**kwargs) |
| 1319 | |
| 1320 | |
| 1321 | class ModelWithHash(dbmodels.Model): |
| 1322 | """Superclass with methods for dealing with a hash column""" |
| 1323 | |
| 1324 | the_hash = dbmodels.CharField(max_length=40, unique=True) |
| 1325 | |
| 1326 | objects = ModelWithHashManager() |
| 1327 | |
| 1328 | class Meta: |
| 1329 | abstract = True |
| 1330 | |
| 1331 | |
| 1332 | @classmethod |
| 1333 | def _compute_hash(cls, **kwargs): |
| 1334 | raise NotImplementedError('Subclasses must override _compute_hash()') |
| 1335 | |
| 1336 | |
| 1337 | def save(self, force_insert=False, **kwargs): |
| 1338 | """Prevents saving the model in most cases |
| 1339 | |
| 1340 | We want these models to be immutable, so the generic save() operation |
| 1341 | will not work. These models should be instantiated through their the |
| 1342 | model.objects.get_or_create() method instead. |
| 1343 | |
| 1344 | The exception is that save(force_insert=True) will be allowed, since |
| 1345 | that creates a new row. However, the preferred way to make instances of |
| 1346 | these models is through the get_or_create() method. |
| 1347 | """ |
| 1348 | if not force_insert: |
| 1349 | # Allow a forced insert to happen; if it's a duplicate, the unique |
| 1350 | # constraint will catch it later anyways |
| 1351 | raise Exception('ModelWithHash is immutable') |
| 1352 | super(ModelWithHash, self).save(force_insert=force_insert, **kwargs) |