Mercurial > public > sg101
changeset 241:27bee3ac85e6
For #93: fix url scheme for downloads.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 15 Sep 2010 01:01:40 +0000 |
parents | 1246a4f1ab4f |
children | 7e8d2dda99e3 |
files | gpp/core/functions.py gpp/downloads/admin.py gpp/downloads/forms.py gpp/downloads/models.py gpp/downloads/templatetags/downloads_tags.py gpp/downloads/urls.py gpp/downloads/views.py gpp/news/views.py gpp/templates/downloads/add.html gpp/templates/downloads/download_detail.html gpp/templates/downloads/download_list.html gpp/templates/downloads/download_summary.html gpp/templates/downloads/index.html gpp/templates/downloads/markdown.html gpp/templates/downloads/navigation.html gpp/templates/downloads/search_results.html gpp/templates/downloads/thanks.html |
diffstat | 17 files changed, 41 insertions(+), 124 deletions(-) [+] |
line wrap: on
line diff
--- a/gpp/core/functions.py Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/core/functions.py Wed Sep 15 01:01:40 2010 +0000 @@ -80,3 +80,16 @@ ip = match.group(1) if match else None return ip + + +def get_page(qdict): + """Attempts to retrieve the value for "page" from the given query dict and + return it as an integer. If the key cannot be found or converted to an + integer, 1 is returned. + """ + n = qdict.get('page', 1) + try: + n = int(n) + except ValueError: + n = 1 + return n
--- a/gpp/downloads/admin.py Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/downloads/admin.py Wed Sep 15 01:01:40 2010 +0000 @@ -14,7 +14,8 @@ class CategoryAdmin(admin.ModelAdmin): - list_display = ('title', 'description', 'count') + list_display = ('title', 'slug', 'description', 'count') + prepopulated_fields = {'slug': ('title', )} readonly_fields = ('count', )
--- a/gpp/downloads/forms.py Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/downloads/forms.py Wed Sep 15 01:01:40 2010 +0000 @@ -10,14 +10,6 @@ from downloads.models import AllowedExtension -class SearchForm(forms.Form): - """Downloads search form.""" - text = forms.CharField(max_length=30) - - def query(self): - return self.cleaned_data['text'] - - class AddDownloadForm(forms.ModelForm): """Form to allow adding downloads.""" title = forms.CharField(required=True,
--- a/gpp/downloads/models.py Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/downloads/models.py Wed Sep 15 01:01:40 2010 +0000 @@ -14,6 +14,7 @@ class Category(models.Model): """Downloads belong to categories.""" title = models.CharField(max_length=64) + slug = models.SlugField(max_length=64) description = models.TextField(blank=True) count = models.IntegerField(default=0, blank=True)
--- a/gpp/downloads/templatetags/downloads_tags.py Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/downloads/templatetags/downloads_tags.py Wed Sep 15 01:01:40 2010 +0000 @@ -3,18 +3,11 @@ """ from django import template -from downloads.forms import SearchForm from downloads.models import Download register = template.Library() -@register.inclusion_tag('downloads/navigation.html', takes_context=True) -def downloads_navigation(context): - return { - 'search_form': SearchForm(), - 'MEDIA_URL': context['MEDIA_URL'], - } @register.inclusion_tag('downloads/latest_tag.html') def latest_downloads():
--- a/gpp/downloads/urls.py Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/downloads/urls.py Wed Sep 15 01:01:40 2010 +0000 @@ -6,7 +6,7 @@ urlpatterns = patterns('downloads.views', url(r'^$', 'index', name='downloads-index'), url(r'^add/$', 'add', name='downloads-add'), - url(r'^category/(?P<category>\d+)/(?P<sort>title|date|rating|hits)/page/(?P<page>\d+)/$', + url(r'^category/(?P<slug>[\w\d-]+)/(?P<sort>title|date|rating|hits)/$', 'category', name='downloads-category'), url(r'^details/(\d+)/$', 'details', name='downloads-details'), @@ -16,6 +16,5 @@ url(r'^random/$', 'random_download', name='downloads-random'), url(r'^rate/$', 'rate_download', name='downloads-rate'), url(r'^rating/$', 'rating', name='downloads-rating'), - url(r'^search/page/(?P<page>\d+)/$', 'search', name='downloads-search'), url(r'^thanks/$', 'thanks', name='downloads-add_thanks'), )
--- a/gpp/downloads/views.py Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/downloads/views.py Wed Sep 15 01:01:40 2010 +0000 @@ -18,15 +18,15 @@ from core.paginator import DiggPaginator from core.functions import email_admins +from core.functions import get_page from downloads.models import Category from downloads.models import Download from downloads.models import VoteRecord from downloads.forms import AddDownloadForm -from downloads.forms import SearchForm ####################################################################### -DLS_PER_PAGE = 10 +DLS_PER_PAGE = 1 def create_paginator(dls): return DiggPaginator(dls, DLS_PER_PAGE, body=5, tail=3, margin=3, padding=2) @@ -54,9 +54,9 @@ } @login_required -def category(request, category, sort='title', page='1'): +def category(request, slug, sort='title'): try: - cat = Category.objects.get(pk=category) + cat = Category.objects.get(slug=slug) except Category.DoesNotExist: raise Http404 @@ -67,8 +67,9 @@ downloads = Download.public_objects.filter(category=cat.pk).order_by( order_by) paginator = create_paginator(downloads) + page = get_page(request.GET) try: - the_page = paginator.page(int(page)) + the_page = paginator.page(page) except InvalidPage: raise Http404 @@ -187,41 +188,6 @@ ####################################################################### -@login_required -def search(request, page=1): - if request.method == 'POST': - form = SearchForm(request.POST) - if form.is_valid(): - query_text = form.query() - page = 1 - else: - return HttpResponseRedirect(reverse('downloads-index')) - else: - if 'query' in request.GET: - query_text = request.GET['query'] - else: - return HttpResponseRedirect(reverse('downloads-index')) - - dls = Download.objects.filter( - Q(title__icontains = query_text) | - Q(description__icontains = query_text)).order_by( - 'title').select_related() - paginator = create_paginator(dls) - try: - the_page = paginator.page(int(page)) - except EmptyPage: - dls = Download.objects.none() - except InvalidPage: - raise Http404 - - return render_to_response('downloads/search_results.html', { - 'query': query_text, - 'page': the_page, - }, - context_instance = RequestContext(request)) - -####################################################################### - @require_POST def rate_download(request): """This function is called by AJAX to rate a download.""" @@ -245,7 +211,8 @@ return HttpResponseBadRequest('Invalid download id.') # prevent multiple votes from the same user - vote_record, created = VoteRecord.objects.get_or_create(download=download, user=request.user) + vote_record, created = VoteRecord.objects.get_or_create( + download=download, user=request.user) if created: new_score = download.vote(rating) download.save()
--- a/gpp/news/views.py Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/news/views.py Wed Sep 15 01:01:40 2010 +0000 @@ -22,6 +22,7 @@ from core.html import clean_html from core.functions import send_mail from core.functions import get_full_name +from core.functions import get_page from core.paginator import DiggPaginator from news.models import Category from news.models import PendingStory @@ -38,25 +39,11 @@ ####################################################################### -def _get_page(qdict): - """Attempts to retrieve the value for "page" from the given query dict and - return it as an integer. If the key cannot be found or converted to an - integer, 1 is returned. - """ - n = qdict.get('page', 1) - try: - n = int(n) - except ValueError: - n = 1 - return n - -####################################################################### - def index(request): stories = Story.objects.all().select_related() paginator = create_paginator(stories) - page = _get_page(request.GET) + page = get_page(request.GET) try: the_page = paginator.page(page) except InvalidPage: @@ -83,7 +70,7 @@ def archive(request, year, month): stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month) paginator = create_paginator(stories) - page = _get_page(request.GET) + page = get_page(request.GET) try: the_page = paginator.page(page) except InvalidPage: @@ -116,7 +103,7 @@ category = get_object_or_404(Category, slug=slug) stories = Story.objects.filter(category=category) paginator = create_paginator(stories) - page = _get_page(request.GET) + page = get_page(request.GET) try: the_page = paginator.page(page) except InvalidPage: @@ -181,7 +168,7 @@ tag = get_object_or_404(Tag, name=tag_name) stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag) paginator = create_paginator(stories) - page = _get_page(request.GET) + page = get_page(request.GET) try: the_page = paginator.page(page) except InvalidPage:
--- a/gpp/templates/downloads/add.html Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/templates/downloads/add.html Wed Sep 15 01:01:40 2010 +0000 @@ -8,7 +8,7 @@ {% endblock %} {% block content %} <h2>Downloads</h2> -{% downloads_navigation %} +{% include 'downloads/navigation.html' %} <h3>Add Download</h3> <p>Use the following form to upload a file. Please note the following:</p>
--- a/gpp/templates/downloads/download_detail.html Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/templates/downloads/download_detail.html Wed Sep 15 01:01:40 2010 +0000 @@ -14,7 +14,7 @@ {% endblock %} {% block content %} <h2>Downloads</h2> -{% downloads_navigation %} +{% include 'downloads/navigation.html' %} <h3>Download Details For {{ download.title }}</h3> <dl>
--- a/gpp/templates/downloads/download_list.html Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/templates/downloads/download_list.html Wed Sep 15 01:01:40 2010 +0000 @@ -11,18 +11,18 @@ {% endblock %} {% block content %} <h2>Downloads</h2> -{% downloads_navigation %} +{% include 'downloads/navigation.html' %} <h3>Category: {{ category.title }}</h3> {% if page.object_list %} <ul class="tab-nav"> - <li><a href="{% url downloads-category category=category.id sort="title" page="1" %}" + <li><a href="{% url downloads-category slug=category.slug sort="title" %}" {% ifequal s "title" %}class="active" {% endifequal %}>Title</a></li> - <li><a href="{% url downloads-category category=category.id sort="date" page="1" %}" + <li><a href="{% url downloads-category slug=category.slug sort="date" %}" {% ifequal s "date" %}class="active"{% endifequal %}>Date</a></li> - <li><a href="{% url downloads-category category=category.id sort="rating" page="1" %}" + <li><a href="{% url downloads-category slug=category.slug sort="rating" %}" {% ifequal s "rating" %}class="active"{% endifequal %}>Rating</a></li> - <li><a href="{% url downloads-category category=category.id sort="hits" page="1" %}" + <li><a href="{% url downloads-category slug=category.slug sort="hits" %}" {% ifequal s "hits" %}class="active"{% endifequal %}>Hits</a></li> </ul>
--- a/gpp/templates/downloads/download_summary.html Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/templates/downloads/download_summary.html Wed Sep 15 01:01:40 2010 +0000 @@ -9,7 +9,7 @@ {% endblock %} {% block content %} <h2>Downloads</h2> -{% downloads_navigation %} +{% include 'downloads/navigation.html' %} <h3>{{ title }}</h3> {% if downloads %}
--- a/gpp/templates/downloads/index.html Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/templates/downloads/index.html Wed Sep 15 01:01:40 2010 +0000 @@ -6,14 +6,14 @@ {% endblock %} {% block content %} <h2>Downloads</h2> -{% downloads_navigation %} +{% include 'downloads/navigation.html' %} <h3>Categories</h3> {% if categories %} <p>We have {{ total_dls }} download{{ total_dls|pluralize }} in {{ categories.count }} categories.</p> <dl> {% for category in categories %} <dt> -<a href="{% url downloads-category category=category.pk sort="title" page=1 %}">{{ category.title }}</a> +<a href="{% url downloads-category slug=category.slug sort="title" %}">{{ category.title }}</a> ({{ category.count }}) </dt> <dd><p>{{ category.description }}</p></dd>
--- a/gpp/templates/downloads/markdown.html Wed Sep 15 00:14:54 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +0,0 @@ -{% load markup %} -{% load smiley_tags %} -{{ data|markdown:"safe"|smilify }}
--- a/gpp/templates/downloads/navigation.html Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/templates/downloads/navigation.html Wed Sep 15 01:01:40 2010 +0000 @@ -1,9 +1,6 @@ <div class="app-logo"> <img src="{{ MEDIA_URL }}icons/downloads-logo.jpg" alt="Downloads Logo" title="Downloads" /> </div> -<form id="downloads-search" action="{% url downloads-search page=1 %}" method="post">{% csrf_token %} - <p>{{ search_form.text }} <input type="submit" value="Search" /></p> -</form> <ul class="app-menu"> <li><a href="{% url downloads-index %}">Categories</a></li> <li><a href="{% url downloads-new %}">New</a></li>
--- a/gpp/templates/downloads/search_results.html Wed Sep 15 00:14:54 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -{% extends 'base.html' %} -{% load downloads_tags %} -{% block title %}Downloads: Search Results{% endblock %} -{% block custom_css %} -<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/downloads.css" /> -<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/tab-nav.css" /> -<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/pagination.css" /> -{% endblock %} -{% block custom_js %} -<script type="text/javascript" src="{{ MEDIA_URL }}js/downloads/rating.js"></script> -{% endblock %} -{% block content %} -<h2>Downloads</h2> -{% downloads_navigation %} -<h3>Search Results: {{ query }} - {{ page.paginator.count }} result{{ page.paginator.count|pluralize }}</h3> - -{% if page.object_list %} -{% include 'core/pagination_query.html' %} - -<dl> -{% for download in page.object_list %} - {% include 'downloads/download.html' %} -{% endfor %} -</dl> - -{% include 'core/pagination_query.html' %} -{% else %} -<p>No results found for "{{ query }}".</p> -{% endif %} -{% endblock %}
--- a/gpp/templates/downloads/thanks.html Wed Sep 15 00:14:54 2010 +0000 +++ b/gpp/templates/downloads/thanks.html Wed Sep 15 01:01:40 2010 +0000 @@ -7,7 +7,7 @@ {% endblock %} {% block content %} <h2>Downloads</h2> -{% downloads_navigation %} +{% include 'downloads/navigation.html' %} <h3>Thanks for the Download</h3> <p>Thank you for sending in a download! Your file will be reviewed by the site staff and made available shortly.</p>