Commits

David Wolever committed ae20d17

Removing dequeue method, throwing an error on bad URLs

  • Participants
  • Parent commits 10c3c60

Comments (0)

Files changed (3)

File queue/models.py

-from django.db.models import Model, CharField, DateTimeField
+from django.db.models import Model, CharField, DateTimeField, URLField
 
 from datetime import datetime
+import re
 
 from urlqueue.libs.django_extensions.db.fields import UUIDField
 
     id = UUIDField(primary_key=True)
 
     queue_name = CharField(max_length=QUEUE_NAME_MAX_LENGTH)
-    url = CharField(max_length=URL_MAX_LENGTH)
+    url = URLField(max_length=URL_MAX_LENGTH)
     title = CharField(max_length=TITLE_MAX_LENGTH)
 
     enqueued = DateTimeField(default=datetime.now)
     class Meta:
         unique_together = (('queue_name', 'url'), )
 
-    def __init__(self, *args, **kwargs):
-        super(QueuedUrl, self).__init__(*args, **kwargs)
-        self.dequeue = self._dequeue_instancemethod
+    class InvalidUrl(Exception):
+        pass
+
+    def save(self, *args, **kwargs):
+        # I think this will match
+        if not re.match('[a-zA-Z]+://.+', self.url):
+            raise self.InvalidUrl('The url %r is not valid.' %(self.url))
+        
+        super(QueuedUrl, self).save(*args, **kwargs)
 
     @classmethod
     def enqueue(cls, queue_name, url, title):
         qrl.save()
         return qrl
 
-    @classmethod
-    def dequeue(cls, queue_name):
-        # XXX: I don't think this is useful any more
-        """ Dequeue the next URL in the given queue and mark it dequeued.
-            If the queue is empty, None is returned.. """
-        qrl = next_in_queue(queue_name)
-        qrl.dequeue()
-        return qrl
-
-    def _dequeue_instancemethod(self):
+    def dequeue(self):
         """ Dequeue this QueuedUrl. """
         self.dequeued = datetime.now()
         self.save()

File queue/tests.py

         assert_equal(urls[0].title, "first")
         assert_equal(urls[1].title, "second")
 
-    def test_dequeue_and_dequeue(self):
+    def test_dequeue(self):
         setup_default_data()
 
-        url = QueuedUrl.dequeue("default")
+        url = QueuedUrl.next_in_queue("default")
+        assert_equal(url.title, "first")
+        url.dequeue()
         assert_not_equal(url.dequeued, None)
-        assert_equal(url.title, "first")
 
-        url = QueuedUrl.dequeue("default")
+        url = QueuedUrl.next_in_queue("default")
+        assert_equal(url.title, "second")
+        url.dequeue()
         assert_not_equal(url.dequeued, None)
-        assert_equal(url.title, "second")
 
-    def test_in_queue_empty_queue(self):
-        url = QueuedUrl.dequeue("empty_queue")
+    def test_next_in_queue_empty_queue(self):
+        url = QueuedUrl.next_in_queue("empty_queue")
         assert_equal(url, None)
 
     def test_queue_name_and_url_unique(self):
         
         assert_equal(list(QueuedUrl.queue_names()), [u'a', u'b', u'c'])
 
+    def test_bad_urls(self):
+        bad_urls = [
+            '/foo/bar',
+            'baz',
+        ]
+
+        for bad_url in bad_urls:
+            assert_raises(QueuedUrl.InvalidUrl, new_queued_url, url=bad_url)
+
 class TestViews(TestCase):
     
     def test_dequeue_with_get(self):
         assert_enqueue(data)
 
         # And now check that the QRL was actually saved
-        qrl = QueuedUrl.dequeue("some_queue")
+        qrl = QueuedUrl.next_in_queue("some_queue")
         assert_not_equal(qrl, None)
         assert_equal(qrl.url, data['url'])
         assert_equal(qrl.title, data['title'])
-        assert qrl.dequeued
+
+        # And dequeue it for the next part of the test
+        qrl.dequeue()
 
         # Now post the same URL, but with a differnt title, and make
         # sure the title has been updated and it is queued again

File queue/views.py

     if qrl_id is not None:
         qrl = get_object_or_404(QueuedUrl, pk=qrl_id)
     else:
-        qrl = QueuedUrl.next_in_queue()
+        qrl = QueuedUrl.next_in_queue(queue_name)
 
     if qrl is None:
         data = { 'queue_name': queue_name }