def new_3(self, name, trustee = None, public_key=None, private_key=None, voting_starts_at=None, voting_ends_at=None, **kw): """ Create the new election. trustees is a JSON list """ # we need a list of admins, or at least a public key if not trustee and not public_key: self.error('Need a list of trustees or a public key') election = do.Election() # hard-wire the type for now, we only have one type of election election.election_type = 'homomorphic' # basic election parameters election.name = name election.admin, election.api_client = self.user(), do.APIClient.get_by_consumer_key(Controller.api_client()) election.voting_starts_at = utils.string_to_datetime(voting_starts_at) election.voting_ends_at = utils.string_to_datetime(voting_ends_at) # serialize the public key to JSON and store it if it was sent if public_key and public_key != "": pk = algs.EGPublicKey.from_dict(utils.from_json(public_key)) election.public_key_json = utils.to_json(pk.to_dict()) # the private key can be stored by the server if private_key and private_key != "": sk = algs.EGSecretKey.from_dict(utils.from_json(private_key)) election.private_key_json = utils.to_json(sk.to_dict()) ## FIXME: transaction! election.save() # go through the trustees if trustee: for t in trustee: if t.strip() == "": continue # create the keyshare keyshare = do.KeyShare() keyshare.parent = election keyshare.election = election keyshare.email = t keyshare.generate_password() keyshare.save() # send out the email self.email_trustees_2(election, 'You have been designated as a trustee of the Helios Election "%s".' % election.name) # user or api_client? if election.admin: raise cherrypy.HTTPRedirect("./%s/view" % str(election.election_id)) else: return str(election.election_id)
def drive_tally(self, election): """ JavaScript-based driver for the entire tallying process, now done in JavaScript. """ election_pk = election.get_pk() election_pk_json = utils.to_json(election_pk.toJSONDict()) election_sk = election.get_sk() if election_sk: election_sk_json = utils.to_json(election_sk.toJSONDict()) return self.render('drive_tally')
def get_voters_hash(self): voters = self.get_voters() voters_json = utils.to_json([ v.toJSONDict(with_vote=False, with_vote_hash=False) for v in voters ]) # logging.info("json for voters is: " + voters_json) return utils.hash_b64(voters_json)
def new_2(self, name, election_type): """ Enter additional information about the election type. """ user = Controller.user() eg_params_json = utils.to_json(ELGAMAL_PARAMS.toJSONDict()) return self.render('new_2')
def voters_manage(self, election): """ Manage voters for the given election. """ # allow managing of voters no matter what, since this is where you can email them user, api_client, election = self.check(election, allow_frozen=True) voters = election.get_voters() voters_json = utils.to_json([v.toJSONDict() for v in voters]) return self.render('voters')
def _before_exec_callback_query_task(self, msg_handler, callback_query: CallbackQuery, tguser: TgUser): tgmessage = TgMessage( tguser=tguser, tg_id=callback_query.from_user.id, from_tg_id=callback_query.from_user.id, message_id=callback_query.message and callback_query.message.message_id, chat_type='callback_query', text=callback_query.data, message=base_utils.to_json(callback_query), date=timezone.now(), ) self._exec_task(callback_query_task, msg_handler['function'], tgmessage, callback_query, tguser)
def _before_exec_message_task(self, msg_handler, message: Message, tguser: TgUser): tgmessage = TgMessage( tguser=tguser, tgchat=tguser.tgchat, tg_id=message.chat.id, from_tg_id=message.from_user.id, message_id=message.message_id, chat_type=message.chat.type, text=message.text or message.caption or 'content_type:%s' % message.content_type, message=base_utils.to_json(message), date=timezone.make_aware(datetime.fromtimestamp(message.date)), ) self._exec_task(message_task, msg_handler['function'], tgmessage, message, tguser)
def _notify_command_handlers(self, handlers, items): for item in items: with base_utils.lock('tguser_%d' % item.from_user.id): with transaction.atomic(): tguser = TgUser.load(item.from_user, item) assert isinstance(tguser, TgUser) if settings.UNDER_CONSTRUCTION and not tguser.is_admin(): tguser.send_message(_('The bot is under construction...'), reply=True, reply_markup=ReplyKeyboardRemove()) continue tries = 0 while True: tries += 1 try: next_raised = False for handler in handlers: if self._test_message_handler( handler, item, tguser): try: if isinstance(item, CallbackQuery): self._before_exec_callback_query_task( handler, item, tguser) elif isinstance(item, Message): self._before_exec_message_task( handler, item, tguser) next_raised = False except bot_utils.NextHandler: next_raised = True continue break else: if settings.DEBUG: logger.debug('Unhandled update: %s', item) if next_raised: logger.warning( 'NextHandler raised but was not proceed! TgUser: %s, message: %s', tguser, base_utils.to_json(item, indent=None)) except bot_utils.RestartHandler: if tries >= 10: raise continue else: break
def set_encrypted_tally(self, tally): self.encrypted_tally = utils.to_json(tally.toJSONDict())
def keyshares_tally_manage(self, election): election_pk = election.get_pk() election_pk_json = utils.to_json(election_pk.toJSONDict()) keyshares = election.get_keyshares() return self.render("keyshares_tally_manage")
def new_3(self, name, trustee=None, public_key=None, private_key=None, voting_starts_at=None, voting_ends_at=None, **kw): """ Create the new election. trustees is a JSON list """ # we need a list of admins, or at least a public key if not trustee and not public_key: self.error('Need a list of trustees or a public key') election = do.Election() # hard-wire the type for now, we only have one type of election election.election_type = 'homomorphic' # basic election parameters election.name = name election.admin, election.api_client = self.user( ), do.APIClient.get_by_consumer_key(Controller.api_client()) election.voting_starts_at = utils.string_to_datetime(voting_starts_at) election.voting_ends_at = utils.string_to_datetime(voting_ends_at) # serialize the public key to JSON and store it if it was sent if public_key and public_key != "": pk = algs.EGPublicKey.from_dict(utils.from_json(public_key)) election.public_key_json = utils.to_json(pk.to_dict()) # the private key can be stored by the server if private_key and private_key != "": sk = algs.EGSecretKey.from_dict(utils.from_json(private_key)) election.private_key_json = utils.to_json(sk.to_dict()) ## FIXME: transaction! election.save() # go through the trustees if trustee: for t in trustee: if t.strip() == "": continue # create the keyshare keyshare = do.KeyShare() keyshare.parent = election keyshare.election = election keyshare.email = t keyshare.generate_password() keyshare.save() # send out the email self.email_trustees_2( election, 'You have been designated as a trustee of the Helios Election "%s".' % election.name) # user or api_client? if election.admin: raise cherrypy.HTTPRedirect("./%s/view" % str(election.election_id)) else: return str(election.election_id)
def tally(self, keyshare): election = self.parent election_pk = election.get_pk() election_pk_json = utils.to_json(election_pk.toJSONDict()) return self.render("tally")
def home(self, keyshare): eg_params_json = utils.to_json(ELGAMAL_PARAMS.toJSONDict()) election = self.parent return self.render("home")
def set_result(self, tally_d, proof_d): self.result_json = utils.to_json(tally_d) self.decryption_proof = utils.to_json(proof_d)
def set_sk(self, sk): self.private_key_json = utils.to_json(sk.to_dict())
def save_questions(self, questions): self.questions_json = utils.to_json(questions) self.update()
def set_running_tally(self, running_tally): self.running_tally = utils.to_json(running_tally.toJSONDict())
def set_pk(self, pk): self.public_key_json = utils.to_json(pk.toJSONDict())
def get_voters_hash(self): voters = self.get_voters() voters_json = utils.to_json([v.toJSONDict(with_vote=False, with_vote_hash=False) for v in voters]) # logging.info("json for voters is: " + voters_json) return utils.hash_b64(voters_json)