def update_primary(cls, sender, document, **kwargs): """ Reset other primary addresses from building """ def q(x): return x if x else "" def nq(x): if not x: x = 0 return "%06d" % int(x) if document.is_primary: # Reset other primary addresses Address._get_collection().update( { "building": document.building.id, "id": { "$ne": document.id } }, {"$set": { "is_primary": False }}) # Fill sort order so = "|".join((str(x) if x else "") for x in [ document.street.name, q(document.street.short_name), nq(document.num), q(document.num_letter), nq(document.num2), nq(document.build), q(document.build_letter), nq(document.struct), q(document.struct_letter), nq(document.struct2), nq(document.estate), q(document.estate_letter), nq(document.estate2) ]) Building._get_collection().update({"_id": document.building.id}, {"$set": { "sort_order": so }})