Permalink
Browse files

Add support to be able to have favourite pages

  • Loading branch information...
cnorthwood committed Nov 23, 2010
1 parent f5a3247 commit 63553d2df3f50fb9afd6e6cf0b89324284c2124d
@@ -60,6 +60,7 @@
)
MIDDLEWARE_CLASSES = (
+ 'django.middleware.csrf.CsrfViewMiddleware',
'molly.wurfl.middleware.WurflMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
@@ -83,6 +84,7 @@
'molly.geolocation.context_processors.geolocation',
'molly.utils.context_processors.full_path',
'molly.utils.context_processors.google_analytics',
+ 'django.core.context_processors.csrf',
)
@@ -311,6 +313,10 @@
# Application('molly.apps.feeds.events', 'events', 'Events',
# ),
+
+ Application('molly.favourites', 'favourites', 'Favourite pages',
+ display_to_user = False,
+ ),
]
API_KEYS = {
@@ -58,6 +58,7 @@
</ol>
<div id="bc-title">
<h1 class="{% if not breadcrumbs.2 %}{{ breadcrumbs.0 }}-header with-icon{% endif %}">{{ breadcrumbs.4 }}</h1>
+ {% include "favourite.html" %}
</div>
</div>
{% else %}
@@ -71,6 +72,7 @@ <h1 class="{% if not breadcrumbs.2 %}{{ breadcrumbs.0 }}-header with-icon{% endi
<div id="bc-title">
<h1>{{ breadcrumbs.4 }}</h1>
</div>
+ {% include "favourite.html" %}
</div>
{% endifnotequal %}
@@ -31,6 +31,7 @@
(r'^desktop/', applications.desktop.urls),
(r'^url-shortener/', applications.url_shortener.urls),
(r'^feature-suggestions/', applications.feature_vote.urls),
+ (r'^favourites/', applications.favourites.urls),
(r'^comments/', include('django.contrib.comments.urls')),
No changes.
@@ -0,0 +1 @@
+# Placeholder file to ensure this app gets correctly picked up by Django
@@ -0,0 +1,13 @@
+{% if is_favouritable %}
+ <div id="favourite-bit">
+ <form action="{% url favourites:index %}" method="post">
+ <input type="hidden" name="URL" value="{{ favourite_url|escape }}" />
+ {% csrf_token %}
+ {% if is_favourite %}
+ <input type="submit" name="unfavourite" value="Unfavourite" />
+ {% else %}
+ <input type="submit" name="favourite" value="Favourite" />
+ {% endif %}
+ </form>
+ </div>
+{% endif %}
@@ -0,0 +1,33 @@
+{% extends "base.html" %}
+
+{% block content %}
+
+<div class="section">
+ <div class="header">
+ <h2>Favourites</h2>
+ </div>
+
+ <table class="content">
+ <tbody>
+ {% for favourite in favourites %}
+ <tr>
+ <td><a href="{{ favourite }}">{{ favourite }}</a></td>
+ <td>
+ <form action="{% url favourites:index %}" method="post">
+ <input type="hidden" name="URL" value="{{ favourite|escape }}" />
+ <input type="hidden" name="return_to_favourites" value="True" />
+ {% csrf_token %}
+ <input type="submit" name="unfavourite" value="Unfavourite" />
+ </form>
+ </td>
+ </tr>
+ {% empty %}
+ <tr>
+ <td colspan="2" style="text-align:center;"><em>You haven't favourited anything yet!</em></td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+</div>
+
+{% endblock %}
View
@@ -0,0 +1,9 @@
+from django.conf.urls.defaults import *
+
+from views import IndexView
+
+urlpatterns = patterns('',
+ (r'^$',
+ IndexView, {},
+ 'index'),
+ )
View
@@ -0,0 +1,101 @@
+from django.http import HttpResponseRedirect, Http404
+from django.core.urlresolvers import resolve
+
+from molly.utils.views import BaseView
+from molly.utils.breadcrumbs import lazy_reverse, Breadcrumb, BreadcrumbFactory
+
+class FavouritableView(BaseView):
+ """
+ A view to inherit from if you want to be favouritable
+ """
+
+ def initial_context(self, request, *args, **kwargs):
+
+ context = super(FavouritableView, self).initial_context(request, *args, **kwargs)
+
+ # Add whether or not this is favouritable to the context
+ context['is_favouritable'] = True
+
+ # Also, add whether or not this particular thing already is favourited
+ context['is_favourite'] = request.path_info in (request.session['favourites'] if 'favourites' in request.session else [])
+ print request.session['favourites']
+ print request.path_info
+ print context['is_favourite']
+
+ # And the URL of this page (so it can be favourited)
+ context['favourite_url'] = request.path_info
+
+ return context
+
+ def get_related(self, request, context, *args, **kwargs):
+ """
+ This allows a view to be queried for any related objects (such as an
+ entity it represents). This allows for things like favouriting places to
+ also get at the particular entity that is favourited.
+ """
+
+ # By default return an empty list
+ return []
+
+class IndexView(BaseView):
+ """
+ Allows for favourites management
+ """
+
+ @BreadcrumbFactory
+ def breadcrumb(self, request, context):
+ return Breadcrumb(
+ self.conf.local_name,
+ None,
+ 'Favourites',
+ lazy_reverse('index'),
+ )
+
+ def handle_GET(self, request, context):
+ """
+ Show a list of favourited things, and allow removal of these
+ """
+ context['favourites'] = request.session['favourites'] if 'favourites' in request.session else []
+ return self.render(request, context, 'favourites/index')
+
+ def handle_POST(self, request, context):
+ """
+ Add and remove favourites. Favourites are stored as URLs (the part of
+ them Django is interested in anyway) in the database. This has the
+ downside of breaking favourites if URLs change.
+ """
+
+ # Alter favourites list
+ if 'URL' in request.POST:
+
+ #if 'favourites' not in request.session:
+ request.session['favourites'] = set()
+
+ if 'favourite' in request.POST:
+ # Add
+ try:
+ resolve(request.POST['URL'])
+ request.session['favourites'].add(request.POST['URL'])
+ request.session.modified = True
+ except Http404:
+ # This means that they tried to save a URL that doesn't exist
+ # or isn't on our site
+ return HttpResponseRedirect(lazy_reverse('favourites:index'))
+
+ elif 'unfavourite' in request.POST and 'favourites' in request.session:
+ # Remove
+ if request.POST['URL'] in request.session['favourites']:
+ request.session['favourites'].remove(request.POST['URL'])
+ request.session.modified = True
+
+ # If the source was the favourites page, redirect back there
+ if 'return_to_favourites' in request.POST:
+ return self.handle_GET(request, context)
+
+ # else the source
+ else:
+ return HttpResponseRedirect(request.POST['URL'])
+
+ else:
+ # Missing POST data, probably a bad request
+ return HttpResponseRedirect(lazy_reverse('favourites:index'))

0 comments on commit 63553d2

Please sign in to comment.