Revision 8288906d
b/snf-astakos-app/astakos/im/models.py | ||
---|---|---|
1303 | 1303 |
### PROJECTS ### |
1304 | 1304 |
################ |
1305 | 1305 |
|
1306 |
def synced_model_metaclass(class_name, class_parents, class_attributes): |
|
1307 |
|
|
1308 |
new_attributes = {} |
|
1309 |
sync_attributes = {} |
|
1310 |
|
|
1311 |
for name, value in class_attributes.iteritems(): |
|
1312 |
sync, underscore, rest = name.partition('_') |
|
1313 |
if sync == 'sync' and underscore == '_': |
|
1314 |
sync_attributes[rest] = value |
|
1315 |
else: |
|
1316 |
new_attributes[name] = value |
|
1317 |
|
|
1318 |
if 'prefix' not in sync_attributes: |
|
1319 |
m = ("you did not specify a 'sync_prefix' attribute " |
|
1320 |
"in class '%s'" % (class_name,)) |
|
1321 |
raise ValueError(m) |
|
1322 |
|
|
1323 |
prefix = sync_attributes.pop('prefix') |
|
1324 |
class_name = sync_attributes.pop('classname', prefix + '_model') |
|
1325 |
|
|
1326 |
for name, value in sync_attributes.iteritems(): |
|
1327 |
newname = prefix + '_' + name |
|
1328 |
if newname in new_attributes: |
|
1329 |
m = ("class '%s' was specified with prefix '%s' " |
|
1330 |
"but it already has an attribute named '%s'" |
|
1331 |
% (class_name, prefix, newname)) |
|
1332 |
raise ValueError(m) |
|
1333 |
|
|
1334 |
new_attributes[newname] = value |
|
1335 |
|
|
1336 |
newclass = type(class_name, class_parents, new_attributes) |
|
1337 |
return newclass |
|
1338 |
|
|
1339 |
|
|
1340 |
def make_synced(prefix='sync', name='SyncedState'): |
|
1341 |
|
|
1342 |
the_name = name |
|
1343 |
the_prefix = prefix |
|
1344 |
|
|
1345 |
class SyncedState(models.Model): |
|
1346 |
|
|
1347 |
sync_classname = the_name |
|
1348 |
sync_prefix = the_prefix |
|
1349 |
__metaclass__ = synced_model_metaclass |
|
1350 |
|
|
1351 |
sync_new_state = models.BigIntegerField(null=True) |
|
1352 |
sync_synced_state = models.BigIntegerField(null=True) |
|
1353 |
STATUS_SYNCED = 0 |
|
1354 |
STATUS_PENDING = 1 |
|
1355 |
sync_status = models.IntegerField(db_index=True) |
|
1356 |
|
|
1357 |
class Meta: |
|
1358 |
abstract = True |
|
1359 |
|
|
1360 |
class NotSynced(Exception): |
|
1361 |
pass |
|
1362 |
|
|
1363 |
def sync_init_state(self, state): |
|
1364 |
self.sync_synced_state = state |
|
1365 |
self.sync_new_state = state |
|
1366 |
self.sync_status = self.STATUS_SYNCED |
|
1367 |
|
|
1368 |
def sync_get_status(self): |
|
1369 |
return self.sync_status |
|
1370 |
|
|
1371 |
def sync_set_status(self): |
|
1372 |
if self.sync_new_state != self.sync_synced_state: |
|
1373 |
self.sync_status = self.STATUS_PENDING |
|
1374 |
else: |
|
1375 |
self.sync_status = self.STATUS_SYNCED |
|
1376 |
|
|
1377 |
def sync_set_synced(self): |
|
1378 |
self.sync_synced_state = self.sync_new_state |
|
1379 |
self.sync_status = self.STATUS_SYNCED |
|
1380 |
|
|
1381 |
def sync_get_synced_state(self): |
|
1382 |
return self.sync_synced_state |
|
1383 |
|
|
1384 |
def sync_set_new_state(self, new_state): |
|
1385 |
self.sync_new_state = new_state |
|
1386 |
self.sync_set_status() |
|
1387 |
|
|
1388 |
def sync_get_new_state(self): |
|
1389 |
return self.sync_new_state |
|
1390 |
|
|
1391 |
def sync_set_synced_state(self, synced_state): |
|
1392 |
self.sync_synced_state = synced_state |
|
1393 |
self.sync_set_status() |
|
1394 |
|
|
1395 |
def sync_get_pending_objects(self): |
|
1396 |
kw = dict((the_prefix + '_status', self.STATUS_PENDING)) |
|
1397 |
return self.objects.filter(**kw) |
|
1398 |
|
|
1399 |
def sync_get_synced_objects(self): |
|
1400 |
kw = dict((the_prefix + '_status', self.STATUS_SYNCED)) |
|
1401 |
return self.objects.filter(**kw) |
|
1402 |
|
|
1403 |
def sync_verify_get_synced_state(self): |
|
1404 |
status = self.sync_get_status() |
|
1405 |
state = self.sync_get_synced_state() |
|
1406 |
verified = (status == self.STATUS_SYNCED) |
|
1407 |
return state, verified |
|
1408 |
|
|
1409 |
def sync_is_synced(self): |
|
1410 |
state, verified = self.sync_verify_get_synced_state() |
|
1411 |
return verified |
|
1412 |
|
|
1413 |
return SyncedState |
|
1414 |
|
|
1415 |
SyncedState = make_synced(prefix='sync', name='SyncedState') |
|
1416 |
|
|
1417 |
|
|
1418 | 1306 |
class ChainManager(ForUpdateManager): |
1419 | 1307 |
|
1420 | 1308 |
def search_by_name(self, *search_strings): |
Also available in: Unified diff