class AddIdeaSchema(Schema): idea = colander.SchemaNode( ObjectType(), widget=idea_choice, title=_('Re-use one or more existing idea(s)'), missing=None, #description=_('Choose an idea') ) new_idea_choice = colander.SchemaNode( colander.Boolean(), widget=deform.widget.CheckboxWidget(css_class="new-idea-control"), label=_('Add a new idea'), title='', missing=False ) new_idea = select(IdeaSchema(factory=Idea, editable=True, omit=['keywords'], widget=SimpleMappingtWidget( mapping_css_class='hide-bloc new-idea-form', ajax=False)), ['challenge', 'title', 'text', 'keywords'])
class CreateIdeaView(FormView): title = _('Create an idea') schema = omit( select(IdeaSchema(factory=Idea, editable=True, omit=('anonymous', )), [ 'challenge', 'title', 'text', 'tree', 'attached_files', 'anonymous' ]), ["_csrf_token_"]) behaviors = [ CrateAndPublishAsProposal, CrateAndPublish, CreateIdea, Cancel ] formid = 'formcreateidea' name = 'createidea' css_class = 'panel-transparent' def before_update(self): user = get_current(self.request) behaviors = [ CrateAndPublishAsProposal, CrateAndPublish, CreateIdea, Cancel ] if 'proposal' not in self.request.content_to_manage: behaviors = [CrateAndPublish, CreateIdea, Cancel] if getattr(self.request, 'moderate_ideas', False): can_submit = getattr(user, 'can_submit_idea', lambda: True) if not can_submit(): behaviors.remove(CrateAndPublish) if len(behaviors) < 4: self.behaviors = behaviors self.behaviors_instances = OrderedDict() specific_behaviors = [ b._class_ for b in self.specific_behaviors_instances ] self._init_behaviors(specific_behaviors) self.schema = update_anonymous_schemanode(self.request.root, self.schema) self.schema = update_challenge_schemanode(self.request, user, self.schema) if not getattr(self, 'is_home_form', False): self.action = self.request.resource_url( self.context, 'novaideoapi', query={ 'op': 'update_action_view', 'node_id': CreateIdea.node_definition.id }) self.schema.widget = deform.widget.FormWidget( css_class='deform novaideo-ajax-form') else: self.action = self.request.resource_url(self.context, 'createidea') self.schema.widget = deform.widget.FormWidget( css_class='material-form deform') def bind(self): if getattr(self, 'is_home_form', False): return {'is_home_form': True} return {}
class CreateIdeaView(FormView): title = _('Transform the comment into an idea') schema = select( IdeaSchema(factory=Idea, editable=True, omit=('anonymous', )), [ 'challenge', 'title', 'text', 'keywords', 'attached_files', 'anonymous' ]) behaviors = [ CrateAndPublishAsProposal, CrateAndPublish, TransformToIdea, Cancel ] formid = 'formcreateidea' name = 'createidea' def default_data(self): data = {'text': self.context.comment} attached_files = self.context.files data['attached_files'] = [] files = [] for file_ in attached_files: file_data = add_file_data(file_) if file_data: files.append(file_data) if files: data['attached_files'] = files challenge = self.context.challenge if challenge and challenge.can_add_content: data['challenge'] = challenge return data def before_update(self): user = get_current(self.request) self.schema = update_anonymous_schemanode(self.request.root, self.schema) self.schema = update_challenge_schemanode(self.request, user, self.schema) self.action = self.request.resource_url( self.context, 'novaideoapi', query={ 'op': 'update_action_view', 'node_id': TransformToIdea.node_definition.id }) self.schema.widget = deform.widget.FormWidget( css_class='deform novaideo-ajax-form')
class DuplicateIdeaView(FormView): title = _('Duplicate the idea') name = 'duplicateidea' schema = select(IdeaSchema(omit=('anonymous', )), [ 'challenge', 'title', 'text', 'tree', 'attached_files', 'anonymous', 'note' ]) behaviors = [DuplicateIdea, Cancel] formid = 'formduplicateidea' item_template = 'novaideo:views/idea_management/templates/panel_item.pt' def before_update(self): user = get_current(self.request) self.schema = update_anonymous_schemanode(self.request.root, self.schema) self.schema = update_challenge_schemanode(self.request, user, self.schema) self.action = self.request.resource_url( self.context, 'novaideoapi', query={ 'op': 'update_action_view', 'node_id': DuplicateIdea.node_definition.id }) self.schema.widget = deform.widget.FormWidget( css_class='deform novaideo-ajax-form') def default_data(self): data = self.context.get_data(self.schema) files = [] for file_ in data.get('attached_files', []): file_data = add_file_data(file_) if file_data: files.append(file_data) if files: data['attached_files'] = files challenge = self.context.challenge if challenge and not challenge.can_add_content: data['challenge'] = '' return data
def mutate(root, args, context, info): idea_schema = select( IdeaSchema(), ['title', 'text', 'keywords', 'attached_files', 'anonymous']) args = dict(args) attached_files = args.pop('attached_files', None) uploaded_files = [] if attached_files: for index, file_ in enumerate(attached_files): file_storage = context.POST.get( 'variables.attachedFiles.'+str(index)) fp = file_storage.file fp.seek(0) uploaded_files.append({ 'fp': fp, 'filename': urllib.parse.unquote(file_storage.filename)}) args['attached_files'] = uploaded_files args = idea_schema.deserialize(args) args['attached_files'] = [f['_object_data'] for f in args['attached_files']] context, request, action, args = get_execution_data( CreateAndPublishIdea.action_id, args) new_idea = None if action: anonymous = args.pop('anonymous', False) new_idea = IdeaClass(**args) appstruct = { '_object_data': new_idea, 'anonymous': anonymous } action.execute(context, request, appstruct) else: raise Exception( request.localizer.translate(_("Authorization failed"))) status = new_idea is not None return CreateAndPublishIdea(idea=new_idea, status=status)
class EditIdeaFormView(FormView): title = _('Edit the idea') schema = select(IdeaSchema(), ['title', 'text', 'tree', 'attached_files', 'note']) behaviors = [EditIdea, Cancel] formid = 'formeditidea' wrapper_template = 'pontus:templates/views_templates/simple_view_wrapper.pt' name = 'editIdea' def before_update(self): self.action = self.request.resource_url(self.context, 'novaideoapi', query={ 'op': 'update_action_view', 'node_id': EditIdea.node_definition.id }) self.schema.widget = deform.widget.FormWidget( css_class='deform novaideo-ajax-form') def default_data(self): return self.context
class NewIdeaSchema(Schema): new_idea = select( IdeaSchema(factory=Idea, editable=True, widget=SimpleMappingWidget()), ['title', 'text', 'keywords'])