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

pull/1/head
Danielle McLean 5 years ago
parent
commit
9a98fcdf4f
Signed by: 00dani
GPG Key ID: 5A5D2D1AFF12EEC5
  1. 1
      lemoncurry/settings/base.py
  2. 1
      lemoncurry/templates/lemoncurry/layout.html
  3. 1
      lemoncurry/urls.py
  4. 1
      micropub/__init__.py
  5. 5
      micropub/apps.py
  6. 7
      micropub/urls.py
  7. 52
      micropub/views.py

1
lemoncurry/settings/base.py

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

1
lemoncurry/templates/lemoncurry/layout.html

@ -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
lemoncurry/urls.py

@ -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

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

5
micropub/apps.py

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

7
micropub/urls.py

@ -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

@ -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