Commits

Luke Plant committed 0953a92

Implemented 'delete' function for 'list bookings' pages

Comments (0)

Files changed (3)

cciw/bookings/tests.py

 
         # Shelf section should disappear.
         self.assertNotContains(resp2, "<h2>Shelf</h2>")
+
+    def test_delete_place(self):
+        self.login()
+        self.create_place()
+        acc = BookingAccount.objects.get(email=self.email)
+        b = acc.bookings.all()[0]
+        resp = self.client.post(reverse('cciw.bookings.views.list_bookings'))
+
+        # Delete button should be there
+        self.assertContains(resp, "name=\"delete_%s\"" % b.id)
+
+        # Now click it
+        resp2 = self.client.post(reverse('cciw.bookings.views.list_bookings'), {'delete_%s' % b.id: '1'})
+
+        # Should be gone
+        self.assertEqual(0, acc.bookings.count())

cciw/bookings/views.py

             return HttpResponseRedirect(reverse('cciw.bookings.views.add_place'))
 
         bookings = request.booking_account.bookings
+        places = (bookings.ready_to_book(get_thisyear(), shelved=True) |
+                  bookings.ready_to_book(get_thisyear(), shelved=False))
+
+        def shelve(place):
+            place.shelved = True
+            place.save()
+
+        def unshelve(place):
+            place.shelved = False
+            place.save()
+
+        def delete(place):
+            place.delete()
+
         for k in request.POST.keys():
             # handle shelve and unshelve buttons
-            m = re.match(r"^(un)?shelve_(\d+)", k)
-            if m is not None:
-                try:
-                    b_id = int(m.groups()[1])
-                    to_shelve = m.groups()[0] == None
-                    place = bookings.ready_to_book(get_thisyear(),
-                                                   shelved=(not to_shelve)).get(id=b_id)
-                    place.shelved = to_shelve
-                    place.save()
-                except (ValueError, Booking.DoesNotExist):
-                    pass
+            for r, action in [(r'shelve_(\d+)', shelve),
+                              (r'unshelve_(\d+)', unshelve),
+                              (r'delete_(\d+)', delete)]:
+                m = re.match(r, k)
+                if m is not None:
+                    try:
+                        b_id = int(m.groups()[0])
+                        place = places.get(id=b_id)
+                        action(place)
+                    except (ValueError, Booking.DoesNotExist):
+                        pass
         return self.get(request, *args, **kwargs)
 
 

templates/cciw/bookings/place_details_row_inc.html

     <td rowspan="2" class="sectionbottom" style="text-align:center;">
       <a href="{% url 'cciw.bookings.views.edit_place' id=b.id %}">View/edit</a><br/>
       {% if basket %}
-        <input type="submit" name="shelve_{{ b.id }}" value="Save for later">
+        <input type="submit" name="shelve_{{ b.id }}" value="Save for later"><br/>
       {% endif %}
       {% if shelf %}
-        <input type="submit" name="unshelve_{{ b.id }}" value="Move to basket">
+        <input type="submit" name="unshelve_{{ b.id }}" value="Move to basket"><br/>
       {% endif %}
+        <input type="submit" name="delete_{{ b.id }}" value="Delete">
     </td>
   </tr>
   <tr class="sectionbottom">
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.