models.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:sentinel-old 作者: monacocoin-net 项目源码 文件源码
def import_gobject_from_monacoCoind(self, monacoCoind, rec):
        import monacoCoinlib
        import inflection

        object_hex = rec['DataHex']
        object_hash = rec['Hash']

        gobj_dict = {
            'object_hash': object_hash,
            'object_fee_tx': rec['CollateralHash'],
            'absolute_yes_count': rec['AbsoluteYesCount'],
            'abstain_count': rec['AbstainCount'],
            'yes_count': rec['YesCount'],
            'no_count': rec['NoCount'],
        }

        # shim/monacoCoind conversion
        object_hex = monacoCoinlib.SHIM_deserialise_from_monacoCoind(object_hex)
        objects = monacoCoinlib.deserialise(object_hex)
        subobj = None

        obj_type, dikt = objects[0:2:1]
        obj_type = inflection.pluralize(obj_type)
        subclass = self._meta.reverse_rel[obj_type].model_class

        # set object_type in govobj table
        gobj_dict['object_type'] = subclass.govobj_type

        # exclude any invalid model data from monacoCoind...
        valid_keys = subclass.serialisable_fields()
        subdikt = {k: dikt[k] for k in valid_keys if k in dikt}

        # get/create, then sync vote counts from monacoCoind, with every run
        govobj, created = self.get_or_create(object_hash=object_hash, defaults=gobj_dict)
        if created:
            printdbg("govobj created = %s" % created)
        count = govobj.update(**gobj_dict).where(self.id == govobj.id).execute()
        if count:
            printdbg("govobj updated = %d" % count)
        subdikt['governance_object'] = govobj

        # get/create, then sync payment amounts, etc. from monacoCoind - monacoCoind is the master
        try:
            subobj, created = subclass.get_or_create(object_hash=object_hash, defaults=subdikt)
        except (peewee.OperationalError, peewee.IntegrityError) as e:
            # in this case, vote as delete, and log the vote in the DB
            printdbg("Got invalid object from monacoCoind! %s" % e)
            if not govobj.voted_on(signal=VoteSignals.delete, outcome=VoteOutcomes.yes):
                govobj.vote(monacoCoind, VoteSignals.delete, VoteOutcomes.yes)
            return (govobj, None)

        if created:
            printdbg("subobj created = %s" % created)
        count = subobj.update(**subdikt).where(subclass.id == subobj.id).execute()
        if count:
            printdbg("subobj updated = %d" % count)

        # ATM, returns a tuple w/gov attributes and the govobj
        return (govobj, subobj)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号