Exemplo n.º 1
0
def emit_event_for_model(obj, *, type:str="change", channel:str="events",
                         content_type:str=None, sessionid:str=None):
    """
    Sends a model change event.
    """

    assert type in set(["create", "change", "delete"])
    assert hasattr(obj, "project_id")

    if not content_type:
        content_type = get_typename_for_model_instance(obj)

    projectid = getattr(obj, "project_id")
    pk = getattr(obj, "pk", None)

    app_name, model_name = content_type.split(".", 1)
    routing_key = "changes.project.{0}.{1}".format(projectid, app_name)

    data = {"type": type,
            "matches": content_type,
            "pk": pk}

    return emit_event(routing_key=routing_key,
                      channel=channel,
                      sessionid=sessionid,
                      data=data)
Exemplo n.º 2
0
def emit_event_for_model(obj,
                         *,
                         type: str = "change",
                         channel: str = "events",
                         content_type: str = None,
                         sessionid: str = None):
    """
    Sends a model change event.
    """

    if obj._importing:
        return None

    assert type in set(["create", "change", "delete"])
    assert hasattr(obj, "project_id")

    if not content_type:
        content_type = get_typename_for_model_instance(obj)

    projectid = getattr(obj, "project_id")
    pk = getattr(obj, "pk", None)

    app_name, model_name = content_type.split(".", 1)
    routing_key = "changes.project.{0}.{1}".format(projectid, app_name)

    data = {"type": type, "matches": content_type, "pk": pk}

    return emit_event(routing_key=routing_key,
                      channel=channel,
                      sessionid=sessionid,
                      data=data)
Exemplo n.º 3
0
def on_delete_any_model(sender, instance, **kwargs):
    # Ignore any object that can not have project_id
    content_type = get_typename_for_model_instance(instance)

    # Ignore any other changes
    if content_type not in events.watched_types:
        return

    sesionid = mw.get_current_session_id()
    events.emit_event_for_model(instance, sessionid=sesionid, type="delete")
Exemplo n.º 4
0
def _serialize(obj):
    content_type = get_typename_for_model_instance(obj)

    if content_type == "userstories.userstory":
        return UserStorySerializer(obj).data
    elif content_type == "issues.issue":
        return IssueSerializer(obj).data
    elif content_type == "tasks.task":
        return TaskSerializer(obj).data
    elif content_type == "wiki.wikipage":
        return WikiPageSerializer(obj).data
    elif content_type == "milestones.milestone":
        return MilestoneSerializer(obj).data
    elif content_type == "history.historyentry":
        return HistoryEntrySerializer(obj).data
Exemplo n.º 5
0
def _serialize(obj):
    content_type = get_typename_for_model_instance(obj)

    if content_type == "userstories.userstory":
        return UserStorySerializer(obj).data
    elif content_type == "issues.issue":
        return IssueSerializer(obj).data
    elif content_type == "tasks.task":
        return TaskSerializer(obj).data
    elif content_type == "wiki.wikipage":
        return WikiPageSerializer(obj).data
    elif content_type == "milestones.milestone":
        return MilestoneSerializer(obj).data
    elif content_type == "history.historyentry":
        return HistoryEntrySerializer(obj).data
Exemplo n.º 6
0
def on_save_any_model(sender, instance, created, **kwargs):
    # Ignore any object that can not have project_id
    if not hasattr(instance, "project_id"):
        return
    content_type = get_typename_for_model_instance(instance)

    # Ignore any other events
    app_config = apps.get_app_config('events')
    if content_type not in app_config.events_watched_types:
        return

    sesionid = mw.get_current_session_id()
    type = "create" if created else "change"

    events.emit_event_for_model(instance, sessionid=sesionid, type=type)
Exemplo n.º 7
0
def on_save_any_model(sender, instance, created, **kwargs):
    # Ignore any object that can not have project_id
    content_type = get_typename_for_model_instance(instance)

    # Ignore any other events
    if content_type not in events.watched_types:
        return

    sesionid = mw.get_current_session_id()

    type = "change"
    if created:
        type = "created"

    emit_event = lambda: events.emit_event_for_model(instance, sessionid=sesionid, type=type)
    connection.on_commit(emit_event)
Exemplo n.º 8
0
def on_save_any_model(sender, instance, created, **kwargs):
    # Ignore any object that can not have project_id
    content_type = get_typename_for_model_instance(instance)

    # Ignore any other events
    if content_type not in events.watched_types:
        return

    sesionid = mw.get_current_session_id()

    type = "change"
    if created:
        type = "create"

    emit_event = lambda: events.emit_event_for_model(instance, sessionid=sesionid, type=type)
    connection.on_commit(emit_event)
Exemplo n.º 9
0
def _get_type(obj):
    content_type = get_typename_for_model_instance(obj)
    return content_type.split(".")[1]
Exemplo n.º 10
0
def _get_type(obj):
    content_type = get_typename_for_model_instance(obj)
    return content_type.split(".")[1]
Exemplo n.º 11
0
def emit_live_notification_for_model(obj, user, history, *, type:str="change", channel:str="events",
                                     sessionid:str="not-existing"):
    """
    Sends a model live notification to users.
    """

    if obj._importing:
        return None

    content_type = get_typename_for_model_instance(obj)
    if content_type == "userstories.userstory":
        if history.type == HistoryType.create:
            title = _("User story created")
            url = resolve("userstory", obj.project.slug, obj.ref)
        elif history.type == HistoryType.change:
            title = _("User story changed")
            url = resolve("userstory", obj.project.slug, obj.ref)
        else:
            title = _("User story deleted")
            url = None
        body = _("US #{} - {}").format(obj.ref, obj.subject)
    elif content_type == "tasks.task":
        if history.type == HistoryType.create:
            title = _("Task created")
            url = resolve("task", obj.project.slug, obj.ref)
        elif history.type == HistoryType.change:
            title = _("Task changed")
            url = resolve("task", obj.project.slug, obj.ref)
        else:
            title = _("Task deleted")
            url = None
        body = _("Task #{} - {}").format(obj.ref, obj.subject)
    elif content_type == "issues.issue":
        if history.type == HistoryType.create:
            title = _("Issue created")
            url = resolve("issue", obj.project.slug, obj.ref)
        elif history.type == HistoryType.change:
            title = _("Issue changed")
            url = resolve("issue", obj.project.slug, obj.ref)
        else:
            title = _("Issue deleted")
            url = None
        body = _("Issue: #{} - {}").format(obj.ref, obj.subject)
    elif content_type == "wiki.wiki_page":
        if history.type == HistoryType.create:
            title = _("Wiki Page created")
            url = resolve("wiki", obj.project.slug, obj.slug)
        elif history.type == HistoryType.change:
            title = _("Wiki Page changed")
            url = resolve("wiki", obj.project.slug, obj.slug)
        else:
            title = _("Wiki Page deleted")
            url = None
        body = _("Wiki Page: {}").format(obj.slug)
    elif content_type == "milestones.milestone":
        if history.type == HistoryType.create:
            title = _("Sprint created")
            url = resolve("taskboard", obj.project.slug, obj.slug)
        elif history.type == HistoryType.change:
            title = _("Sprint changed")
            url = resolve("taskboard", obj.project.slug, obj.slug)
        else:
            title = _("Sprint deleted")
            url = None
        body = _("Sprint: {}").format(obj.name)
    else:
        return None

    return emit_event(
        {
            "title": title,
            "body": "Project: {}\n{}".format(obj.project.name, body),
            "url": url,
            "timeout": 10000,
            "id": history.id
        },
        "live_notifications.{}".format(user.id),
        sessionid=sessionid
    )
Exemplo n.º 12
0
def emit_live_notification_for_model(obj, user, history, *, type:str="change", channel:str="events",
                                     sessionid:str="not-existing"):
    """
    Sends a model live notification to users.
    """

    if obj._importing:
        return None

    content_type = get_typename_for_model_instance(obj)
    if content_type == "userstories.userstory":
        if history.type == HistoryType.create:
            title = _("User story created")
            url = resolve("userstory", obj.project.slug, obj.ref)
        elif history.type == HistoryType.change:
            title = _("User story changed")
            url = resolve("userstory", obj.project.slug, obj.ref)
        else:
            title = _("User story deleted")
            url = None
        body = _("US #{} - {}").format(obj.ref, obj.subject)
    elif content_type == "tasks.task":
        if history.type == HistoryType.create:
            title = _("Task created")
            url = resolve("task", obj.project.slug, obj.ref)
        elif history.type == HistoryType.change:
            title = _("Task changed")
            url = resolve("task", obj.project.slug, obj.ref)
        else:
            title = _("Task deleted")
            url = None
        body = _("Task #{} - {}").format(obj.ref, obj.subject)
    elif content_type == "issues.issue":
        if history.type == HistoryType.create:
            title = _("Issue created")
            url = resolve("issue", obj.project.slug, obj.ref)
        elif history.type == HistoryType.change:
            title = _("Issue changed")
            url = resolve("issue", obj.project.slug, obj.ref)
        else:
            title = _("Issue deleted")
            url = None
        body = _("Issue: #{} - {}").format(obj.ref, obj.subject)
    elif content_type == "wiki.wiki_page":
        if history.type == HistoryType.create:
            title = _("Wiki Page created")
            url = resolve("wiki", obj.project.slug, obj.slug)
        elif history.type == HistoryType.change:
            title = _("Wiki Page changed")
            url = resolve("wiki", obj.project.slug, obj.slug)
        else:
            title = _("Wiki Page deleted")
            url = None
        body = _("Wiki Page: {}").format(obj.slug)
    elif content_type == "milestones.milestone":
        if history.type == HistoryType.create:
            title = _("Sprint created")
            url = resolve("taskboard", obj.project.slug, obj.slug)
        elif history.type == HistoryType.change:
            title = _("Sprint changed")
            url = resolve("taskboard", obj.project.slug, obj.slug)
        else:
            title = _("Sprint deleted")
            url = None
        body = _("Sprint: {}").format(obj.name)
    else:
        return None

    return emit_event(
        {
            "title": title,
            "body": "Project: {}\n{}".format(obj.project.name, body),
            "url": url,
            "timeout": 10000,
            "id": history.id
        },
        "live_notifications.{}".format(user.id),
        sessionid=sessionid
    )