def test_after_outside_forms_context(self): import webob.multidict import schemaish from pyramid.view import render_view_to_response from pyramid_formish.zcml import FormAction from zope.configuration.config import ConfigurationMachine context = ConfigurationMachine() context.route_prefix = '' context.autocommit = True context.registry = self.config.registry request = testing.DummyRequest() request.registry = self.config.registry title = schemaish.String() factory = make_controller_factory(fields=[('title', title)]) directive = self._makeOne(context, factory) directive._actions = [FormAction('submit', 'title', True)] directive.after() display = render_view_to_response(None, request, '') self.assertEqual(display.body, '123') request = testing.DummyRequest() request.params = webob.multidict.MultiDict() request.params['submit'] = True display = render_view_to_response(None, request, '') self.assertEqual(display.body, 'submitted')
def test_novalidate(self): import schemaish import validatish from pyramid_formish.zcml import FormAction title = schemaish.String(validator=validatish.validator.Required()) action = FormAction('cancel', 'cancel', False) actions = [action] factory = make_controller_factory(fields=[('title', title)]) view = self._makeOne(factory, action, actions) context = testing.DummyModel() request = testing.DummyRequest() result = view(context, request) self.assertEqual(result.body, 'cancelled')
def test_validate_no_error(self): import schemaish from pyramid_formish.zcml import FormAction title = schemaish.String() action = FormAction('submit', 'submit', True) actions = [action] factory = make_controller_factory(fields=[('title', title)], defaults={'title': 'the title'}) view = self._makeOne(factory, action, actions) context = testing.DummyModel() request = testing.DummyRequest() result = view(context, request) self.assertEqual(result.body, 'submitted') self.failUnless(request.form) self.assertEqual(dict(request.form.defaults), {'title': 'the title'})
def test_with_method_GET(self): import schemaish import validatish import formish from pyramid_formish.zcml import FormAction action = FormAction(None, 'cancel', False) title = schemaish.String(validator=validatish.validator.Required()) actions = [action] titlewidget = formish.Input() factory = make_controller_factory(fields=[('title', title)], widgets={'title': titlewidget}) view = self._makeOne(factory, action, actions, method='GET') context = testing.DummyModel() request = testing.DummyRequest() result = view(context, request) self.assertEqual(result.body, '123')
def test_with_actionsuccess(self): import schemaish from pyramid_formish.zcml import FormAction title = schemaish.String() L = [] def success(controller, converted): L.append(converted) return 'success' action = FormAction('submit', 'submit', True, success) actions = [action] factory = make_controller_factory(fields=[('title', title)], defaults={'title': 'the title'}) view = self._makeOne(factory, action, actions) context = testing.DummyModel() request = testing.DummyRequest() result = view(context, request) self.assertEqual(result, 'success') self.assertEqual(L, [{'title': None}])
def show_blogentry_view(context, request): post_url = resource_url(context, request, "comments", "add_comment.html") karldates = getUtility(IKarlDates) profiles = find_profiles(context) workflow = get_workflow(IBlogEntry, 'security', context) if workflow is None: security_states = [] else: security_states = get_security_states(workflow, context, request) # Convert blog comments into a digestable form for the template comments = [] page_title = context.title api = TemplateAPI(context, request, page_title) for comment in context['comments'].values(): profile = profiles.get(comment.creator) author_name = profile.title author_url = resource_url(profile, request) newc = {} newc['id'] = comment.__name__ if has_permission('edit', comment, request): newc['edit_url'] = resource_url(comment, request, 'edit.html') else: newc['edit_url'] = None if has_permission('delete', comment, request): newc['delete_url'] = resource_url(comment, request, 'delete.html') else: newc['delete_url'] = None if has_permission('administer', comment, request): newc['advanced_url'] = resource_url(comment, request, 'advanced.html') else: newc['advanced_url'] = None # Display portrait photo = profile.get('photo') if photo is not None: photo_url = thumb_url(photo, request, PROFILE_THUMB_SIZE) else: photo_url = api.static_url + "/images/defaultUser.gif" newc["portrait_url"] = photo_url newc['author_url'] = author_url newc['author_name'] = author_name newc['date'] = karldates(comment.created, 'longform') newc['timestamp'] = comment.created newc['text'] = comment.text # Fetch the attachments info newc['attachments'] = fetch_attachments(comment, request) comments.append(newc) comments.sort(key=lambda c: c['timestamp']) client_json_data = dict(tagbox=get_tags_client_data(context, request), ) actions = [] if has_permission('edit', context, request): actions.append(('Edit', 'edit.html')) if has_permission('edit', context, request): actions.append(('Delete', 'delete.html')) if has_permission('administer', context, request): actions.append(('Advanced', 'advanced.html')) api.is_taggable = True byline_info = getMultiAdapter((context, request), IBylineInfo) blog = find_interface(context, IBlog) backto = { 'href': resource_url(blog, request), 'title': blog.title, } # manually construct formish comment form controller = AddCommentFormController(context['comments'], request) form_schema = schemaish.Structure() form_fields = controller.form_fields() for fieldname, field in form_fields: form_schema.add(fieldname, field) form_action_url = '%sadd_comment.html' % resource_url( context['comments'], request) comment_form = Form(form_schema, add_default_action=False, name='save', action_url=form_action_url) form_defaults = controller.form_defaults() comment_form.defaults = form_defaults request.form_defaults = form_defaults form_actions = [ FormAction('submit', 'submit'), FormAction('cancel', 'cancel', validate=False) ] for action in form_actions: comment_form.add_action(action.name, action.title) widgets = controller.form_widgets(form_fields) for name, widget in widgets.items(): comment_form[name].widget = widget # this is for enable imagedrawer for adding blog comments api.karl_client_data['text'] = dict(enable_imagedrawer_upload=True, ) return dict( api=api, actions=actions, comments=comments, attachments=fetch_attachments(context['attachments'], request), head_data=convert_to_script(client_json_data), comment_form=comment_form, post_url=post_url, byline_info=byline_info, backto=backto, security_states=security_states, )
def show_forum_topic_view(context, request): post_url = resource_url(context, request, "comments", "add_comment.html") karldates = getUtility(IKarlDates) profiles = find_profiles(context) # Convert comments into a digestable form for the template comments = [] page_title = context.title actions = [] if has_permission('edit', context, request): actions.append(('Edit', 'edit.html')) if has_permission('delete', context, request): actions.append(('Delete', 'delete.html')) if has_permission('administer', context, request): actions.append(('Advanced', 'advanced.html')) api = TemplateAPI(context, request, page_title) for comment in context['comments'].values(): profile = profiles.get(comment.creator) author_name = profile.title author_url = resource_url(profile, request) newc = {} newc['id'] = comment.__name__ if has_permission('edit', comment, request): newc['edit_url'] = resource_url(comment, request, 'edit.html') else: newc['edit_url'] = None if has_permission('delete', comment, request): newc['delete_url'] = resource_url(comment, request, 'delete.html') else: newc['delete_url'] = None if has_permission('administer', comment, request): newc['advanced_url'] = resource_url(comment, request, 'advanced.html') else: newc['advanced_url'] = None # Display portrait photo = profile.get('photo') photo_url = {} if photo is not None: photo_url = thumb_url(photo, request, PROFILE_THUMB_SIZE) else: photo_url = api.static_url + "/images/defaultUser.gif" newc["portrait_url"] = photo_url newc['author_url'] = author_url newc['author_name'] = author_name newc['date'] = karldates(comment.created, 'longform') newc['timestamp'] = comment.created newc['text'] = comment.text # Fetch the attachments info newc['attachments'] = fetch_attachments(comment, request) comments.append(newc) comments.sort(key=lambda x: x['timestamp']) byline_info = getMultiAdapter((context, request), IBylineInfo) forum = find_interface(context, IForum) backto = { 'href': resource_url(forum, request), 'title': forum.title, } # provide client data for rendering current tags in the tagbox client_json_data = dict( tagbox = get_tags_client_data(context, request), ) # Get a layout layout_provider = get_layout_provider(context, request) old_layout = layout_provider('community') if support_attachments(context): attachments = fetch_attachments(context['attachments'], request) else: attachments = () # manually construct formish comment form controller = AddCommentFormController(context['comments'], request) form_schema = schemaish.Structure() form_fields = controller.form_fields() for fieldname, field in form_fields: form_schema.add(fieldname, field) form_action_url = '%sadd_comment.html' % resource_url(context['comments'], request) comment_form = Form(form_schema, add_default_action=False, name='save', action_url=form_action_url) form_defaults = controller.form_defaults() comment_form.defaults = form_defaults request.form_defaults = form_defaults form_actions = [FormAction('submit', 'submit'), FormAction('cancel', 'cancel', validate=False)] for action in form_actions: comment_form.add_action(action.name, action.title) widgets = controller.form_widgets(form_fields) for name, widget in widgets.items(): comment_form[name].widget = widget # enable imagedrawer for adding forum replies (comments) api.karl_client_data['text'] = dict( enable_imagedrawer_upload = True, ) # ux2 layout = request.layout_manager.layout layout.section_style = "none" layout.head_data['panel_data']['tinymce'] = api.karl_client_data['text'] layout.head_data['panel_data']['tagbox'] = client_json_data['tagbox'] layout.add_portlet('tagbox') return render_to_response( 'templates/show_forum_topic.pt', dict(api=api, actions=actions, comments=comments, attachments=attachments, formfields=api.formfields, post_url=post_url, byline_info=byline_info, head_data=convert_to_script(client_json_data), backto=backto, old_layout=old_layout, comment_form=comment_form), request=request, )