def set_scores(self, calculated_scores, existing=None): calculated_scores = list(self.iterate_scores(calculated_scores)) score_dict = {(score[0], score[1]): score[2] for score in calculated_scores} updates = {} if existing: scores = existing else: scores = self.find_existing_scores(calculated_scores) or [] for score in scores: id, date, user, score, reviewed, suggested, translated = score newscore = score_dict.get((date, user), None) if newscore is None: # delete ? continue oldscore = dict(score=score, reviewed=reviewed, translated=translated, suggested=suggested) for k in ["score", "suggested", "reviewed", "translated"]: _newscore = round(newscore.get(k, 0), 2) if round(oldscore[k], 2) != _newscore: updates[id] = updates.get(id, {}) updates[id][k] = _newscore del score_dict[(date, user)] if updates: update.send(self.score_model, updates=updates) if score_dict: self.create_scores(score_dict)
def update(self, keys=None): parents = list(self.parents.values_list("id", flat=True)) revisions = self.get_revisions(parents, keys=keys) missing_revisions = [] existing_ids = [] revision_map = { '%s-%s' % (x['object_id'], x['key']): x['id'] for x in revisions.values("id", "object_id", "key") } for parent in parents: for key in keys or [""]: id = '%s-%s' % (parent, key) if id in revision_map: existing_ids.append(revision_map[id]) else: missing_revisions.append(dict(object_id=parent, key=key)) new_revision = self.new_revision updates = {id: dict(value=new_revision) for id in existing_ids} if updates: update.send(Revision, updates=updates) if missing_revisions: create.send(Revision, objects=list( self.create_missing_revisions( missing_revisions, new_revision)))
def save_data(self, fields=None): update.send(self.data.__class__, instance=self.data, update_fields=fields) # this ensures that any calling code gets the # correct revision. It doesnt refresh the last # created/updated fks tho self.model.data = self.data
def set_check_data(self, store_data=None): checks = {} existing_checks = self.model.check_data.values_list( "pk", "category", "name", "count") for pk, category, name, count in existing_checks: checks[(category, name)] = (pk, count) to_update = [] to_add = [] for check in store_data["checks"]: category = check["category"] name = check["name"] count = check["count"] check_exists = (checks.get((category, name))) if not check_exists: to_add.append(check) continue elif checks[(category, name)][1] != count: to_update.append( (checks[(category, name)][0], dict(count=count))) del checks[(category, name)] check_data = None for category, name in checks.keys(): if check_data is None: check_data = self.model.check_data.filter(category=category, name=name) else: check_data = check_data | self.model.check_data.filter( category=category, name=name) if checks: delete.send(check_data.model, objects=check_data) if to_update: to_update = dict(to_update) update.send(self.model.check_data.model, updates=to_update) if not to_add: return create.send(self.model.check_data.model, objects=[ self.check_data_field.related_model( **{ self.related_name: self.model, "category": check["category"], "name": check["name"], "count": check["count"] }) for check in to_add ])
def _callback_handler(model, updated): # delete to_delete = None if updated.delete_ids is not None: to_delete = model.objects.filter(pk__in=updated.delete_ids) if updated.delete_qs is not None: to_delete = (updated.delete_qs if to_delete is None else to_delete | updated.delete_qs) if to_delete is not None: delete.send(model, objects=to_delete) # create if updated.create is not None: create.send(model, objects=updated.create) # update should_update = (updated.update_objects is not None or updated.updates is not None) if should_update: update.send(model, objects=updated.update_objects, updates=updated.updates, update_fields=updated.update_fields)
def set_scores(self, calculated_scores, existing=None): update.send(self.score_model, updates={ user: dict(score=score) for user, score in calculated_scores.iterator() })