Skip to content

Commit

Permalink
Add from_comment field to attachments and allow attach files from com…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
jespino authored and bameda committed Jan 18, 2017
1 parent fbf6340 commit 2ab743a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 6 deletions.
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-01-17 07:45
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('attachments', '0006_auto_20160617_1233'),
]

operations = [
migrations.AddField(
model_name='attachment',
name='from_comment',
field=models.BooleanField(default=False, verbose_name='from_comment'),
),
]
1 change: 1 addition & 0 deletions taiga/projects/attachments/models.py
Expand Up @@ -58,6 +58,7 @@ class Attachment(models.Model):
sha1 = models.CharField(default="", max_length=40, verbose_name=_("sha1"), blank=True)

is_deprecated = models.BooleanField(default=False, verbose_name=_("is deprecated"))
from_comment = models.BooleanField(default=False, verbose_name=_("from_comment"))
description = models.TextField(null=False, blank=True, verbose_name=_("description"))
order = models.IntegerField(default=0, null=False, blank=False, verbose_name=_("order"))

Expand Down
16 changes: 11 additions & 5 deletions taiga/projects/attachments/permissions.py
Expand Up @@ -27,10 +27,16 @@ def check_permissions(self, request, view, obj=None):
return request.user == obj.owner
return False

class CommentAttachmentPerm(PermissionComponent):
def check_permissions(self, request, view, obj=None):
if obj.from_comment:
return True
return False


class EpicAttachmentPermission(TaigaResourcePermission):
retrieve_perms = HasProjectPerm('view_epics') | IsAttachmentOwnerPerm()
create_perms = HasProjectPerm('modify_epic')
create_perms = HasProjectPerm('modify_epic') | (CommentAttachmentPerm() & HasProjectPerm('comment_epic'))
update_perms = HasProjectPerm('modify_epic') | IsAttachmentOwnerPerm()
partial_update_perms = HasProjectPerm('modify_epic') | IsAttachmentOwnerPerm()
destroy_perms = HasProjectPerm('modify_epic') | IsAttachmentOwnerPerm()
Expand All @@ -39,7 +45,7 @@ class EpicAttachmentPermission(TaigaResourcePermission):

class UserStoryAttachmentPermission(TaigaResourcePermission):
retrieve_perms = HasProjectPerm('view_us') | IsAttachmentOwnerPerm()
create_perms = HasProjectPerm('modify_us')
create_perms = HasProjectPerm('modify_us') | (CommentAttachmentPerm() & HasProjectPerm('comment_us'))
update_perms = HasProjectPerm('modify_us') | IsAttachmentOwnerPerm()
partial_update_perms = HasProjectPerm('modify_us') | IsAttachmentOwnerPerm()
destroy_perms = HasProjectPerm('modify_us') | IsAttachmentOwnerPerm()
Expand All @@ -48,7 +54,7 @@ class UserStoryAttachmentPermission(TaigaResourcePermission):

class TaskAttachmentPermission(TaigaResourcePermission):
retrieve_perms = HasProjectPerm('view_tasks') | IsAttachmentOwnerPerm()
create_perms = HasProjectPerm('modify_task')
create_perms = HasProjectPerm('modify_task') | (CommentAttachmentPerm() & HasProjectPerm('comment_task'))
update_perms = HasProjectPerm('modify_task') | IsAttachmentOwnerPerm()
partial_update_perms = HasProjectPerm('modify_task') | IsAttachmentOwnerPerm()
destroy_perms = HasProjectPerm('modify_task') | IsAttachmentOwnerPerm()
Expand All @@ -57,7 +63,7 @@ class TaskAttachmentPermission(TaigaResourcePermission):

class IssueAttachmentPermission(TaigaResourcePermission):
retrieve_perms = HasProjectPerm('view_issues') | IsAttachmentOwnerPerm()
create_perms = HasProjectPerm('modify_issue')
create_perms = HasProjectPerm('modify_issue') | (CommentAttachmentPerm() & HasProjectPerm('comment_issue'))
update_perms = HasProjectPerm('modify_issue') | IsAttachmentOwnerPerm()
partial_update_perms = HasProjectPerm('modify_issue') | IsAttachmentOwnerPerm()
destroy_perms = HasProjectPerm('modify_issue') | IsAttachmentOwnerPerm()
Expand All @@ -66,7 +72,7 @@ class IssueAttachmentPermission(TaigaResourcePermission):

class WikiAttachmentPermission(TaigaResourcePermission):
retrieve_perms = HasProjectPerm('view_wiki_pages') | IsAttachmentOwnerPerm()
create_perms = HasProjectPerm('modify_wiki_page')
create_perms = HasProjectPerm('modify_wiki_page') | (CommentAttachmentPerm() & HasProjectPerm('comment_wiki_page'))
update_perms = HasProjectPerm('modify_wiki_page') | IsAttachmentOwnerPerm()
partial_update_perms = HasProjectPerm('modify_wiki_page') | IsAttachmentOwnerPerm()
destroy_perms = HasProjectPerm('modify_wiki_page') | IsAttachmentOwnerPerm()
Expand Down
1 change: 1 addition & 0 deletions taiga/projects/attachments/serializers.py
Expand Up @@ -35,6 +35,7 @@ class AttachmentSerializer(serializers.LightSerializer):
url = Field()
description = Field()
is_deprecated = Field()
from_comment = Field()
created_date = Field()
modified_date = Field()
object_id = Field()
Expand Down
2 changes: 1 addition & 1 deletion taiga/projects/attachments/validators.py
Expand Up @@ -29,5 +29,5 @@ class Meta:
model = models.Attachment
fields = ("id", "project", "owner", "name", "attached_file", "size",
"description", "is_deprecated", "created_date",
"modified_date", "object_id", "order", "sha1")
"modified_date", "object_id", "order", "sha1", "from_comment")
read_only_fields = ("owner", "created_date", "modified_date", "sha1")
Expand Up @@ -1027,3 +1027,67 @@ def test_wiki_attachment_list(client, data, data_wiki):

results = helper_test_http_method_and_count(client, 'get', url, None, users)
assert results == [(200, 2), (200, 2), (200, 2), (200, 4), (200, 4)]


def test_create_attachment_by_external_user_without_comment_permission(client):
issue = f.create_issue()
user = f.UserFactory()

assert issue.owner != user
assert issue.project.owner != user

url = reverse("issue-attachments-list")

data = {"description": "test",
"object_id": issue.pk,
"project": issue.project.id,
"attached_file": SimpleUploadedFile("test.txt", b"test"),
"from_comment": True}

client.login(user)
response = client.post(url, data)
assert response.status_code == 403


def test_create_attachment_by_external_user_with_comment_permission_but_without_from_comment_flag(client):
project = f.ProjectFactory(public_permissions=['comment_issue'])
issue = f.create_issue(project=project)

user = f.UserFactory()

assert issue.owner != user
assert issue.project.owner != user

url = reverse("issue-attachments-list")

data = {"description": "test",
"object_id": issue.pk,
"project": issue.project.id,
"attached_file": SimpleUploadedFile("test.txt", b"test"),
"from_comment": False}

client.login(user)
response = client.post(url, data)
assert response.status_code == 403


def test_create_attachment_by_external_user_with_comment_permission_and_with_from_comment_flag(client):
project = f.ProjectFactory(public_permissions=['comment_issue'])
issue = f.create_issue(project=project)

user = f.UserFactory()

assert issue.owner != user
assert issue.project.owner != user

url = reverse("issue-attachments-list")

data = {"description": "test",
"object_id": issue.pk,
"project": issue.project.id,
"attached_file": SimpleUploadedFile("test.txt", b"test"),
"from_comment": True}

client.login(user)
response = client.post(url, data)
assert response.status_code == 201

0 comments on commit 2ab743a

Please sign in to comment.