class ProjectForm(BaseProjectForm): def __init__(self, model_instance = None, *args, **kwargs): super(ProjectForm, self).__init__(*args, **kwargs) self.model = None self.language.label_attr = 'name' self.language.query.order('name') if model_instance: self.name.data = model_instance.name self.description.data = model_instance.description self.language.data = str(model_instance.language.key()) self.github_url.data = model_instance.github_url self.documentation_url.data = model_instance.documentation_url self.model = model_instance def save(self): language = ProgrammingLanguage.get(self.language.data) if self.model: self.model.name = self.name.data self.model.github_url = self.github_url.data self.model.documentation_url = self.documentation_url.data self.model.language = language self.model.description = self.description.data else: self.model = Project( name = self.name.data, github_url = self.github_url.data, documentation_url = self.documentation_url.data, language = language, description = self.description.data ) self.model.put() return self.model
def test_delete_a_project(self): "Should delete a project on URL /projects/<slug>/delete" self._mock_logged_in() from labs.models import Project project = Project(name = u'Ruby on Rails', description = 'Bla bla bla', language = self.language) project.put() project_slug = project.slug url = '/admin/projects/%s/delete' %(project_slug) response = self.client.get(url) project = Project.all().filter('slug = ', project_slug).get() assert_true(project is None)
def test_get_project_url(self): "The project should contain a URL in the format: /<language-slug>/<project-slug> using url_for for building" project = Project(name = u'Comunicação avançada', language = self.language, description='Bla bla bla') project.put() url = '/%s/%s' %(project.language.slug, project.slug) url_for_mocked = self.mocker.replace('flask.url_for') url_for_mocked(mocker.ANY, language_slug = self.language.slug, project_slug = project.slug) self.mocker.result(url) self.mocker.replay() assert_equals(project.get_url(), url)
def test_update_a_project(self): "Should update a project with given data by post" self._mock_logged_in(times = 2) project = Project(name = u'The big project', description='Bla bla bla', language = self.language) project.put() slug = project.slug github_url = 'http://github.com/franciscosouza/labs' data = { 'name' : project.name, 'language' : project.language.key(), 'description' : 'Bla bla bla', 'github_url' : github_url } self.client.post('/admin/projects/%s' % slug, data = data, follow_redirects = True) project = Project.all().filter('slug =', slug).get() assert_equals(project.github_url, github_url)
def tearDown(self): self.mocker.restore() self.language.delete() from labs.models import Project from google.appengine.ext import db projects = Project.all().fetch(1000) db.delete(projects)
class TestFrontendProjects(unittest.TestCase): def setUp(self): self.mocker = mocker.Mocker() self.client = labs.app.test_client() self.language = ProgrammingLanguage(name = u'Python') self.language.put() self.project = Project(name = u'Talks', language = self.language, github_url = 'http://github.com/franciscosouza/talks', description = 'Bla bla bla') self.project.put() self.project_url = '/%s/%s' %(self.language.slug, self.project.slug) def test_show_a_project(self): "Should show a project in the URL /<language-slug>/<project-slug>" title = '<h2>%s</h2>' % self.project.name response = self.client.get(self.project_url) assert_true(title in response.data) def test_link_github(self): "Should link Github repository when it is present when viewing a project (/<language-slug>/<project-slug>)" response = self.client.get(self.project_url) dom = html.fromstring(response.data) path = '//a[@href="%s"]' % self.project.github_url link_list = dom.xpath(path) assert_equals(len(link_list), 1) def test_list_projects(self): "Should list all projects on /projects" mocked_url_for = self.mocker.replace('flask.url_for') mocked_url_for(mocker.ANY, language_slug = self.project.language.slug, project_slug = self.project.slug) self.mocker.result(self.project_url) self.mocker.replay() response = self.client.get('/projects') dom = html.fromstring(response.data) project_link_list = dom.xpath('//a[@href="%s"]' % self.project_url) assert_true(len(project_link_list) > 0) self.mocker.verify() def tearDown(self): self.mocker.restore() self.project.delete() self.language.delete()
def setUp(self): self.mocker = mocker.Mocker() self.client = labs.app.test_client() self.language = ProgrammingLanguage(name = u'Python') self.language.put() self.project = Project(name = u'Testing everything', github_url = u'http://github.com/franciscosouza/test', description = 'Bla bla bla', language = self.language) self.project.put()
def update_project(slug): project = Project.all().filter('slug =', slug).get() form = ProjectForm() if form.validate_on_submit(): form.model = project form.save() flash('Project updated') return redirect(url_for('list_projects')) return render_template('admin/projects/edit.html', form=form, project=project)
def setUp(self): self.mocker = mocker.Mocker() self.client = labs.app.test_client() self.language = ProgrammingLanguage(name = u'Python') self.language.put() self.project = Project(name = u'Talks', language = self.language, github_url = 'http://github.com/franciscosouza/talks', description = 'Bla bla bla') self.project.put() self.project_url = '/%s/%s' %(self.language.slug, self.project.slug)
def test_create_a_project(self): "Should create a project with given data by post" self._mock_logged_in(times = 2) expected_name = u'The project' from labs.util import slugify expected_slug = slugify(expected_name) data = { 'name' : expected_name, 'language' : self.language.key(), 'description' : 'Bla bla bla' } response = self.client.post('/admin/projects', data = data, follow_redirects = True) from labs.models import Project project = Project.all().filter('slug = ', expected_slug).get() assert_equals(project.name, expected_name)
def save(self): language = ProgrammingLanguage.get(self.language.data) if self.model: self.model.name = self.name.data self.model.github_url = self.github_url.data self.model.documentation_url = self.documentation_url.data self.model.language = language self.model.description = self.description.data else: self.model = Project( name = self.name.data, github_url = self.github_url.data, documentation_url = self.documentation_url.data, language = language, description = self.description.data ) self.model.put() return self.model
def show_project(language_slug, project_slug): project = Project.all().filter('slug = ', project_slug).get() return render_template('frontend/projects/show.html', project=project)
def edit_project(slug): project = Project.all().filter('slug =', slug).get() form = ProjectForm(project) return render_template('admin/projects/edit.html', form=form, project=project)
def delete_project(slug): project = Project.all().filter('slug =', slug).get() project.delete() flash('Project successful deleted') return redirect(url_for('list_projects'))
def list_projects(): projects = Project.all().order('name') return render_template('admin/projects/list.html', projects=projects)
class TestAdminProjects(unittest.TestCase): def setUp(self): self.mocker = mocker.Mocker() self.client = labs.app.test_client() self.language = ProgrammingLanguage(name = u'Python') self.language.put() self.project = Project(name = u'Testing everything', github_url = u'http://github.com/franciscosouza/test', description = 'Bla bla bla', language = self.language) self.project.put() def _mock_logged_in(self, times = 1): logged_in = self.mocker.replace('google.appengine.api.users.is_current_user_admin') logged_in() self.mocker.result(True) self.mocker.count(times) self.mocker.replay() def test_list_projects(self): "Should list all projects in /admin/projects" self._mock_logged_in() response = self.client.get('/admin/projects') assert_true('Projects list' in response.data) def test_project_form(self): "Should show a form for new projects" self._mock_logged_in() response = self.client.get('/admin/projects/new') assert_true('<h2>New project</h2>' in response.data) def test_create_a_project(self): "Should create a project with given data by post" self._mock_logged_in(times = 2) expected_name = u'The project' from labs.util import slugify expected_slug = slugify(expected_name) data = { 'name' : expected_name, 'language' : self.language.key(), 'description' : 'Bla bla bla' } response = self.client.post('/admin/projects', data = data, follow_redirects = True) from labs.models import Project project = Project.all().filter('slug = ', expected_slug).get() assert_equals(project.name, expected_name) def test_create_a_duplicated_name_project(self): "Should generate a new slug to a duplicated project" self._mock_logged_in(times = 4) expected_name = u'The project' from labs.util import slugify expected_slug = slugify(expected_name) + '-1' data = { 'name' : expected_name, 'language' : self.language.key(), 'description' : 'Bla bla bla' } response = self.client.post('/admin/projects', data = data, follow_redirects = True) response = self.client.post('/admin/projects', data = data, follow_redirects = True) from labs.models import Project project = Project.all().filter('slug = ', expected_slug).get() assert_equals(project.name, expected_name) def test_validate_creating_a_project(self): "Should validate the new project form" self._mock_logged_in(times = 2) response = self.client.post('/admin/projects', data = {}, follow_redirects = True) assert_true('This field is required' in response.data) def test_form_edit_project(self): "Should show a form with the project data on /admin/projects/<slug>/edit" self._mock_logged_in() response = self.client.get('/admin/projects/%s/edit' % self.project.slug) dom = html.fromstring(response.data) name_field = dom.xpath('//input[@type="text" and @name="name"]')[0] github_field = dom.xpath('//input[@type="text" and @name="github_url"]')[0] docs_field = dom.xpath('//input[@type="text" and @name="documentation_url"]')[0] language_select = dom.xpath('//select[@name="language"]')[0] selected_language = language_select.xpath('//option[@selected="selected"]')[0] assert_equals(name_field.value, self.project.name) assert_equals(github_field.value, self.project.github_url) assert_equals(docs_field.value, '') assert_equals(selected_language.text, self.project.language.name) def test_update_a_project(self): "Should update a project with given data by post" self._mock_logged_in(times = 2) project = Project(name = u'The big project', description='Bla bla bla', language = self.language) project.put() slug = project.slug github_url = 'http://github.com/franciscosouza/labs' data = { 'name' : project.name, 'language' : project.language.key(), 'description' : 'Bla bla bla', 'github_url' : github_url } self.client.post('/admin/projects/%s' % slug, data = data, follow_redirects = True) project = Project.all().filter('slug =', slug).get() assert_equals(project.github_url, github_url) def test_failing_update(self): "Should not update if the data was not provided" self._mock_logged_in(times = 2) response = self.client.post('/admin/projects/%s' % self.project.slug, data = {}, follow_redirects = True) assert_true('This field is required' in response.data) def test_delete_a_project(self): "Should delete a project on URL /projects/<slug>/delete" self._mock_logged_in() from labs.models import Project project = Project(name = u'Ruby on Rails', description = 'Bla bla bla', language = self.language) project.put() project_slug = project.slug url = '/admin/projects/%s/delete' %(project_slug) response = self.client.get(url) project = Project.all().filter('slug = ', project_slug).get() assert_true(project is None) def tearDown(self): self.mocker.restore() self.language.delete() from labs.models import Project from google.appengine.ext import db projects = Project.all().fetch(1000) db.delete(projects)
def test_save_project_with_slug(self): "Before put a project, should generate a slug" project = Project(name = u'My Project', language = self.language, description='Bla bla bla') project.put() assert_equals(project.slug, u'my-project')
def list_projects(): projects = Project.all() return render_template('frontend/projects/list.html', projects=projects)