Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_project(self):
db = self.application.DBSession()
projects = [
database.Project(name="first project", description=""),
database.Project(name="a test", description="Test project"),
database.Project(name="private P", description="admin can't see"),
database.Project(name="last project", description="Other"),
]
for project in projects:
db.add(project)
db.commit()
for i in [1, 2, 4]:
db.add(database.ProjectMember(
project_id=i,
user_login='admin',
privileges=database.Privileges.ADMIN))
db.commit()
# Authenticate with token
response = self.get('/?token=' + self.application.single_user_token)
self.assertEqual(response.code, 302)
self.assertEqual(response.headers['Location'], '/')
# Check project list
response = self.get('/')
self.assertEqual(response.code, 200)
self.assertIn(b"first project", response.body)
self.assertIn(b"a test", response.body)
self.assertNotIn(b"private P", response.body)
login,
)
self.db.add(cmd)
commands.append(cmd)
else:
try:
privileges = database.Privileges[user['privileges']]
except KeyError:
self.set_status(400)
return self.send_json({'error': "Invalid privileges %r" %
user.get('privileges')})
if login in members:
members[login].privileges = privileges
else:
self.db.add(
database.ProjectMember(project=project,
user_login=login,
privileges=privileges)
)
cmd = database.Command.member_add(
self.current_user, project.id,
login, privileges,
)
self.db.add(cmd)
commands.append(cmd)
self.db.commit()
for cmd in commands:
self.db.refresh(cmd)
self.application.notify_project(project.id, cmd)
self.set_status(204)
def post(self):
name = self.get_body_argument('name', '')
description = self.get_body_argument('description', '')
try:
validate.project_name(name)
validate.project_description(description)
# Create project
project = database.Project(name=name, description=description)
self.db.add(project)
# Add user as admin
membership = database.ProjectMember(
project=project,
user_login=self.current_user,
privileges=database.Privileges.ADMIN
)
self.db.add(membership)
# Add default tags
self.db.add(database.Tag(
project=project,
# TRANSLATORS: Default tag name
path=self.gettext("interesting"),
# TRANSLATORS: Default tag description
description=self.gettext("Further review required")),
)
self.db.commit()
return self.redirect(self.reverse_url('project', project.id))
def post(self):
name = self.get_body_argument('name', '')
description = self.get_body_argument('description', '')
if not name:
return self.render('project_new.html',
name=name, description=description,
error="Please enter a name")
# Create project
project = database.Project(name=name, description=description)
self.db.add(project)
# Add user as admin
membership = database.ProjectMember(
project=project,
user_login=self.current_user,
privileges=database.Privileges.ADMIN
)
self.db.add(membership)
# Add default set of tags
self.db.add(database.Tag(project=project, path='interesting',
description="Further review required"))
self.db.add(database.Tag(project=project, path='people',
description="Known people"))
self.db.commit()
self.redirect(self.reverse_url('project', project.id))
def get_project(self, project_id):
try:
project_id = int(project_id)
except ValueError:
raise HTTPError(404)
project_member = (
self.db.query(database.ProjectMember)
.options(joinedload(database.ProjectMember.project))
.get((project_id, self.current_user))
)
if project_member is None:
raise HTTPError(404)
return project_member.project
},
sort_keys=True,
))
tags_json = jinja2.Markup(json.dumps(
{
str(tag.id): {'id': tag.id,
'path': tag.path,
'description': tag.description,
'count': tag.highlights_count}
for tag in project.tags
},
sort_keys=True,
))
members = (
self.db.query(database.ProjectMember)
.filter(database.ProjectMember.project_id == project_id)
).all()
can_delete_project = any(
member.user_login == self.current_user and
member.privileges.can_delete_project()
for member in members
)
members_json = jinja2.Markup(json.dumps(
{member.user_login: {'privileges': member.privileges.name}
for member in members}
))
_ = self.xsrf_token # Make sure XSRF cookie is set
return self.render(
'project.html',
project=project,
last_event=(project.last_event
if project.last_event is not None
def get_project(self, project_id):
try:
project_id = int(project_id)
except ValueError:
raise HTTPError(404)
project_member = (
self.db.query(database.ProjectMember)
.options(joinedload(database.ProjectMember.project))
.get((project_id, self.current_user))
)
if project_member is None:
raise HTTPError(404)
return project_member.project, project_member.privileges
def get_document(self, project_id, document_id, contents=False):
try:
project_id = int(project_id)
document_id = int(document_id)
except ValueError:
raise HTTPError(404)
q = (
self.db.query(database.ProjectMember, database.Document)
.filter(database.Document.project_id == project_id)
.filter(database.Document.id == document_id)
.filter(database.ProjectMember.user_login == self.current_user)
.filter(database.ProjectMember.project_id == project_id)
)
if contents:
q = q.options(undefer(database.Document.contents))
res = q.one_or_none()
if res is None:
raise HTTPError(404)
member, document = res
return document, member.privileges
for doc in project.documents
},
sort_keys=True,
))
tags_json = jinja2.Markup(json.dumps(
{
str(tag.id): {'id': tag.id,
'path': tag.path,
'description': tag.description,
'count': tag.highlights_count}
for tag in project.tags
},
sort_keys=True,
))
members = (
self.db.query(database.ProjectMember)
.filter(database.ProjectMember.project_id == project_id)
).all()
can_delete_project = any(
member.user_login == self.current_user and
member.privileges.can_delete_project()
for member in members
)
members_json = jinja2.Markup(json.dumps(
{member.user_login: {'privileges': member.privileges.name}
for member in members}
))
_ = self.xsrf_token # Make sure XSRF cookie is set
return self.render(
'project.html',
project=project,
last_event=(project.last_event