Merge pull request #331 from taigaio/bug/importer-perrmisisons-and-memberships
Fix a bug that create non-ouner memberships where the importer user is a member of the importer projectremotes/origin/enhancement/email-actions
commit
e3ebc35670
|
@ -88,11 +88,38 @@ class ProjectImporterViewSet(mixins.ImportThrottlingPolicyMixin, CreateModelMixi
|
||||||
data = request.DATA.copy()
|
data = request.DATA.copy()
|
||||||
data['owner'] = data.get('owner', request.user.email)
|
data['owner'] = data.get('owner', request.user.email)
|
||||||
|
|
||||||
|
# Create Project
|
||||||
project_serialized = service.store_project(data)
|
project_serialized = service.store_project(data)
|
||||||
|
|
||||||
if project_serialized is None:
|
if not project_serialized:
|
||||||
raise exc.BadRequest(service.get_errors())
|
raise exc.BadRequest(service.get_errors())
|
||||||
|
|
||||||
|
# Create roles
|
||||||
|
roles_serialized = None
|
||||||
|
if "roles" in data:
|
||||||
|
roles_serialized = service.store_roles(project_serialized.object, data)
|
||||||
|
|
||||||
|
if not roles_serialized:
|
||||||
|
raise exc.BadRequest(_("We needed at least one role"))
|
||||||
|
|
||||||
|
# Create memberships
|
||||||
|
if "memberships" in data:
|
||||||
|
service.store_memberships(project_serialized.object, data)
|
||||||
|
|
||||||
|
try:
|
||||||
|
owner_membership = project_serialized.object.memberships.get(user=project_serialized.object.owner)
|
||||||
|
owner_membership.is_owner = True
|
||||||
|
owner_membership.save()
|
||||||
|
except Membership.DoesNotExist:
|
||||||
|
Membership.objects.create(
|
||||||
|
project=project_serialized.object,
|
||||||
|
email=project_serialized.object.owner.email,
|
||||||
|
user=project_serialized.object.owner,
|
||||||
|
role=project_serialized.object.roles.all().first(),
|
||||||
|
is_owner=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create project values choicess
|
||||||
if "points" in data:
|
if "points" in data:
|
||||||
service.store_choices(project_serialized.object, data,
|
service.store_choices(project_serialized.object, data,
|
||||||
"points", serializers.PointsExportSerializer)
|
"points", serializers.PointsExportSerializer)
|
||||||
|
@ -127,6 +154,7 @@ class ProjectImporterViewSet(mixins.ImportThrottlingPolicyMixin, CreateModelMixi
|
||||||
"severities" in data):
|
"severities" in data):
|
||||||
service.store_default_choices(project_serialized.object, data)
|
service.store_default_choices(project_serialized.object, data)
|
||||||
|
|
||||||
|
# Created custom attributes
|
||||||
if "userstorycustomattributes" in data:
|
if "userstorycustomattributes" in data:
|
||||||
service.store_custom_attributes(project_serialized.object, data,
|
service.store_custom_attributes(project_serialized.object, data,
|
||||||
"userstorycustomattributes",
|
"userstorycustomattributes",
|
||||||
|
@ -142,26 +170,12 @@ class ProjectImporterViewSet(mixins.ImportThrottlingPolicyMixin, CreateModelMixi
|
||||||
"issuecustomattributes",
|
"issuecustomattributes",
|
||||||
serializers.IssueCustomAttributeExportSerializer)
|
serializers.IssueCustomAttributeExportSerializer)
|
||||||
|
|
||||||
if "roles" in data:
|
# Is there any error?
|
||||||
service.store_roles(project_serialized.object, data)
|
|
||||||
|
|
||||||
if "memberships" in data:
|
|
||||||
service.store_memberships(project_serialized.object, data)
|
|
||||||
|
|
||||||
if project_serialized.object.memberships.filter(user=project_serialized.object.owner).count() == 0:
|
|
||||||
if project_serialized.object.roles.all().count() > 0:
|
|
||||||
Membership.objects.create(
|
|
||||||
project=project_serialized.object,
|
|
||||||
email=project_serialized.object.owner.email,
|
|
||||||
user=project_serialized.object.owner,
|
|
||||||
role=project_serialized.object.roles.all().first(),
|
|
||||||
is_owner=True
|
|
||||||
)
|
|
||||||
|
|
||||||
errors = service.get_errors()
|
errors = service.get_errors()
|
||||||
if errors:
|
if errors:
|
||||||
raise exc.BadRequest(errors)
|
raise exc.BadRequest(errors)
|
||||||
|
|
||||||
|
# Importer process is OK
|
||||||
response_data = project_serialized.data
|
response_data = project_serialized.data
|
||||||
response_data['id'] = project_serialized.object.id
|
response_data['id'] = project_serialized.object.id
|
||||||
headers = self.get_success_headers(response_data)
|
headers = self.get_success_headers(response_data)
|
||||||
|
|
|
@ -155,7 +155,10 @@ def store_role(project, role):
|
||||||
def store_roles(project, data):
|
def store_roles(project, data):
|
||||||
results = []
|
results = []
|
||||||
for role in data.get("roles", []):
|
for role in data.get("roles", []):
|
||||||
results.append(store_role(project, role))
|
serialized = store_role(project, role)
|
||||||
|
if serialized:
|
||||||
|
results.append(serialized)
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -53,17 +53,17 @@ def test_valid_project_import_without_extra_data(client):
|
||||||
data = {
|
data = {
|
||||||
"name": "Imported project",
|
"name": "Imported project",
|
||||||
"description": "Imported project",
|
"description": "Imported project",
|
||||||
|
"roles": [{"name": "Role"}]
|
||||||
}
|
}
|
||||||
|
|
||||||
response = client.post(url, json.dumps(data), content_type="application/json")
|
response = client.post(url, json.dumps(data), content_type="application/json")
|
||||||
assert response.status_code == 201
|
assert response.status_code == 201
|
||||||
response_data = json.loads(response.content.decode("utf-8"))
|
response_data = json.loads(response.content.decode("utf-8"))
|
||||||
must_empty_children = [
|
must_empty_children = [
|
||||||
"issues", "user_stories", "roles", "us_statuses", "wiki_pages", "priorities",
|
"issues", "user_stories", "us_statuses", "wiki_pages", "priorities",
|
||||||
"severities", "milestones", "points", "issue_types", "task_statuses",
|
"severities", "milestones", "points", "issue_types", "task_statuses",
|
||||||
"memberships", "issue_statuses", "wiki_links",
|
"issue_statuses", "wiki_links",
|
||||||
]
|
]
|
||||||
|
|
||||||
assert all(map(lambda x: len(response_data[x]) == 0, must_empty_children))
|
assert all(map(lambda x: len(response_data[x]) == 0, must_empty_children))
|
||||||
assert response_data["owner"] == user.email
|
assert response_data["owner"] == user.email
|
||||||
|
|
||||||
|
@ -166,6 +166,22 @@ def test_valid_project_import_with_extra_data(client):
|
||||||
assert response_data["owner"] == user.email
|
assert response_data["owner"] == user.email
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_project_import_without_roles(client):
|
||||||
|
user = f.UserFactory.create()
|
||||||
|
client.login(user)
|
||||||
|
|
||||||
|
url = reverse("importer-list")
|
||||||
|
data = {
|
||||||
|
"name": "Imported project",
|
||||||
|
"description": "Imported project",
|
||||||
|
}
|
||||||
|
|
||||||
|
response = client.post(url, json.dumps(data), content_type="application/json")
|
||||||
|
assert response.status_code == 400
|
||||||
|
response_data = json.loads(response.content.decode("utf-8"))
|
||||||
|
assert len(response_data) == 2
|
||||||
|
assert Project.objects.filter(slug="imported-project").count() == 0
|
||||||
|
|
||||||
def test_invalid_project_import_with_extra_data(client):
|
def test_invalid_project_import_with_extra_data(client):
|
||||||
user = f.UserFactory.create()
|
user = f.UserFactory.create()
|
||||||
client.login(user)
|
client.login(user)
|
||||||
|
@ -174,7 +190,10 @@ def test_invalid_project_import_with_extra_data(client):
|
||||||
data = {
|
data = {
|
||||||
"name": "Imported project",
|
"name": "Imported project",
|
||||||
"description": "Imported project",
|
"description": "Imported project",
|
||||||
"roles": [{}],
|
"roles": [{
|
||||||
|
"permissions": [],
|
||||||
|
"name": "Test"
|
||||||
|
}],
|
||||||
"us_statuses": [{}],
|
"us_statuses": [{}],
|
||||||
"severities": [{}],
|
"severities": [{}],
|
||||||
"priorities": [{}],
|
"priorities": [{}],
|
||||||
|
@ -187,7 +206,7 @@ def test_invalid_project_import_with_extra_data(client):
|
||||||
response = client.post(url, json.dumps(data), content_type="application/json")
|
response = client.post(url, json.dumps(data), content_type="application/json")
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
response_data = json.loads(response.content.decode("utf-8"))
|
response_data = json.loads(response.content.decode("utf-8"))
|
||||||
assert len(response_data) == 8
|
assert len(response_data) == 7
|
||||||
assert Project.objects.filter(slug="imported-project").count() == 0
|
assert Project.objects.filter(slug="imported-project").count() == 0
|
||||||
|
|
||||||
|
|
||||||
|
@ -198,6 +217,10 @@ def test_valid_project_import_with_custom_attributes(client):
|
||||||
data = {
|
data = {
|
||||||
"name": "Imported project",
|
"name": "Imported project",
|
||||||
"description": "Imported project",
|
"description": "Imported project",
|
||||||
|
"roles": [{
|
||||||
|
"permissions": [],
|
||||||
|
"name": "Test"
|
||||||
|
}],
|
||||||
"userstorycustomattributes": [{
|
"userstorycustomattributes": [{
|
||||||
"name": "custom attribute example 1",
|
"name": "custom attribute example 1",
|
||||||
"description": "short description 1",
|
"description": "short description 1",
|
||||||
|
@ -234,6 +257,10 @@ def test_invalid_project_import_with_custom_attributes(client):
|
||||||
data = {
|
data = {
|
||||||
"name": "Imported project",
|
"name": "Imported project",
|
||||||
"description": "Imported project",
|
"description": "Imported project",
|
||||||
|
"roles": [{
|
||||||
|
"permissions": [],
|
||||||
|
"name": "Test"
|
||||||
|
}],
|
||||||
"userstorycustomattributes": [{ }],
|
"userstorycustomattributes": [{ }],
|
||||||
"taskcustomattributes": [{ }],
|
"taskcustomattributes": [{ }],
|
||||||
"issuecustomattributes": [{ }]
|
"issuecustomattributes": [{ }]
|
||||||
|
|
Loading…
Reference in New Issue