# HG changeset patch # User Brian Neal # Date 1284512500 0 # Node ID 27bee3ac85e6f7601290aba346a8037fc7790ae6 # Parent 1246a4f1ab4f016d56452f43eda68689ad0a7900 For #93: fix url scheme for downloads. diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/core/functions.py --- 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 diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/downloads/admin.py --- 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', ) diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/downloads/forms.py --- 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, diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/downloads/models.py --- 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) diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/downloads/templatetags/downloads_tags.py --- 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(): diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/downloads/urls.py --- 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\d+)/(?Ptitle|date|rating|hits)/page/(?P\d+)/$', + url(r'^category/(?P[\w\d-]+)/(?Ptitle|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\d+)/$', 'search', name='downloads-search'), url(r'^thanks/$', 'thanks', name='downloads-add_thanks'), ) diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/downloads/views.py --- 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() diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/news/views.py --- 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: diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/add.html --- 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 %}

Downloads

-{% downloads_navigation %} +{% include 'downloads/navigation.html' %}

Add Download

Use the following form to upload a file. Please note the following:

diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/download_detail.html --- 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 %}

Downloads

-{% downloads_navigation %} +{% include 'downloads/navigation.html' %}

Download Details For {{ download.title }}

diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/download_list.html --- 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 %}

Downloads

-{% downloads_navigation %} +{% include 'downloads/navigation.html' %}

Category: {{ category.title }}

{% if page.object_list %} diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/download_summary.html --- 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 %}

Downloads

-{% downloads_navigation %} +{% include 'downloads/navigation.html' %}

{{ title }}

{% if downloads %} diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/index.html --- 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 %}

Downloads

-{% downloads_navigation %} +{% include 'downloads/navigation.html' %}

Categories

{% if categories %}

We have {{ total_dls }} download{{ total_dls|pluralize }} in {{ categories.count }} categories.

{% for category in categories %}
-{{ category.title }} +{{ category.title }} ({{ category.count }})

{{ category.description }}

diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/markdown.html --- 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 }} diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/navigation.html --- 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 @@ -
  • Categories
  • New
  • diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/search_results.html --- 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 %} - - - -{% endblock %} -{% block custom_js %} - -{% endblock %} -{% block content %} -

    Downloads

    -{% downloads_navigation %} -

    Search Results: {{ query }} - {{ page.paginator.count }} result{{ page.paginator.count|pluralize }}

    - -{% if page.object_list %} -{% include 'core/pagination_query.html' %} - -
    -{% for download in page.object_list %} - {% include 'downloads/download.html' %} -{% endfor %} -
    - -{% include 'core/pagination_query.html' %} -{% else %} -

    No results found for "{{ query }}".

    -{% endif %} -{% endblock %} diff -r 1246a4f1ab4f -r 27bee3ac85e6 gpp/templates/downloads/thanks.html --- 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 %}

    Downloads

    -{% downloads_navigation %} +{% include 'downloads/navigation.html' %}

    Thanks for the Download

    Thank you for sending in a download! Your file will be reviewed by the site staff and made available shortly.