def mutate(root, args, context, info): cls = models.GaugeVoteSpecification vote_spec_id = args.get('id') vote_spec_id = int(Node.from_global_id(vote_spec_id)[1]) title_entries = args.get('title_entries') instructions_entries = args.get('instructions_entries') choices = args.get('choices') with cls.default_db.no_autoflush as db: vote_spec = cls.get(vote_spec_id) require_instance_permission(CrudPermissions.UPDATE, vote_spec, context) update_langstring_from_input_entries(vote_spec, 'title', title_entries) update_langstring_from_input_entries(vote_spec, 'instructions', instructions_entries) vote_spec.is_custom = args.get('is_custom') existing_choices = { choice.id: choice for choice in vote_spec.choices } updated_choices = set() for idx, choice_input in enumerate(choices): if not choice_input.get('id', '-1').startswith('-'): # update the choice id_ = int(Node.from_global_id(choice_input['id'])[1]) updated_choices.add(id_) choice = models.GaugeChoiceSpecification.get(id_) update_langstring_from_input_entries( choice, 'label', choice_input['label_entries']) choice.value = choice_input['value'] else: # create a choice label_ls = langstring_from_input_entries( choice_input.get('label_entries', None)) value = choice_input.get('value') vote_spec.choices.append( models.GaugeChoiceSpecification(label=label_ls, value=value)) # remove choices that are not in choices input for choice_id in set( existing_choices.keys()).difference(updated_choices): db.delete(existing_choices[choice_id]) db.flush() return UpdateGaugeVoteSpecification(vote_specification=vote_spec)
def mutate(root, args, context, info): cls = models.GaugeVoteSpecification require_cls_permission(CrudPermissions.CREATE, cls, context) vote_session_id = args.get('vote_session_id') vote_session_id = int(Node.from_global_id(vote_session_id)[1]) title_entries = args.get('title_entries') instructions_entries = args.get('instructions_entries') choices = args.get('choices') with cls.default_db.no_autoflush as db: vote_session = db.query(models.VoteSession).get(vote_session_id) title_ls = langstring_from_input_entries(title_entries) instructions_ls = langstring_from_input_entries(instructions_entries) vote_spec = cls( title=title_ls, instructions=instructions_ls, is_custom=args.get('is_custom') ) proposal_id = args.get('proposal_id') if proposal_id: proposal_id = int(Node.from_global_id(proposal_id)[1]) proposal = models.VoteProposal.get(proposal_id) vote_spec.criterion_idea = proposal vote_spec_template_id = args.get('vote_spec_template_id') if vote_spec_template_id: vote_spec_template_id = int(Node.from_global_id(vote_spec_template_id)[1]) vote_spec.vote_spec_template_id = vote_spec_template_id for idx, choice in enumerate(choices): label_ls = langstring_from_input_entries( choice['label_entries']) value = choice['value'] vote_spec.choices.append( models.GaugeChoiceSpecification( label=label_ls, value=value) ) db.add(vote_spec) vote_session.vote_specifications.append(vote_spec) db.flush() return CreateGaugeVoteSpecification(vote_specification=vote_spec)