Source

copelco / lib / copelco / apps / aggregator / views.py

from __future__ import absolute_import

from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic.list_detail import object_list
from .models import FeedItem, Feed, FeedType
from .forms import FeedModelForm
from ..shortcuts import render

def index(request):
    """
    Displays the latest feeds of each type.
    """
    ctx = {'feedtype_list': FeedType.objects.all()}
    return render(request, 'aggregator/index.html', ctx)

def feed_list(request, feed_type_slug):
    """
    Shows the latest feeds for the given type.
    """
    feed_type = get_object_or_404(FeedType, slug=feed_type_slug)
    return object_list(request, 
        queryset = FeedItem.objects.filter(feed__feed_type=feed_type), 
        paginate_by = 25,
        extra_context = {'feed_type': feed_type},
    )

@login_required
def my_feeds(request):
    """
    Lets the user see, edit, and delete all of their owned feeds.
    """
    feed_types = FeedType.objects.all()
    if not request.user.is_superuser:
        feed_types = feed_types.filter(can_self_add=True)

    ctx = {
        'feeds': Feed.objects.filter(owner=request.user),
        'feed_types': feed_types
    }
    return render(request, 'aggregator/my-feeds.html', ctx)

@login_required
def add_feed(request, feed_type_slug):
    """
    Lets users add new feeds to the aggregator.

    Users only get to add new feeds of types indicated as "can self add."
    """
    ft = get_object_or_404(FeedType, slug=feed_type_slug, can_self_add=True)
    if not ft.can_self_add and not request.user.is_superuser:
        return render(request, 'aggregator/denied.html')
        
    instance = Feed(feed_type=ft, owner=request.user)
    f = FeedModelForm(request.POST or None, instance=instance)
    if f.is_valid():
        f.save()
        return redirect('community-index')

    ctx = {'form': f, 'feed_type': ft, 'adding': True}
    return render(request, 'aggregator/edit-feed.html', ctx)

@login_required
def edit_feed(request, feed_id):
    """
    Lets a user edit a feed they've previously added.

    Only feeds the user "owns" can be edited.
    """
    feed = get_object_or_404(Feed, pk=feed_id, owner=request.user)
    f = FeedModelForm(request.POST or None, instance=feed)
    if f.is_valid():
        f.save()
        return redirect('community-my-feeds')
    
    ctx = {'form': f, 'feed': feed, 'adding': False}
    return render(request, 'aggregator/edit-feed.html', ctx)

@login_required
def delete_feed(request, feed_id):
    """
    Lets a user delete a feed they've previously added.

    Only feeds the user "owns" can be deleted.
    """
    feed = get_object_or_404(Feed, pk=feed_id, owner=request.user)
    if request.method == 'POST':
        feed.delete()
        return redirect('community-my-feeds')
    return render(request, 'aggregator/delete-confirm.html', {'feed': feed})
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.