Ejemplo n.º 1
0
    def test_get_all_limit(self):
        msg = "message{0}"
        sender = "sender{0}"
        receiver = "receiver{0}"
        number_of_messages = 100

        # From 90 to 60 (both included)
        offset = 10
        count = 30

        for i in range(number_of_messages):
            m = RMessage(msg.format(i),
                         sender.format(i),
                         receiver.format(i))
            m.save()

        messages = RMessage.get_all(offset, count)

        self.assertEquals(count, len(messages))

        number_of_messages = number_of_messages - offset - 1  # Because we start in 0

        for i in messages:
            self.assertEquals(msg.format(number_of_messages), i.message)
            self.assertEquals(sender.format(number_of_messages), i.by)
            self.assertEquals(receiver.format(number_of_messages), i.to)

            number_of_messages = number_of_messages - 1
Ejemplo n.º 2
0
def show_messages(request):

    if request.method == "POST":
        form = MessageForm(data=request.POST)
        if form.is_valid():
            data = form.cleaned_data

            message = RMessage(data['message'], data['by'], data['to'])
            message.save()
            messages.success(request, "Your new message is posted")

            # Create again the form
            form = MessageForm()
    else:
        form = MessageForm()

    # Get messages information
    rmessages = RMessage.get_all(0, MESSAGES_PER_PAGE)

    context = {
        "form": form,
        "rmessages": rmessages
    }

    return render_to_response('messagewall/messages.html',
                              context,
                              context_instance=RequestContext(request))
Ejemplo n.º 3
0
    def test_get_count(self):
        number_of_messages = 100

        for i in range(number_of_messages):
            m = RMessage("test", "test", "test")
            m.save()

        self.assertEquals(number_of_messages, RMessage.count())
Ejemplo n.º 4
0
    def test_get_all_one(self):
        m = RMessage("message", "sender", "receiver")
        m.save()

        messages = RMessage.get_all()

        self.assertEquals(1, len(messages))

        self.assertEquals("message", messages[0].message)
        self.assertEquals("sender", messages[0].by)
        self.assertEquals("receiver", messages[0].to)
Ejemplo n.º 5
0
    def test_save(self):

        msg = RMessage("Microsoft is crap", "Richard Stallman", "Bill Gates")
        msg.save()

        r = utils.get_redis_connection()
        res = r.hgetall(RMessage.MESSAGE_KEY.format(msg.id))

        self.assertEquals(1, r.llen(RMessage.MESSAGES_KEY))
        self.assertEquals(msg.date, int(res['date']))
        self.assertEquals(msg.id, res['id'])
        self.assertEquals(msg.by, res['by'])
        self.assertEquals(msg.to, res['to'])
        self.assertEquals(msg.message, res['message'])
Ejemplo n.º 6
0
    def test_get_all(self):
        msg = "message{0}"
        sender = "sender{0}"
        receiver = "receiver{0}"
        number_of_messages = 100

        for i in range(number_of_messages):
            m = RMessage(msg.format(i),
                         sender.format(i),
                         receiver.format(i))
            m.save()

        messages = RMessage.get_all()

        for i in messages:
            number_of_messages = number_of_messages - 1
            self.assertEquals(msg.format(number_of_messages), i.message)
            self.assertEquals(sender.format(number_of_messages), i.by)
            self.assertEquals(receiver.format(number_of_messages), i.to)
Ejemplo n.º 7
0
def index(request):

    # Get messages information
    count = RMessage.count()

    context = {
        "number_of_messages": count
    }

    return render_to_response('messagewall/index.html',
                              context,
                              context_instance=RequestContext(request))
Ejemplo n.º 8
0
def ajax_show_messages(request):
    page = int(request.GET.get('page', 0))
    offset = page * MESSAGES_PER_PAGE
    count = MESSAGES_PER_PAGE

    rmessages = RMessage.get_all(offset, count)

    if len(rmessages) == 0:
        raise Http404

    return_data = []
    for i in rmessages:
        m = i.__dict__
        m['date'] = _date(unix_timestamp_to_datetime(
            m['date']),
            "M j, Y \a\t H:i:s"
        )
        return_data.append(m)

    return HttpResponse(json.dumps(return_data),
                        mimetype="application/json")