def test_get_comment_versions(client):
    project = f.create_project()
    us = f.create_userstory(project=project)
    f.MembershipFactory.create(project=project,
                               user=project.owner,
                               is_admin=True)
    key = make_key_from_model_object(us)
    history_entry = f.HistoryEntryFactory.create(
        project=project,
        type=HistoryType.change,
        comment="testing",
        key=key,
        diff={},
        user={"pk": project.owner.id},
        edit_comment_date=timezone.now(),
        comment_versions=[{
            "comment_html": "<p>test</p>",
            "date": "2016-05-09T09:34:27.221Z",
            "comment": "test",
            "user": {
                "id": project.owner.id,
            }
        }])

    client.login(project.owner)
    url = reverse("userstory-history-comment-versions", args=(us.id, ))
    url = "%s?id=%s" % (url, history_entry.id)

    response = client.get(url, content_type="application/json")
    assert 200 == response.status_code, response.status_code
    assert response.data[0]["user"]["username"] == project.owner.username
Example #2
0
    def _import_activity(self, obj, activity, options):
        activity_data = self._transform_activity_data(obj, activity, options)
        if activity_data is None:
            return

        change_old = activity_data['change_old']
        change_new = activity_data['change_new']
        hist_type = activity_data['hist_type']
        comment = activity_data['comment']
        user = activity_data['user']

        key = make_key_from_model_object(activity_data['obj'])
        typename = get_typename_for_model_class(type(activity_data['obj']))

        diff = make_diff_from_dicts(change_old, change_new)
        fdiff = FrozenDiff(key, diff, {})

        entry = HistoryEntry.objects.create(
            user=user,
            project_id=obj.project.id,
            key=key,
            type=hist_type,
            snapshot=None,
            diff=fdiff.diff,
            values=make_diff_values(typename, fdiff),
            comment=comment,
            comment_html=mdrender(obj.project, comment),
            is_hidden=False,
            is_snapshot=False,
        )
        HistoryEntry.objects.filter(id=entry.id).update(
            created_at=activity['occurred_at'])
        return HistoryEntry.objects.get(id=entry.id)
def test_edit_comment(client):
    project = f.create_project()
    us = f.create_userstory(project=project)
    f.MembershipFactory.create(project=project,
                               user=project.owner,
                               is_admin=True)
    key = make_key_from_model_object(us)
    history_entry = f.HistoryEntryFactory.create(type=HistoryType.change,
                                                 project=project,
                                                 comment="testing",
                                                 key=key,
                                                 diff={},
                                                 user={"pk": project.owner.id})

    history_entry_created_at = history_entry.created_at
    assert history_entry.comment_versions == None
    assert history_entry.edit_comment_date == None

    client.login(project.owner)
    url = reverse("userstory-history-edit-comment", args=(us.id, ))
    url = "%s?id=%s" % (url, history_entry.id)

    data = json.dumps({"comment": "testing update comment"})
    response = client.post(url, data, content_type="application/json")
    assert 200 == response.status_code, response.status_code

    history_entry = HistoryEntry.objects.get(id=history_entry.id)
    assert len(history_entry.comment_versions) == 1
    assert history_entry.comment == "testing update comment"
    assert history_entry.comment_versions[0]["comment"] == "testing"
    assert history_entry.edit_comment_date != None
    assert history_entry.comment_versions[0]["user"]["id"] == project.owner.id
Example #4
0
def send_notifications(obj, *, history):
    if history.is_hidden:
        return None

    key = make_key_from_model_object(obj)
    owner = get_user_model().objects.get(pk=history.user["pk"])
    notification, created = (
        HistoryChangeNotification.objects.select_for_update().get_or_create(
            key=key,
            owner=owner,
            project=obj.project,
            history_type=history.type))
    notification.updated_datetime = timezone.now()
    notification.save()
    notification.history_entries.add(history)

    # Get a complete list of notifiable users for current
    # object and send the change notification to them.
    notify_users = get_users_to_notify(obj,
                                       history=history,
                                       discard_users=[notification.owner])
    notification.notify_users.add(*notify_users)

    # If we are the min interval is 0 it just work in a synchronous and spamming way
    if settings.CHANGE_NOTIFICATIONS_MIN_INTERVAL == 0:
        send_sync_notifications(notification.id)

    live_notify_users = get_users_to_notify(obj,
                                            history=history,
                                            discard_users=[notification.owner],
                                            live=True)
    for user in live_notify_users:
        events.emit_live_notification_for_model(obj, user, history)
Example #5
0
    def _import_event(self, obj, event, options, cumulative_data):
        typename = get_typename_for_model_class(type(obj))
        key = make_key_from_model_object(obj)
        event_data = self._transform_event_data(obj, event, options, cumulative_data)
        if event_data is None:
            return

        change_old = event_data['change_old']
        change_new = event_data['change_new']
        user = event_data['user']

        diff = make_diff_from_dicts(change_old, change_new)
        fdiff = FrozenDiff(key, diff, {})
        values = make_diff_values(typename, fdiff)
        values.update(event_data['update_values'])
        entry = HistoryEntry.objects.create(
            user=user,
            project_id=obj.project.id,
            key=key,
            type=HistoryType.change,
            snapshot=None,
            diff=fdiff.diff,
            values=values,
            comment="",
            comment_html="",
            is_hidden=False,
            is_snapshot=False,
        )
        HistoryEntry.objects.filter(id=entry.id).update(created_at=event['created_at'])
        return HistoryEntry.objects.get(id=entry.id)
Example #6
0
def _store_history(project, obj, history, statuses={}):
    validator = validators.HistoryExportValidator(data=history, context={"project": project, "statuses": statuses})
    if validator.is_valid():
        validator.object.key = make_key_from_model_object(obj)
        if validator.object.diff is None:
            validator.object.diff = []
        validator.object.project_id = project.id
        validator.object._importing = True
        validator.save()
        return validator
    add_errors("history", validator.errors)
    return validator
def test_delete_comment_by_project_owner(client):
    project = f.create_project()
    us = f.create_userstory(project=project)
    f.MembershipFactory.create(project=project,
                               user=project.owner,
                               is_admin=True)
    key = make_key_from_model_object(us)
    history_entry = f.HistoryEntryFactory.create(type=HistoryType.change,
                                                 project=project,
                                                 comment="testing",
                                                 key=key,
                                                 diff={},
                                                 user={"pk": project.owner.id})

    client.login(project.owner)
    url = reverse("userstory-history-delete-comment", args=(us.id, ))
    url = "%s?id=%s" % (url, history_entry.id)
    response = client.post(url, content_type="application/json")
    assert 200 == response.status_code, response.status_code
def test_get_comment_versions_from_history_entry_without_comment(client):
    project = f.create_project()
    us = f.create_userstory(project=project)
    f.MembershipFactory.create(project=project,
                               user=project.owner,
                               is_admin=True)
    key = make_key_from_model_object(us)
    history_entry = f.HistoryEntryFactory.create(project=project,
                                                 type=HistoryType.change,
                                                 key=key,
                                                 diff={},
                                                 user={"pk": project.owner.id})

    client.login(project.owner)
    url = reverse("userstory-history-comment-versions", args=(us.id, ))
    url = "%s?id=%s" % (url, history_entry.id)

    response = client.get(url, content_type="application/json")
    assert 200 == response.status_code, response.status_code
    assert response.data == None
Example #9
0
    def _import_history(self, project, obj, history, options):
        key = make_key_from_model_object(obj)
        typename = get_typename_for_model_class(obj.__class__)
        history_data = self._transform_history_data(project, obj, history,
                                                    options)
        if history_data is None:
            return

        change_old = history_data['change_old']
        change_new = history_data['change_new']
        hist_type = history_data['hist_type']
        comment = history_data['comment']
        user = history_data['user']

        diff = make_diff_from_dicts(change_old, change_new)
        fdiff = FrozenDiff(key, diff, {})

        values = make_diff_values(typename, fdiff)
        values.update(history_data['update_values'])

        entry = HistoryEntry.objects.create(
            user=user,
            project_id=obj.project.id,
            key=key,
            type=hist_type,
            snapshot=None,
            diff=fdiff.diff,
            values=values,
            comment=comment,
            comment_html=mdrender(obj.project, comment),
            is_hidden=False,
            is_snapshot=False,
        )
        HistoryEntry.objects.filter(id=entry.id).update(
            created_at=history['created'])
        return HistoryEntry.objects.get(id=entry.id)