Exemplo n.º 1
0
    def test_is_reply(self):
        from djangosms.core.models import Connection
        conn1 = Connection(uri="test://1")
        conn1.save()
        conn2 = Connection(uri="test://2")
        conn2.save()

        from djangosms.core.models import Incoming
        from djangosms.core.models import Outgoing
        from djangosms.core.models import Request

        message = Incoming(connection=conn1)
        message.save()

        request = Request(message=message)
        request.save()

        response = Outgoing(connection=conn1, in_response_to=request)
        response.save()
        self.assertTrue(response.is_reply())

        alert = Outgoing(connection=conn2, in_response_to=request)
        alert.save()
        self.assertFalse(alert.is_reply())

        unsolicited = Outgoing(in_response_to=None)
        unsolicited.save()
        self.assertFalse(unsolicited.is_reply())
Exemplo n.º 2
0
def index(req):
    columns = (
        ("id", "#", "id", None),
        ("name", "Name", "name", None),
        ("group", "Location", "group__name", None),
        ("role", "Role", "roles__name", None),
        ("activity", "Last activity", "activity", Max("connections__messages__time")),
        (None, "Messages", None, None),
        (None, "Erroneous", None, None),
        )

    sort_column, sort_descending = _get_sort_info(
        req, default_sort_column="id", default_sort_descending=True)

    search_string = req.REQUEST.get("q", "")

    if search_string == "":
        query = Reporter.objects
    else:
        query = query_reporters(search_string)

    query = query.filter(active=True)

    form = SandboxForm(req.POST)
    if req.method == 'POST' and form.is_valid():
        pks = req.POST.getlist('reporter')
        text = form.cleaned_data.get('text') or None

        if not req.POST.get('all'):
            query = Reporter.objects.filter(pk__in=pks)

        graduated = []

        if text:
            request = Request(text=text)
            request.save()
        else:
            request = None

        for reporter in query.all():
            try:
                graduate.send(sender=req.user, reporter=reporter)
            except GraduateFailed:
                continue

            reporter.active = False
            reporter.save()

            if request is not None:
                uri = reporter.most_recent_connection.uri
                message = Outgoing(text=text, uri=uri, in_response_to=request)
                message.save()

            graduated.append(reporter)

        if graduated:
            names = [title(reporter.name) for reporter in graduated]
            separator = [", "] * len(names)
            if len(names) > 1:
                separator[-2] = " and "
            separator[-1] = ""

            if request is not None:
                notification_sent_message = " Notification sent: \"%s\"." % text
            else:
                notification_sent_message = ""

            req.notifications.add(u"%d reporter(s) graduated: %s.%s" % (
                len(graduated),
                "".join(chain(*zip(names, separator))),
                notification_sent_message,
                ))

        # redirect to GET action
        return HttpResponseRedirect(req.path)

    for name, label, sorting, aggregate in columns:
        if aggregate:
            query = query.annotate(**{name: aggregate})

        if name == sort_column:
            sort_desc_string = "-" if sort_descending else ""
            query = query.order_by("%s%s" % (sort_desc_string, sorting))

    try:
        page = int(req.GET.get('page', '1'))
    except ValueError:
        page = 1

    paginator = Paginator(query, 25).page(page)

    entries = []
    for reporter in paginator.object_list:
        messages = Incoming.objects.filter(
            connection__user__pk=reporter.pk)
        incoming = messages.count()
        erroneous = Request.objects.filter(
            erroneous=True, message__in=messages).count()
        entries.append((reporter, incoming, erroneous))

    return render_to_response("sandbox/index.html", {
        "paginator": paginator,
        "entries": entries,
        "columns": columns,
        "count": query.count(),
        "sort_column": sort_column,
        "sort_descending": sort_descending,
        "search_string": search_string,
        "req": req,
        }, RequestContext(req))
Exemplo n.º 3
0
def index(req):
    columns = (
        ("id", "#", "id", None),
        ("name", "Name", "name", None),
        ("group", "Location", "group__name", None),
        ("role", "Role", "roles__name", None),
        ("activity", "Last activity", "activity", Max("connections__messages__time")),
        (None, "Message", None, None),
        )

    sort_column, sort_descending = _get_sort_info(
        req, default_sort_column="id", default_sort_descending=True)

    search_string = req.REQUEST.get("q", "")

    if search_string == "":
        query = Reporter.objects
    else:
        query = query_reporters(search_string)

    query = query.filter(active=True)

    form = SendForm(req.POST)
    if req.method == 'POST' and form.is_valid():
        text = form.cleaned_data.get('text') or None
        reporters = req.POST.getlist('reporter')

        if text is None:
            req.notifications.add(u"No text was submitted; ignored.")
        else:
            if not req.POST.get('all'):
                query = Reporter.objects.filter(pk__in=reporters)

            request = Request(text=text)
            request.save()

            for reporter in query.all():
                uri = reporter.most_recent_connection.uri
                message = Outgoing(text=text, uri=uri, in_response_to=request)
                message.save()

            req.notifications.add(
                u"Message sent to %d recipient(s)." % query.count())

        # redirect to GET action
        return HttpResponseRedirect(req.path)

    for name, title, sorting, aggregate in columns:
        if aggregate:
            query = query.annotate(**{name: aggregate})

        if name == sort_column:
            sort_desc_string = "-" if sort_descending else ""
            query = query.order_by("%s%s" % (sort_desc_string, sorting)).all()

    entries = []

    try:
        page = int(req.GET.get('page', '1'))
    except ValueError:
        page = 1

    paginator = Paginator(query, 25)
    count = paginator.count
    paginator = paginator.page(page)

    for reporter in paginator.object_list:
        try:
            message = Message.objects.filter(
                connection__in=reporter.connections.all()).latest()

        except Message.DoesNotExist:
            message = None
            is_outgoing_message = 0

        else:
            is_outgoing_message = Outgoing.objects.filter(
                    pk=message.pk).count()

        entries.append((reporter, message, is_outgoing_message))

    return render_to_response("reporters/index.html", {
        "entries": entries,
        "paginator": paginator,
        "columns": columns,
        "sort_column": sort_column,
        "sort_descending": sort_descending,
        "search_string": search_string,
        "req": req,
        "count" : count
        }, RequestContext(req))