Ticket #802: 802-sitemap-generation.patch

File 802-sitemap-generation.patch, 3.2 KB (added by digital-dreamer, 11 years ago)
  • mediagoblin/routing.py

    diff --git a/mediagoblin/routing.py b/mediagoblin/routing.py
    index 1393f01..93222c5 100644
    a b def get_url_map():  
    2929    add_route('index', '/', 'mediagoblin.views:root_view')
    3030    add_route('terms_of_service','/terms_of_service',
    3131        'mediagoblin.views:terms_of_service')
     32    add_route('sitemap','/sitemap.xml','mediagoblin.views:sitemap')
     33    add_route('robots','/robots.txt','mediagoblin.views:robots')
    3234    mount('/auth', auth_routes)
    3335    mount('/mod', moderation_routes)
    3436
  • new file mediagoblin/templates/mediagoblin/robots.txt

    diff --git a/mediagoblin/templates/mediagoblin/robots.txt b/mediagoblin/templates/mediagoblin/robots.txt
    new file mode 100644
    index 0000000..be86b19
    - +  
     1Sitemap: {{ request.host_url }}sitemap.xml
  • new file mediagoblin/templates/mediagoblin/sitemap.xml

    diff --git a/mediagoblin/templates/mediagoblin/sitemap.xml b/mediagoblin/templates/mediagoblin/sitemap.xml
    new file mode 100644
    index 0000000..877856e
    - +  
     1<?xml version="1.0" encoding="utf-8"?>
     2 
     3<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     4{% for page in page_list %}
     5    <url>
     6        <loc>{{ page.loc }}</loc>
     7    </url>{% endfor %}
     8</urlset>
  • mediagoblin/views.py

    diff --git a/mediagoblin/views.py b/mediagoblin/views.py
    index 4185c1b..8449b43 100644
    a b from mediagoblin.db.models import MediaEntry  
    1919from mediagoblin.tools.pagination import Pagination
    2020from mediagoblin.tools.response import render_to_response, render_404
    2121from mediagoblin.decorators import uses_pagination, user_not_banned
     22from mediagoblin.db.models import (MediaEntry, Collection, User)
    2223
    2324
    2425@user_not_banned
    def terms_of_service(request):  
    5152
    5253    return render_to_response(request,
    5354        'mediagoblin/terms_of_service.html', {})
     55
     56def sitemap(request):
     57    page_list = []
     58    host_url = request.host_url[:-1]
     59    list_of_users = User.query.all()
     60    list_of_media = MediaEntry.query.filter_by(
     61        state=u'processed')
     62    list_of_collections = Collection.query.all()
     63   
     64    for item in list_of_users:
     65        if item.has_privilege(u'active'):
     66            page_list.append({"loc": host_url + request.urlgen("mediagoblin.user_pages.user_home",
     67                                                        user=item.username)})
     68            page_list.append({"loc": host_url + request.urlgen("mediagoblin.user_pages.user_gallery",
     69                                                        user=item.username)})
     70            page_list.append({"loc": host_url + request.urlgen("mediagoblin.user_pages.collection_list",
     71                                                        user=item.username)})       
     72    for item in list_of_media:
     73        page_list.append({"loc": host_url + item.url_for_self(request.urlgen)})
     74    for item in list_of_collections:
     75        page_list.append({"loc": host_url + item.url_for_self(request.urlgen)})
     76                       
     77    return render_to_response(request,
     78        'mediagoblin/sitemap.xml', {"page_list": page_list})
     79
     80def robots(request):
     81    return render_to_response(request,'mediagoblin/robots.txt', {})