Introduce some very, VERY basic support for micropub - only h-entry works, and only the name and content properties can actually be set, but it works

This commit is contained in:
Danielle McLean 2017-11-03 20:45:29 +11:00
parent a3f23e3b4d
commit 9a98fcdf4f
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
7 changed files with 68 additions and 0 deletions

View file

@ -85,6 +85,7 @@ INSTALLED_APPS = [
'entries',
'home',
'lemonauth',
'micropub',
'users',
'wellknowns',
]

View file

@ -13,6 +13,7 @@
<link rel="authorization_endpoint" href="{{ origin }}{% url 'lemonauth:indie' %}" />
<link rel="token_endpoint" href="{{ origin }}{% url 'lemonauth:token' %}" />
<link rel="micropub_endpoint" href="{{ origin }}{% url 'micropub:micropub' %}" />
<link rel="openid.delegate" href="{{ origin }}" />
<link rel="openid.server" href="https://openid.indieauth.com/openid" />

View file

@ -39,6 +39,7 @@ urlpatterns = [
url('^.well-known/', include('wellknowns.urls')),
url('^admin/', otp_admin_site.urls),
url('^auth/', include('lemonauth.urls')),
url('^micropub', include('micropub.urls')),
url('^s/', include('shorturls.urls')),
url(r'^sitemap\.xml$', sitemap.index, maps, name='sitemap'),

1
micropub/__init__.py Normal file
View file

@ -0,0 +1 @@
default_app_config = 'micropub.apps.MicropubConfig'

5
micropub/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class MicropubConfig(AppConfig):
name = 'micropub'

7
micropub/urls.py Normal file
View file

@ -0,0 +1,7 @@
from django.conf.urls import url
from . import views
app_name = 'micropub'
urlpatterns = (
url('^$', views.micropub, name='micropub'),
)

52
micropub/views.py Normal file
View file

@ -0,0 +1,52 @@
from django.contrib.auth import get_user_model
from django.http import HttpResponse
from django_push.publisher import ping_hub
from django.urls import reverse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from urllib.parse import urljoin
from entries.models import Entry
from entries.kinds import Article, Note
from lemoncurry import utils
from lemonauth import tokens
@csrf_exempt
@require_POST
def micropub(request):
auth = request.META.get('HTTP_AUTHORIZATION', '').split(' ')
if auth[0] != 'Bearer':
return utils.bad_req('only Bearer auth supported')
try:
token = tokens.decode(auth[1])
except Exception:
return utils.forbid('invalid token')
user = get_user_model().objects.get(pk=token['uid'])
post = request.POST
if post.get('h') != 'entry':
return utils.bad_req('only h=entry supported')
entry = Entry(author=user, kind=Note.id)
if 'name' in post:
entry.name = post['name']
entry.kind = Article.id
if 'content' in post:
entry.content = post['content']
entry.save()
base = utils.origin(request)
perma = urljoin(base, entry.url)
others = (urljoin(base, url) for url in (
reverse('home:index'),
reverse('entries:atom'),
reverse('entries:rss'),
))
ping_hub(perma)
for url in others:
ping_hub(url)
res = HttpResponse(status=201)
res['Location'] = perma
return res