Ejemplo n.º 1
0
    def create(self, request):
        code = request.data.get('code', '')
        if (length_of_code(code) > 140):
            raise ValidationError("Code longer than 140 characters")

        remix_of_pk = request.data.get('remix_of', -1)
        # Using filter().first() will return None if it doesn't exist
        # instead of raising a DoesNotExist exception
        remix_of = self.queryset.filter(pk=remix_of_pk).first()

        d = Dweet(code=code,
                  author=request.user,
                  reply_to=remix_of,
                  posted=timezone.now())
        d.save()
        d.likes.add(d.author)
        d.save()

        first_comment = request.data.get('first-comment', '')
        if first_comment:
            c = Comment(text=first_comment,
                        posted=timezone.now(),
                        author=request.user,
                        reply_to=d)
            c.save()

        return self.retrieve(request, d.id)
Ejemplo n.º 2
0
def dweet_reply(request, dweet_id):
    reply_to = get_object_or_404(Dweet, id=dweet_id)
    d = Dweet(code=request.POST['code'],
              reply_to=reply_to,
              author=request.user,
              posted=timezone.now())
    d.save()
    return HttpResponseRedirect(reverse('root'))
Ejemplo n.º 3
0
def dweet_reply(request, dweet_id):
    reply_to = get_object_or_404(Dweet, id=dweet_id)
    d = Dweet(code=request.POST['code'],
              reply_to=reply_to,
              author=request.user,
              posted=timezone.now())
    d.save()
    d.likes.add(d.author)
    d.hotness = hot(1, d.posted)
    d.save()

    new_dweet_message(request, d.id)

    return HttpResponseRedirect(
        reverse('dweet_show', kwargs={'dweet_id': d.id}))
Ejemplo n.º 4
0
def dweet_reply(request, dweet_id):
    reply_to = get_object_or_404(Dweet, id=dweet_id)
    d = Dweet(code=request.POST['code'],
              reply_to=reply_to,
              author=request.user,
              posted=timezone.now())
    d.save()
    d.likes.add(d.author)
    d.hotness = hot(1, d.posted)
    d.save()

    new_dweet_message(request, d.id)

    return HttpResponseRedirect(reverse('dweet_show',
                                kwargs={'dweet_id': d.id}))
Ejemplo n.º 5
0
def dweet(request):
    code = request.POST['code']

    if (len(code.replace('\r\n', '\n')) > 140):
        return HttpResponseBadRequest("Dweet code too long! Code: " + code)

    d = Dweet(code=code, author=request.user, posted=timezone.now())
    d.save()
    d.likes.add(d.author)
    d.hotness = hot(1, d.posted)
    d.save()

    new_dweet_message(request, d.id)

    return HttpResponseRedirect(
        reverse('dweet_show', kwargs={'dweet_id': d.id}))
Ejemplo n.º 6
0
def dweet(request):
    code = request.POST['code']

    if(len(code.replace('\r\n', '\n')) > 140):
        return HttpResponseBadRequest("Dweet code too long! Code: " + code)

    d = Dweet(code=code,
              author=request.user,
              posted=timezone.now())
    d.save()
    d.likes.add(d.author)
    d.hotness = hot(1, d.posted)
    d.save()

    new_dweet_message(request, d.id)

    return HttpResponseRedirect(reverse('dweet_show',
                                kwargs={'dweet_id': d.id}))
Ejemplo n.º 7
0
def dweet(request):
  d = Dweet(code = request.POST['code']
      , author = request.user 
      , posted = timezone.now() )
  d.save()
  return HttpResponseRedirect(reverse('root'))
Ejemplo n.º 8
0
class HashtagTestCase(TestCase):
    def setUp(self):
        user1 = User.objects.create(username="******", password="")
        self.user2 = User.objects.create(username="******", password="")

        now = timezone.now()

        self.dweet1 = Dweet.objects.create(id=1,
                                           code="dweet1 code",
                                           posted=now - timedelta(minutes=1),
                                           author=user1)

        self.dweet2 = Dweet.objects.create(id=2,
                                           code="dweet2 code",
                                           posted=now,
                                           reply_to=self.dweet1,
                                           author=self.user2)

        Comment.objects.create(
            id=1,
            text="comment1 text with #hashtag #hash2 #2hash",
            posted=now - timedelta(minutes=1),
            reply_to=self.dweet2,
            author=user1)

        Comment.objects.create(id=2,
                               text="comment2 text #hashtag #1hash #hash1",
                               posted=now,
                               reply_to=self.dweet1,
                               author=self.user2)

    def test_hashtags_created(self):
        h = Hashtag.objects.get(name='hashtag')

        h1 = Hashtag.objects.get(name='hash1')
        h2 = Hashtag.objects.get(name='hash2')

        with self.assertRaises(Hashtag.DoesNotExist):
            illegal1 = Hashtag.objects.get(name='1hash')
            self.assertEqual(illegal1, True)

        with self.assertRaises(Hashtag.DoesNotExist):
            illegal2 = Hashtag.objects.get(name='2hash')
            self.assertEqual(illegal2, True)

        self.assertEqual(h is None, False)
        self.assertEqual(h1 is None, False)
        self.assertEqual(h2 is None, False)
        self.assertEqual(h.dweets.count(), 2)
        self.assertEqual(h1.dweets.all()[0], self.dweet1)
        self.assertEqual(h2.dweets.all()[0], self.dweet2)

    def test_same_hashtag(self):
        self.dweet3 = Dweet(id=3,
                            code="dweet3 code",
                            posted=timezone.now(),
                            reply_to=None,
                            author=self.user2)
        self.dweet3.save()

        c = Comment(id=3,
                    text="comment3 text #hashtag #h_3 #_3",
                    posted=timezone.now(),
                    reply_to=self.dweet3,
                    author=self.user2)
        c.save()

        h = Hashtag.objects.get(name='hashtag')
        h3 = Hashtag.objects.get(name='h_3')
        h_3 = Hashtag.objects.get(name='_3')
        self.assertEqual(h is None, False)
        self.assertEqual(h3 is None, False)
        self.assertEqual(h_3 is None, False)
        self.assertEqual(h.dweets.count(), 3)
        self.assertEqual(h.dweets.get(id=3), self.dweet3)
        self.assertEqual(h3.dweets.all()[0], self.dweet3)
        self.assertEqual(h_3.dweets.all()[0], self.dweet3)

    def test_no_double_adding(self):
        # add #hashtag and #1hash to dweet1 again and see that the size
        # of those hashtags doesn't change
        Comment.objects.create(id=4,
                               text="comment2 text #hashtag #hash1",
                               posted=timezone.now(),
                               reply_to=self.dweet1,
                               author=self.user2)
        h = Hashtag.objects.get(name='hashtag')
        h1 = Hashtag.objects.get(name='hash1')
        self.assertEqual(h.dweets.count(), 2)
        self.assertEqual(h1.dweets.count(), 1)