Permalink
Browse files

Add subscriptions

  • Loading branch information...
1 parent 3e306e6 commit 3dbd20bc271d01071eb25a0f8dc14970bd91203b @markpasc committed May 1, 2011
Showing with 76 additions and 3 deletions.
  1. +5 −0 apropost/forms.py
  2. +14 −1 apropost/models.py
  3. +12 −0 apropost/templates/apropost/subscribe.html
  4. +2 −0 apropost/urls.py
  5. +43 −2 apropost/views.py
View
@@ -10,3 +10,8 @@ class UserCreationForm(django.contrib.auth.forms.UserCreationForm):
error_messages={
'invalid': _("This value may contain only letters and numbers (and '-' characters, if they're in the middle)."),
})
+
+
+class AddSubscriptionForm(forms.Form):
+
+ url = forms.URLField()
View
@@ -6,6 +6,7 @@
from django.contrib.auth.models import User
from django.db import models
import django_push.subscriber.signals
+from django_push.subscriber.models import Subscription
import iso8601
@@ -52,6 +53,12 @@ def from_element(cls, author_el):
return author
+class AuthorSubscription(models.Model):
+
+ author = models.ForeignKey(Author)
+ subscription = models.ForeignKey(Subscription, unique=True)
+
+
class Image(models.Model):
image_url = models.CharField(max_length=200, unique=True)
@@ -175,9 +182,15 @@ def yo_hay(notification, **kwargs):
def save_items(notification, **kwargs):
feed_author_el = notification.find('./{http://www.w3.org/2005/Atom}author')
+ if feed_author_el is None:
+ sender = kwargs['sender']
+ assert isinstance(sender, Subscription)
+ feed_author = AuthorSubscription.objects.get(subscription=sender).author
+ else:
+ feed_author = Author.from_element(feed_author_el)
for entry_el in notification.findall('{http://www.w3.org/2005/Atom}entry'):
- post = Post.from_element(entry_el, feed_author_el=feed_author_el)
+ post = Post.from_element(entry_el, feed_author=feed_author)
post.save()
# TODO: only UserStream the ones that a user has subscribed to?
@@ -0,0 +1,12 @@
+{% extends "apropost/base.html" %}
+
+{% block content %}
+
+ <form method="POST" action="{% url subscribe %}">
+ {% csrf_token %}
+ {{ form.as_p }}
+
+ <p><input type="submit" value="Subscribe"></p>
+ </form>
+
+{% endblock %}
View
@@ -5,4 +5,6 @@
url(r'^$', 'root', name='root'),
url(r'^register$', 'register', name='register'),
url(r'^home$', 'home', name='home'),
+
+ url(r'^subscribe$', 'add_subscription', name='subscribe'),
)
View
@@ -7,9 +7,11 @@
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
+from django_push.subscriber.models import Subscription
+import feedparser
-from apropost.models import UserStream, Author
-from apropost.forms import UserCreationForm
+from apropost.models import UserStream, Author, AuthorSubscription
+from apropost.forms import UserCreationForm, AddSubscriptionForm
def root(request):
@@ -47,3 +49,42 @@ def home(request):
}
return render(request, 'apropost/home.html', data)
+
+
+@login_required
+def add_subscription(request):
+ if request.method == 'POST':
+ form = AddSubscriptionForm(request.POST)
+ if form.is_valid():
+
+ url = form.cleaned_data['url']
+ # Is this a feed or a web page with a feed?
+
+ feed_url = url
+ buh = feedparser.parse(feed_url)
+ if buh.bozo and buh['headers']['content-type'].startswith('text/html'):
+ feedlinks = [l['href'] for l in buh.feed.links if l['rel'] == 'alternate' and l['type'] in ('application/atom+xml', 'application/rss+xml')]
+ if feedlinks:
+ feed_url = feedlinks[0]
+ buh = feedparser.parse(feed_url)
+
+ hublinks = [l['href'] for l in buh.feed.links if l['rel'] == 'hub']
+ if hublinks:
+ hub_url = hublinks[0]
+ else:
+ raise ValueError("No such hub for %s :(" % feed_url)
+
+ sub = Subscription.objects.subscribe(feed_url, hub=hub_url)
+ author = Author.objects.create(
+ atom_id=url,
+ display_name=buh.feed.title,
+ homepage_url=url,
+ )
+ AuthorSubscription.objects.create(author=author, subscription=sub)
+
+ return HttpResponseRedirect(reverse('home'))
+ else:
+ form = AddSubscriptionForm()
+
+ data = {'form': form}
+ return render(request, 'apropost/subscribe.html', data)

0 comments on commit 3dbd20b

Please sign in to comment.