Skip to content

Commit

Permalink
Editable comments #59
Browse files Browse the repository at this point in the history
Added tests and an edited flag.
  • Loading branch information
Zitrax committed Nov 21, 2016
1 parent 3108202 commit e2391bf
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def run_migrations_online():
context.configure(
connection=connection,
target_metadata=target_metadata,
render_as_batch=True
render_as_batch=False
)

with context.begin_transaction():
Expand Down
26 changes: 26 additions & 0 deletions alembic/versions/4c0d58a4687c_added_comments_edited.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Added comments.edited
Revision ID: 4c0d58a4687c
Revises: b5739a261104
Create Date: 2016-11-21 21:42:01.804309
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
from sqlalchemy import false

revision = '4c0d58a4687c'
down_revision = 'b5739a261104'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('comment', sa.Column('edited', sa.Boolean(), server_default=false(), nullable=False))


def downgrade():
op.drop_column('comment', 'edited')
4 changes: 4 additions & 0 deletions artifakt/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class Vcs(Base):

repository = relationship("Repository")

def __str__(self):
return "VCS({}, {}, {})".format(self.id, self.repository_id, self.revision)


class VcsSchema(BaseSchema):
repository = fields.Nested(RepositorySchema, exclude=('id',))
Expand Down Expand Up @@ -314,6 +317,7 @@ class Comment(Base):
user_id = Column(Integer, ForeignKey('users.id'))
parent_id = Column(Integer, ForeignKey('comment.id'), nullable=True)
deleted = Column(Boolean, default=False, nullable=False)
edited = Column(Boolean, default=False, nullable=False)

artifakt = relationship('Artifakt', backref=backref('comments', cascade="all, delete-orphan"))
user = relationship('User')
Expand Down
2 changes: 1 addition & 1 deletion artifakt/templates/artifact.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
<span data-id="{{ comment.id }}" class="comment {{ "editable_comment" if comment.user.id == request.user.id else "" }}">
{{ comment.comment }}</span>&nbsp;&mdash;&nbsp;
<span class="comment_by">{{ "" if comment.deleted else comment.user }}</span>
<span class="comment_age" title="{{ comment.time }}">{{ comment.age }} ago</span>&nbsp;
<span class="comment_age" title="{{ comment.time }}">{{ comment.age }} ago {{ "(Edited)" if comment.edited else "" }}</span>&nbsp;
{% if not comment.deleted %}
<span class="comment_reply" data-comment-id="{{ comment.id }}"><span class="reply_arrow">↩</span></span>
{% if comment.user.id == request.user.id %}
Expand Down
45 changes: 44 additions & 1 deletion artifakt/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from artifakt.utils.time import duration_string
from artifakt.views.admin import admin, verify_fs
from artifakt.views.artifacts import artifact_delete, artifact_download, artifact_comment_add, artifact_delivery_add, \
artifact_archive_view, artifact_delivery_delete, artifacts_json, artifact_json
artifact_archive_view, artifact_delivery_delete, artifacts_json, artifact_json, artifact_comment_edit, \
artifact_comment_delete
from artifakt.views.artifacts import artifacts
from artifakt.views.upload import upload_post
from nose.tools import assert_in, assert_true, assert_raises, assert_is_not_none, \
Expand Down Expand Up @@ -447,6 +448,33 @@ def add_comment(self, reply=True, mail_count=0, user=None, parent_id=None, af=No

return af

def test_edit_comment(self):
af = self.add_comment(reply=False)
comment = af.comments[0]
assert_false(comment.edited)
request = self.generic_request(user=self.user2)
request.matchdict['id'] = comment.id
artifact_comment_edit(request)
eq_(request.response.status_code, HTTPForbidden.code)
request.user = self.user
artifact_comment_edit(request)
eq_(request.response.status_code, HTTPBadRequest.code)
request.POST['value'] = 'test_edited'
eq_({'OK': 'OK'}, artifact_comment_edit(request))
assert_true(comment.edited)
eq_(comment.comment, "test_edited")

def test_delete_comment(self):
af = self.add_comment(reply=False)
comment = af.comments[0]
request = self.generic_request(user=self.user2)
request.matchdict['id'] = comment.id
assert_raises(HTTPForbidden, artifact_comment_delete, request)
request.user = self.user
artifact_comment_delete(request)
DBSession.refresh(af)
eq_(0, len(af.comments))

def test_add_comment_mails(self):
# Adding two comments as different users - this should trigger an email
af = self.add_comment(reply=False)
Expand Down Expand Up @@ -550,6 +578,21 @@ def test_artifact_json(self):
eq_(af.filename, data['filename'])
eq_(af.sha1, data['sha1'])

# def test_password_reset(self):
#
# request = self.generic_request()
# # Mock request.registry['config'].fullauth
# o = type('', (), {})()
# o.fullauth = ''
# request.registry['config'] = o
# # Mock localization
# request._ = dummy_autotranslate
# # Request must have an email
# request.POST['email'] = request.user.email
#
# prv = PasswordResetView(request)
# eq_(1, prv.post())


class TestAdmin(BaseTest):
# TODO: Use something like pyramid.paster.get_app('testing.ini') to test the permissions
Expand Down
1 change: 1 addition & 0 deletions artifakt/views/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def artifact_comment_edit(request):
return 'ERROR: Missing value in request'
value = request.POST['value']
comment.comment = value
comment.edited = True
return {'OK': 'OK'}


Expand Down

0 comments on commit e2391bf

Please sign in to comment.