def _export(self, obj): res = { "id": obj.id, "title": obj.title, "create_time": encode_time(obj.create_time), "description": (None if obj.description is None else obj.description.head.text), "creator": self._user_transform._compute_key(obj.creator), "adhocracy_type": "proposal", "category": obj.category.title if obj.category else None, "tags": [o.name for o, _ in obj.tags], "badges": [o.badge.title for o in obj.delegateablebadges if o.badge.polymorphic_identity != "category"], } if self._options.get("include_proposal_creator_badges", False): res["creator_badges"] = ([o.badge.title for o in obj.creator.userbadges],) if self._options.get("include_ratings", False): res.update( { "rate_pro": obj.rate_poll.tally.num_for, "rate_contra": obj.rate_poll.tally.num_against, "rate_neutral": obj.rate_poll.tally.num_abstain, } ) if votedetail.is_enabled(): vd = votedetail.calc_votedetail_dict(obj.instance, obj.rate_poll, badge_title_only=True) if vd: res["votedetail_rate_poll"] = vd if self._options.get("include_instance_proposal_comment", False): ctransform = CommentTransform(self._options, obj.description.comments, None, self._user_transform) res["comments"] = ctransform.export_all() return res
def _settings_voting_form(self, id): c.page_instance = self._get_current_instance(id) c.settings_menu = settings_menu(c.page_instance, 'voting') c.delay_options = [] for delay in ((0, _("No delay")), (1, _("1 Day")), (2, _("2 Days")), (7, _("One Week")), (14, _("Two Weeks")), (28, _("Four Weeks"))): c.delay_options.append( {'value': delay[0], 'label': h.literal(delay[1]), 'selected': c.page_instance.activation_delay == delay[0]}) c.majority_options = [] for majority in ((0.5, _("A simple majority (½ of vote)")), (0.66, _("A two-thirds majority"))): c.majority_options.append( {'value': majority[0], 'label': h.literal(majority[1]), 'selected': c.page_instance.required_majority == majority[0]}) if votedetail.is_enabled(): c.votedetail_all_userbadges = model.UserBadge.all( instance=c.page_instance, include_global=True) else: c.votedetail_all_userbadges = None return render("/instance/settings_voting.html")
def vote(self, id, format): c.poll = self._get_open_poll(id) if c.poll.action != model.Poll.ADOPT: abort(400, _("This is not an adoption poll.")) require.poll.vote(c.poll) decision = democracy.Decision(c.user, c.poll) votes = decision.make(self.form_result.get("position")) model.meta.Session.commit() if not asbool(config.get('adhocracy.hide_individual_votes', 'false')): for vote in votes: event.emit(event.T_VOTE_CAST, vote.user, instance=c.instance, topics=[c.poll.scope], vote=vote, poll=c.poll) if format == 'json': vdetail = votedetail.calc_votedetail_dict(c.instance, c.poll)\ if votedetail.is_enabled() else None return render_json( dict(decision=decision, score=c.poll.tally.score, votedetail=vdetail)) else: redirect(h.entity_url(c.poll.subject))
def _modify(self, o, data): _set_optional(o, data, 'label') if 'creator' in data: creator = self._user_transform._get_by_key(data['creator']) if creator: o.creator = creator _set_optional(o, data, 'description') _set_optional(o, data, 'required_majority', 'adhocracy_') _set_optional(o, data, 'activation_delay', 'adhocracy_') if 'create_time' in data: o.create_time = decode_time(data['create_time']) if 'adhocracy_access_time' in data: o.access_time = decode_time(data['adhocracy_access_time']) if 'adhocracy_delete_time' in data: if data['adhocracy_delete_time'] is None: o.delete_time = None else: o.delete_time = decode_time(data['adhocracy_delete_time']) _set_optional(o, data, 'default_group_id', 'adhocracy_') _set_optional(o, data, 'allow_adopt', 'adhocracy_') _set_optional(o, data, 'allow_delegate', 'adhocracy_') _set_optional(o, data, 'allow_propose', 'adhocracy_') _set_optional(o, data, 'allow_index', 'adhocracy_') _set_optional(o, data, 'hidden', 'adhocracy_') if 'locale' in data: if data['locale'] is None: o.locale = data['locale'] else: o.locale = decode_locale(data['locale']) _set_optional(o, data, 'css', 'adhocracy_') _set_optional(o, data, 'frozen') _set_optional(o, data, 'milestones', 'adhocracy_') _set_optional(o, data, 'use_norms', 'adhocracy_') _set_optional(o, data, 'require_selection', 'adhocracy_') _set_optional(o, data, 'is_authenticated', 'adhocracy_') _set_optional(o, data, 'hide_global_categories', 'adhocracy_') _set_optional(o, data, 'editable_comments_default', 'adhocracy_') _set_optional(o, data, 'editable_proposals_default', 'adhocracy_') _set_optional(o, data, 'require_valid_email', 'adhocracy_') _set_optional(o, data, 'allow_thumbnailbadges', 'adhocracy_') _set_optional(o, data, 'thumbnailbadges_height', 'adhocracy_') _set_optional(o, data, 'thumbnailbadges_width', 'adhocracy_') if self._options.get('include_instance_proposal'): ptransform = ProposalTransform(self._options, o, self._user_transform) ptransform.import_all(data.get('proposals', {})) if self._badge_transform and votedetail.is_enabled(): if 'adhocracy_votedetail_userbadges' in data: o.votedetail_userbadges = [ self._badge_transform._get_by_key(bid) for bid in data['adhocracy_votedetail_userbadges'] ]
def _modify(self, o, data): _set_optional(o, data, "label") if "creator" in data: creator = self._user_transform._get_by_key(data["creator"]) if creator: o.creator = creator _set_optional(o, data, "description") _set_optional(o, data, "required_majority", "adhocracy_") _set_optional(o, data, "activation_delay", "adhocracy_") if "create_time" in data: o.create_time = decode_time(data["create_time"]) if "adhocracy_access_time" in data: o.access_time = decode_time(data["adhocracy_access_time"]) if "adhocracy_delete_time" in data: if data["adhocracy_delete_time"] is None: o.delete_time = None else: o.delete_time = decode_time(data["adhocracy_delete_time"]) _set_optional(o, data, "default_group_id", "adhocracy_") _set_optional(o, data, "allow_adopt", "adhocracy_") _set_optional(o, data, "allow_delegate", "adhocracy_") _set_optional(o, data, "allow_propose", "adhocracy_") _set_optional(o, data, "allow_index", "adhocracy_") _set_optional(o, data, "hidden", "adhocracy_") if "locale" in data: if data["locale"] is None: o.locale = data["locale"] else: o.locale = decode_locale(data["locale"]) _set_optional(o, data, "css", "adhocracy_") _set_optional(o, data, "frozen") _set_optional(o, data, "milestones", "adhocracy_") _set_optional(o, data, "use_norms", "adhocracy_") _set_optional(o, data, "require_selection", "adhocracy_") _set_optional(o, data, "is_authenticated", "adhocracy_") _set_optional(o, data, "hide_global_categories", "adhocracy_") _set_optional(o, data, "editable_comments_default", "adhocracy_") _set_optional(o, data, "editable_proposals_default", "adhocracy_") _set_optional(o, data, "require_valid_email", "adhocracy_") _set_optional(o, data, "allow_thumbnailbadges", "adhocracy_") _set_optional(o, data, "thumbnailbadges_height", "adhocracy_") _set_optional(o, data, "thumbnailbadges_width", "adhocracy_") if self._options.get("include_instance_proposal"): ptransform = ProposalTransform(self._options, o, self._user_transform) ptransform.import_all(data.get("proposals", {})) if self._badge_transform and votedetail.is_enabled(): if "adhocracy_votedetail_userbadges" in data: o.votedetail_userbadges = [ self._badge_transform._get_by_key(bid) for bid in data["adhocracy_votedetail_userbadges"] ]
def rate(self, id, format): # rating is like polling but steps via abstention, i.e. if you have # first voted "for", rating will first go to "abstain" and only # then produce "against"- c.poll = self._get_open_poll(id) if c.poll.action not in [model.Poll.RATE, model.Poll.SELECT]: abort(400, _("This is not a rating poll.")) require.poll.vote(c.poll) decision = democracy.Decision(c.user, c.poll) old = decision.result new = self.form_result.get("position") positions = { (model.Vote.YES, model.Vote.YES): model.Vote.YES, (model.Vote.ABSTAIN, model.Vote.YES): model.Vote.YES, (model.Vote.NO, model.Vote.YES): model.Vote.ABSTAIN, (model.Vote.YES, model.Vote.NO): model.Vote.ABSTAIN, (model.Vote.ABSTAIN, model.Vote.NO): model.Vote.NO, (model.Vote.NO, model.Vote.NO): model.Vote.NO } position = positions.get((old, new), new) votes = decision.make(position) tally = model.Tally.create_from_poll(c.poll) event_type = { model.Poll.RATE: event.T_RATING_CAST, model.Poll.SELECT: event.T_SELECT_VARIANT }.get(c.poll.action) model.meta.Session.commit() if not asbool(config.get('adhocracy.hide_individual_votes', 'false')): for vote in votes: event.emit(event_type, vote.user, instance=c.instance, topics=[c.poll.scope], vote=vote, poll=c.poll) if format == 'json': vdetail = votedetail.calc_votedetail_dict(c.instance, c.poll)\ if votedetail.is_enabled() else None return render_json( dict(decision=decision, tally=tally.to_dict(), votedetail=vdetail)) elif format == 'ajax': return self.widget(id, format=self.form_result.get('cls')) elif c.poll.action == model.Poll.SELECT: redirect(h.entity_url(c.poll.selection)) else: redirect(h.entity_url(c.poll.subject))
def _export(self, obj): res = { 'adhocracy_type': 'instance', 'key': obj.key, 'label': obj.label, 'creator': self._user_transform._compute_key(obj.creator), 'description': obj.description, 'adhocracy_required_majority': obj.required_majority, 'adhocracy_activation_delay': obj.activation_delay, 'create_time': encode_time(obj.create_time), 'adhocracy_access_time': encode_time(obj.access_time), 'adhocracy_delete_time': (encode_time(obj.delete_time) if obj.delete_time else None), 'adhocracy_default_group_id': obj.default_group_id, 'adhocracy_allow_adopt': obj.allow_adopt, 'adhocracy_allow_delegate': obj.allow_delegate, 'adhocracy_allow_propose': obj.allow_propose, 'adhocracy_allow_index': obj.allow_index, 'adhocracy_hidden': obj.hidden, 'locale': encode_locale(obj.locale), 'adhocracy_css': obj.css, 'frozen': obj.frozen, 'adhocracy_milestones': obj.milestones, 'adhocracy_use_norms': obj.use_norms, 'adhocracy_require_selection': obj.require_selection, 'adhocracy_is_authenticated': obj.is_authenticated, 'adhocracy_hide_global_categories': obj.hide_global_categories, 'adhocracy_editable_comments_default': ( obj.editable_comments_default), 'adhocracy_editable_proposals_default': ( obj.editable_proposals_default), 'adhocracy_require_valid_email': obj.require_valid_email, 'adhocracy_allow_thumbnailbadges': obj.allow_thumbnailbadges, 'adhocracy_thumbnailbadges_height': obj.thumbnailbadges_height, 'adhocracy_thumbnailbadges_width': obj.thumbnailbadges_width, } if self._options.get('include_instance_proposal'): ptransform = ProposalTransform(self._options, obj, self._user_transform) res['proposals'] = ptransform.export_all() if self._badge_transform and votedetail.is_enabled(): res['adhocracy_votedetail_userbadges'] = [ self._badge_transform._compute_key(b) for b in obj.votedetail_userbadges ] return res
def settings_voting(self, id): c.page_instance = self._get_current_instance(id) require.instance.edit(c.page_instance) defaults = { '_method': 'PUT', 'required_majority': c.page_instance.required_majority, 'activation_delay': c.page_instance.activation_delay, 'allow_adopt': c.page_instance.allow_adopt, 'allow_delegate': c.page_instance.allow_delegate, '_tok': csrf.token_id()} if votedetail.is_enabled(): defaults['votedetail_badges'] = [ b.id for b in c.page_instance.votedetail_userbadges] return htmlfill.render( self._settings_voting_form(id), defaults=defaults)
def settings_voting_update(self, id, format='html'): c.page_instance = self._get_current_instance(id) require.instance.edit(c.page_instance) updated_attributes = ['allow_delegate'] if not config.get_bool('adhocracy.hide_final_adoption_votings'): updated_attributes.extend( ['required_majority', 'activation_delay', 'allow_adopt']) updated = update_attributes( c.page_instance, self.form_result, updated_attributes) if votedetail.is_enabled(): new_badges = self.form_result['votedetail_badges'] updated_vd = c.page_instance.votedetail_userbadges != new_badges if updated_vd: c.page_instance.votedetail_userbadges = new_badges updated = updated or updated_vd return self._settings_result(updated, c.page_instance, 'voting')
def rate(self, id, format): # rating is like polling but steps via abstention, i.e. if you have # first voted "for", rating will first go to "abstain" and only # then produce "against"- c.poll = self._get_open_poll(id) if c.poll.action not in [model.Poll.RATE, model.Poll.SELECT]: abort(400, _("This is not a rating poll.")) require.poll.vote(c.poll) decision = democracy.Decision(c.user, c.poll) old = decision.result new = self.form_result.get("position") positions = {(model.Vote.YES, model.Vote.YES): model.Vote.YES, (model.Vote.ABSTAIN, model.Vote.YES): model.Vote.YES, (model.Vote.NO, model.Vote.YES): model.Vote.ABSTAIN, (model.Vote.YES, model.Vote.NO): model.Vote.ABSTAIN, (model.Vote.ABSTAIN, model.Vote.NO): model.Vote.NO, (model.Vote.NO, model.Vote.NO): model.Vote.NO} position = positions.get((old, new), new) votes = decision.make(position) tally = model.Tally.create_from_poll(c.poll) event_type = {model.Poll.RATE: event.T_RATING_CAST, model.Poll.SELECT: event.T_SELECT_VARIANT }.get(c.poll.action) model.meta.Session.commit() if not config.get_bool('adhocracy.hide_individual_votes'): for vote in votes: event.emit(event_type, vote.user, instance=c.instance, topics=[c.poll.scope], vote=vote, poll=c.poll) if format == 'json': vdetail = votedetail.calc_votedetail_dict(c.instance, c.poll)\ if votedetail.is_enabled() else None return render_json(dict(decision=decision, tally=tally.to_dict(), votedetail=vdetail)) elif format == 'ajax': return self.widget(id, format=self.form_result.get('cls')) elif c.poll.action == model.Poll.SELECT: redirect(h.entity_url(c.poll.selection)) else: redirect(h.entity_url(c.poll.subject))
def _export(self, obj): res = { "adhocracy_type": "instance", "key": obj.key, "label": obj.label, "creator": self._user_transform._compute_key(obj.creator), "description": obj.description, "adhocracy_required_majority": obj.required_majority, "adhocracy_activation_delay": obj.activation_delay, "create_time": encode_time(obj.create_time), "adhocracy_access_time": encode_time(obj.access_time), "adhocracy_delete_time": (encode_time(obj.delete_time) if obj.delete_time else None), "adhocracy_default_group_id": obj.default_group_id, "adhocracy_allow_adopt": obj.allow_adopt, "adhocracy_allow_delegate": obj.allow_delegate, "adhocracy_allow_propose": obj.allow_propose, "adhocracy_allow_index": obj.allow_index, "adhocracy_hidden": obj.hidden, "locale": encode_locale(obj.locale), "adhocracy_css": obj.css, "frozen": obj.frozen, "adhocracy_milestones": obj.milestones, "adhocracy_use_norms": obj.use_norms, "adhocracy_require_selection": obj.require_selection, "adhocracy_is_authenticated": obj.is_authenticated, "adhocracy_hide_global_categories": obj.hide_global_categories, "adhocracy_editable_comments_default": (obj.editable_comments_default), "adhocracy_editable_proposals_default": (obj.editable_proposals_default), "adhocracy_require_valid_email": obj.require_valid_email, "adhocracy_allow_thumbnailbadges": obj.allow_thumbnailbadges, "adhocracy_thumbnailbadges_height": obj.thumbnailbadges_height, "adhocracy_thumbnailbadges_width": obj.thumbnailbadges_width, } if self._options.get("include_instance_proposal"): ptransform = ProposalTransform(self._options, obj, self._user_transform) res["proposals"] = ptransform.export_all() if self._badge_transform and votedetail.is_enabled(): res["adhocracy_votedetail_userbadges"] = [ self._badge_transform._compute_key(b) for b in obj.votedetail_userbadges ] return res
def _export(self, obj): res = { 'id': obj.id, 'title': obj.title, 'description': (None if obj.description is None else obj.description.head.text), 'creator': self._user_transform._compute_key(obj.creator), 'adhocracy_type': 'proposal', 'category': obj.category.title if obj.category else None, 'tags': [o.name for o, _ in obj.tags], 'badges': [ o.badge.title for o in obj.delegateablebadges if o.badge.polymorphic_identity != 'category' ], } if self._options.get('include_proposal_creator_badges', False): res['creator_badges'] = [ o.badge.title for o in obj.creator.userbadges ], if self._options.get('include_ratings', False): res.update({ 'rate_pro': obj.rate_poll.tally.num_for, 'rate_contra': obj.rate_poll.tally.num_against, 'rate_neutral': obj.rate_poll.tally.num_abstain, }) if votedetail.is_enabled(): vd = votedetail.calc_votedetail_dict(obj.instance, obj.rate_poll, badge_title_only=True) if vd: res['votedetail_rate_poll'] = vd if self._options.get('include_instance_proposal_comment', False): ctransform = CommentTransform(self._options, obj.description.comments, None, self._user_transform) res['comments'] = ctransform.export_all() return res
def vote(self, id, format): c.poll = self._get_open_poll(id) if c.poll.action != model.Poll.ADOPT: abort(400, _("This is not an adoption poll.")) require.poll.vote(c.poll) decision = democracy.Decision(c.user, c.poll) votes = decision.make(self.form_result.get("position")) model.meta.Session.commit() if not config.get_bool('adhocracy.hide_individual_votes'): for vote in votes: event.emit(event.T_VOTE_CAST, vote.user, instance=c.instance, topics=[c.poll.scope], vote=vote, poll=c.poll) if format == 'json': vdetail = votedetail.calc_votedetail_dict(c.instance, c.poll)\ if votedetail.is_enabled() else None return render_json(dict(decision=decision, score=c.poll.tally.score, votedetail=vdetail)) else: redirect(h.entity_url(c.poll.subject))