Browse Source

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

tags/v1.6.0
Danielle McLean 1 year ago
parent
commit
9a98fcdf4f
Signed by: Danielle McLean <dani@00dani.me> GPG Key ID: 5A5D2D1AFF12EEC5

+ 1
- 0
lemoncurry/settings/base.py View File

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

+ 1
- 0
lemoncurry/templates/lemoncurry/layout.html 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" />


+ 1
- 0
lemoncurry/urls.py 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
- 0
micropub/__init__.py View File

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

+ 5
- 0
micropub/apps.py View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig


class MicropubConfig(AppConfig):
name = 'micropub'

+ 7
- 0
micropub/urls.py 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
- 0
micropub/views.py 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

Loading…
Cancel
Save