def article_editor(req): namespace = req.matchdict.get('namespace') slug = req.matchdict.get('slug') code = req.params.get('code', None) # update project = get_project(namespace, user=req.user) site = get_site(slug, project=project) if site.type != 'publication': raise HTTPNotFound publication = site.instance if code: article = publication.articles.where(Article.code == code).get() else: code = Article.grab_unique_code() # as path article = Article(path=code, code=code) editor_form = build_article_editor_form(req, article) settings_form = build_article_settings_form(req, article) return dict( editor_form=editor_form, settings_form=settings_form, project=project, site=site, publication=publication, article=article, )
def test_path_availability_check(mocker, dummy_request): article = Article(path='scrolliris') dummy_request.params = dummy_request.POST = MultiDict() form = build_article_settings_form(dummy_request, article) with pytest.raises(ValidationError): field = mocker.Mock('field', data=article.path) path_availability_check(form, field)
def test_path_duplication_check(mocker, users, dummy_request): user = users['oswald'] article = Article() dummy_request.params = dummy_request.POST = MultiDict() form = build_article_settings_form(dummy_request, article) with pytest.raises(ValidationError): user = users['oswald'] existing_article = user.articles[0] field = mocker.Mock('field', data=existing_article.path) path_duplication_check(form, field)
def save_meta(req, article): form = build_article_settings_form(req, article) if form.validate(): with req.db.cardinal.atomic(): article.title = form.title.data article.scope = 'private' if not form.scope.data else 'public' article.progress_state = Article.progress_states[ int(form.progress_state.data)] # optional article.path = form.path.data article.save() return (article, form.errors)
def test_path_validations_with_invalid_path_inputs(path, scope, progress_state, dummy_request): article = Article(path=path) dummy_request.params = dummy_request.POST = MultiDict({ 'csrf_token': dummy_request.session.get_csrf_token(), 'title': 'Title', 'path': path, 'scope': scope, 'progress_state': progress_state['value'], }) form = build_article_settings_form(dummy_request, article) assert not form.validate() assert form.path.errors
def test_build_article_settings_form(dummy_request): dummy_request.params = dummy_request.POST = MultiDict() article = Article() form = build_article_settings_form(dummy_request, article) assert isinstance(form, ArticleSettingsForm)