예제 #1
0
    def mutate(root, args, context, info):
        cls = models.TokenVoteSpecification
        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')
        exclusive_categories = args.get('exclusive_categories')
        token_categories = args.get('token_categories')
        is_custom = args.get('is_custom')

        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,
                            exclusive_categories=exclusive_categories,
                            is_custom=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

            if token_categories and (not vote_spec.criterion_idea_id
                                     or vote_spec.is_custom):
                for idx, token_category in enumerate(token_categories):
                    title_ls = langstring_from_input_entries(
                        token_category.get('title_entries', None))
                    total_number = token_category.get('total_number')
                    typename = token_category.get('typename',
                                                  'category{}'.format(idx + 1))
                    color = token_category.get('color')

                    vote_spec.token_categories.append(
                        models.TokenCategorySpecification(
                            total_number=total_number,
                            typename=typename,
                            name=title_ls,
                            color=color))

            db.add(vote_spec)
            vote_session.vote_specifications.append(vote_spec)
            db.flush()

        return CreateTokenVoteSpecification(vote_specification=vote_spec)
예제 #2
0
    def mutate(root, args, context, info):
        cls = models.TokenVoteSpecification
        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')
        exclusive_categories = args.get('exclusive_categories')
        token_categories = args.get('token_categories')

        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')
            vote_spec.exclusive_categories = exclusive_categories
            existing_token_categories = {
                token_category.id: token_category for token_category in vote_spec.token_categories}

            updated_token_categories = set()
            if token_categories and (not vote_spec.criterion_idea_id or vote_spec.is_custom):
                for idx, token_category_input in enumerate(token_categories):
                    if not token_category_input.get('id', '-1').startswith('-'):
                        id_ = int(Node.from_global_id(token_category_input['id'])[1])
                        updated_token_categories.add(id_)
                        token_category = models.TokenCategorySpecification.get(id_)
                        update_langstring_from_input_entries(
                            token_category, 'name', token_category_input['title_entries'])
                        token_category.total_number = token_category_input.get('total_number')
                        token_category.color = token_category_input.get('color')
                    else:
                        title_ls = langstring_from_input_entries(
                            token_category_input.get('title_entries', None))
                        total_number = token_category_input.get('total_number')
                        typename = token_category_input.get('typename', 'category{}'.format(idx + 1))
                        color = token_category_input.get('color')
                        vote_spec.token_categories.append(
                            models.TokenCategorySpecification(
                                total_number=total_number,
                                typename=typename, name=title_ls,
                                color=color)
                        )

                # remove token categories that are not in token_categories input
                for token_category_id in set(existing_token_categories.keys()
                                             ).difference(updated_token_categories):
                    db.delete(existing_token_categories[token_category_id])

            db.flush()

        return UpdateTokenVoteSpecification(vote_specification=vote_spec)