def update_category_badge(self, id): try: params = request.params.copy() params['id'] = id self.form_result = CategoryBadgeUpdateForm().to_python(params) except Invalid as i: return self.edit(id, i.unpack_errors()) badge = self._get_badge_or_redirect(id) title, color, visible, description, impact, instance =\ self._get_common_fields(self.form_result) child_descr = self.form_result.get("select_child_description") child_descr = child_descr.replace("$badge_title", title) #TODO global badges must have only global badges children, joka parent = self.form_result.get("parent") if parent and parent.id == id: parent = None badge.title = title badge.color = color badge.visible = visible badge.description = description if badge.impact != impact: badge.impact = impact for delegateable in badge.delegateables: update_entity(delegateable, UPDATE) badge.instance = instance badge.select_child_description = child_descr badge.parent = parent meta.Session.commit() h.flash(_("Badge changed successfully"), 'success') redirect(self.base_url)
def update_thumbnail_badge(self, id): try: self.form_result = ThumbnailBadgeForm().to_python(request.params) except Invalid as i: return self.edit(id, i.unpack_errors()) badge = self._get_badge_or_redirect(id) title, color, visible, description, impact, instance =\ self._get_common_fields(self.form_result) thumbnail = self.form_result.get("thumbnail") if isinstance(thumbnail, FieldStorage): badge.thumbnail = thumbnail.file.read() if 'delete_thumbnail' in self.form_result: badge.thumbnail = None badge.title = title badge.color = color badge.visible = visible badge.description = description if badge.impact != impact: badge.impact = impact for delegateable in badge.delegateables: update_entity(delegateable, UPDATE) badge.instance = instance meta.Session.commit() h.flash(_("Badge changed successfully"), 'success') redirect(self.base_url)
def update_user_badge(self, id): try: self.form_result = UserBadgeForm().to_python(request.params) except Invalid as i: return self.edit(id, i.unpack_errors()) badge = self._get_badge_or_redirect(id) title, color, visible, description, impact, instance =\ self._get_common_fields(self.form_result) group = self.form_result.get('group') display_group = self.form_result.get('display_group') badge.group = group badge.title = title badge.color = color badge.visible = visible badge.description = description if badge.impact != impact: badge.impact = impact for user in badge.users: update_entity(user, UPDATE) badge.instance = instance badge.display_group = display_group if behavior_enabled(): badge.behavior_proposal_sort_order = self.form_result.get( 'behavior_proposal_sort_order') meta.Session.commit() h.flash(_("Badge changed successfully"), 'success') redirect(self.base_url)
def update_badges(self, id): user = get_entity_or_abort(model.User, id) badges = self.form_result.get('badge') if not has('global.admin'): # instance admins may only add user badges limited to this instance for badge in badges: if not badge.instance == c.instance: h.flash(_(u'Invalid badge choice.'), u'error') redirect(h.entity_url(user)) creator = c.user added = [] removed = [] for badge in user.badges: if badge not in badges: removed.append(badge) user.badges.remove(badge) for badge in badges: if badge not in user.badges: badge.assign(user, creator) added.append(badge) model.meta.Session.flush() # FIXME: needs commit() cause we do an redirect() which raises # an Exception. model.meta.Session.commit() update_entity(user, model.UPDATE) redirect(h.entity_url(user, instance=c.instance))
def delete(self, id): badge = self._get_badge_or_redirect(id) for badge_instance in badge.badges(): meta.Session.delete(badge_instance) update_entity(badge_instance.badged_entity(), UPDATE) meta.Session.delete(badge) meta.Session.commit() h.flash(_(u"Badge deleted successfully"), 'success') redirect(self.base_url)
def untag_all(self, format='html'): # HACK create a proper permission require.instance.edit(c.instance) delegateable = self.form_result.get('delegateable') tag = self.form_result.get('tag') for tagging in delegateable.taggings: if tagging.tag == tag: tagging.delete() update_entity(delegateable, model.UPDATE) model.meta.Session.commit() return ret_success( message=_('Tag "%s" has been removed from %s "%s".') % (tag.name, _(delegateable.type), delegateable.label), category='success', entity=delegateable, format=format)
def post_update(entity, operation): ''' Post an update task for the entity and any related objects. ''' from adhocracy.lib import queue queue.update_entity(entity, operation) ## Do subsequent updates to reindex related content # NOTE: This may post duplicate update tasks if an entity # is part of the session, and also updated depending on # another entity. Ignored for now cause the real work # is asynchronous and (probably) not expensive. # NOTE: Move the decisions about which other objects to # update to the models if isinstance(entity, Poll): queue.update_entity(entity.scope, UPDATE)
def untag_all(self, format='html'): # HACK create a proper permission require.instance.edit(c.instance) delegateable = self.form_result.get('delegateable') tag = self.form_result.get('tag') for tagging in delegateable.taggings: if tagging.tag == tag: tagging.delete() update_entity(delegateable, model.UPDATE) model.meta.Session.commit() return ret_success( message=_('Tag "%s" has been removed from %s "%s".') % ( tag.name, _(delegateable.type), delegateable.label), category='success', entity=delegateable, format=format)
def update_instance_badge(self, id): try: self.form_result = BadgeForm().to_python(request.params) except Invalid as i: return self.edit(id, i.unpack_errors()) badge = self._get_badge_or_redirect(id) title, color, visible, description, impact, instance =\ self._get_common_fields(self.form_result) badge.title = title badge.color = color badge.visible = visible badge.description = description if badge.impact != impact: badge.impact = impact for instance in badge.instances: update_entity(instance, UPDATE) badge.instance = instance meta.Session.commit() h.flash(_("Badge changed successfully"), 'success') redirect(self.base_url)
def update_badges(self, id, format='html'): instance = get_entity_or_abort(model.Instance, id) editable_badges = self._editable_badges(instance) badges = self.form_result.get('badge') #remove badges for badge in instance.badges: if badge not in editable_badges: # the user can not edit the badge, so we don't remove it continue if badge not in badges: instance.badges.remove(badge) #add badges for badge in badges: if badge not in instance.badges: badge.assign(instance, c.user) model.meta.Session.commit() update_entity(instance, model.UPDATE) if format == 'ajax': obj = {'html': render_def('/badge/tiles.html', 'badges', badges=instance.badges)} return render_json(obj)