Commits

David Wolever committed 10c3c60

Can now dequeue individual URLs

  • Participants
  • Parent commits a847228

Comments (0)

Files changed (4)

File queue/models.py

 
     @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.. """
-        url = cls.objects_in_queue(queue_name)[:1]
-        if len(url) > 0:
-            url[0].dequeue()
-            return url[0]
-        return None
+        qrl = next_in_queue(queue_name)
+        qrl.dequeue()
+        return qrl
 
     def _dequeue_instancemethod(self):
         """ Dequeue this QueuedUrl. """
         self.save()
 
     @classmethod
+    def next_in_queue(cls, queue_name):
+        """ Get the next object in a queue, or None if the queue is empty. """
+        url = cls.objects_in_queue(queue_name)[:1]
+        if len(url) > 0:
+            return url[0]
+        return None
+
+    @classmethod
     def objects_in_queue(cls, queue_name):
         """ Get a QuerySet matching all the queued items the given queue,
             ordered such that the oldest item is first. """

File queue/templates/queue/queue_show.html

 <h1>Showing queue <tt>{{ queue_name }}</tt></h1>
 <ul>
-  {% for url_ in urls %}
-  <li><a href="{{ url_.url }}">{{ url_.title }}</a>
-      (enqueued {{ url_.enqueued|timesince }} ago)</li>
+  {% for qrl in qrls %}
+  <li>
+    <a href="{{ qrl.url }}">{{ qrl.title }}</a> -
+    <form method="POST" action="{% url queue-dequeue queue_name qrl.id %}"
+      style="display: inline;">
+      <input type="submit" value="dequeue" />
+    </form>
+    (enqueued {{ qrl.enqueued|timesince }} ago)
+  </li>
   {% endfor %}
 </ul>
 

File queue/urls.py

 
 matchers = {
     'queue_name': '[^/]+',
+    'qrl_id': '[a-zA-Z0-9-]+',
 }
 
 urlpatterns = patterns('urlqueue.queue.views',
         'queue_show', name='queue-show'),
     url(r'^(%(queue_name)s)/enqueue$' % matchers,
         'queue_enqueue', name='queue-enqueue'),
-    url(r'^(%(queue_name)s)/dequeue$' % matchers,
-        'queue_dequeue'),
+    url(r'^(%(queue_name)s)/dequeue(?:/(%(qrl_id)s))?$' % matchers,
+        'queue_dequeue', name='queue-dequeue'),
 )

File queue/views.py

-from django.shortcuts import render_to_response, redirect
-from django.http import HttpResponseNotAllowed
+from django.shortcuts import render_to_response, redirect, get_object_or_404
+from django.views.decorators.http import require_http_methods, \
+                                         require_GET, require_POST
 
 from functools import partial
 
 from models import QueuedUrl
 
-def assert_method(*expected_methods):
-    def assert_method_helper(decorated, request, *args, **kwargs):
-        if request.method not in expected_methods:
-            return HttpResponseNotAllowed('%s: Method not expected' %
-                                          request.method )
-        return decorated(request, *args, **kwargs)
-
-    def decorate(function):
-        return partial(assert_method_helper, function)
-    return decorate
-
-@assert_method("GET")
+@require_GET
 def queue_show(request, queue_name):
     data = {
-        'urls': QueuedUrl.objects_in_queue(queue_name),
+        'qrls': QueuedUrl.objects_in_queue(queue_name),
         'queue_name': queue_name,
     }
 
     return render_to_response('queue/queue_show.html', data)
 
-@assert_method("POST")
-def queue_dequeue(request, queue_name):
-    next_url = QueuedUrl.dequeue(queue_name)
+@require_POST
+def queue_dequeue(request, queue_name, qrl_id=None):
+    if qrl_id is not None:
+        qrl = get_object_or_404(QueuedUrl, pk=qrl_id)
+    else:
+        qrl = QueuedUrl.next_in_queue()
 
-    if next_url is None:
+    if qrl is None:
         data = { 'queue_name': queue_name }
         return render_to_response('queue/queue_empty.html', data)
 
-    return redirect(next_url.url)
+    qrl.dequeue()
+    return redirect(qrl.url)
 
-@assert_method("GET", "POST")
+@require_http_methods(["GET", "POST"])
 def queue_enqueue(request, queue_name):
     if request.method == "POST":
         (url, title) = (request.POST['url'], request.POST['title'])