Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Add support for self-hosted posts #66

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
Django==1.4
PyMeta==0.5.0
certifi==0.0.8
chardet==1.0.1
distribute==0.6.24
django-autoslug==1.5.0
gunicorn==0.14.2
httplib2==0.7.4
-e git://github.com/brosner/python-oauth2.git@82a05f96878f187f67c1af44befc1bec562e5c1f#egg=oauth2-dev
oauthlib==0.1.3
pyasn1==0.1.3
pybars==0.0.1
requests==0.12.1
rsa==3.0.1
wsgiref==0.1.2
psycopg2==2.4.5
gunicorn==0.14.2
-e git://github.com/brosner/python-oauth2.git#egg=oauth2
pybars==0.0.1
62 changes: 62 additions & 0 deletions syte/auth_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Modified from http://djangosnippets.org/snippets/243/
import base64

from django.http import HttpResponse
from django.contrib.auth import authenticate, login
from django.conf import settings

#############################################################################
#
def view_or_basicauth(view, request, realm = "", *args, **kwargs):
if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META['HTTP_AUTHORIZATION'].split()
if len(auth) == 2:
# NOTE: We are only support basic authentication for now.
#
if auth[0].lower() == "basic":
uname, passwd = base64.b64decode(auth[1]).split(':')
if uname == settings.BUILTIN_POST_USERNAME and passwd == settings.BUILTIN_POST_PASSWORD:
return view(request, *args, **kwargs)

response = HttpResponse()
response.status_code = 401
response['WWW-Authenticate'] = 'Basic realm="%s"' % realm
return response

#############################################################################
#
def logged_in_or_basicauth(realm = ""):
"""
A simple decorator that requires a user to be logged in. If they are not
logged in the request is examined for a 'authorization' header.

If the header is present it is tested for basic authentication and
the user is logged in with the provided credentials.

If the header is not present a http 401 is sent back to the
requestor to provide credentials.

The purpose of this is that in several django projects I have needed
several specific views that need to support basic authentication, yet the
web site as a whole used django's provided authentication.

The uses for this are for urls that are access programmatically such as
by rss feed readers, yet the view requires a user to be logged in. Many rss
readers support supplying the authentication credentials via http basic
auth (and they do NOT support a redirect to a form where they post a
username/password.)

Use is simple:

@logged_in_or_basicauth
def your_view:
...

You can provide the name of the realm to ask for authentication within.
"""
def view_decorator(func):
def wrapper(request, *args, **kwargs):
return view_or_basicauth(func, request,
realm, *args, **kwargs)
return wrapper
return view_decorator
20 changes: 20 additions & 0 deletions syte/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import models
from autoslug import AutoSlugField


class Tag(models.Model):
name = models.CharField(max_length=50)


class Post(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()

published = models.BooleanField(default=False)
publish_date = models.DateTimeField(auto_now_add=True, null=True)

slug = AutoSlugField(populate_from='title',always_update=True)
tags = models.ManyToManyField(Tag, related_name="posts")

class Meta:
ordering = ["-publish_date"]
7 changes: 7 additions & 0 deletions syte/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@

from syte_settings import *

if BUILTIN_POST_ENABLED:

INSTALLED_APPS += ("syte",)

DATABASES = {
'default': BUILTIN_POST_DATABASE_SETTINGS
}
Loading